diff --git a/src/sw-media-auth-recovery.test.ts b/src/sw-media-auth-recovery.test.ts index 2a019493a..7d5720958 100644 --- a/src/sw-media-auth-recovery.test.ts +++ b/src/sw-media-auth-recovery.test.ts @@ -13,19 +13,25 @@ describe('service worker media auth recovery', () => { let swTestHooks: SwTestHooks; let clients: Map; let addEventListener: ReturnType; + let persistedSessions: string | undefined; beforeEach(async () => { vi.resetModules(); clients = new Map(); addEventListener = vi.fn(); + persistedSessions = undefined; vi.stubGlobal('self', { __WB_MANIFEST: [], addEventListener, caches: { open: vi.fn(async () => ({ delete: vi.fn(async () => true), - match: vi.fn(async () => undefined), - put: vi.fn(async () => undefined), + match: vi.fn(async () => + persistedSessions ? new Response(persistedSessions) : undefined + ), + put: vi.fn(async (_key: string, response: Response) => { + persistedSessions = await response.text(); + }), })), }, clients: { @@ -76,6 +82,52 @@ describe('service worker media auth recovery', () => { expect(fetch).not.toHaveBeenCalled(); }); + it('removes a stale persisted account when a client switches users', async () => { + await swTestHooks.setSession( + 'client-a', + 'alice-token', + 'https://matrix.example.org', + '@alice:example.org' + ); + await swTestHooks.setSession( + 'client-a', + 'bob-token', + 'https://matrix.example.org', + '@bob:example.org' + ); + + expect(Object.keys(JSON.parse(persistedSessions ?? '{}'))).toEqual(['@bob:example.org']); + }); + + it('keeps a persisted account until its final client logs out', async () => { + await swTestHooks.setSession( + 'alice-a', + 'alice-token', + 'https://matrix.example.org', + '@alice:example.org' + ); + await swTestHooks.setSession( + 'alice-b', + 'alice-token', + 'https://matrix.example.org', + '@alice:example.org' + ); + await swTestHooks.setSession( + 'alice-a', + 'bob-token', + 'https://matrix.example.org', + '@bob:example.org' + ); + + expect(Object.keys(JSON.parse(persistedSessions ?? '{}')).toSorted()).toEqual([ + '@alice:example.org', + '@bob:example.org', + ]); + + await swTestHooks.setSession('alice-b', undefined, undefined); + expect(Object.keys(JSON.parse(persistedSessions ?? '{}'))).toEqual(['@bob:example.org']); + }); + it('shares recovery and preserves each Range header on retry', async () => { const client = { id: 'client-a', diff --git a/src/sw.ts b/src/sw.ts index 8795a898b..c11abfc17 100644 --- a/src/sw.ts +++ b/src/sw.ts @@ -167,7 +167,8 @@ function setSession( baseUrl: unknown, userId?: unknown ): Promise { - let persistence: Promise; + const previous = sessions.get(clientId); + const persistence: Promise[] = []; if (typeof accessToken === 'string' && typeof baseUrl === 'string') { const info: SessionInfo = { accessToken, @@ -176,13 +177,19 @@ function setSession( }; sessions.set(clientId, info); console.debug('[SW] setSession: stored', clientId, baseUrl); - persistence = info.userId ? updatePersistedSession(info.userId, info) : Promise.resolve(); + if (info.userId) persistence.push(updatePersistedSession(info.userId, info)); } else { // Logout or invalid session - const previous = sessions.get(clientId); sessions.delete(clientId); console.debug('[SW] setSession: removed', clientId); - persistence = previous?.userId ? updatePersistedSession(previous.userId) : Promise.resolve(); + } + + if ( + previous?.userId && + previous.userId !== sessions.get(clientId)?.userId && + ![...sessions.values()].some((session) => session.userId === previous.userId) + ) { + persistence.push(updatePersistedSession(previous.userId)); } const pending = pendingSessionRequests.get(clientId); @@ -191,7 +198,7 @@ function setSession( pendingSessionRequests.delete(clientId); } - return persistence; + return Promise.all(persistence).then(() => undefined); } function requestSession(client: Client): Promise {