diff --git a/packages/account/src/core/session.scheduler.test.ts b/packages/account/src/core/session.scheduler.test.ts index 136dd0de5ed..4aa9a3731d1 100644 --- a/packages/account/src/core/session.scheduler.test.ts +++ b/packages/account/src/core/session.scheduler.test.ts @@ -312,7 +312,7 @@ describe('opt-in refresh scheduler', () => { }) it('reports each scheduled outcome to the host hook, and only scheduled ones', async () => { - const outcomes: string[] = [] + const reported: ScheduledRefreshReport[] = [] const fetchImpl = vi .fn() .mockImplementationOnce(async () => mintResponse('jwt-1')) @@ -322,7 +322,7 @@ describe('opt-in refresh scheduler', () => { const client = makeClient({ fetchImpl, refreshScheduler: { - onScheduledOutcome: (report) => outcomes.push(report.outcome) + onScheduledOutcome: (report) => reported.push(report) } }) const identity = manualIdentity() @@ -333,26 +333,32 @@ describe('opt-in refresh scheduler', () => { expect(client.getToken()).toBe('jwt-1') }) expect( - outcomes, + reported, 'the login mint is not a scheduled refresh; cloud does not track it' ).toEqual([]) await vi.advanceTimersByTimeAsync( NINETY_MINUTES_MS - DEFAULT_BUFFER_MS + 10 ) - expect(outcomes).toEqual(['retry_scheduled']) + expect(reported).toEqual([{ outcome: 'retry_scheduled' }]) await vi.advanceTimersByTimeAsync(5000 + 10) - expect(outcomes).toEqual(['retry_scheduled', 'succeeded']) + expect(reported).toEqual([ + { outcome: 'retry_scheduled' }, + { outcome: 'succeeded' } + ]) await vi.advanceTimersByTimeAsync(NINETY_MINUTES_MS * 10) - expect(outcomes).toEqual([ - 'retry_scheduled', - 'succeeded', - 'retry_scheduled', - 'retry_scheduled', - 'retry_scheduled', - 'retries_exhausted', - 'expired' + expect(reported).toEqual([ + { outcome: 'retry_scheduled' }, + { outcome: 'succeeded' }, + { outcome: 'retry_scheduled' }, + { outcome: 'retry_scheduled' }, + { outcome: 'retry_scheduled' }, + { outcome: 'retries_exhausted' }, + { + outcome: 'expired', + failure: { status: 'error', code: 'TOKEN_EXCHANGE_FAILED' } + } ]) }) @@ -437,6 +443,20 @@ describe('opt-in refresh scheduler', () => { ).toEqual(['ACCESS_DENIED', 'permanent_failure', 'succeeded']) }) + it('rejects a success carrying a failure that reaches the contract through a variable', () => { + const builtElsewhere = { + outcome: 'succeeded' as const, + failure: { status: 'error' as const, code: 'ACCESS_DENIED' as const } + } + // @ts-expect-error a success cannot carry a failure payload + const laundered: ScheduledRefreshReport = builtElsewhere + + expect( + laundered.outcome, + 'the @ts-expect-error above is the coverage, and only failure?: never earns it; excess-property checking accepts an invalid report once it is no longer a fresh object literal' + ).toBe('succeeded') + }) + it('reports a permanent scheduled failure to the host hook', async () => { const outcomes: string[] = [] const fetchImpl = vi diff --git a/packages/account/src/core/sessionContracts.ts b/packages/account/src/core/sessionContracts.ts index 2ef59584d50..575a558d53c 100644 --- a/packages/account/src/core/sessionContracts.ts +++ b/packages/account/src/core/sessionContracts.ts @@ -66,11 +66,16 @@ export type SessionFailure = Extract * outcomes that committed a failure carry it and the rest structurally cannot, * so a permanent failure without its error — or a success with one — cannot be * represented. + * + * `failure?: never` is what makes "structurally cannot" true rather than + * aspirational. Without it, excess-property checking still rejects a bad + * object literal, but a report built in a variable or returned from a helper + * assigns cleanly. */ export type ScheduledRefreshReport = - | { readonly outcome: 'succeeded' } - | { readonly outcome: 'retry_scheduled' } - | { readonly outcome: 'retries_exhausted' } + | { readonly outcome: 'succeeded'; readonly failure?: never } + | { readonly outcome: 'retry_scheduled'; readonly failure?: never } + | { readonly outcome: 'retries_exhausted'; readonly failure?: never } | { readonly outcome: 'permanent_failure'; readonly failure: SessionFailure } /** Retries ran out and the credential reached expiry; the client failed closed. */ | { readonly outcome: 'expired'; readonly failure: SessionFailure }