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

147 lines
5.0 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { createVersionSource, extractVersionFromManifest, fetchAtomCodeVersion } from "../version.mjs";
const MANIFEST = '[package]\nname = "atomcode"\nversion = "9.9.9"\n\n[workspace.package]\nversion = "5.2.3"\nedition = "2021"\n';
test("只认 [workspace.package] 的 version,忽略 [package] 的", () => {
assert.equal(extractVersionFromManifest(MANIFEST), "5.2.3");
});
test("缺段 / 缺字段 / 非语义版本 → null", () => {
assert.equal(extractVersionFromManifest('[package]\nversion = "1.2.3"\n'), null);
assert.equal(extractVersionFromManifest('[workspace.package]\nedition = "2021"\n'), null);
assert.equal(extractVersionFromManifest('[workspace.package]\nversion = "nightly"\n'), null);
assert.equal(extractVersionFromManifest(""), null);
assert.equal(extractVersionFromManifest(undefined), null);
});
test("CRLF / 缩进 / 前导空白容忍", () => {
assert.equal(extractVersionFromManifest('[workspace.package]\r\n version = "6.0.1" \r\n'), "6.0.1");
});
test("fetchAtomCodeVersion:正常返回版本,非 2xx 抛错", async () => {
const version = await fetchAtomCodeVersion({
fetchImpl: async () => new Response(MANIFEST, { status: 200 }),
});
assert.equal(version, "5.2.3");
await assert.rejects(
fetchAtomCodeVersion({ fetchImpl: async () => new Response("nope", { status: 404 }) }),
/manifest returned 404/,
);
});
test("版本源:冷启动 ensure 立即返回兜底,后台完成后再升级", async () => {
let finish;
const pending = new Promise((resolve) => {
finish = resolve;
});
const src = createVersionSource({
fallback: "5.2.3",
fetchImpl: async () => {
await pending;
return new Response('[workspace.package]\nversion = "6.0.1"\n');
},
});
assert.equal(src.get(), "5.2.3");
assert.equal(await src.ensure(), "5.2.3");
finish();
await waitFor(() => src.get() === "6.0.1");
assert.equal(src.get(), "6.0.1");
});
test("TTL 内不重复拉取", async () => {
let calls = 0;
const src = createVersionSource({
fallback: "5.2.3",
fetchImpl: async () => {
calls++;
return new Response('[workspace.package]\nversion = "6.0.1"\n');
},
});
await src.ensure();
await src.ensure();
assert.equal(calls, 1);
});
test("拉取失败:退回兜底、标记 quiet,失败窗口内不再碰网、只回调一次 onError", async () => {
let calls = 0;
const logs = [];
const src = createVersionSource({
fallback: "5.2.3",
failTtlMs: 60_000,
fetchImpl: async () => {
calls++;
throw new Error("offline");
},
onError: (e) => logs.push(e.message),
});
assert.equal(await src.ensure(), "5.2.3");
assert.equal(await src.ensure(), "5.2.3");
assert.equal(calls, 1);
assert.deepEqual(logs, ["offline"]);
});
test("fetchAtomCodeVersion:超过 1 MiB 的清单边读边停,不会先整块吃下", async () => {
let pulls = 0;
const chunk = new Uint8Array(64 * 1024);
const stream = new ReadableStream({
pull(controller) {
pulls += 1;
if (pulls > 200) return controller.close();
controller.enqueue(chunk);
return undefined;
},
});
await assert.rejects(
fetchAtomCodeVersion({ fetchImpl: async () => new Response(stream, { status: 200 }) }),
/exceeds 1 MiB/,
);
// 1 MiB / 64 KiB = 16 块;读到第 17 块就该中止,远不到 200。
assert.ok(pulls <= 30, `应在越过上限时立刻停止读取,实际读了 ${pulls} 块`);
});
test("fetchAtomCodeVersion:上限之内的较大清单仍能解析出真实版本", async () => {
const padding = "#".repeat(200 * 1024);
const text = `[workspace.package]\nversion = "6.1.0"\n${padding}\n`;
const version = await fetchAtomCodeVersion({
fetchImpl: async () => new Response(text, { status: 200 }),
});
assert.equal(version, "6.1.0");
});
test("poke:后台拉取完成后 get() 升级(宿主常驻场景)", async () => {
const src = createVersionSource({
fallback: "5.2.3",
fetchImpl: async () => new Response('[workspace.package]\nversion = "6.0.0"\n'),
});
assert.equal(src.get(), "5.2.3");
src.poke();
await new Promise((resolve) => setTimeout(resolve, 10));
assert.equal(src.get(), "6.0.0");
});
test("版本源报告来源并通过 onChange 通知持久化观察者", async () => {
const changes = [];
const src = createVersionSource({
fallback: "5.2.3",
fetchImpl: async () => new Response('[workspace.package]\nversion = "6.2.0"\n'),
onChange: (status) => changes.push(status),
});
assert.equal(src.status().source, "fallback");
src.poke();
await waitFor(() => src.get() === "6.2.0");
assert.equal(src.status().source, "manifest");
assert.equal(changes.at(-1).version, "6.2.0");
assert.equal(changes.at(-1).live, true);
});
async function waitFor(predicate, timeoutMs = 1000) {
const deadline = Date.now() + timeoutMs;
while (!predicate()) {
if (Date.now() >= deadline) throw new Error("condition was not met before timeout");
await new Promise((resolve) => setTimeout(resolve, 5));
}
}