Skip to content

Commit

Permalink
test: update test setup for webRTC (libp2p#1836)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Sep 21, 2023
1 parent b7fa73c commit 6022a52
Showing 1 changed file with 20 additions and 32 deletions.
52 changes: 20 additions & 32 deletions packages/transport-webrtc/test/compliance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,59 @@

import tests from '@libp2p/interface-compliance-tests/transport'
import { multiaddr } from '@multiformats/multiaddr'
import { webRTC } from '../src/index.js'
import { connectionPair, mockUpgrader } from '@libp2p/interface-compliance-tests/mocks'
import { mockRegistrar } from '@libp2p/interface-compliance-tests/mocks'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
import { stubInterface } from 'sinon-ts'
import type { Connection } from '@libp2p/interface/connection'
import { SIGNALING_PROTO_ID } from '../src/private-to-private/transport.js'
import { SIGNALING_PROTO_ID, WebRTCTransport } from '../src/private-to-private/transport.js'
import { pipe } from 'it-pipe'
import drain from 'it-drain'




describe('interface-transport compliance', () => {
tests({
async setup() {

const peerId = await createEd25519PeerId()

const fakeDial = async (): Promise<Connection> => {
// const connection = mockConnection(mockMultiaddrConnection(mockDuplex(), peerId))
// const newStreamStub = sinon.stub().withArgs(SIGNALING_PROTO_ID).resolves({ close: sinon.stub(), abort: sinon.stub() })

// connection.newStream = newStreamStub

const peerA = {
peerId: await createEd25519PeerId(),
registrar: mockRegistrar()
}
const peerAId = await createEd25519PeerId()
const peerBId = await createEd25519PeerId()

const fakeDial = async (peerA: any): Promise<Connection> => {
const peerB = {
peerId: await createEd25519PeerId(),
registrar: mockRegistrar()
peerId: peerBId,
registrar: mockRegistrar(),
upgrader: mockUpgrader()
}

await peerB.registrar.handle(SIGNALING_PROTO_ID, ({ stream }) => {
void pipe(stream, drain)
})

const [connectionA] = connectionPair(peerA, peerB)
const [_, connectionToB] = connectionPair(peerA, peerB)

return connectionA
return connectionToB
}

const components: any = {
peerId,
const peerA: any = {
peerAId,
registrar: mockRegistrar(),
upgrader: mockUpgrader(),
transportManager: stubInterface<TransportManager>({
dial: fakeDial(),
}),
}

const wrtc = webRTC()(components)
peerA.transportManager = stubInterface<TransportManager>({
dial: fakeDial(peerA),
})

const wrtc = new WebRTCTransport(peerA)

await wrtc.start()

const addrs = [
multiaddr('/ip4/127.0.0.1/tcp/9091/ws'),
multiaddr('/ip4/127.0.0.1/tcp/9092/ws'),
multiaddr('/dns4/ipfs.io/tcp/9092/ws'),
multiaddr('/dns4/ipfs.io/tcp/9092/ws')
multiaddr('/ip4/1.2.3.4/udp/1234/webrtc-direct/certhash/uEiAUqV7kzvM1wI5DYDc1RbcekYVmXli_Qprlw3IkiEg6tQ/p2p/12D3KooWGDMwwqrpcYKpKCgxuKT2NfqPqa94QnkoBBpqvCaiCzWd')
]

const listeningAddrs = [
multiaddr('/ip4/127.0.0.1/tcp/57708/ws/p2p/12D3KooWRqAUEzPwKMoGstpfJVqr3aoinwKVPu4DLo9nQncbnuLk/p2p-circuit/p2p/12D3KooWBZyVLJfQkofqLK4op9TPkHuUumCZt1ybQrPvNm7TVQV9/p2p-circuit/webrtc/p2p/12D3KooWBZyVLJfQkofqLK4op9TPkHuUumCZt1ybQrPvNm7TVQV9'),
multiaddr(`/ip4/127.0.0.1/tcp/57708/ws/p2p/12D3KooWRqAUEzPwKMoGstpfJVqr3aoinwKVPu4DLo9nQncbnuLk/p2p-circuit/p2p/${peerBId}/p2p-circuit/webrtc/p2p/${peerBId}`),
]

// Used by the dial tests to simulate a delayed connect
Expand Down

0 comments on commit 6022a52

Please sign in to comment.