Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const mocks = vi.hoisted(() => ({ restoreInOffscreen: vi.fn(), getStoredSession: vi.fn(), markSessionExpired: vi.fn(), remove: vi.fn(), createAlarm: vi.fn(),}))
vi.mock('./lib/oauth-offscreen', () => ({ restoreInOffscreen: mocks.restoreInOffscreen }))vi.mock('./lib/session-store', () => ({ SESSION_EXPIRED_KEY: 'sessionExpired', getStoredSession: mocks.getStoredSession, markSessionExpired: mocks.markSessionExpired,}))
const { SESSION_REFRESH_ALARM, SESSION_REFRESH_PERIOD_MINUTES, refreshStoredSession, scheduleSessionRefresh,} = await import('./session-refresh')
const SESSION = { did: 'did:plc:abc' }
beforeEach(() => { vi.clearAllMocks() mocks.getStoredSession.mockResolvedValue(SESSION) vi.stubGlobal('chrome', { alarms: { create: mocks.createAlarm }, storage: { local: { remove: mocks.remove } }, })})afterEach(() => vi.unstubAllGlobals())
describe('scheduleSessionRefresh', () => { it('creates one named daily alarm, replacing any existing one', () => { scheduleSessionRefresh() expect(mocks.createAlarm).toHaveBeenCalledWith(SESSION_REFRESH_ALARM, { delayInMinutes: 1, periodInMinutes: SESSION_REFRESH_PERIOD_MINUTES, }) })})
describe('refreshStoredSession', () => { it('does nothing when there is no session to keep alive', async () => { mocks.getStoredSession.mockResolvedValue(undefined) await refreshStoredSession('test') expect(mocks.restoreInOffscreen).not.toHaveBeenCalled() expect(mocks.markSessionExpired).not.toHaveBeenCalled() })
it('rotates the token with refresh forced', async () => { mocks.restoreInOffscreen.mockResolvedValue({ status: 'ok' }) await refreshStoredSession('test') expect(mocks.restoreInOffscreen).toHaveBeenCalledWith(SESSION.did, true) expect(mocks.remove).toHaveBeenCalledWith('sessionExpired') })
it('marks an ended session expired so the badge appears', async () => { mocks.restoreInOffscreen.mockResolvedValue({ status: 'expired', reason: 'invalid_grant' }) await refreshStoredSession('test') expect(mocks.markSessionExpired).toHaveBeenCalledTimes(1) expect(mocks.remove).not.toHaveBeenCalled() })
// The reason the restore result is three-valued rather than a boolean: a // downed PDS must not sign anyone out. it('leaves the session alone when the PDS is unreachable', async () => { mocks.restoreInOffscreen.mockResolvedValue({ status: 'unreachable', reason: 'Failed to fetch' }) await refreshStoredSession('test') expect(mocks.markSessionExpired).not.toHaveBeenCalled() expect(mocks.remove).not.toHaveBeenCalled() })
it('does not even open an OAuth host while offline', async () => { vi.stubGlobal('navigator', { onLine: false }) await refreshStoredSession('test') expect(mocks.restoreInOffscreen).not.toHaveBeenCalled() expect(mocks.markSessionExpired).not.toHaveBeenCalled() })
// Chrome fires startup and install together. Two restores would spend the // same refresh token twice; one shared restore keeps the rotation single. it('shares one restore between concurrent startup and update checks', async () => { let finish!: (outcome: { status: 'ok' }) => void mocks.restoreInOffscreen.mockReturnValue( new Promise((resolve) => { finish = resolve }), )
const startup = refreshStoredSession('startup') const update = refreshStoredSession('installed:chrome_update') await vi.waitFor(() => expect(mocks.restoreInOffscreen).toHaveBeenCalledTimes(1))
finish({ status: 'ok' }) await Promise.all([startup, update])
expect(mocks.restoreInOffscreen).toHaveBeenCalledTimes(1) expect(mocks.remove).toHaveBeenCalledWith('sessionExpired') })})