Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: disconnect managers in teardown, not inline #698

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions test-e2e/local-peers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import {
connectPeers,
createManagers,
disconnectPeers,
waitForPeers,
} from './utils.js'
import { connectPeers, createManagers, waitForPeers } from './utils.js'

test('Local peers discovery each other and share device info', async (t) => {
const mobileManagers = await createManagers(5, t, 'mobile')
const desktopManagers = await createManagers(5, t, 'desktop')
const managers = [...mobileManagers, ...desktopManagers]
connectPeers(managers, { discovery: true })
t.after(() => disconnectPeers(managers))
const disconnectPeers = connectPeers(managers, { discovery: true })
t.after(disconnectPeers)
await waitForPeers(managers, { waitForDeviceInfo: true })
const deviceInfos = [...mobileManagers, ...desktopManagers].map((m) =>
m.getDeviceInfo()
Expand Down
39 changes: 15 additions & 24 deletions test-e2e/manager-invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ import test from 'node:test'
import assert from 'node:assert/strict'
import { InviteResponse_Decision } from '../src/generated/rpc.js'
import { once } from 'node:events'
import {
connectPeers,
createManagers,
disconnectPeers,
waitForPeers,
} from './utils.js'
import { connectPeers, createManagers, waitForPeers } from './utils.js'
import { COORDINATOR_ROLE_ID, MEMBER_ROLE_ID } from '../src/roles.js'

/** @typedef {import('../src/generated/rpc.js').Invite} Invite */

test('member invite accepted', async (t) => {
const [creator, joiner] = await createManagers(2, t)
connectPeers([creator, joiner])
const disconnectPeers = connectPeers([creator, joiner])
t.after(disconnectPeers)
await waitForPeers([creator, joiner])

const createdProjectId = await creator.createProject({ name: 'Mapeo' })
Expand Down Expand Up @@ -66,14 +62,13 @@ test('member invite accepted', async (t) => {
await joinerProject.$member.getMany(),
'Project members match'
)

await disconnectPeers([creator, joiner])
})

test('chain of invites', async (t) => {
const managers = await createManagers(4, t)
const [creator, ...joiners] = managers
connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const createdProjectId = await creator.createProject({ name: 'Mapeo' })
Expand Down Expand Up @@ -116,16 +111,14 @@ test('chain of invites', async (t) => {
'Project members match'
)
}

await disconnectPeers(managers)
})

test('generates new invite IDs for each invite', async (t) => {
const inviteCount = 10

const [creator, joiner] = await createManagers(2, t)
connectPeers([creator, joiner])
t.after(() => disconnectPeers([creator, joiner]))
const disconnectPeers = connectPeers([creator, joiner])
t.after(disconnectPeers)
await waitForPeers([creator, joiner])

const createdProjectId = await creator.createProject({ name: 'Mapeo' })
Expand Down Expand Up @@ -156,7 +149,8 @@ test('generates new invite IDs for each invite', async (t) => {
test("member can't invite", { skip: true }, async (t) => {
const managers = await createManagers(3, t)
const [creator, member, joiner] = managers
connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const createdProjectId = await creator.createProject({ name: 'Mapeo' })
Expand Down Expand Up @@ -188,13 +182,12 @@ test("member can't invite", { skip: true }, async (t) => {
assert.fail('should not send invite')
)
await exceptionPromise

await disconnectPeers(managers)
})

test('member invite rejected', async (t) => {
const [creator, joiner] = await createManagers(2, t)
connectPeers([creator, joiner])
const disconnectPeers = connectPeers([creator, joiner])
t.after(disconnectPeers)
await waitForPeers([creator, joiner])

const createdProjectId = await creator.createProject({ name: 'Mapeo' })
Expand Down Expand Up @@ -234,14 +227,12 @@ test('member invite rejected', async (t) => {
1,
'Only 1 member in project still'
)

await disconnectPeers([creator, joiner])
})

test('cancelation', async (t) => {
const [creator, joiner] = await createManagers(2, t)
connectPeers([creator, joiner])
t.after(() => disconnectPeers([creator, joiner]))
const disconnectPeers = connectPeers([creator, joiner])
t.after(disconnectPeers)
await waitForPeers([creator, joiner])

const createdProjectId = await creator.createProject({ name: 'Mapeo' })
Expand Down Expand Up @@ -278,8 +269,8 @@ test('cancelation', async (t) => {

test('canceling nothing', async (t) => {
const [creator, joiner] = await createManagers(2, t)
connectPeers([creator, joiner])
t.after(() => disconnectPeers([creator, joiner]))
const disconnectPeers = connectPeers([creator, joiner])
t.after(disconnectPeers)

const createdProjectId = await creator.createProject({ name: 'Mapeo' })
const creatorProject = await creator.getProject(createdProjectId)
Expand Down
33 changes: 14 additions & 19 deletions test-e2e/members.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import {
connectPeers,
createManagers,
disconnectPeers,
invite,
waitForPeers,
waitForSync,
Expand Down Expand Up @@ -100,7 +99,8 @@ test('getting yourself after adding project (but not yet synced)', async (t) =>
test('getting invited member after invite rejected', async (t) => {
const managers = await createManagers(2, t)
const [invitor, invitee] = managers
connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const projectId = await invitor.createProject({ name: 'Mapeo' })
Expand All @@ -125,13 +125,13 @@ test('getting invited member after invite rejected', async (t) => {
!members.find((m) => m.deviceId === invitee.deviceId),
'invited member not found'
)
await disconnectPeers(managers)
})

test('getting invited member after invite accepted', async (t) => {
const managers = await createManagers(2, t)
const [invitor, invitee] = managers
connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const { name: inviteeName } = invitee.getDeviceInfo()
Expand Down Expand Up @@ -163,13 +163,13 @@ test('getting invited member after invite accepted', async (t) => {
)

// TODO: Test that device info of invited member can be read from invitor after syncing
await disconnectPeers(managers)
})

test('invite uses custom role name when provided', async (t) => {
const managers = await createManagers(2, t)
const [invitor, invitee] = managers
connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const projectId = await invitor.createProject({ name: 'Mapeo' })
Expand All @@ -186,14 +186,13 @@ test('invite uses custom role name when provided', async (t) => {

const [{ roleName }] = await inviteReceivedPromise
assert.equal(roleName, 'friend', 'roleName should be equal')

await disconnectPeers(managers)
})

test('invite uses default role name when not provided', async (t) => {
const managers = await createManagers(2, t)
const [invitor, invitee] = managers
connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const projectId = await invitor.createProject({ name: 'Mapeo' })
Expand All @@ -213,8 +212,6 @@ test('invite uses default role name when not provided', async (t) => {
ROLES[MEMBER_ROLE_ID].name,
'`roleName` should use the fallback by deriving `roleId`'
)

await disconnectPeers(managers)
})

test('roles - creator role and role assignment', async (t) => {
Expand Down Expand Up @@ -337,7 +334,8 @@ test('roles - getMany() on newly invited device before sync', async (t) => {
test('roles - assignRole()', async (t) => {
const managers = await createManagers(2, t)
const [invitor, invitee] = managers
connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const projectId = await invitor.createProject({ name: 'Mapeo' })
Expand Down Expand Up @@ -439,14 +437,12 @@ test('roles - assignRole()', async (t) => {
'invitee now has member role from invitee perspective'
)
})

await disconnectPeers(managers)
})

test('roles - assignRole() with forked role', async (t) => {
const managers = await createManagers(3, t)
const [invitor, invitee1, invitee2] = managers
connectPeers(managers)
let disconnectPeers = connectPeers(managers)
await waitForPeers(managers)

const projectId = await invitor.createProject({ name: 'Mapeo' })
Expand All @@ -466,15 +462,16 @@ test('roles - assignRole() with forked role', async (t) => {

const [invitorProject, invitee1Project] = projects

await disconnectPeers(managers)
await disconnectPeers()

// 2. Create fork by two devices assigning a role to invitee2 while disconnected
// TODO: Assign different roles and test fork resolution prefers the role with least power (code for this is not written yet)

await invitorProject.$member.assignRole(invitee2.deviceId, MEMBER_ROLE_ID)
await invitee1Project.$member.assignRole(invitee2.deviceId, MEMBER_ROLE_ID)

await connectPeers(managers)
disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForSync(projects, 'initial')

// 3. Verify that invitee2 role is now forked
Expand All @@ -494,6 +491,4 @@ test('roles - assignRole() with forked role', async (t) => {
invitee2.deviceId
)
assert.equal(invitee2RoleMerged.forks.length, 0, 'invitee2 role has no forks')

await disconnectPeers(managers)
})
26 changes: 10 additions & 16 deletions test-e2e/project-leave.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
connectPeers,
createManager,
createManagers,
disconnectPeers,
getDiskUsage,
invite,
waitForPeers,
Expand All @@ -36,7 +35,8 @@ test('Creator cannot leave project if they are the only member', async (t) => {
test('Creator cannot leave project if no other coordinators exist', async (t) => {
const managers = await createManagers(2, t)

connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const [creator, member] = managers
Expand Down Expand Up @@ -68,14 +68,13 @@ test('Creator cannot leave project if no other coordinators exist', async (t) =>
await assert.rejects(async () => {
await creator.leaveProject(projectId)
}, 'creator attempting to leave project with no other coordinators fails')

await disconnectPeers(managers)
})

test('Blocked member cannot leave project', async (t) => {
const managers = await createManagers(2, t)

connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const [creator, member] = managers
Expand Down Expand Up @@ -113,14 +112,13 @@ test('Blocked member cannot leave project', async (t) => {
await assert.rejects(async () => {
await member.leaveProject(projectId)
}, 'Member attempting to leave project fails')

await disconnectPeers(managers)
})

test('Creator can leave project if another coordinator exists', async (t) => {
const managers = await createManagers(2, t)

connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const [creator, coordinator] = managers
Expand Down Expand Up @@ -166,14 +164,13 @@ test('Creator can leave project if another coordinator exists', async (t) => {
ROLES[LEFT_ROLE_ID],
'coordinator can still retrieve info about creator who left'
)

await disconnectPeers(managers)
})

test('Member can leave project if creator exists', async (t) => {
const managers = await createManagers(2, t)

connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const [creator, member] = managers
Expand Down Expand Up @@ -219,14 +216,13 @@ test('Member can leave project if creator exists', async (t) => {
ROLES[LEFT_ROLE_ID],
'creator can still retrieve info about member who left'
)

await disconnectPeers(managers)
})

test('Data access after leaving project', async (t) => {
const managers = await createManagers(3, t)

connectPeers(managers)
const disconnectPeers = connectPeers(managers)
t.after(disconnectPeers)
await waitForPeers(managers)

const [creator, coordinator, member] = managers
Expand Down Expand Up @@ -303,8 +299,6 @@ test('Data access after leaving project', async (t) => {
await assert.rejects(async () => {
await coordinatorProject.$setProjectSettings({ name: 'foo' })
}, 'coordinator cannot update project settings after leaving')

await disconnectPeers(managers)
})

test('leaving a project deletes data from disk', async (t) => {
Expand Down
6 changes: 4 additions & 2 deletions test-e2e/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ test('Create and sync data', { timeout: 100_000 }, async (t) => {
return acc
}, new Set())

connectPeers(managers, { discovery: false })
const disconnectPeers = connectPeers(managers, { discovery: false })
t.after(disconnectPeers)
await waitForSync(projects, 'initial')

await Promise.all(
Expand Down Expand Up @@ -376,7 +377,8 @@ test('shares cores', async function (t) {
const COUNT = 5
const managers = await createManagers(COUNT, t)
const [invitor, ...invitees] = managers
connectPeers(managers, { discovery: false })
const disconnectPeers = connectPeers(managers, { discovery: false })
t.after(disconnectPeers)
const projectId = await invitor.createProject({ name: 'Mapeo' })
await invite({ invitor, invitees, projectId })

Expand Down
Loading
Loading