Skip to content

Commit

Permalink
chore: enable electron-main webrtc tests (#1918)
Browse files Browse the repository at this point in the history
Runs WebRTC tests on Electron as well as node/browsers.
  • Loading branch information
achingbrain committed Apr 15, 2024
1 parent 08dabd3 commit 31c78f4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
1 change: 1 addition & 0 deletions packages/transport-webrtc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"test:node": "aegir test -t node --cov",
"test:chrome": "aegir test -t browser --cov",
"test:firefox": "aegir test -t browser -- --browser firefox",
"test:electron-main": "aegir test -t electron-main",
"lint": "aegir lint",
"lint:fix": "aegir lint --fix",
"clean": "aegir clean",
Expand Down
83 changes: 49 additions & 34 deletions packages/transport-webrtc/test/peer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@ import type { ConnectionManager, TransportManager } from '@libp2p/interface-inte

const browser = detect()

interface Initiator {
multiaddr: Multiaddr
peerConnection: RTCPeerConnection
connectionManager: StubbedInstance<ConnectionManager>
transportManager: StubbedInstance<TransportManager>
connection: StubbedInstance<Connection>
stream: Stream
log: Logger
}

interface Recipient {
peerConnection: RTCPeerConnection
connection: StubbedInstance<Connection>
abortController: AbortController
signal: AbortSignal
stream: Stream
log: Logger
}

interface PrivateToPrivateComponents {
initiator: {
multiaddr: Multiaddr
peerConnection: RTCPeerConnection
connectionManager: StubbedInstance<ConnectionManager>
transportManager: StubbedInstance<TransportManager>
connection: StubbedInstance<Connection>
stream: Stream
log: Logger
}
recipient: {
peerConnection: RTCPeerConnection
connection: StubbedInstance<Connection>
abortController: AbortController
signal: AbortSignal
stream: Stream
log: Logger
}
initiator: Initiator
recipient: Recipient
}

async function getComponents (): Promise<PrivateToPrivateComponents> {
Expand Down Expand Up @@ -85,9 +89,16 @@ async function getComponents (): Promise<PrivateToPrivateComponents> {

describe('webrtc basic', () => {
const isFirefox = ((browser != null) && browser.name === 'firefox')
let initiator: Initiator
let recipient: Recipient

afterEach(() => {
initiator?.peerConnection?.close()
recipient?.peerConnection?.close()
})

it('should connect', async () => {
const { initiator, recipient } = await getComponents()
({ initiator, recipient } = await getComponents())

// no existing connection
initiator.connectionManager.getConnections.returns([])
Expand All @@ -114,14 +125,11 @@ describe('webrtc basic', () => {
expect(initiator.peerConnection.connectionState).eq('connected')
expect(recipient.peerConnection.connectionState).eq('connected')
})

initiator.peerConnection.close()
recipient.peerConnection.close()
})

it('should survive aborting during connection', async () => {
({ initiator, recipient } = await getComponents())
const abortController = new AbortController()
const { initiator, recipient } = await getComponents()

// no existing connection
initiator.connectionManager.getConnections.returns([])
Expand Down Expand Up @@ -150,15 +158,20 @@ describe('webrtc basic', () => {
handleIncomingStream(recipient)
]))
.to.eventually.be.rejected.with.property('message', 'Oh noes!')

initiator.peerConnection.close()
recipient.peerConnection.close()
})
})

describe('webrtc receiver', () => {
let initiator: Initiator
let recipient: Recipient

afterEach(() => {
initiator?.peerConnection?.close()
recipient?.peerConnection?.close()
})

it('should fail receiving on invalid sdp offer', async () => {
const { initiator, recipient } = await getComponents()
({ initiator, recipient } = await getComponents())
const receiverPeerConnectionPromise = handleIncomingStream(recipient)
const stream = pbStream(initiator.stream).pb(Message)

Expand All @@ -171,8 +184,16 @@ describe('webrtc receiver', () => {
})

describe('webrtc dialer', () => {
let initiator: Initiator
let recipient: Recipient

afterEach(() => {
initiator?.peerConnection?.close()
recipient?.peerConnection?.close()
})

it('should fail receiving on invalid sdp answer', async () => {
const { initiator, recipient } = await getComponents()
({ initiator, recipient } = await getComponents())

// existing connection already exists
initiator.connectionManager.getConnections.returns([
Expand All @@ -190,13 +211,10 @@ describe('webrtc dialer', () => {

await stream.write({ type: Message.Type.SDP_ANSWER, data: 'bad' })
await expect(initiatorPeerConnectionPromise).to.be.rejectedWith(/Failed to set remoteDescription/)

initiator.peerConnection.close()
recipient.peerConnection.close()
})

it('should fail on receiving a candidate before an answer', async () => {
const { initiator, recipient } = await getComponents()
({ initiator, recipient } = await getComponents())

// existing connection already exists
initiator.connectionManager.getConnections.returns([
Expand Down Expand Up @@ -226,9 +244,6 @@ describe('webrtc dialer', () => {
await expect(initiatorPeerConnectionPromise).to.be.rejectedWith(/Remote should send an SDP answer/)

pc.close()

initiator.peerConnection.close()
recipient.peerConnection.close()
})
})

Expand Down

0 comments on commit 31c78f4

Please sign in to comment.