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

59 lines
2.5 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { MASTER_KEY_HEX, signAtomGitRequest } from "../sign.mjs";
/**
* Atom2Api's independent signing fixture. This is the compatibility contract
* for every model request; a plugin release must never change it silently.
*/
test("signing fixture matches Atom2Api", () => {
const headers = signAtomGitRequest({
method: "POST",
path: "/v1/chat/completions",
body: '{"model":"deepseek-v4-flash"}',
oauthToken: "token-fixture",
userId: "user-fixture",
clientVersion: "5.0.2",
timestamp: 1767225600,
nonce: Buffer.from("000102030405060708090a0b0c0d0e0f", "hex"),
});
assert.equal(
headers["X-AtomCode-Sig"],
"v1:c5bafa716c0fdc31a6e2738412d0b21a9d9e9d1ca09cc44eb17949a1b4fa4bdd",
);
assert.equal(headers["X-AtomCode-Ts"], "1767225600");
assert.equal(headers["X-AtomCode-Nonce"], "000102030405060708090a0b0c0d0e0f");
assert.equal(headers["X-AtomCode-Alg"], "1");
assert.equal(headers["X-AtomCode-Ver"], "5.0.2");
});
test("signature covers token, user, version and raw body bytes", () => {
const base = {
method: "POST",
path: "/v1/chat/completions",
body: new TextEncoder().encode("{}"),
oauthToken: "token",
userId: "user",
clientVersion: "5.2.3",
timestamp: 1767225600,
nonce: Buffer.alloc(16, 7),
};
const signature = (overrides = {}) => signAtomGitRequest({ ...base, ...overrides })["X-AtomCode-Sig"];
assert.notEqual(signature(), signature({ oauthToken: "other-token" }));
assert.notEqual(signature(), signature({ userId: "other-user" }));
assert.notEqual(signature(), signature({ clientVersion: "5.2.4" }));
assert.notEqual(signature(), signature({ body: new TextEncoder().encode('{"x":1}') }));
});
test("signing rejects incomplete inputs instead of producing a partial signature", () => {
assert.throws(() => signAtomGitRequest({ method: "", path: "/v1", oauthToken: "t", userId: "u", clientVersion: "v" }), /method/);
assert.throws(() => signAtomGitRequest({ method: "GET", path: "v1", oauthToken: "t", userId: "u", clientVersion: "v" }), /path/);
assert.throws(() => signAtomGitRequest({ method: "GET", path: "/v1", oauthToken: "", userId: "u", clientVersion: "v" }), /token/);
assert.throws(() => signAtomGitRequest({ method: "GET", path: "/v1", oauthToken: "t", userId: "", clientVersion: "v" }), /user id/);
assert.throws(() => signAtomGitRequest({ method: "GET", path: "/v1", oauthToken: "t", userId: "u", clientVersion: "" }), /version/);
assert.match(MASTER_KEY_HEX, /^[0-9a-f]{64}$/);
});