Skip to content

Commit 2221128

Browse files
authored
feat: add webrtc-direct connect tests (#153)
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.
1 parent 6135583 commit 2221128

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/connect/index.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import type { Daemon, DaemonFactory, NodeType, SpawnOptions, TransportType } fro
33

44
export function connectTests (factory: DaemonFactory): void {
55
const nodeTypes: NodeType[] = ['js', 'go']
6-
const transportTypes: TransportType[] = ['tcp', 'webtransport']
6+
const transportTypes: TransportType[] = ['tcp', 'webtransport', 'webrtc-direct']
77

88
for (const typeA of nodeTypes) {
99
for (const typeB of nodeTypes) {
1010
transportTypes.forEach(transport => {
1111
runConnectTests(
1212
transport,
1313
factory,
14-
{ type: typeA, transport },
14+
{ type: typeA, transport, noListen: true },
1515
{ type: typeB, transport }
1616
)
1717
})
@@ -23,13 +23,23 @@ function runConnectTests (name: string, factory: DaemonFactory, optionsA: SpawnO
2323
describe(`connection.${name}`, () => {
2424
let daemonA: Daemon
2525
let daemonB: Daemon
26+
let skipped: boolean
2627

2728
// Start Daemons
2829
before(async function () {
2930
this.timeout(20 * 1000)
3031

31-
daemonA = await factory.spawn(optionsA)
32-
daemonB = await factory.spawn(optionsB)
32+
try {
33+
daemonA = await factory.spawn(optionsA)
34+
daemonB = await factory.spawn(optionsB)
35+
} catch (err: any) {
36+
if (err.name === 'UnsupportedError') {
37+
skipped = true
38+
return
39+
}
40+
41+
throw err
42+
}
3343
})
3444

3545
// Stop daemons
@@ -44,6 +54,10 @@ function runConnectTests (name: string, factory: DaemonFactory, optionsA: SpawnO
4454
it(`${optionsA.type} peer to ${optionsB.type} peer over ${name}`, async function () {
4555
this.timeout(10 * 1000)
4656

57+
if (skipped) {
58+
return this.skip()
59+
}
60+
4761
const identify1 = await daemonA.client.identify()
4862
const identify2 = await daemonB.client.identify()
4963

src/index.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export type PeerIdType = 'rsa' | 'ed25519' | 'secp256k1'
5959
export type PubSubRouter = 'gossipsub' | 'floodsub'
6060
export type Muxer = 'mplex' | 'yamux'
6161
export type Encryption = 'noise' | 'tls'
62-
export type TransportType = 'tcp' | 'webtransport'
62+
export type TransportType = 'tcp' | 'webtransport' | 'webrtc-direct'
6363

6464
export interface SpawnOptions {
6565
type: NodeType
@@ -95,3 +95,15 @@ export {
9595
streamTests as streamInteropTests,
9696
relayTests as relayInteropTests
9797
}
98+
99+
/**
100+
* Some tests allow skipping certain configurations. When this is necessary,
101+
* `DaemonFactory.spawn` should thow an instance of this error.
102+
*/
103+
export class UnsupportedError extends Error {
104+
constructor (message = 'Unsupported test configuration') {
105+
super(message)
106+
107+
this.name = 'UnsupportedError'
108+
}
109+
}

0 commit comments

Comments
 (0)