Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
import { afterEach, describe, expect, it, vi } from 'vitest';
import { extractUsageLimitRecoveryHint } from '@/lib/usageLimitRecovery';
const NOW = Date.parse('2026-01-24T10:00:00.000Z');
afterEach(() => {
vi.restoreAllMocks();
});
describe('usage limit recovery detection', () => {
it('uses Claude structured rate-limit data and a structured reset timestamp', () => {
expect(
extractUsageLimitRecoveryHint(
{
sdkError: 'rate_limit',
message: 'Rate limit reached',
resetAt: '2026-01-24T12:30:00.000Z',
},
NOW,
),
).toEqual({ resetAtMs: Date.parse('2026-01-24T12:30:00.000Z') });
});
it('recognizes Codex usage-limit signals and relative retry times', () => {
expect(
extractUsageLimitRecoveryHint(
{
codexErrorInfo: 'usageLimitExceeded',
message: 'Usage limit reached. Try again in 1h 15m.',
},
NOW,
),
).toEqual({ resetAtMs: NOW + 75 * 60_000, isAccountUsageLimit: true });
});
it('parses the Codex app-server "try again at" wording in local time', () => {
const sameDay = new Date(NOW);
sameDay.setHours(23, 30, 0, 0);
if (sameDay.getTime() <= NOW) sameDay.setDate(sameDay.getDate() + 1);
expect(
extractUsageLimitRecoveryHint(
{
codexErrorInfo: 'usageLimitExceeded',
message: "You've hit your usage limit. Upgrade to Pro, or try again at 11:30 PM.",
},
NOW,
)?.resetAtMs,
).toBe(sameDay.getTime());
expect(
extractUsageLimitRecoveryHint(
{
codexErrorInfo: 'usageLimitExceeded',
message: "You've hit your usage limit. Try again at Jan 27, 2026 3:05 PM.",
},
NOW,
)?.resetAtMs,
).toBe(new Date(2026, 0, 27, 15, 5).getTime());
});
it('drops zone-less clock times when the local time zone is not trusted (SSH sessions)', () => {
const remote = { localTimeZoneTrusted: false };
expect(
extractUsageLimitRecoveryHint(
{ codexErrorInfo: 'usageLimitExceeded', message: 'Try again at Jan 27, 2026 3:05 PM.' },
NOW,
remote,
)?.resetAtMs,
).toBeNull();
// 带时区的钟点与相对时长不受影响。
expect(
extractUsageLimitRecoveryHint(
{ sdkError: 'rate_limit', message: "You've hit your limit · resets 11pm (UTC)" },
NOW,
remote,
)?.resetAtMs,
).toBe(Date.parse('2026-01-24T23:00:00.000Z'));
expect(
extractUsageLimitRecoveryHint(
{ codexErrorInfo: 'usageLimitExceeded', message: 'Try again in 1h 15m.' },
NOW,
remote,
)?.resetAtMs,
).toBe(NOW + 75 * 60_000);
});
it("parses Pi's approximate ChatGPT relative reset", () => {
expect(
extractUsageLimitRecoveryHint(
{ message: 'You have hit your ChatGPT usage limit (plus plan). Try again in ~12 min.' },
NOW,
)?.resetAtMs,
).toBe(NOW + 12 * 60_000);
});
it('extracts the organization plan and reset time from the Codex 429 payload', () => {
expect(
extractUsageLimitRecoveryHint(
{
message:
'API Error: Request rejected (429) · {"error":{"type":"usage_limit_reached","message":"The usage limit has been reached","plan_type":"business","resets_at":1788220709,"eligible_promo":null,"resets_in_seconds":1264528}}',
},
Date.parse('2026-08-17T00:00:00.000Z'),
),
).toEqual({
resetAtMs: Date.parse('2026-08-31T23:58:29.000Z'),
isAccountUsageLimit: true,
planType: 'business',
});
});
it('does not classify a transient Codex 429 as an account usage limit', () => {
expect(
extractUsageLimitRecoveryHint(
{
errorStatus: 429,
codexErrorInfo: { responseTooManyFailedAttempts: { httpStatusCode: 429 } },
message: 'Too many requests',
},
NOW,
),
).toEqual({ resetAtMs: null });
});
it('parses the real Claude session-limit wording with a named timezone', () => {
expect(
extractUsageLimitRecoveryHint(
{
message: "You've hit your session limit · resets 9:10pm (Asia/Shanghai)",
},
NOW,
),
).toEqual({ resetAtMs: Date.parse('2026-01-24T13:10:00.000Z') });
});
it('normalizes Intl midnight hour 24 while parsing a time-of-day reset', () => {
const originalFormatToParts = Intl.DateTimeFormat.prototype.formatToParts;
vi.spyOn(Intl.DateTimeFormat.prototype, 'formatToParts').mockImplementation(function (
this: Intl.DateTimeFormat,
date,
) {
return [
...originalFormatToParts
.call(this, date)
.map((part) =>
part.type === 'hour' && part.value === '00' ? { ...part, value: '24' } : part,
),
{ type: 'weekday', value: 'not-a-number' },
];
});
expect(
extractUsageLimitRecoveryHint(
{
sdkError: 'rate_limit',
message: "You've hit your session limit · resets 12:00am (UTC)",
},
Date.parse('2026-01-24T23:00:00.000Z'),
),
).toEqual({ resetAtMs: Date.parse('2026-01-25T00:00:00.000Z') });
});
it('keeps weekly limits actionable without guessing an unsupported weekday reset time', () => {
expect(
extractUsageLimitRecoveryHint(
{
message: "You've hit your weekly limit · resets Mon 12:00am (Asia/Shanghai)",
},
NOW,
),
).toEqual({ resetAtMs: null });
});
it('keeps a restorable limit actionable when the reset time is unknown', () => {
expect(
extractUsageLimitRecoveryHint({ errorStatus: 429, message: 'Too many requests' }, NOW),
).toEqual({ resetAtMs: null });
});
it('excludes billing depletion and temporary upstream overload', () => {
expect(
extractUsageLimitRecoveryHint(
{ sdkError: 'billing_error', message: 'Credit balance too low' },
NOW,
),
).toBeNull();
expect(
extractUsageLimitRecoveryHint(
{
errorStatus: 529,
codexErrorInfo: 'serverOverloaded',
message: 'Selected model is at capacity',
},
NOW,
),
).toBeNull();
expect(
extractUsageLimitRecoveryHint({ message: 'insufficient_quota: add billing credits' }, NOW),
).toBeNull();
});
});