From b242cc4d8b12d94c229b0237b79b46306e7d621f Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 6 Jun 2024 13:11:43 +0100 Subject: [PATCH] feat: add webrtc-direct connect tests Adds tests that use webrtc-direct transports and also an `UnsupportedError` error that can be thrown to skip the current test if it's not supported by the current platform. --- src/connect/index.ts | 22 ++++++++++++++++++---- src/index.ts | 14 +++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/connect/index.ts b/src/connect/index.ts index 2e6ede1..6e8ebb1 100644 --- a/src/connect/index.ts +++ b/src/connect/index.ts @@ -3,7 +3,7 @@ import type { Daemon, DaemonFactory, NodeType, SpawnOptions, TransportType } fro export function connectTests (factory: DaemonFactory): void { const nodeTypes: NodeType[] = ['js', 'go'] - const transportTypes: TransportType[] = ['tcp', 'webtransport'] + const transportTypes: TransportType[] = ['tcp', 'webtransport', 'webrtc-direct'] for (const typeA of nodeTypes) { for (const typeB of nodeTypes) { @@ -11,7 +11,7 @@ export function connectTests (factory: DaemonFactory): void { runConnectTests( transport, factory, - { type: typeA, transport }, + { type: typeA, transport, noListen: true }, { type: typeB, transport } ) }) @@ -23,13 +23,23 @@ function runConnectTests (name: string, factory: DaemonFactory, optionsA: SpawnO describe(`connection.${name}`, () => { let daemonA: Daemon let daemonB: Daemon + let skipped: boolean // Start Daemons before(async function () { this.timeout(20 * 1000) - daemonA = await factory.spawn(optionsA) - daemonB = await factory.spawn(optionsB) + try { + daemonA = await factory.spawn(optionsA) + daemonB = await factory.spawn(optionsB) + } catch (err: any) { + if (err.name === 'UnsupportedError') { + skipped = true + return + } + + throw err + } }) // Stop daemons @@ -44,6 +54,10 @@ function runConnectTests (name: string, factory: DaemonFactory, optionsA: SpawnO it(`${optionsA.type} peer to ${optionsB.type} peer over ${name}`, async function () { this.timeout(10 * 1000) + if (skipped) { + return this.skip() + } + const identify1 = await daemonA.client.identify() const identify2 = await daemonB.client.identify() diff --git a/src/index.ts b/src/index.ts index 03a3836..f5d0e3a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -59,7 +59,7 @@ export type PeerIdType = 'rsa' | 'ed25519' | 'secp256k1' export type PubSubRouter = 'gossipsub' | 'floodsub' export type Muxer = 'mplex' | 'yamux' export type Encryption = 'noise' | 'tls' -export type TransportType = 'tcp' | 'webtransport' +export type TransportType = 'tcp' | 'webtransport' | 'webrtc-direct' export interface SpawnOptions { type: NodeType @@ -95,3 +95,15 @@ export { streamTests as streamInteropTests, relayTests as relayInteropTests } + +/** + * Some tests allow skipping certain configurations. When this is necessary, + * `DaemonFactory.spawn` should thow an instance of this error. + */ +export class UnsupportedError extends Error { + constructor (message = 'Unsupported test configuration') { + super(message) + + this.name = 'UnsupportedError' + } +}