Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions packages/account/src/core/session.scheduler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof fetch>()
.mockImplementationOnce(async () => mintResponse('jwt-1'))
Expand All @@ -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()
Expand All @@ -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' }
}
])
})

Expand Down Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions packages/account/src/core/sessionContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,16 @@ export type SessionFailure = Extract<SessionResult, { status: 'error' }>
* 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 }
Expand Down
Loading