Files
plugins/packages/atomcode/test/quota.test.mjs
T
LEOLI 8765cf8f9c atomcode 0.3.0: AtomGit CodingPlan provider plugin (LILEOO, #716)
Adds packages/atomcode (@magpie-community/opencode-atomcode-auth 0.3.0), an AtomGit CodingPlan provider: browser OAuth and AtomCode CLI sign-in (~/.atomcode/auth.toml), signed requests to AtomGit's own gateway, per-account models and quota, daily claim, a loopback-only diagnostics page, and OpenCode 1.x/2.x entries. Registry and README entries added. By LILEOO.

Reviewed at c57b099fff4689c0198c1c6414f04b27614b4092. The last blocking point (an inline TOML comment on a known line went into the uid and was written back) is fixed: values end at an unquoted #, quoted/escaped/literal strings keep their #, and a rewritten line keeps its comment.

Run on a separate test machine (Bun 1.4.2, sandbox HOME):
- PR head merged onto origin/main 24da52b8: bun scripts/check.mjs exit 0 (atomcode ok); whole-repo bun test 6103 pass, 0 fail. origin/main alone: 5942 pass, 0 fail.
- packages/atomcode at the head: bun test 161 pass; node --test test/*.test.mjs 161 pass.
- The new auth-file test against 55ff272's auth-file.mjs: red (1 fail), green at the head.
- Probe: the review's file (id = "123456" # c) parses to uid 123456 and renders back as id = "123456" # c; also #c with no space, CRLF with a tab before #, "1#2", and a literal-string id all round-trip to the same uid. At 55ff272 each gave a uid with the comment in it.

Not tried: a real AtomGit account. A [user] header with a trailing comment is refused ("no user id"), not corrupted; the CLI never writes one. First npm version needs an owner publish; until then magpie installs it from the dist branch.
2026-11-05 16:54:02 +08:00

247 lines
6.3 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { strings } from "../i18n.mjs";
import { quotaFromStatus } from "../quota.mjs";
const t = strings("en");
test("quotaFromStatus maps every rate-limit window", () => {
const usage = quotaFromStatus(
{
codingplan_free: {
plan_name: "CodingPlan Pro",
expires_at: "2026-12-01",
},
current_usage: {
usage_percent: 99,
reset_at: "2026-11-01T10:00:00Z",
},
rate_limit_windows: [
{
window_hours: 5,
call_limit: 200,
calls_used: 50,
usage_percent: 25,
seconds_until_reset: 1200,
},
{
window_size_seconds: 86400,
call_limit: 1000,
calls_used: 250,
usage_percent: 25,
},
],
},
t,
{ user: "alice" },
);
assert.equal(usage.plan, "CodingPlan Pro");
assert.equal(usage.user, "alice");
assert.equal(usage.until, "2026-12-01T15:59:59.999Z");
assert.equal(usage.windows.length, 2);
assert.deepEqual(usage.windows[0], {
name: "5h window",
used: 25,
span: 18000,
amount: 50,
unit: "calls",
limit: 200,
display: "50 / 200 calls",
resetSecs: 1200,
});
assert.equal(usage.windows[1].name, "24h window");
assert.equal(usage.windows[1].display, "250 / 1000 calls");
});
test("quotaFromStatus falls back to current_usage when no window rows exist", () => {
const usage = quotaFromStatus(
{
codingplan_free: { plan_name: "CodingPlan Lite" },
current_usage: {
usage_percent: 120,
window_hours: 3,
seconds_until_reset: 60,
},
},
t,
);
assert.deepEqual(usage.windows, [
{
name: "3h window",
used: 120,
span: 10800,
unit: "calls",
resetSecs: 60,
},
]);
});
test("quotaFromStatus returns a stable empty shape for malformed status", () => {
assert.deepEqual(quotaFromStatus(null, t), {
plan: "AtomCode CodingPlan",
windows: [],
});
});
test("quotaFromStatus keeps unknown shares unknown and trusts each window's own data", () => {
const usage = quotaFromStatus(
{
codingplan_free: { plan_name: "Plan", remaining_days: null, expires_at: "2099-12-31" },
current_usage: { usage_percent: 99, window_hours: 5, reset_at: "2099-12-31T10:00:00" },
rate_limit_windows: [
{ window_hours: 5, call_limit: 100, usage_percent: 12, reset_at: "2099-12-31T10:00:00" },
{ window_hours: 24 },
],
},
t,
);
assert.equal(usage.windows[0].used, 12);
assert.equal(usage.windows[0].amount, undefined);
assert.equal(usage.windows[1].used, undefined);
assert.equal(usage.windows[1].amount, undefined);
});
test("quotaFromStatus adds a token window when the current window declares tokens", () => {
const usage = quotaFromStatus(
{
codingplan_free: { plan_name: "Plan" },
current_usage: {
unit: "tokens",
window_hours: 5,
window_tokens_used: 250000,
window_token_limit: 1000000,
seconds_until_reset: 60,
},
rate_limit_windows: [],
},
t,
);
assert.equal(usage.windows.length, 1);
assert.equal(usage.windows[0].unit, "tokens");
assert.equal(usage.windows[0].used, 25);
assert.equal(usage.windows[0].amount, 250000);
assert.equal(usage.windows[0].limit, 1000000);
});
test("quotaFromStatus ignores legacy token-named call counters in current_usage", () => {
const usage = quotaFromStatus(
{
codingplan_free: { plan_name: "Plan" },
current_usage: {
window_hours: 5,
window_tokens_used: 237,
window_token_limit: 1600,
usage_percent: 14,
seconds_until_reset: 60,
},
rate_limit_windows: [
{
window_size_seconds: 18000,
window_hours: 5,
call_limit: 1600,
calls_used: 237,
usage_percent: 14,
seconds_until_reset: 60,
},
],
},
t,
);
assert.equal(usage.windows.length, 1);
assert.equal(usage.windows[0].unit, "calls");
assert.equal(usage.windows[0].amount, 237);
assert.equal(usage.windows[0].limit, 1600);
});
test("quotaFromStatus separates token and call rows and honours explicit units", () => {
const usage = quotaFromStatus(
{
codingplan_free: { plan_name: "Plan" },
current_usage: {
usage_percent: 20,
window_tokens_used: 500,
window_token_limit: 1000,
window_hours: 5,
},
rate_limit_windows: [
{
unit: "tokens",
window_hours: 24,
tokens_used: 400,
token_limit: 2000,
token_usage_percent: 20,
},
{
unit: "calls",
window_hours: 5,
requests_used: 10,
requests_limit: 100,
},
{
window_hours: 7,
call_limit: 50,
calls_used: 5,
},
],
},
t,
);
assert.deepEqual(
usage.windows.map((window) => [window.unit, window.span, window.used]),
[
["tokens", 86400, 20],
["calls", 18000, 10],
["calls", 25200, 10],
],
);
assert.equal(usage.windows[0].display, "400 / 2000 tokens");
assert.equal(usage.windows[1].display, "10 / 100 calls");
});
test("quotaFromStatus deduplicates current_usage by unit, not span alone", () => {
const usage = quotaFromStatus(
{
codingplan_free: { plan_name: "Plan" },
current_usage: {
usage_percent: 30,
window_hours: 5,
window_tokens_used: 300,
window_token_limit: 1000,
},
rate_limit_windows: [
{
unit: "tokens",
window_hours: 5,
tokens_used: 300,
token_limit: 1000,
},
],
},
t,
);
assert.deepEqual(
usage.windows.map((window) => [window.unit, window.span, window.used]),
[
["tokens", 18000, 30],
["calls", 18000, 30],
],
);
});
test("quotaFromStatus keeps an explicit token window unknown instead of inventing zero", () => {
const usage = quotaFromStatus(
{
codingplan_free: { plan_name: "Plan" },
current_usage: {},
rate_limit_windows: [{ unit: "tokens", window_hours: 3 }],
},
t,
);
assert.equal(usage.windows.length, 1);
assert.equal(usage.windows[0].unit, "tokens");
assert.equal(usage.windows[0].used, undefined);
assert.equal(usage.windows[0].amount, undefined);
});