Files
orca/src/shared/codex-reset-credit-scope.test.ts
T
kazu-42andBrennan Benson 4a71a0ecb2 feat(mobile): add safe Codex rate-limit resets (#9394)
* feat(mobile): add safe Codex rate-limit resets

* fix(mobile): address reset credit review feedback

* review: purge removed-account reset attempts, shared capability constant, rebase test mocks

* review: preserve host compatibility and reset durability

* fix(mobile): recover reset capability after cutover

* fix(mobile): validate runtime capability payloads

* fix(mobile): enforce capability payload contract

* fix(mobile): route mock terminals to selected worktree

* test(mobile): pin malformed probe retry behavior

---------

Co-authored-by: Brennan Benson <79079362+brennanb2025@users.noreply.github.com>
2026-07-24 14:42:04 -07:00

141 lines
4.0 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import type { ProviderRateLimits } from './rate-limit-types'
import type { CodexManagedAccountSummary } from './types'
import { buildCodexResetCreditExpectedScope } from './codex-reset-credit-scope'
const account: CodexManagedAccountSummary = {
id: 'account-host',
email: 'dev@example.com',
managedHomeRuntime: 'host',
wslDistro: null,
createdAt: 10,
updatedAt: 20,
lastAuthenticatedAt: 20
}
const limits: ProviderRateLimits = {
provider: 'codex',
session: {
usedPercent: 100,
windowMinutes: 300,
resetsAt: 1_000,
resetDescription: 'soon'
},
weekly: null,
rateLimitResetCredits: {
availableCount: 1,
totalEarnedCount: 2,
nextExpiresAt: 2_000,
credits: [{ status: 'available', expiresAt: 2_000, grantedAt: 500 }]
},
updatedAt: 30,
error: null,
status: 'ok'
}
describe('buildCodexResetCreditExpectedScope', () => {
it('builds a deterministic exact host scope', () => {
const first = buildCodexResetCreditExpectedScope({
target: { runtime: 'host', wslDistro: null },
account,
limits
})
const second = buildCodexResetCreditExpectedScope({
target: { runtime: 'host', wslDistro: null },
account,
limits: {
...limits,
rateLimitResetCredits: {
...limits.rateLimitResetCredits!,
credits: [
{ status: 'available', expiresAt: 3_000, grantedAt: 600 },
...limits.rateLimitResetCredits!.credits!
].toReversed()
}
}
})
const firstWithBothRows = buildCodexResetCreditExpectedScope({
target: { runtime: 'host', wslDistro: null },
account,
limits: {
...limits,
rateLimitResetCredits: {
...limits.rateLimitResetCredits!,
credits: [
...limits.rateLimitResetCredits!.credits!,
{ status: 'available', expiresAt: 3_000, grantedAt: 600 }
]
}
}
})
expect(firstWithBothRows).toEqual(second)
expect(first).toMatchObject({
target: { runtime: 'host', wslDistro: null },
accountId: account.id,
accountRevision: account.updatedAt,
offerRevision: expect.stringMatching(/^v1:/)
})
})
it('changes the offer revision when fetched credit or window data changes', () => {
const original = buildCodexResetCreditExpectedScope({
target: { runtime: 'host', wslDistro: null },
account,
limits
})
const refreshed = buildCodexResetCreditExpectedScope({
target: { runtime: 'host', wslDistro: null },
account,
limits: { ...limits, updatedAt: limits.updatedAt + 1 }
})
expect(refreshed?.offerRevision).not.toBe(original?.offerRevision)
})
it.each([
{
name: 'system default account',
target: { runtime: 'host', wslDistro: null } as const,
candidate: null,
candidateLimits: limits
},
{
name: 'no available credit',
target: { runtime: 'host', wslDistro: null } as const,
candidate: account,
candidateLimits: {
...limits,
rateLimitResetCredits: { ...limits.rateLimitResetCredits!, availableCount: 0 }
}
},
{
name: 'unknown WSL distro',
target: { runtime: 'wsl', wslDistro: null } as const,
candidate: { ...account, managedHomeRuntime: 'wsl' as const, wslDistro: 'Ubuntu' },
candidateLimits: limits
},
{
name: 'runtime mismatch',
target: { runtime: 'wsl', wslDistro: 'Ubuntu' } as const,
candidate: account,
candidateLimits: limits
},
{
name: 'WSL distro mismatch',
target: { runtime: 'wsl', wslDistro: 'Ubuntu' } as const,
candidate: { ...account, managedHomeRuntime: 'wsl' as const, wslDistro: 'Debian' },
candidateLimits: limits
}
])('fails closed for $name', ({ target, candidate, candidateLimits }) => {
expect(
buildCodexResetCreditExpectedScope({
target,
account: candidate,
limits: candidateLimits
})
).toBeNull()
})
})