Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032import { PasswordSession, type PasswordSessionOptions, type SessionData,} from '@atproto/lex-password-session'import {beforeEach, describe, expect, it, jest} from '@jest/globals'
import {type SessionAccount} from '../types'
jest.mock('#/state/events', () => ({ emitNetworkConfirmed: jest.fn(), emitNetworkLost: jest.fn(),}))
/* * `makeSessionHooks` reports a throwing `onSessionChange` through the logger * rather than letting it escape into PasswordSession's session promise. Stub * the module so that report is assertable (and so nothing reaches the real * transports). */const mockLoggerError = jest.fn()jest.mock('#/logger', () => { const noopLogger = { error: (...args: unknown[]) => mockLoggerError(...args), warn: () => {}, info: () => {}, log: () => {}, debug: () => {}, } return { logger: noopLogger, Logger: { create: () => noopLogger, Context: new Proxy({}, {get: (_t, key) => String(key)}), Level: {}, }, }})
/* * The factory tail awaits `features.refresh(...)`, the one genuine prep await * in each factory (moderation config is synchronous), so it is where the * factory tests inject a mid-prep token rotation. Stubbing the analytics module * also keeps GrowthBook (and its native deps) out of this lightweight suite. * The default is a no-op; tests install behavior through `duringPreparation`. */const mockFeaturesRefresh = jest.fn<() => Promise<void>>(() => Promise.resolve(),)jest.mock('#/analytics', () => ({ features: {refresh: () => mockFeaturesRefresh()},}))
/* * `configureModerationForAccount` is synchronous (the labeler cache is a local * MMKV read), so it is not a prep await - but it still runs inside each factory * with the freshly built bundle. The factory tests capture the bundle with this * mock, then inject a real refresh into the awaited gate refresh so a token * rotation happens during prep, before arm(). * The default is a no-op so other tests are unaffected. * (jest requires out-of-scope factory references to be `mock`-prefixed.) */const mockConfigureModerationForAccount = jest.fn<(bundle: unknown, account: unknown) => void>()jest.mock('../moderation', () => ({ configureModerationForAccount: (bundle: unknown, account: unknown) => mockConfigureModerationForAccount(bundle, account), configureModerationForGuest: () => {},}))
jest.mock('jwt-decode', () => ({ jwtDecode(token: string) { if (token === 'queued-access-jwt') { return {scope: 'com.atproto.signupQueued'} } /* * A far-future exp so isSessionExpired() reads this stored token as still * valid, which routes resume() through the sync (no-network) fast path. * That isolates the prep-time refresh as the ONLY token rotation. */ if (token === 'valid-access-jwt') { return {scope: 'com.atproto.access', exp: 4102444800} } return {scope: 'com.atproto.access'} },}))
import { type AtpSessionEvent, buildBundle, createSessionBundleFromStoredAccount, disposeBundle, finishPreparation, makeSessionHooks, type PasswordSessionBundle as SessionBundle, registerBundleKillSwitch, sessionAccountToSessionData, sessionDataToSessionAccount,} from '../session-core'import { asFetch, DID, HANDLE, makeAccount, makeDidDoc, makeMockFetch, PDS_HOST as PDS_URL, SERVICE,} from './mock-fetch'
function synthDidDoc( did: string, pdsUrl: string,): NonNullable<SessionData['didDoc']> { return makeDidDoc(pdsUrl, did)}
function makeSessionData(overrides: Partial<SessionData> = {}): SessionData { return { accessJwt: 'access-jwt', refreshJwt: 'refresh-jwt', handle: HANDLE, did: DID, email: 'alice@example.com', emailConfirmed: true, emailAuthFactor: false, active: true, service: 'https://blacksky.app', ...overrides, }}
describe('sessionDataToSessionAccount', () => { it('returns undefined for a missing session', () => { expect(sessionDataToSessionAccount(undefined, 'https://blacksky.app')).toBe( undefined, ) expect(sessionDataToSessionAccount(null, 'https://blacksky.app')).toBe( undefined, ) })
it('maps fields for a hosted account (no didDoc)', () => { const account = sessionDataToSessionAccount( makeSessionData(), 'https://blacksky.app', )! expect(account).toEqual({ service: 'https://blacksky.app/', did: DID, handle: HANDLE, email: 'alice@example.com', emailConfirmed: true, emailAuthFactor: false, refreshJwt: 'refresh-jwt', accessJwt: 'access-jwt', signupQueued: false, active: true, status: undefined, pdsUrl: undefined, isSelfHosted: false, }) })
it('serializes service as a normalized URL', () => { const account = sessionDataToSessionAccount( makeSessionData(), 'https://blacksky.app', )! expect(account.service).toBe('https://blacksky.app/') })
it('serializes the didDoc PDS endpoint as a normalized URL', () => { const account = sessionDataToSessionAccount( makeSessionData({didDoc: synthDidDoc(DID, PDS_URL)}), 'https://blacksky.app', )! expect(account.pdsUrl).toBe(`${PDS_URL}/`) })
it('leaves pdsUrl undefined for hosted accounts (no service fallback)', () => { const account = sessionDataToSessionAccount( makeSessionData({didDoc: undefined}), 'https://blacksky.app', )! expect(account.pdsUrl).toBe(undefined) })
it('retains the stored PDS when a valid didDoc has no PDS service', () => { const account = sessionDataToSessionAccount( makeSessionData({didDoc: {id: DID}}), 'https://blacksky.app', PDS_URL, )! expect(account.pdsUrl).toBe(`${PDS_URL}/`) })
it('derives isSelfHosted from the service URL', () => { const hosted = sessionDataToSessionAccount( makeSessionData(), 'https://blacksky.app', )! expect(hosted.isSelfHosted).toBe(false)
const selfHosted = sessionDataToSessionAccount( makeSessionData({service: 'https://pds.example.com'}), 'https://pds.example.com', )! expect(selfHosted.isSelfHosted).toBe(true) })
it('derives signupQueued from the access token scope', () => { const queued = sessionDataToSessionAccount( makeSessionData({accessJwt: 'queued-access-jwt'}), 'https://blacksky.app', )! expect(queued.signupQueued).toBe(true)
const notQueued = sessionDataToSessionAccount( makeSessionData(), 'https://blacksky.app', )! expect(notQueued.signupQueued).toBe(false) })
it('coerces missing email flags to false', () => { const account = sessionDataToSessionAccount( makeSessionData({ email: undefined, emailConfirmed: undefined, emailAuthFactor: undefined, }), 'https://blacksky.app', )! expect(account.email).toBe(undefined) expect(account.emailConfirmed).toBe(false) expect(account.emailAuthFactor).toBe(false) })
it('preserves the exact SessionAccount field order', () => { /* * Byte-stability guard: the reducer's JSON.stringify fast path and the * session test snapshots depend on this exact persisted key order. */ const account = sessionDataToSessionAccount( makeSessionData({didDoc: synthDidDoc(DID, PDS_URL)}), 'https://blacksky.app', )! const golden: SessionAccount = { service: 'https://blacksky.app/', did: DID, handle: HANDLE, email: 'alice@example.com', emailConfirmed: true, emailAuthFactor: false, refreshJwt: 'refresh-jwt', accessJwt: 'access-jwt', signupQueued: false, active: true, status: undefined, pdsUrl: `${PDS_URL}/`, isSelfHosted: false, } expect(Object.keys(account)).toEqual(Object.keys(golden)) expect(JSON.stringify(account)).toBe(JSON.stringify(golden)) })})
describe('sessionAccountToSessionData', () => { const baseAccount: SessionAccount = { service: 'https://blacksky.app/', did: DID, handle: HANDLE, email: 'alice@example.com', emailConfirmed: true, emailAuthFactor: false, refreshJwt: 'refresh-jwt', accessJwt: 'access-jwt', signupQueued: false, active: true, status: undefined, pdsUrl: undefined, isSelfHosted: false, }
it('maps fields with empty-string token fallbacks and active default', () => { const data = sessionAccountToSessionData({ ...baseAccount, accessJwt: undefined, refreshJwt: undefined, active: undefined, }) expect(data.accessJwt).toBe('') expect(data.refreshJwt).toBe('') expect(data.active).toBe(true) expect(data.did).toBe(DID) expect(data.handle).toBe(HANDLE) expect(data.service).toBe('https://blacksky.app/') })
it('omits didDoc when the account has no stored pdsUrl', () => { const data = sessionAccountToSessionData(baseAccount) expect('didDoc' in data).toBe(false) })
it('does not synthesize protocol data from a stored pdsUrl', () => { const data = sessionAccountToSessionData({ ...baseAccount, pdsUrl: `${PDS_URL}/`, }) expect('didDoc' in data).toBe(false) })
it('round-trips account -> SessionData -> account preserving all fields', () => { const withPds: SessionAccount = { ...baseAccount, pdsUrl: `${PDS_URL}/`, } for (const account of [baseAccount, withPds]) { const data = sessionAccountToSessionData(account) const roundTripped = sessionDataToSessionAccount( data, account.service, account.pdsUrl, )! expect(roundTripped).toEqual(account) expect(JSON.stringify(roundTripped)).toBe(JSON.stringify(account)) } })
it('round-trips signupQueued via the access token scope', () => { const queued: SessionAccount = { ...baseAccount, accessJwt: 'queued-access-jwt', signupQueued: true, } const roundTripped = sessionDataToSessionAccount( sessionAccountToSessionData(queued), queued.service, )! expect(roundTripped.signupQueued).toBe(true) expect(roundTripped).toEqual(queued) })
it('round-trips a self-hosted account', () => { const selfHosted: SessionAccount = { ...baseAccount, service: 'https://pds.example.com/', pdsUrl: 'https://pds.example.com/', isSelfHosted: true, } const roundTripped = sessionDataToSessionAccount( sessionAccountToSessionData(selfHosted), selfHosted.service, selfHosted.pdsUrl, )! expect(roundTripped).toEqual(selfHosted) })})
describe('createSessionBundleFromStoredAccount', () => { it('builds three clients over one session', async () => { const result = createSessionBundleFromStoredAccount( makeAccount(), jest.fn(), )!
/* every client reads its identity straight through the shared session */ expect(result.bundle.session.session.accessJwt).toBe('access-jwt') expect(result.bundle.appviewClient.did).toBe(DID) expect(result.bundle.pdsClient.did).toBe(DID) expect(result.bundle.chatClient.did).toBe(DID) expect(result.bundle.service.toString()).toBe(`${SERVICE}/`) disposeBundle(result.bundle) /* disposal disables the transport every client shares */ await expect( result.bundle.session.fetchHandler('/xrpc/test', {}), ).rejects.toThrow('session disposed') })
it('disposes a bundle rejected by the activation guard', async () => { const onSessionChange = jest.fn() let rejectedBundle: SessionBundle | undefined const result = createSessionBundleFromStoredAccount( makeAccount(), onSessionChange, bundle => { rejectedBundle = bundle return false }, )
expect(result).toBeUndefined() await expect( rejectedBundle!.session.fetchHandler('/xrpc/test', {}), ).rejects.toThrow('session disposed') expect(onSessionChange).not.toHaveBeenCalled() })})
describe('makeSessionHooks arm-latch + event mapping', () => { /* * The hooks read neither `this` (the PasswordSession) nor their data * argument, so we invoke them with empty stand-ins cast to the declared * parameter types. This keeps the test focused on the arm-latch + event * mapping. */ const fakeSession = {} as PasswordSession const fakeData = {} as SessionData
function setup() { const onSessionChange = jest.fn< ( bundle: SessionBundle, did: string, event: AtpSessionEvent, sessionData?: SessionData, ) => void >() /* the hook only passes this through by identity; a stub bundle suffices */ const bundle = {} as SessionBundle const hooks = makeSessionHooks({ onSessionChange, getBundle: () => bundle, getDid: () => DID, }) return {onSessionChange, bundle, hooks} }
it('swallows events before arm()', () => { const {onSessionChange, hooks} = setup() void hooks.onUpdated?.call(fakeSession, fakeData) expect(onSessionChange).not.toHaveBeenCalled() })
it("maps onUpdated -> 'update' after arm(), passing the bundle + payload through", () => { const {onSessionChange, bundle, hooks} = setup() hooks.arm() void hooks.onUpdated?.call(fakeSession, fakeData) expect(onSessionChange).toHaveBeenCalledTimes(1) expect(onSessionChange.mock.calls[0][0]).toBe(bundle) expect(onSessionChange.mock.calls[0][1]).toBe(DID) expect(onSessionChange.mock.calls[0][2]).toBe('update') /* the fresh SessionData the library delivers is threaded through verbatim */ expect(onSessionChange.mock.calls[0][3]).toBe(fakeData) })
it("maps onDeleted -> 'expired' after arm()", () => { const {onSessionChange, hooks} = setup() hooks.arm() void hooks.onDeleted?.call(fakeSession, fakeData) expect(onSessionChange.mock.calls[0][2]).toBe('expired') })
it("maps onUpdateFailure -> 'network-error' after arm()", () => { const {onSessionChange, hooks} = setup() hooks.arm() void hooks.onUpdateFailure?.call( fakeSession, fakeData, {} as Parameters<NonNullable<typeof hooks.onUpdateFailure>>[1], ) expect(onSessionChange.mock.calls[0][2]).toBe('network-error') })
it("threads the dying session payload on the 'expired' path", () => { /* * onDeleted maps to 'expired' AND threads the dying SessionData through * (the library hands onDeleted the session being destroyed, before it nulls * its internal state). The provider reads the dying refreshJwt from this * payload to drive the cross-tab expiry rescue. The provider still guards * `refreshedAccount` on `event === 'update' && sessionData`, so the payload * on 'expired' does NOT produce a refreshedAccount (reducer still clears * tokens + logs out when no rescue applies) - see the provider test below. */ const {onSessionChange, hooks} = setup() hooks.arm() void hooks.onDeleted?.call(fakeSession, fakeData) expect(onSessionChange.mock.calls[0][2]).toBe('expired') expect(onSessionChange.mock.calls[0][3]).toBe(fakeData) })})
/* * The exact derivation from the provider's onSessionChange (index.tsx). Pinned * here because payload threading and this mapping together read tokens from * the delivered payload on 'update' and force * undefined on the drop paths so the reducer logs the user out. */function deriveRefreshedAccount( event: AtpSessionEvent, sessionData?: SessionData,): SessionAccount | undefined { return event === 'update' && sessionData ? sessionDataToSessionAccount(sessionData, sessionData.service) : undefined}
/* * `PasswordSession` fires onUpdated with the fresh session BEFORE committing it * internally, so the live getter is still stale at hook time. */describe('session-hook payload threading (pre-commit ordering)', () => { it('delivers the NEW tokens via the payload even though the live getter is still pre-commit stale', async () => { const fetchMock = makeMockFetch() let session!: PasswordSession let liveGetterAtHookTime: SessionAccount | undefined let refreshedAccountAtHookTime: SessionAccount | undefined const onSessionChange = jest.fn( ( _bundle: SessionBundle, _did: string, event: AtpSessionEvent, sessionData?: SessionData, ) => { /* Capture the live getter to demonstrate its pre-commit state. */ liveGetterAtHookTime = sessionDataToSessionAccount( session.session, session.session.service, ) /* Derive fresh data from the delivered payload. */ refreshedAccountAtHookTime = deriveRefreshedAccount(event, sessionData) }, ) const hooks = makeSessionHooks({ onSessionChange, getBundle: () => ({}) as SessionBundle, getDid: () => DID, }) session = new PasswordSession(sessionAccountToSessionData(makeAccount()), { ...hooks, fetch: asFetch(fetchMock), }) hooks.arm()
await session.refresh()
/* at hook time the live getter still held the previous tokens */ expect(liveGetterAtHookTime?.accessJwt).toBe('access-jwt') /* the payload already contains the fresh tokens */ expect(refreshedAccountAtHookTime?.accessJwt).toBe('access-jwt-2') expect(refreshedAccountAtHookTime?.refreshJwt).toBe('refresh-jwt-2') /* and the session does eventually commit those same tokens */ expect(session.session.accessJwt).toBe('access-jwt-2') })
it("yields refreshedAccount === undefined on the 'expired' path (forces logout)", async () => { const fetchMock = makeMockFetch({ 'com.atproto.server.refreshSession': () => new Response( JSON.stringify({error: 'ExpiredToken', message: 'Token expired'}), {status: 400, headers: {'content-type': 'application/json'}}, ), }) let refreshedAccountAtHookTime: SessionAccount | undefined = makeAccount() let observedEvent: AtpSessionEvent | undefined let observedSessionData: SessionData | undefined const onSessionChange = jest.fn( ( _bundle: SessionBundle, _did: string, event: AtpSessionEvent, sessionData?: SessionData, ) => { observedEvent = event observedSessionData = sessionData refreshedAccountAtHookTime = deriveRefreshedAccount(event, sessionData) }, ) const hooks = makeSessionHooks({ onSessionChange, getBundle: () => ({}) as SessionBundle, getDid: () => DID, }) const session = new PasswordSession( sessionAccountToSessionData(makeAccount()), {...hooks, fetch: asFetch(fetchMock)}, ) hooks.arm()
await expect(session.refresh()).rejects.toBeDefined()
expect(observedEvent).toBe('expired') /* * The dying SessionData IS threaded on 'expired' (its refreshJwt drives the * provider's cross-tab rescue), but it does NOT become a refreshedAccount: * deriveRefreshedAccount only maps the 'update' path, so the reducer still * sees `undefined` and logs out when no rescue applies. */ expect(observedSessionData?.refreshJwt).toBe('refresh-jwt') expect(refreshedAccountAtHookTime).toBe(undefined) })})
/* * `PasswordSession` exposes no local * destroy, so disposeBundle neutralizes the session by tripping the flag inside * the injected fetch - after disposal every request (direct or auto-refresh, * which shares this same captured fetch) throws before touching the network. */describe('disposeBundle kill-switch', () => { it('the injected fetch throws after disposeBundle', () => { const hooks = makeSessionHooks({ onSessionChange: jest.fn(), getBundle: () => ({}) as SessionBundle, getDid: () => DID, }) /* the injected fetch is the kill-switch wrapper makeSessionHooks bakes in */ const injectedFetch = hooks.fetch!
/* * A live session is required for disposeBundle to act (it early-returns on * a null/destroyed session). */ const session = new PasswordSession( sessionAccountToSessionData(makeAccount()), {...hooks}, ) const bundle = buildBundle(session) registerBundleKillSwitch(bundle, hooks.kill)
/* * Before disposal the wrapper does NOT throw synchronously - it delegates * to the async networkAwareFetch and returns a promise. Swallow that * promise's rejection (the real network is unavailable under jest); we only * care that no synchronous throw happened here. */ const pending = injectedFetch('https://bsky.social/xrpc/x') expect(pending).toBeInstanceOf(Promise) void pending.catch(() => {})
disposeBundle(bundle)
/* after disposal every call through the injected fetch throws */ expect(() => injectedFetch('https://bsky.social/xrpc/x')).toThrow( 'session disposed', ) })})
/* * PasswordSession lifecycle over a mocked fetch, covering the resume fast path * and the hooks that makeSessionHooks maps into reducer events. */describe('PasswordSession lifecycle over mocked fetch', () => { it('resume fast path: constructing does not hit the network', () => { const fetchMock = makeMockFetch() /* not expired -> new PasswordSession(...) with no refresh */ void new PasswordSession(sessionAccountToSessionData(makeAccount()), { fetch: asFetch(fetchMock), }) expect(fetchMock.mock.calls.length).toBe(0) })
it('resume network path fires onUpdated with fresh tokens', async () => { const fetchMock = makeMockFetch() const onUpdated = jest.fn<NonNullable<PasswordSessionOptions['onUpdated']>>() const session = await PasswordSession.resume( sessionAccountToSessionData(makeAccount()), {fetch: asFetch(fetchMock), onUpdated}, ) expect(onUpdated).toHaveBeenCalledTimes(1) expect(session.session.accessJwt).toBe('access-jwt-2') })
it('onDeleted fires when refresh returns a declared invalid-token error', async () => { const onDeleted = jest.fn<NonNullable<PasswordSessionOptions['onDeleted']>>() const onUpdated = jest.fn<NonNullable<PasswordSessionOptions['onUpdated']>>() const fetchMock = makeMockFetch({ 'com.atproto.server.refreshSession': () => new Response( JSON.stringify({error: 'ExpiredToken', message: 'Token expired'}), {status: 400, headers: {'content-type': 'application/json'}}, ), }) const session = new PasswordSession( sessionAccountToSessionData(makeAccount()), {fetch: asFetch(fetchMock), onDeleted, onUpdated}, ) await expect(session.refresh()).rejects.toBeDefined() expect(onDeleted).toHaveBeenCalledTimes(1) expect(onUpdated).not.toHaveBeenCalled() })
it('onUpdateFailure fires on a transient (500) refresh error, session preserved', async () => { const onDeleted = jest.fn<NonNullable<PasswordSessionOptions['onDeleted']>>() const onUpdateFailure = jest.fn<NonNullable<PasswordSessionOptions['onUpdateFailure']>>() const fetchMock = makeMockFetch({ 'com.atproto.server.refreshSession': () => new Response(JSON.stringify({error: 'InternalServerError'}), { status: 500, headers: {'content-type': 'application/json'}, }), }) const session = new PasswordSession( sessionAccountToSessionData(makeAccount()), { fetch: asFetch(fetchMock), onDeleted, onUpdateFailure, }, ) await session.refresh() expect(onUpdateFailure).toHaveBeenCalledTimes(1) expect(onDeleted).not.toHaveBeenCalled() /* session data is preserved (still the original tokens) */ expect(session.session.accessJwt).toBe('access-jwt') })})
/* * A refresh through a bundle's armed hooks: the app has no `refreshSession` api * of its own, so every rotation the reducer sees originates here, in * `PasswordSession.refresh()`. On success the armed hooks dispatch exactly one * 'update' event and the post-refresh snapshot carries the rotated tokens; * rejections propagate to the caller. */describe('PasswordSession.refresh through armed hooks', () => { it('refresh() resolves updated data and the armed hooks dispatch exactly one update', async () => { const fetchMock = makeMockFetch() const onSessionChange = jest.fn< (bundle: SessionBundle, did: string, event: AtpSessionEvent) => void >() const bundle = {} as SessionBundle const hooks = makeSessionHooks({ onSessionChange, getBundle: () => bundle, getDid: () => DID, }) /* * makeSessionHooks bakes in networkAwareFetch (the real global fetch); * override it with the mock while keeping the arm-latched callbacks (they * close over the same `armed` flag, so hooks.arm() below still applies). */ const session = new PasswordSession( sessionAccountToSessionData(makeAccount()), {...hooks, fetch: asFetch(fetchMock)}, ) hooks.arm()
await session.refresh()
/* the refreshed tokens are live on the session */ expect(session.session.accessJwt).toBe('access-jwt-2') /* exactly one 'update' event reached the reducer via the armed hooks */ expect(onSessionChange).toHaveBeenCalledTimes(1) expect(onSessionChange.mock.calls[0][2]).toBe('update')
/* the post-refresh SessionAccount snapshot carries the rotated tokens */ const snapshot = sessionDataToSessionAccount( session.session, session.session.service, )! expect(snapshot.accessJwt).toBe('access-jwt-2') expect(snapshot.refreshJwt).toBe('refresh-jwt-2') })
it('propagates a rejection from refresh() (invalid session)', async () => { const fetchMock = makeMockFetch({ 'com.atproto.server.refreshSession': () => new Response( JSON.stringify({error: 'ExpiredToken', message: 'Token expired'}), {status: 400, headers: {'content-type': 'application/json'}}, ), }) const session = new PasswordSession( sessionAccountToSessionData(makeAccount()), {fetch: asFetch(fetchMock)}, ) await expect(session.refresh()).rejects.toBeDefined() })})
/** * Load a fresh factory graph whose network leaf captures `fetch`. * * `session-core`'s network leaf reads `globalThis.fetch` at module load, and * that captured fetch is what `PasswordSession`'s auto-refresh routes through, * so the module has to be re-required after the override is in place. */async function withFreshFactory( fetch: typeof globalThis.fetch, run: (core: typeof import('../session-core')) => Promise<void>,) { const realFetch = globalThis.fetch globalThis.fetch = fetch try { await jest.isolateModulesAsync(async () => { const core = require('../session-core') as typeof import('../session-core') await run(core) }) } finally { globalThis.fetch = realFetch }}
/* * The resume/login factories must snapshot the returned account after * the prep awaits, not before. A 401 during prep triggers PasswordSession's * internal auto-refresh (rotating BOTH tokens and firing an onUpdated the * disarmed latch drops); an early snapshot would persist the stale refreshJwt, * which is dead on the next cold start. * * We simulate the mid-prep rotation by capturing the bundle from the mocked * `configureModerationForAccount` and making the mocked gate refresh (a * genuine prep await in each factory) run a real `session.refresh()`. The * factory itself is re-required inside * `jest.isolateModulesAsync` AFTER overriding `globalThis.fetch`, because * the network leaf captures `globalThis.fetch` at module load - and that * captured fetch is what PasswordSession's auto-refresh routes through. *//* * A factory starts the gate refresh before it has built the bundle, so the * refresh is held open until the moderation step hands the bundle over, and * only then runs `step`. */function duringPreparation(step: (bundle: SessionBundle) => Promise<unknown>) { const captured: {bundle?: SessionBundle} = {} let built!: (bundle: SessionBundle) => void const bundleBuilt = new Promise<SessionBundle>(resolve => { built = resolve }) mockConfigureModerationForAccount.mockImplementationOnce( (bundle: unknown) => { captured.bundle = bundle as SessionBundle built(captured.bundle) }, ) mockFeaturesRefresh.mockImplementationOnce(async () => { await step(await bundleBuilt) }) return captured}
function resetPreparationMocks() { mockConfigureModerationForAccount.mockReset() mockFeaturesRefresh.mockReset() mockFeaturesRefresh.mockImplementation(() => Promise.resolve())}
describe('factory account snapshot after preparation', () => { beforeEach(resetPreparationMocks)
it('resume: returned account carries the tokens rotated DURING prep', async () => { /* * A refresh mid-prep rotates the session to access-jwt-2/refresh-jwt-2. * `valid-access-jwt` decodes as non-expired, so resume() takes the sync * fast path and the only refresh is the one prep triggers. The bundle is * captured from the (synchronous) moderation call, and the rotation is * injected into the awaited gate refresh. */ duringPreparation(bundle => bundle.session.refresh()) const fetchMock = makeMockFetch()
await withFreshFactory(asFetch(fetchMock), async core => { const {account, bundle} = await core.createSessionBundleAndResume( makeAccount({accessJwt: 'valid-access-jwt'}), jest.fn(), ) /* the moderation prep step ran the refresh */ expect(mockConfigureModerationForAccount).toHaveBeenCalledTimes(1) /* the RETURNED account carries the POST-prep (rotated) tokens */ expect(account.accessJwt).toBe('access-jwt-2') expect(account.refreshJwt).toBe('refresh-jwt-2') /* and it matches the session's committed state */ expect(bundle.session.session.accessJwt).toBe('access-jwt-2') }) })
it('resume: returned account falls back to the stored account when the fast path yields no live token change', async () => { /* * With no mid-prep refresh, the snapshot still reflects the valid stored * tokens. */ mockConfigureModerationForAccount.mockReturnValueOnce(undefined) const fetchMock = makeMockFetch()
await withFreshFactory(asFetch(fetchMock), async core => { const {account} = await core.createSessionBundleAndResume( makeAccount({accessJwt: 'valid-access-jwt'}), jest.fn(), ) expect(account.accessJwt).toBe('valid-access-jwt') expect(account.refreshJwt).toBe('refresh-jwt') }) })})
/* * Preparation is the window between building the bundle and arming its hooks. * The session is live but its events are swallowed, so a session that dies in * here would otherwise leave the app holding a bundle that looks signed in and * can only make unauthenticated requests, with nothing left to log it out. * Both failure modes must therefore dispose the bundle and reject. */describe('a session destroyed or rejected during preparation', () => { beforeEach(resetPreparationMocks)
it('resume: rejects with the revoked-during-prep error and disposes the bundle', async () => { /* * A revoked refresh token: the session's own refresh during prep gets a * declared invalid-token error, which destroys it. `logout()` is the only * way to drive a session to `destroyed` from outside, and it takes the same * `deleteSession` -> onDeleted -> destroyed path the real 401 rescue does. */ const captured = duringPreparation(bundle => bundle.session.logout()) const fetchMock = makeMockFetch()
await withFreshFactory(asFetch(fetchMock), async core => { /* * The clean error, not PasswordSession's opaque `Logged out` getter * throw, which is what a naive `snapshot()` on a destroyed session would * surface. */ await expect( core.createSessionBundleAndResume( makeAccount({accessJwt: 'valid-access-jwt'}), jest.fn(), ), ).rejects.toThrow('Session was revoked while it was being prepared')
/* the bundle the caller never received reads as logged out */ expect(captured.bundle!.session.destroyed).toBe(true) }) })
it('resume: a prep rejection propagates and disposes the bundle', async () => { const captured = duringPreparation(() => Promise.reject(new Error('prefetch blew up')), ) const fetchMock = makeMockFetch()
await withFreshFactory(asFetch(fetchMock), async core => { await expect( core.createSessionBundleAndResume( makeAccount({accessJwt: 'valid-access-jwt'}), jest.fn(), ), ).rejects.toThrow('prefetch blew up')
/* the still-live session was disposed rather than left refreshing */ await expect( captured.bundle!.session.fetchHandler('/xrpc/test', {}), ).rejects.toThrow('session disposed') }) })
it('finishPreparation disposes and rethrows without running the snapshot', async () => { const hooks = makeSessionHooks({ onSessionChange: jest.fn(), getBundle: () => bundle, getDid: () => DID, }) const session = new PasswordSession( sessionAccountToSessionData(makeAccount()), /* * The hooks' own `fetch` is what the kill switch disables, so it must not * be overridden here. Nothing in this test reaches the network: the * post-disposal `fetchHandler` call throws before dispatching. */ hooks, ) const bundle = buildBundle(session) registerBundleKillSwitch(bundle, hooks.kill) const snapshot = jest.fn(() => 'never')
await expect( finishPreparation(bundle, Promise.reject(new Error('nope')), snapshot), ).rejects.toThrow('nope')
expect(snapshot).not.toHaveBeenCalled() await expect(bundle.session.fetchHandler('/xrpc/test', {})).rejects.toThrow( 'session disposed', ) })})
/* * A hook must never throw: PasswordSession awaits its hooks inside the * assignment to its internal session promise, so an escaping throw would leave * that promise permanently rejected - every later request fails, while the * session is never marked destroyed, so disposeBundle cannot even see that the * bundle is dead. */describe('a throwing onSessionChange does not brick the session', () => { beforeEach(() => { mockLoggerError.mockClear() })
it('reports through logger.error and leaves the session usable', async () => { const fetchMock = makeMockFetch() const onSessionChange = jest.fn(() => { throw new Error('reducer side effect exploded') }) let bundle!: SessionBundle const hooks = makeSessionHooks({ onSessionChange, getBundle: () => bundle, getDid: () => DID, }) const session = new PasswordSession( sessionAccountToSessionData(makeAccount()), {...hooks, fetch: asFetch(fetchMock)}, ) bundle = buildBundle(session) hooks.arm()
await session.refresh()
expect(onSessionChange).toHaveBeenCalledTimes(1) expect(mockLoggerError).toHaveBeenCalledTimes(1) expect(mockLoggerError.mock.calls[0][1]).toEqual({ message: "session: onSessionChange threw for a 'update' event", })
/* the session still committed the rotation, and can still refresh again */ expect(session.session.accessJwt).toBe('access-jwt-2') await expect(session.refresh()).resolves.toBeDefined() await expect( session.fetchHandler('/xrpc/app.bsky.actor.getProfile', {}), ).resolves.toBeDefined() })})