Skip to content

Commit

Permalink
new tests for duplicate connections
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Nov 14, 2023
1 parent 07cac79 commit 3407e0c
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions tests/local-peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,98 @@ test('Send invite immediately', async (t) => {
t.is(await responsePromise, LocalPeers.InviteResponse.ACCEPT)
})

test('Send invite, duplicate connections', async (t) => {
const r1 = new LocalPeers()
const r2 = new LocalPeers()

const invite = {
projectKey: Buffer.allocUnsafe(32).fill(0),
encryptionKeys: { auth: randomBytes(32) },
}

const kp1 = NoiseSecretStream.keyPair()
const kp2 = NoiseSecretStream.keyPair()

const destroy1 = replicate(r1, r2, { kp1, kp2 })
const [peers1] = await once(r1, 'peers')
const destroy2 = replicate(r1, r2, { kp1, kp2 })
const [peers2] = await once(r1, 'peers')

t.is(peers1.length, 1)
t.is(peers2.length, 1)
t.is(peers1[0].connectedAt, peers2[0].connectedAt, 'first connected is used')

{
const responsePromise = r1.invite(peers1[0].deviceId, invite)
const [peerId, receivedInvite] = await once(r2, 'invite')
t.alike(receivedInvite, invite)

r2.inviteResponse(peerId, {
projectKey: receivedInvite.projectKey,
decision: LocalPeers.InviteResponse.ACCEPT,
})

t.is(await responsePromise, LocalPeers.InviteResponse.ACCEPT)
}

destroy1()
const [peers3] = await once(r1, 'peers')

t.is(peers3.length, 1)
t.ok(
peers3[0].connectedAt > peers1[0].connectedAt,
'later connected peer is not used'
)

{
const responsePromise = r1.invite(peers1[0].deviceId, invite)
const [peerId, receivedInvite] = await once(r2, 'invite')
t.alike(receivedInvite, invite)

r2.inviteResponse(peerId, {
projectKey: receivedInvite.projectKey,
decision: LocalPeers.InviteResponse.ACCEPT,
})

t.is(await responsePromise, LocalPeers.InviteResponse.ACCEPT)
}

const now = Date.now()
destroy2()
const [peers4] = await once(r1, 'peers')
t.is(peers4.length, 1)
t.is(peers4[0].status, 'disconnected')
t.ok(peers4[0].disconnectedAt >= now, 'most recently disconnected exposed')
})

test('Duplicate connections with immediate disconnect', async (t) => {
const r1 = new LocalPeers()
const r2 = new LocalPeers()

const invite = {
projectKey: Buffer.allocUnsafe(32).fill(0),
encryptionKeys: { auth: randomBytes(32) },
}

const kp1 = NoiseSecretStream.keyPair()
const kp2 = NoiseSecretStream.keyPair()

replicate(r1, r2, { kp1, kp2 })
const destroy2 = replicate(r1, r2, { kp1, kp2 })
destroy2()

const responsePromise = r1.invite(kp2.publicKey.toString('hex'), invite)
const [peerId, receivedInvite] = await once(r2, 'invite')
t.alike(receivedInvite, invite)

r2.inviteResponse(peerId, {
projectKey: receivedInvite.projectKey,
decision: LocalPeers.InviteResponse.ACCEPT,
})

t.is(await responsePromise, LocalPeers.InviteResponse.ACCEPT)
})

test('Send invite and reject', async (t) => {
t.plan(3)
const r1 = new LocalPeers()
Expand Down

0 comments on commit 3407e0c

Please sign in to comment.