Files
plugins/packages/atomcode/test/codingplan.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

203 lines
7.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test } from "node:test";
import assert from "node:assert/strict";
import { claimCodingPlan, fetchModels, planNeedsClaim, planOf, planTier } from "../codingplan.mjs";
test("planTier:Max/Pro 判定与 Lite 兜底(含「体验版」写法)", () => {
assert.equal(planTier("CodingPlan Max"), "Max");
assert.equal(planTier("MAX 旗舰"), "Max");
assert.equal(planTier("CodingPlan Pro"), "Pro");
assert.equal(planTier("CodingPlan Lite-体验版"), "Lite");
assert.equal(planTier("Lite-beta"), "Lite");
assert.equal(planTier(undefined), "Lite");
});
test("planOf:codingplan_free 优先,plan 兜底,缺失/非对象 → null", () => {
assert.deepEqual(planOf({ codingplan_free: { plan_name: "x" } }), { plan_name: "x" });
assert.deepEqual(planOf({ plan: { plan_name: "y" } }), { plan_name: "y" });
assert.equal(planOf({}), null);
assert.equal(planOf(null), null);
});
/** 按调用次序回放响应的小工具,多余调用复用最后一个响应。 */
function sequenceFetch(responses) {
const seen = [];
const fetchImpl = async (url, init = {}) => {
seen.push({ url, init });
const next = responses[Math.min(seen.length - 1, responses.length - 1)];
return new Response(next.body, { status: next.status ?? 200 });
};
return { fetchImpl, seen };
}
test("claim:第一档 success 即停,请求形状正确", async () => {
const { fetchImpl, seen } = sequenceFetch([
{ body: JSON.stringify({ success: true, message: "ok", plan_name: "CodingPlan Max", plan_type: "Max" }) },
]);
const r = await claimCodingPlan("https://gitcode.example", "tok", { fetchImpl, clientVersion: "5.2.3" });
assert.equal(r.claimed, true);
assert.equal(r.duplicate, false);
assert.equal(r.tier, "Max");
assert.equal(r.planName, "CodingPlan Max");
assert.equal(r.attempts.length, 1);
assert.equal(seen.length, 1);
assert.equal(new URL(seen[0].url).pathname, "/coding-plan/claim-v2");
assert.equal(JSON.parse(seen[0].init.body).plan_type, "Max");
assert.equal(seen[0].init.headers.authorization, "Bearer tok");
assert.equal(seen[0].init.headers["user-agent"], "atomcode/5.2.3");
});
test("claim:Max/Pro 失败 → Lite duplicate 停;错误响应体的 message 不丢", async () => {
const { fetchImpl } = sequenceFetch([
{ status: 400, body: JSON.stringify({ success: false, message: "无可用计划" }) },
{ status: 400, body: JSON.stringify({ success: false, message: "无可用计划" }) },
{ body: JSON.stringify({ success: false, duplicate: true, plan_type: "Lite" }) },
]);
const r = await claimCodingPlan("https://gitcode.example", "tok", { fetchImpl, clientVersion: "5.2.3" });
assert.equal(r.claimed, true);
assert.equal(r.duplicate, true);
assert.equal(r.tier, "Lite");
assert.equal(r.attempts.length, 3);
assert.equal(r.attempts[0].message, "无可用计划");
});
test("claim:三档都失败 → claimed false 且不抛出", async () => {
const { fetchImpl } = sequenceFetch([
{ status: 400, body: JSON.stringify({ success: false, message: "a" }) },
{ status: 400, body: JSON.stringify({ success: false, message: "b" }) },
{ status: 400, body: JSON.stringify({ success: false, message: "c" }) },
]);
const r = await claimCodingPlan("https://gitcode.example", "tok", { fetchImpl, clientVersion: "5.2.3" });
assert.equal(r.claimed, false);
assert.equal(r.duplicate, false);
assert.equal(r.message, "c");
});
test("planNeedsClaim:缺失/空名 → 需要领取", () => {
assert.equal(planNeedsClaim({}), true);
assert.equal(planNeedsClaim(null), true);
assert.equal(planNeedsClaim({ codingplan_free: { plan_name: " " } }), true);
});
test("planNeedsClaim:套餐有效(无日期字段也保守放行)→ false", () => {
assert.equal(planNeedsClaim({ codingplan_free: { plan_name: "CodingPlan Lite-体验版" } }), false);
assert.equal(
planNeedsClaim({ codingplan_free: { plan_name: "Lite", remaining_days: 4, expires_at: "2099-01-01" } }),
false,
);
});
test("planNeedsClaim:remaining_days ≤ 0 → 需要重领", () => {
assert.equal(
planNeedsClaim({ codingplan_free: { plan_name: "Lite", remaining_days: 0, expires_at: "2099-01-01" } }),
true,
);
assert.equal(
planNeedsClaim({ codingplan_free: { plan_name: "Lite", remaining_days: -2, expires_at: "2099-01-01" } }),
true,
);
});
test("planNeedsClaim:remaining_days 为 null/undefined 时视为未知,不误触发", () => {
for (const remaining_days of [null, undefined]) {
assert.equal(
planNeedsClaim({
codingplan_free: {
plan_name: "CodingPlan Pro",
remaining_days,
expires_at: "2099-01-01",
},
}),
false,
);
}
});
test("planNeedsClaim:expires_at 过期超 24h → 需要重领;24h 宽限内 → false", () => {
const now = Date.parse("2026-10-16T12:00:00Z");
assert.equal(
planNeedsClaim({ codingplan_free: { plan_name: "Lite", expires_at: "2026-10-14T00:00:00Z" } }, now),
true,
);
assert.equal(
planNeedsClaim({ codingplan_free: { plan_name: "Lite", expires_at: "2026-10-16T00:00:00Z" } }, now),
false,
);
assert.equal(
planNeedsClaim({ codingplan_free: { plan_name: "Lite", expires_at: "2099-01-01T00:00:00Z" } }, now),
false,
);
});
test("claim:网络异常只尝试一次,不把三档当三次重试", async () => {
let calls = 0;
const r = await claimCodingPlan("https://gitcode.example", "tok", {
fetchImpl: async () => {
calls += 1;
throw new Error("boom");
},
clientVersion: "5.2.3",
});
assert.equal(r.claimed, false);
assert.equal(calls, 1);
assert.equal(r.attempts.length, 1);
assert.match(r.message, /boom/);
});
test("claim:429/5xx/401 不继续尝试其他档位", async () => {
for (const status of [401, 429, 503]) {
let calls = 0;
const r = await claimCodingPlan("https://gitcode.example", "tok", {
fetchImpl: async () => {
calls += 1;
return new Response(JSON.stringify({ message: "stop" }), { status });
},
clientVersion: "5.2.3",
});
assert.equal(calls, 1, `HTTP ${status} should stop the cascade`);
assert.equal(r.claimed, false);
}
});
test("fetchModels:plan_type 编码进 query,过滤空名/不可用/弃用行", async () => {
const good = { display_model_name: "ok/model", context_window: 100, plan_available: true };
const { fetchImpl, seen } = sequenceFetch([
{
body: JSON.stringify([
good,
{ display_model_name: " " },
{ display_model_name: "hidden", plan_available: false },
{ display_model_name: "old", status: "deprecated" },
]),
},
]);
const rows = await fetchModels("https://gitcode.example", "tok", "Lite/dark", {
fetchImpl,
clientVersion: "5.2.3",
});
assert.equal(rows.length, 1);
assert.deepEqual(rows[0], good);
const u = new URL(seen[0].url);
assert.equal(u.pathname, "/coding-plan/models-v2");
assert.equal(u.searchParams.get("plan_type"), "Lite/dark");
assert.equal(seen[0].init.headers.authorization, "Bearer tok");
});
test("fetchModels:非数组响应抛错", async () => {
const { fetchImpl } = sequenceFetch([{ body: JSON.stringify({ nope: 1 }) }]);
await assert.rejects(
() => fetchModels("https://x", "t", "Lite", { fetchImpl, clientVersion: "5.2.3" }),
/did not return a list/,
);
});
test("CodingPlan 401/403 标记 signIn expired", async () => {
const { fetchImpl } = sequenceFetch([{ status: 401, body: "{}" }]);
const error = await fetchModels("https://x", "t", "Lite", { fetchImpl, clientVersion: "5.2.3" }).then(
() => null,
(e) => e,
);
assert.ok(error);
assert.equal(error.signIn, "expired");
});