+67
-339
@@ -1,7 +1,6 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import type { Schema } from "effect";
|
||||
import { normalizeQuota, QuotaTracker } from "../src/quota.js";
|
||||
import { formatQuotaWindow, loadQuotaStatus } from "../src/tui.js";
|
||||
|
||||
const plus = {
|
||||
plan_type: "plus",
|
||||
@@ -24,11 +23,35 @@ const plus = {
|
||||
},
|
||||
};
|
||||
|
||||
test("normalizes representative Plus and Pro payloads and hides absent windows", () => {
|
||||
expect(normalizeQuota(plus, 123)).toEqual({
|
||||
plan: "plus",
|
||||
fetchedAt: 123,
|
||||
windows: [
|
||||
test("normalizes Plus and Pro payloads, hiding absent windows and excluded Spark and reserve limits", () => {
|
||||
{
|
||||
expect(normalizeQuota(plus, 123)).toEqual({
|
||||
plan: "plus",
|
||||
fetchedAt: 123,
|
||||
windows: [
|
||||
{
|
||||
id: "codex:primary",
|
||||
label: null,
|
||||
seconds: 18_000,
|
||||
used: 12.5,
|
||||
resetsAt: 2_000_000_000_000,
|
||||
},
|
||||
{
|
||||
id: "codex:secondary",
|
||||
label: null,
|
||||
seconds: 604_800,
|
||||
used: 41,
|
||||
resetsAt: 2_000_500_000_000,
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(normalizeQuota({ ...plus, plan_type: "pro" }, 123)?.windows).toHaveLength(2);
|
||||
expect(
|
||||
normalizeQuota({
|
||||
plan_type: "pro",
|
||||
rate_limit: { primary_window: plus.rate_limit.primary_window },
|
||||
})?.windows,
|
||||
).toEqual([
|
||||
{
|
||||
id: "codex:primary",
|
||||
label: null,
|
||||
@@ -36,124 +59,54 @@ test("normalizes representative Plus and Pro payloads and hides absent windows",
|
||||
used: 12.5,
|
||||
resetsAt: 2_000_000_000_000,
|
||||
},
|
||||
{
|
||||
id: "codex:secondary",
|
||||
label: null,
|
||||
seconds: 604_800,
|
||||
used: 41,
|
||||
resetsAt: 2_000_500_000_000,
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(normalizeQuota({ ...plus, plan_type: "pro" }, 123)?.windows).toHaveLength(2);
|
||||
expect(
|
||||
normalizeQuota({
|
||||
plan_type: "pro",
|
||||
rate_limit: { primary_window: plus.rate_limit.primary_window },
|
||||
})?.windows,
|
||||
).toEqual([
|
||||
{
|
||||
id: "codex:primary",
|
||||
label: null,
|
||||
seconds: 18_000,
|
||||
used: 12.5,
|
||||
resetsAt: 2_000_000_000_000,
|
||||
},
|
||||
]);
|
||||
expect(normalizeQuota({ plan_type: "plus", rate_limit: null })).toBeUndefined();
|
||||
});
|
||||
|
||||
test("normalizes additional named limits with stable IDs distinct from equal durations", () => {
|
||||
const normalized = normalizeQuota({
|
||||
...plus,
|
||||
additional_rate_limits: [
|
||||
{
|
||||
limit_name: "GPT-5.6 Luna",
|
||||
metered_feature: "codex_luna",
|
||||
rate_limit: { primary_window: plus.rate_limit.primary_window },
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(normalized?.windows.map((window) => window.id)).toEqual([
|
||||
"codex:primary",
|
||||
"codex:secondary",
|
||||
"codex_luna:primary",
|
||||
]);
|
||||
expect(normalized?.windows[2]?.label).toBe("GPT-5.6 Luna");
|
||||
expect(
|
||||
normalizeQuota({
|
||||
plan_type: "pro",
|
||||
rate_limit: null,
|
||||
]);
|
||||
expect(normalizeQuota({ plan_type: "plus", rate_limit: null })).toBeUndefined();
|
||||
}
|
||||
{
|
||||
const normalized = normalizeQuota({
|
||||
...plus,
|
||||
additional_rate_limits: [
|
||||
{
|
||||
limit_name: "gpt 5_3 codex spark",
|
||||
metered_feature: "CODEX-BENGALFOX",
|
||||
rate_limit: {
|
||||
primary_window: { used_percent: "invalid" },
|
||||
},
|
||||
},
|
||||
{
|
||||
limit_name: "Luna Reserve",
|
||||
metered_feature: "codex_luna",
|
||||
rate_limit: { primary_window: { used_percent: "invalid" } },
|
||||
},
|
||||
{
|
||||
limit_name: "Luna overflow",
|
||||
metered_feature: "codex_luna_reserve",
|
||||
rate_limit: { primary_window: { used_percent: "invalid" } },
|
||||
},
|
||||
{
|
||||
limit_name: "GPT-5.6 Luna",
|
||||
metered_feature: "codex_luna",
|
||||
rate_limit: { primary_window: plus.rate_limit.primary_window },
|
||||
},
|
||||
],
|
||||
})?.windows.map((window) => window.id),
|
||||
).toEqual(["codex_luna:primary"]);
|
||||
});
|
||||
|
||||
test("ignores Spark and representative reserve limits while preserving other quotas", () => {
|
||||
const normalized = normalizeQuota({
|
||||
...plus,
|
||||
additional_rate_limits: [
|
||||
{
|
||||
limit_name: "gpt 5_3 codex spark",
|
||||
metered_feature: "CODEX-BENGALFOX",
|
||||
rate_limit: {
|
||||
primary_window: { used_percent: "invalid" },
|
||||
},
|
||||
},
|
||||
{
|
||||
limit_name: "Luna Reserve",
|
||||
metered_feature: "codex_luna",
|
||||
rate_limit: { primary_window: { used_percent: "invalid" } },
|
||||
},
|
||||
{
|
||||
limit_name: "Luna overflow",
|
||||
metered_feature: "codex_luna_reserve",
|
||||
rate_limit: { primary_window: { used_percent: "invalid" } },
|
||||
},
|
||||
{
|
||||
limit_name: "GPT-5.6 Luna",
|
||||
metered_feature: "codex_luna",
|
||||
rate_limit: { primary_window: plus.rate_limit.primary_window },
|
||||
},
|
||||
{
|
||||
limit_name: "Preserved capacity",
|
||||
metered_feature: "codex_preserved",
|
||||
rate_limit: { primary_window: plus.rate_limit.primary_window },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(normalized?.windows.map((window) => window.id)).toEqual([
|
||||
"codex:primary",
|
||||
"codex:secondary",
|
||||
"codex_luna:primary",
|
||||
"codex_preserved:primary",
|
||||
]);
|
||||
expect(normalized?.windows.slice(2).map((window) => window.label)).toEqual([
|
||||
"GPT-5.6 Luna",
|
||||
"Preserved capacity",
|
||||
]);
|
||||
});
|
||||
|
||||
test("returns no quota for a Spark-only payload", () => {
|
||||
expect(
|
||||
normalizeQuota({
|
||||
plan_type: "pro",
|
||||
additional_rate_limits: [
|
||||
{
|
||||
limit_name: "GPT-5.3-Codex-Spark",
|
||||
metered_feature: "codex_bengalfox",
|
||||
limit_name: "Preserved capacity",
|
||||
metered_feature: "codex_preserved",
|
||||
rate_limit: { primary_window: plus.rate_limit.primary_window },
|
||||
},
|
||||
],
|
||||
}),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
expect(normalized?.windows.map((window) => window.id)).toEqual([
|
||||
"codex:primary",
|
||||
"codex:secondary",
|
||||
"codex_luna:primary",
|
||||
"codex_preserved:primary",
|
||||
]);
|
||||
expect(normalized?.windows.slice(2).map((window) => window.label)).toEqual([
|
||||
"GPT-5.6 Luna",
|
||||
"Preserved capacity",
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
function fixture(
|
||||
@@ -242,72 +195,6 @@ function fixture(
|
||||
};
|
||||
}
|
||||
|
||||
async function accountFingerprint(id: string) {
|
||||
const bytes = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`account:${id}`));
|
||||
return Buffer.from(bytes).toString("hex");
|
||||
}
|
||||
|
||||
test("reads cached snapshots and removes excluded windows", async () => {
|
||||
const cached = {
|
||||
account: await accountFingerprint("account-one"),
|
||||
latest: {
|
||||
plan: "pro",
|
||||
fetchedAt: 123,
|
||||
windows: [
|
||||
{
|
||||
id: "codex:primary",
|
||||
label: null,
|
||||
seconds: 18_000,
|
||||
used: 12.5,
|
||||
resetsAt: 2_000_000_000_000,
|
||||
},
|
||||
{
|
||||
id: "CODEX-BENGALFOX:primary",
|
||||
label: "GPT 5.3 Codex Spark",
|
||||
seconds: 18_000,
|
||||
used: 80,
|
||||
resetsAt: 2_000_000_000_000,
|
||||
},
|
||||
{
|
||||
id: "codex_luna_reserve:primary",
|
||||
label: "Luna Reserve",
|
||||
seconds: 18_000,
|
||||
used: 60,
|
||||
resetsAt: 2_000_000_000_000,
|
||||
},
|
||||
{
|
||||
id: "codex_luna:primary",
|
||||
label: "GPT-5.6 Luna",
|
||||
seconds: 18_000,
|
||||
used: 20,
|
||||
resetsAt: 2_000_000_000_000,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const result = fixture(
|
||||
{
|
||||
type: "oauth",
|
||||
access: "opaque",
|
||||
refresh: "refresh",
|
||||
metadata: { accountID: "account-one" },
|
||||
},
|
||||
[new Error("offline")],
|
||||
1_000_000,
|
||||
[Promise.resolve(cached)],
|
||||
);
|
||||
|
||||
await result.tracker.init();
|
||||
const status = result.tracker.status();
|
||||
expect(status?.windows.map((window) => window.id)).toEqual([
|
||||
"codex:primary",
|
||||
"codex_luna:primary",
|
||||
]);
|
||||
expect(status?.windows[0]).not.toHaveProperty("sessionConsumed");
|
||||
expect(result.calls()).toBe(1);
|
||||
await result.tracker.dispose();
|
||||
});
|
||||
|
||||
test("activates only for OAuth and refreshes quota at initialization and request start", async () => {
|
||||
const key = fixture({ type: "key", key: "secret" }, [plus]);
|
||||
await key.tracker.init();
|
||||
@@ -347,124 +234,6 @@ test("activates only for OAuth and refreshes quota at initialization and request
|
||||
await oauth.tracker.dispose();
|
||||
});
|
||||
|
||||
test("polls, tolerates errors, and clears on logout", async () => {
|
||||
const reset = {
|
||||
...plus,
|
||||
rate_limit: {
|
||||
...plus.rate_limit,
|
||||
primary_window: {
|
||||
...plus.rate_limit.primary_window,
|
||||
used_percent: 2,
|
||||
reset_at: 2_000_100_000,
|
||||
},
|
||||
},
|
||||
};
|
||||
const result = fixture(
|
||||
{
|
||||
type: "oauth",
|
||||
access: "opaque",
|
||||
refresh: "refresh",
|
||||
metadata: { accountID: "account-one" },
|
||||
},
|
||||
[plus, new Error("offline"), reset, reset, reset, reset],
|
||||
10,
|
||||
);
|
||||
await result.tracker.init();
|
||||
await result.tracker.start("root");
|
||||
await Bun.sleep(35);
|
||||
const status = result.tracker.status();
|
||||
expect(result.calls()).toBeGreaterThanOrEqual(3);
|
||||
expect(status?.windows[0]?.remaining).toBe(98);
|
||||
result.logout();
|
||||
await result.tracker.authChanged(true);
|
||||
expect(result.tracker.status()).toBeUndefined();
|
||||
const calls = result.calls();
|
||||
await result.tracker.dispose();
|
||||
await Bun.sleep(20);
|
||||
expect(result.calls()).toBe(calls);
|
||||
});
|
||||
|
||||
test("sidebar text places reset timing beside latest remaining quota", () => {
|
||||
expect(
|
||||
formatQuotaWindow(
|
||||
{
|
||||
seconds: 604_800,
|
||||
id: "codex:secondary",
|
||||
label: null,
|
||||
used: 25,
|
||||
remaining: 75,
|
||||
resetsAt: 100_000 + 2 * 86_400_000 + 5 * 3_600_000,
|
||||
},
|
||||
100_000,
|
||||
),
|
||||
).toBe("7d: 75% · 2d 5h");
|
||||
});
|
||||
|
||||
test("sidebar reset timing uses compact future units and handles boundaries", () => {
|
||||
const now = 1_000_000;
|
||||
const format = (resetIn: number) =>
|
||||
formatQuotaWindow(
|
||||
{
|
||||
seconds: 18_000,
|
||||
id: "codex:primary",
|
||||
label: null,
|
||||
used: 25,
|
||||
remaining: 75,
|
||||
resetsAt: now + resetIn,
|
||||
},
|
||||
now,
|
||||
);
|
||||
|
||||
expect(format(2 * 3_600_000 + 15 * 60_000)).toBe("5h: 75% · 2h 15m");
|
||||
expect(format(3_600_000)).toBe("5h: 75% · 1h");
|
||||
expect(format(15 * 60_000)).toBe("5h: 75% · 15m");
|
||||
expect(format(60_000)).toBe("5h: 75% · 1m");
|
||||
expect(format(59_999)).toBe("5h: 75% · <1m");
|
||||
});
|
||||
|
||||
test("sidebar reset timing preserves labels and distinguishes stale and unavailable timestamps", () => {
|
||||
const window = {
|
||||
seconds: 18_000,
|
||||
id: "codex:primary",
|
||||
label: "GPT-5.6 Luna",
|
||||
used: 99.95,
|
||||
remaining: 0.05,
|
||||
};
|
||||
expect(formatQuotaWindow({ ...window, resetsAt: 999 }, 1_000)).toBe(
|
||||
"GPT-5.6 Luna 5h: <0.1% · due",
|
||||
);
|
||||
expect(formatQuotaWindow({ ...window, resetsAt: Number.NaN }, 1_000)).toBe(
|
||||
"GPT-5.6 Luna 5h: <0.1% · unavailable",
|
||||
);
|
||||
});
|
||||
|
||||
test("request completion refreshes once across terminal and idle events", async () => {
|
||||
const used = (percent: number) => ({
|
||||
...plus,
|
||||
rate_limit: {
|
||||
...plus.rate_limit,
|
||||
primary_window: { ...plus.rate_limit.primary_window, used_percent: percent },
|
||||
},
|
||||
});
|
||||
const result = fixture(
|
||||
{
|
||||
type: "oauth",
|
||||
access: "opaque",
|
||||
refresh: "refresh",
|
||||
metadata: { accountID: "account-one" },
|
||||
},
|
||||
[used(10), used(11), used(30), new Error("temporary"), used(30), used(33), used(35)],
|
||||
);
|
||||
await result.tracker.init();
|
||||
await result.tracker.start("root");
|
||||
await result.tracker.complete("root");
|
||||
expect(result.tracker.status()?.windows[0]?.remaining).toBe(70);
|
||||
await result.tracker.complete("root");
|
||||
expect(result.calls()).toBe(3);
|
||||
expect(result.tracker.status()?.windows[0]?.remaining).toBe(70);
|
||||
await result.tracker.dispose();
|
||||
});
|
||||
|
||||
test("an auth switch detaches an in-flight old-account response and immediately fetches the new account", async () => {
|
||||
const pending = Promise.withResolvers<unknown>();
|
||||
const second = {
|
||||
@@ -507,44 +276,3 @@ test("an auth switch detaches an in-flight old-account response and immediately
|
||||
expect(result.tracker.status()?.windows[0]).not.toHaveProperty("sessionConsumed");
|
||||
await result.tracker.dispose();
|
||||
});
|
||||
|
||||
test("an auth switch during an old account storage read cannot restore stale state or polling", async () => {
|
||||
const pending = Promise.withResolvers<Schema.Json | undefined>();
|
||||
const second = {
|
||||
...plus,
|
||||
account_id: "account-two",
|
||||
rate_limit: {
|
||||
...plus.rate_limit,
|
||||
primary_window: { ...plus.rate_limit.primary_window, used_percent: 64 },
|
||||
},
|
||||
};
|
||||
const result = fixture(
|
||||
{ type: "oauth", access: "old", refresh: "old", metadata: { accountID: "account-one" } },
|
||||
[second],
|
||||
1_000_000,
|
||||
[pending.promise],
|
||||
);
|
||||
const old = result.tracker.init();
|
||||
while (result.storageCalls() === 0) await Bun.sleep(1);
|
||||
result.switch({
|
||||
type: "oauth",
|
||||
access: "new",
|
||||
refresh: "new",
|
||||
metadata: { accountID: "account-two" },
|
||||
});
|
||||
await result.tracker.authChanged(true);
|
||||
expect(result.calls()).toBe(1);
|
||||
pending.resolve(undefined);
|
||||
await old;
|
||||
expect(result.tracker.status()?.windows[0]?.used).toBe(64);
|
||||
expect(result.headers[0]?.get("chatgpt-account-id")).toBe("account-two");
|
||||
await result.tracker.dispose();
|
||||
});
|
||||
|
||||
test("TUI quota loading converts RPC failures to an empty render state", async () => {
|
||||
expect(
|
||||
await loadQuotaStatus(async () => {
|
||||
throw new Error("disconnected");
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user