fix(quota): derive Codex windows from duration
This commit is contained in:
@@ -487,16 +487,18 @@ const fetchCodexQuota = async (): Promise<ProviderResult> => {
|
|||||||
|
|
||||||
const windows: Record<string, UsageWindow> = {};
|
const windows: Record<string, UsageWindow> = {};
|
||||||
if (primary) {
|
if (primary) {
|
||||||
windows['5h'] = toUsageWindow({
|
const windowSeconds = toNumber(primary.limit_window_seconds);
|
||||||
|
windows[resolveWindowLabel(windowSeconds)] = toUsageWindow({
|
||||||
usedPercent: toNumber(primary.used_percent),
|
usedPercent: toNumber(primary.used_percent),
|
||||||
windowSeconds: toNumber(primary.limit_window_seconds),
|
windowSeconds,
|
||||||
resetAt: toTimestamp(primary.reset_at),
|
resetAt: toTimestamp(primary.reset_at),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (secondary) {
|
if (secondary) {
|
||||||
windows['weekly'] = toUsageWindow({
|
const windowSeconds = toNumber(secondary.limit_window_seconds);
|
||||||
|
windows[resolveWindowLabel(windowSeconds)] = toUsageWindow({
|
||||||
usedPercent: toNumber(secondary.used_percent),
|
usedPercent: toNumber(secondary.used_percent),
|
||||||
windowSeconds: toNumber(secondary.limit_window_seconds),
|
windowSeconds,
|
||||||
resetAt: toTimestamp(secondary.reset_at),
|
resetAt: toTimestamp(secondary.reset_at),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
toUsageWindow,
|
toUsageWindow,
|
||||||
toNumber,
|
toNumber,
|
||||||
toTimestamp,
|
toTimestamp,
|
||||||
|
resolveWindowLabel,
|
||||||
formatMoney
|
formatMoney
|
||||||
} from '../utils/index.js';
|
} from '../utils/index.js';
|
||||||
|
|
||||||
@@ -65,16 +66,18 @@ export const fetchQuota = async () => {
|
|||||||
|
|
||||||
const windows = {};
|
const windows = {};
|
||||||
if (primary) {
|
if (primary) {
|
||||||
windows['5h'] = toUsageWindow({
|
const windowSeconds = toNumber(primary.limit_window_seconds);
|
||||||
|
windows[resolveWindowLabel(windowSeconds)] = toUsageWindow({
|
||||||
usedPercent: toNumber(primary.used_percent),
|
usedPercent: toNumber(primary.used_percent),
|
||||||
windowSeconds: toNumber(primary.limit_window_seconds),
|
windowSeconds,
|
||||||
resetAt: toTimestamp(primary.reset_at)
|
resetAt: toTimestamp(primary.reset_at)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (secondary) {
|
if (secondary) {
|
||||||
windows['weekly'] = toUsageWindow({
|
const windowSeconds = toNumber(secondary.limit_window_seconds);
|
||||||
|
windows[resolveWindowLabel(windowSeconds)] = toUsageWindow({
|
||||||
usedPercent: toNumber(secondary.used_percent),
|
usedPercent: toNumber(secondary.used_percent),
|
||||||
windowSeconds: toNumber(secondary.limit_window_seconds),
|
windowSeconds,
|
||||||
resetAt: toTimestamp(secondary.reset_at)
|
resetAt: toTimestamp(secondary.reset_at)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||||
|
|
||||||
|
vi.mock('../../opencode/auth.js', () => ({
|
||||||
|
readAuthFile: () => ({ openai: { access: 'test-token' } }),
|
||||||
|
}));
|
||||||
|
|
||||||
|
import { fetchQuota } from './codex.js';
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
});
|
||||||
|
|
||||||
|
const mockUsage = (rateLimit) => {
|
||||||
|
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: async () => ({ rate_limit: rateLimit }),
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('Codex quota windows', () => {
|
||||||
|
it('labels a weekly-only primary window from its duration', async () => {
|
||||||
|
mockUsage({
|
||||||
|
primary_window: {
|
||||||
|
used_percent: 3,
|
||||||
|
limit_window_seconds: 604800,
|
||||||
|
reset_at: 1784491827,
|
||||||
|
},
|
||||||
|
secondary_window: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await fetchQuota();
|
||||||
|
|
||||||
|
expect(result.usage.windows.weekly.usedPercent).toBe(3);
|
||||||
|
expect(result.usage.windows['5h']).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('labels five-hour and weekly windows from their durations', async () => {
|
||||||
|
mockUsage({
|
||||||
|
primary_window: { used_percent: 10, limit_window_seconds: 18000 },
|
||||||
|
secondary_window: { used_percent: 20, limit_window_seconds: 604800 },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await fetchQuota();
|
||||||
|
|
||||||
|
expect(result.usage.windows['5h'].usedPercent).toBe(10);
|
||||||
|
expect(result.usage.windows.weekly.usedPercent).toBe(20);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user