Files
AgentHub/server/__tests__/quota.test.js
T
nntrivi2001 daf5b7be92 feat: edit in the Changes panel, git graph history, plan quota (v2.3.0)
- Changes panel: a working file opens in a CodeMirror editor with its diff
  inline (DiffEditor.tsx, @codemirror/merge, lazy-loaded); Changes folds the
  unchanged lines, Whole file shows them; side by side at 50/50 and full
  width; a Revert per block; original from HEAD or the branch's merge base
  (`mergeBase` on GET /git/changes?range=branch)
- Every diff: green = line added whole, red = removed whole, blue = edited
  in place (--diff-mod)
- History drawn as a commit graph (#58: `parents` on GET /git/log,
  --date-order; lib/commitGraph.ts)
- Per-workspace never-stage list, drawn orange in the Files tree (--kept);
  closed Terminal rows past three deleted
- Status bar plan quota for Claude (per account) and Codex
  (GET /api/pricing/quota, server/lib/quota.js); compact hover panel that
  stays open, Refresh button with a spinner
- Settings → Appearance holds language, theme and pet; Ctrl+Shift+C copies;
  compact sidebar navigation and short session times
2026-10-06 15:38:25 +07:00

110 lines
4.2 KiB
JavaScript

/**
* @file Tests server/lib/quota.js: a rollout's `rate_limits`
* window mapped to the status bar's label/percent/reset, and a window that has
* reset since Codex last wrote reading as empty, and the Codex login's
* account + expiry read from `auth.json`; and a saved Claude profile asked
* with its own token only while it is valid, never refreshed.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { test } = require("node:test");
const assert = require("node:assert");
const { codexWindow, windowLabel } = require("../lib/quota");
test("windowLabel names the plan windows", () => {
assert.strictEqual(windowLabel(300), "5h");
assert.strictEqual(windowLabel(10080), "wk");
assert.strictEqual(windowLabel(43200), "mo");
assert.strictEqual(windowLabel(2880), "2d");
});
test("codexWindow keeps a live window and empties a reset one", () => {
const future = Math.floor(Date.now() / 1000) + 3600;
assert.deepStrictEqual(
codexWindow({ used_percent: 27, window_minutes: 300, resets_at: future }, Date.now()),
{ label: "5h", usedPercent: 27, resetsAt: new Date(future * 1000).toISOString() }
);
const past = codexWindow(
{ used_percent: 92, window_minutes: 10080, resets_in_seconds: 60 },
Date.now() - 120_000
);
assert.strictEqual(past.usedPercent, 0);
assert.strictEqual(codexWindow(null, 0), null);
});
test("codexAccount reads the email and access-token expiry, never a token", () => {
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { codexAccount } = require("../lib/quota");
const jwt = (claims) => `h.${Buffer.from(JSON.stringify(claims)).toString("base64url")}.s`;
const home = fs.mkdtempSync(path.join(os.tmpdir(), "quota-codex-"));
const prev = process.env.CODEX_HOME;
process.env.CODEX_HOME = home;
try {
assert.deepStrictEqual(codexAccount(), {});
fs.writeFileSync(
path.join(home, "auth.json"),
JSON.stringify({
tokens: { id_token: jwt({ email: "a@b.c" }), access_token: jwt({ exp: 1_800_000_000 }) },
})
);
assert.deepStrictEqual(codexAccount(), {
account: "a@b.c",
expiresAt: new Date(1_800_000_000_000).toISOString(),
});
} finally {
if (prev === undefined) delete process.env.CODEX_HOME;
else process.env.CODEX_HOME = prev;
fs.rmSync(home, { recursive: true, force: true });
}
});
test("a saved profile is asked with its own token only while valid, never refreshed", async () => {
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { quotas, __resetCache } = require("../lib/quota");
const home = fs.mkdtempSync(path.join(os.tmpdir(), "quota-claude-"));
const dir = path.join(home, "agenthub-profiles");
fs.mkdirSync(dir);
const profile = (name, token, expiresAt) =>
fs.writeFileSync(
path.join(dir, `${name}.json`),
JSON.stringify({
label: name,
account: { accountUuid: name, emailAddress: `${name}@x.y` },
credentials: { accessToken: token, refreshToken: "r", expiresAt },
})
);
profile("fresh", "tok-fresh", Date.now() + 3600_000);
profile("stale", "tok-stale", Date.now() - 1000);
const prev = { home: process.env.CLAUDE_HOME, fetch: global.fetch };
const asked = [];
process.env.CLAUDE_HOME = home;
global.fetch = async (url, opts) => {
asked.push(`${url} ${opts.headers.Authorization}`);
return {
status: 200,
ok: true,
json: async () => ({ five_hour: { utilization: 40, resets_at: null } }),
};
};
try {
__resetCache();
const claude = (await quotas()).tools.filter((t) => t.id === "claude");
assert.deepStrictEqual(asked, ["https://api.anthropic.com/api/oauth/usage Bearer tok-fresh"]);
const by = Object.fromEntries(claude.map((t) => [t.label, t]));
assert.strictEqual(by.fresh.active, false);
assert.strictEqual(by.fresh.windows[0].usedPercent, 40);
assert.strictEqual(by.stale.error, "EPROFILEEXPIRED");
assert.strictEqual(by.stale.account, "stale@x.y");
} finally {
if (prev.home === undefined) delete process.env.CLAUDE_HOME;
else process.env.CLAUDE_HOME = prev.home;
global.fetch = prev.fetch;
__resetCache();
fs.rmSync(home, { recursive: true, force: true });
}
});