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

feat: share deviceType with local peers #461

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class MapeoManager extends TypedEmitter {
#localDiscovery
#loggerBase
#l
/** @readonly */
#deviceType
gmaclennan marked this conversation as resolved.
Show resolved Hide resolved

/**
Expand Down
18 changes: 5 additions & 13 deletions test-e2e/local-peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ test('Local peers discovery each other and share device info', async (t) => {
const desktopManagers = await createManagers(5, t, 'desktop')
const managers = [...mobileManagers, ...desktopManagers]
connectPeers(managers, { discovery: true })
t.teardown(() => {
disconnectPeers(managers)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think this callback should return a promise. One of these should work:

Suggested change
t.teardown(() => {
disconnectPeers(managers)
})
t.teardown(async () => {
await disconnectPeers(managers)
})

...or:

Suggested change
t.teardown(() => {
disconnectPeers(managers)
})
t.teardown(() => disconnectPeers(managers))

The latter doesn't immediately work because the callback to t.teardown() is expected to return void | Promise<void>, but disconnectPeers returns Promise<void[]>. I'd fix that by updating test-e2e/utils.js like this:

diff --git a/test-e2e/utils.js b/test-e2e/utils.js
index e5fd403..7dd5575 100644
--- a/test-e2e/utils.js
+++ b/test-e2e/utils.js
@@ -24,7 +24,7 @@ const clientMigrationsFolder = new URL('../drizzle/client', import.meta.url)
  * @param {readonly MapeoManager[]} managers
  */
 export async function disconnectPeers(managers) {
-  return Promise.all(
+  await Promise.all(
     managers.map(async (manager) => {
       return manager.stopLocalPeerDiscovery({ force: true })
     })

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh this is where typescript annoys me. It gave the error when writing it the original way, and to keep it happy I added the braces, which ends up introducing this bug. Good example of typescript prompting me to write buggy code when how I wrote it (which errors typescript) would not actually cause any bugs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it works for us, but we might be able to enable a "no floating promises" ESLint rule to help with this kind of problem.

await waitForPeers(managers, { waitForDeviceInfo: true })
const deviceInfos = [
...(await Promise.all(mobileManagers.map((m) => m.getDeviceInfo()))).map(
Expand All @@ -29,12 +32,11 @@ test('Local peers discovery each other and share device info', async (t) => {
deviceType: p.deviceType,
}))
t.alike(
actualDeviceInfos.sort(sortByDeviceId),
expectedDeviceInfos.sort(sortByDeviceId),
new Set(actualDeviceInfos),
new Set(expectedDeviceInfos),
`manager ${i} has correct peers`
)
}
await disconnectPeers(managers)
})

/**
Expand All @@ -44,13 +46,3 @@ test('Local peers discovery each other and share device info', async (t) => {
function removeElementAt(array, i) {
return array.slice(0, i).concat(array.slice(i + 1))
}

/**
* @param {any} a
* @param {any} b
*/
function sortByDeviceId(a, b) {
if (a.deviceId < b.deviceId) return -1
if (a.deviceId > b.deviceId) return 1
return 0
}
5 changes: 0 additions & 5 deletions tests/local-peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ test('Invite to unknown peer', async (t) => {
roleName: DEFAULT_CAPABILITIES[MEMBER_ROLE_ID].name,
invitorName: 'device0',
}),
// @ts-ignore
UnknownPeerError
)
await t.exception(
Expand All @@ -226,7 +225,6 @@ test('Invite to unknown peer', async (t) => {
projectKey,
decision: LocalPeers.InviteResponse.ACCEPT,
}),
// @ts-ignore
UnknownPeerError
)
})
Expand Down Expand Up @@ -378,7 +376,6 @@ test('Disconnect results in rejected invite', async (t) => {
})
await t.exception(
invite,
// @ts-ignore
PeerDisconnectedError,
'invite rejected with PeerDisconnectedError'
)
Expand Down Expand Up @@ -535,7 +532,6 @@ test('Invite timeout', async (t) => {
roleName: DEFAULT_CAPABILITIES[MEMBER_ROLE_ID].name,
invitorName: 'device0',
}),
// @ts-ignore
TimeoutError
)
clock.tickAsync(5005)
Expand All @@ -558,7 +554,6 @@ test('Send invite to non-existent peer', async (t) => {
roleName: DEFAULT_CAPABILITIES[MEMBER_ROLE_ID].name,
invitorName: 'device0',
}),
// @ts-ignore
UnknownPeerError
)
})
Expand Down
30 changes: 22 additions & 8 deletions types/brittle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,31 @@ declare module 'brittle' {
coercively(actual: any, expected: any, message?: string): void
}

type AnyErrorConstructor = new () => Error

interface ExceptionAssertion {
<T>(
fn: T | Promise<T>,
error?: RegExp | Error,
(fn: () => unknown, message?: string): void
(
fn: () => unknown,
error?: RegExp | AnyErrorConstructor,
message?: string
): void
(fn: Promise<unknown>, message?: string): Promise<void>
(
fn: Promise<unknown>,
error?: RegExp | AnyErrorConstructor,
message?: string
): Promise<void>
<T>(fn: T | Promise<T>, message?: string): Promise<void>
all<T>(fn: T | Promise<T>, message?: string): Promise<void>
all<T>(
fn: T | Promise<T>,
error?: RegExp | Error,
all(fn: () => unknown, message?: string): void
all(
fn: () => unknown,
error?: RegExp | AnyErrorConstructor,
message?: string
): void
all(fn: Promise<unknown>, message?: string): Promise<void>
all(
fn: Promise<unknown>,
error?: RegExp | AnyErrorConstructor,
message?: string
): Promise<void>
}
Expand Down
Loading