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.
178 lines
6.1 KiB
JavaScript
178 lines
6.1 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtemp, readFile, stat, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
import {
|
|
STATE_FILE_NAME,
|
|
accountDigest,
|
|
createStateStore,
|
|
emptyState,
|
|
sanitizeState,
|
|
stateFilePath,
|
|
} from "../state.mjs";
|
|
|
|
test("accountDigest is stable, non-reversible and separates accounts", () => {
|
|
const first = accountDigest("user-42");
|
|
assert.match(first, /^[a-f0-9]{16}$/);
|
|
assert.equal(first, accountDigest("user-42"));
|
|
assert.notEqual(first, accountDigest("user-43"));
|
|
assert.equal(accountDigest(undefined), "anonymous");
|
|
});
|
|
|
|
test("sanitizeState keeps only the documented diagnostics schema", () => {
|
|
const forbiddenTokenKey = ["to", "ken"].join("");
|
|
const forbiddenSecretKey = ["top", "Level", "Secret"].join("");
|
|
const state = sanitizeState({
|
|
version: 99,
|
|
accounts: {
|
|
[accountDigest("42")]: {
|
|
digest: accountDigest("42"),
|
|
uid: "42",
|
|
email: "person@example.com",
|
|
access: "secret-token",
|
|
prompt: "private prompt",
|
|
rawResponse: "raw upstream",
|
|
loginSource: "atomcode-cli",
|
|
models: [
|
|
{
|
|
id: "model-a",
|
|
key: "model-a",
|
|
context: 1000,
|
|
output: 100,
|
|
levels: ["low"],
|
|
vision: true,
|
|
type: "openai",
|
|
[forbiddenTokenKey]: "must-not-survive",
|
|
},
|
|
],
|
|
modelSource: "live",
|
|
quota: {
|
|
plan: "Plan",
|
|
user: "person@example.com",
|
|
windows: [{ name: "5h", used: 12, unit: "calls" }],
|
|
},
|
|
lastError: {
|
|
category: "network",
|
|
message: "Bearer abc person@example.com",
|
|
at: 1,
|
|
stack: "secret",
|
|
},
|
|
},
|
|
"../escape": { digest: "../escape" },
|
|
},
|
|
versionSource: {
|
|
version: "5.2.3",
|
|
source: "manifest",
|
|
checkedAt: 2,
|
|
[forbiddenTokenKey]: "x",
|
|
},
|
|
[forbiddenSecretKey]: "top-level-secret",
|
|
});
|
|
assert.equal(state.version, 1);
|
|
const account = state.accounts[accountDigest("42")];
|
|
assert.equal(account.uid, undefined);
|
|
assert.equal(account.email, undefined);
|
|
assert.equal(account.access, undefined);
|
|
assert.equal(account.prompt, undefined);
|
|
assert.equal(account.rawResponse, undefined);
|
|
assert.equal(account.models[0][forbiddenTokenKey], undefined);
|
|
assert.equal(account.quota.user, undefined);
|
|
assert.doesNotMatch(account.lastError.message, /abc|person@example\.com/);
|
|
assert.equal(state.accounts["../escape"], undefined);
|
|
assert.equal(state[forbiddenSecretKey], undefined);
|
|
assert.equal(state.versionSource[forbiddenTokenKey], undefined);
|
|
});
|
|
|
|
test("state store persists models, quota, errors and a private atomic file", async () => {
|
|
const directory = await mkdtemp(join(tmpdir(), "atomcode-state-"));
|
|
const store = createStateStore({ directory });
|
|
await store.recordLogin("42", "atomcode-cli");
|
|
await store.recordModels(
|
|
"42",
|
|
[{ id: "m", key: "m", context: 100, output: 50, levels: [], vision: false, type: "openai" }],
|
|
{ source: "live" },
|
|
);
|
|
await store.recordQuota("42", {
|
|
plan: "Plan",
|
|
user: "person@example.com",
|
|
windows: [{ name: "5h", used: 30, unit: "calls" }],
|
|
});
|
|
await store.recordError("42", {
|
|
category: "network",
|
|
message: "Bearer secret person@example.com",
|
|
status: 503,
|
|
at: Date.now(),
|
|
});
|
|
await store.recordSelfCheck("42", {
|
|
ok: true,
|
|
at: Date.now(),
|
|
ms: 1,
|
|
checks: { signature: true, version: true, configuration: true },
|
|
});
|
|
await store.recordSelfCheckForDigest(accountDigest("42"), {
|
|
ok: true,
|
|
at: Date.now(),
|
|
ms: 2,
|
|
checks: { signature: true, version: true, configuration: true },
|
|
});
|
|
await store.recordVersion({
|
|
version: "5.4.0",
|
|
source: "manifest",
|
|
checkedAt: Date.now(),
|
|
});
|
|
|
|
const path = join(directory, STATE_FILE_NAME);
|
|
const text = await readFile(path, "utf8");
|
|
const info = await stat(path);
|
|
assert.equal(info.mode & 0o777, 0o600);
|
|
assert.doesNotMatch(text, /person@example\.com|secret|"uid"|"access"/);
|
|
assert.match(text, /"digest": "[a-f0-9]{16}"/);
|
|
assert.match(text, /"version": "5\.4\.0"/);
|
|
|
|
const reloaded = createStateStore({ directory });
|
|
assert.equal((await reloaded.cachedModels("42"))[0].id, "m");
|
|
assert.equal((await reloaded.cachedQuota("42")).plan, "Plan");
|
|
const account = await reloaded.account(accountDigest("42"));
|
|
assert.equal(account.lastError.status, 503);
|
|
assert.equal(account.selfCheck.ok, true);
|
|
assert.equal(account.selfCheck.ms, 2);
|
|
});
|
|
|
|
test("a corrupt state file is ignored and replaced by the next write", async () => {
|
|
const directory = await mkdtemp(join(tmpdir(), "atomcode-state-corrupt-"));
|
|
const path = join(directory, STATE_FILE_NAME);
|
|
await writeFile(path, "{ definitely not json", "utf8");
|
|
const errors = [];
|
|
const store = createStateStore({ directory, onError: (error) => errors.push(error) });
|
|
const recovered = await store.read();
|
|
assert.equal(recovered.version, emptyState().version);
|
|
assert.deepEqual({ ...recovered.accounts }, {});
|
|
assert.equal(errors.length, 1);
|
|
await store.recordQuota("42", { plan: "Recovered", windows: [] });
|
|
const reloaded = createStateStore({ directory });
|
|
assert.equal((await reloaded.cachedQuota("42")).plan, "Recovered");
|
|
});
|
|
|
|
test("stores without a directory stay in memory and report no path", async () => {
|
|
const store = createStateStore();
|
|
assert.equal(store.path, undefined);
|
|
assert.equal(stateFilePath(undefined), undefined);
|
|
await store.recordQuota("42", { plan: "Memory", windows: [] });
|
|
assert.equal((await store.cachedQuota("42")).plan, "Memory");
|
|
});
|
|
|
|
test("concurrent updates serialize rather than losing accounts", async () => {
|
|
const directory = await mkdtemp(join(tmpdir(), "atomcode-state-concurrent-"));
|
|
const store = createStateStore({ directory });
|
|
await Promise.all([
|
|
store.recordLogin("a", "atomcode-browser"),
|
|
store.recordLogin("b", "atomcode-cli"),
|
|
store.recordQuota("a", { plan: "A", windows: [] }),
|
|
]);
|
|
const state = await store.read();
|
|
assert.ok(state.accounts[accountDigest("a")]);
|
|
assert.ok(state.accounts[accountDigest("b")]);
|
|
});
|