Skip to content

Commit

Permalink
Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dguenther committed May 3, 2024
1 parent 9d2d6ff commit 54a7482
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 43 deletions.
6 changes: 0 additions & 6 deletions ironfish/src/network/peerNetwork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,10 @@ describe('PeerNetwork', () => {

describe('when peers connect', () => {
it('changes isReady', async () => {
const dc = await import('node-datachannel')

const peerNetwork = new PeerNetwork({
identity: mockPrivateIdentity('local'),
agent: 'sdk/1/cli',
webSocket: ws,
nodeDataChannel: dc,
node: mockNode(),
chain: mockChain(),
minPeers: 1,
Expand Down Expand Up @@ -138,13 +135,10 @@ describe('PeerNetwork', () => {
describe('when at max peers', () => {
it('rejects websocket connections', async () => {
const wsActual = jest.requireActual<typeof WSWebSocket>('ws')
const dc = await import('node-datachannel')

const peerNetwork = new PeerNetwork({
identity: mockPrivateIdentity('local'),
agent: 'sdk/1/cli',
webSocket: wsActual,
nodeDataChannel: dc,
node: mockNode(),
chain: mockChain(),
listen: true,
Expand Down
12 changes: 4 additions & 8 deletions ironfish/src/network/peers/connections/webRtcConnection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import { WebRtcConnection } from './webRtcConnection'
describe('WebRtcConnection', () => {
describe('send', () => {
describe('with no datachannel', () => {
it('returns false', async () => {
const nodeDataChannel = await import('node-datachannel')

const connection = new WebRtcConnection(nodeDataChannel, false, createRootLogger())
it('returns false', () => {
const connection = new WebRtcConnection(false, createRootLogger())
const message = new IdentifyMessage({
agent: '',
head: Buffer.alloc(32, 0),
Expand All @@ -31,10 +29,8 @@ describe('WebRtcConnection', () => {
})

describe('with a valid message', () => {
it('serializes and sends the message on the datachannel', async () => {
const nodeDataChannel = await import('node-datachannel')

const connection = new WebRtcConnection(nodeDataChannel, true, createRootLogger())
it('serializes and sends the message on the datachannel', () => {
const connection = new WebRtcConnection(true, createRootLogger())
const sendSpy = jest.spyOn(connection, '_send')

const message = new IdentifyMessage({
Expand Down
42 changes: 14 additions & 28 deletions ironfish/src/network/peers/peer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ describe('setWebSocketConnection', () => {
})

describe('setWebRtcConnection', () => {
it('Changes to CONNECTING when in DISCONNECTED', async () => {
const nodeDataChannel = await import('node-datachannel')

it('Changes to CONNECTING when in DISCONNECTED', () => {
const identity = mockIdentity('peer')
const peer = new Peer(identity)
const connection = new WebRtcConnection(nodeDataChannel, false, createRootLogger())
const connection = new WebRtcConnection(false, createRootLogger())

peer.setWebRtcConnection(connection)
expect(peer.state).toEqual({
Expand All @@ -67,10 +65,8 @@ describe('setWebRtcConnection', () => {
})
})

it('Times out WebRTC handshake', async () => {
const nodeDataChannel = await import('node-datachannel')

const connection = new WebRtcConnection(nodeDataChannel, false, createRootLogger())
it('Times out WebRTC handshake', () => {
const connection = new WebRtcConnection(false, createRootLogger())
expect(connection.state.type).toEqual('CONNECTING')

const peer = new Peer(null)
Expand Down Expand Up @@ -114,10 +110,8 @@ it('Times out WebRTC handshake', async () => {
})

describe('Handles message send failure', () => {
it('Disconnects peer on error in _send', async () => {
const nodeDataChannel = await import('node-datachannel')

const connection = new WebRtcConnection(nodeDataChannel, true, createRootLogger())
it('Disconnects peer on error in _send', () => {
const connection = new WebRtcConnection(true, createRootLogger())
expect(connection.state.type).toEqual('CONNECTING')

const peer = new Peer(null)
Expand Down Expand Up @@ -155,10 +149,8 @@ describe('Handles message send failure', () => {
expect(peer.state.type).toEqual('CONNECTED')
})

it('Falls back to WebSockets if available and WebRTC send fails', async () => {
const nodeDataChannel = await import('node-datachannel')

const wrtcConnection = new WebRtcConnection(nodeDataChannel, true, createRootLogger())
it('Falls back to WebSockets if available and WebRTC send fails', () => {
const wrtcConnection = new WebRtcConnection(true, createRootLogger())
const wsConnection = new WebSocketConnection(
new ws(''),
ConnectionDirection.Outbound,
Expand Down Expand Up @@ -255,9 +247,7 @@ it('Transitions to CONNECTED when adding a connection with state CONNECTED', ()
})
})

it('Stays in CONNECTED when adding an additional connection', async () => {
const nodeDataChannel = await import('node-datachannel')

it('Stays in CONNECTED when adding an additional connection', () => {
const identity = mockIdentity('peer')
const peer = new Peer(null)
const connection = new WebSocketConnection(
Expand All @@ -275,7 +265,7 @@ it('Stays in CONNECTED when adding an additional connection', async () => {
connection.setState({ type: 'CONNECTED', identity })

// Add in an additional connection
const wrtcConnection = new WebRtcConnection(nodeDataChannel, true, createRootLogger())
const wrtcConnection = new WebRtcConnection(true, createRootLogger())
peer.setWebRtcConnection(wrtcConnection)
expect(wrtcConnection.state.type).not.toBe('CONNECTED')

Expand All @@ -287,9 +277,7 @@ it('Stays in CONNECTED when adding an additional connection', async () => {
})

describe('Stays in CONNECTED when one connection disconnects', () => {
it('WebSocket disconnects', async () => {
const nodeDataChannel = await import('node-datachannel')

it('WebSocket disconnects', () => {
const identity = mockIdentity('peer')
const peer = new Peer(null)

Expand All @@ -303,7 +291,7 @@ describe('Stays in CONNECTED when one connection disconnects', () => {
connection.setState({ type: 'CONNECTED', identity })

// Add a CONNECTED WebRTC connection
const wrtcConnection = new WebRtcConnection(nodeDataChannel, true, createRootLogger())
const wrtcConnection = new WebRtcConnection(true, createRootLogger())
peer.setWebRtcConnection(wrtcConnection)
wrtcConnection.setState({ type: 'CONNECTED', identity })

Expand All @@ -318,9 +306,7 @@ describe('Stays in CONNECTED when one connection disconnects', () => {
})
})

it('WebRTC disconnects', async () => {
const nodeDataChannel = await import('node-datachannel')

it('WebRTC disconnects', () => {
const identity = mockIdentity('peer')
const peer = new Peer(null)

Expand All @@ -334,7 +320,7 @@ describe('Stays in CONNECTED when one connection disconnects', () => {
connection.setState({ type: 'CONNECTED', identity })

// Add a CONNECTED WebRTC connection
const wrtcConnection = new WebRtcConnection(nodeDataChannel, true, createRootLogger())
const wrtcConnection = new WebRtcConnection(true, createRootLogger())
peer.setWebRtcConnection(wrtcConnection)
wrtcConnection.setState({ type: 'CONNECTED', identity })

Expand Down
2 changes: 1 addition & 1 deletion ironfish/src/network/peers/peerManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ describe('PeerManager', () => {

peer.onMessage.emit(message, connection)
expect(initWebRtcConnectionMock).toHaveBeenCalledTimes(1)
expect(initWebRtcConnectionMock).toHaveBeenCalledWith(peer, expect.anything(), true)
expect(initWebRtcConnectionMock).toHaveBeenCalledWith(peer, true)
expect(pm['getBrokeringPeers'](peer)[0]).toEqual(peer)
})

Expand Down

0 comments on commit 54a7482

Please sign in to comment.