Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto substandard.blog
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485import { 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() })})