Files
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

140 lines
3.9 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import {
includeStreamUsage,
normalizeRequestBody,
} from "../request-body.mjs";
test("normalizeRequestBody accepts replayable fetch bodies", async () => {
const cases = [
"hello",
Buffer.from("buffer"),
new TextEncoder().encode("view"),
new TextEncoder().encode("array").buffer,
new Blob(["blob"], { type: "application/json" }),
];
for (const body of cases) {
const normalized = await normalizeRequestBody(body);
assert.ok(normalized.body !== undefined);
assert.ok(normalized.signBody !== undefined);
}
const empty = await normalizeRequestBody(undefined, {
request: new Request("https://example.test/"),
fromRequest: true,
});
assert.equal(empty.signBody, "");
});
test("normalizeRequestBody rejects bodies that cannot be signed exactly", async () => {
for (const body of [
new URLSearchParams({ a: "1" }),
new FormData(),
new ReadableStream(),
]) {
await assert.rejects(() => normalizeRequestBody(body), /unsupported request body/i);
}
});
test("normalizeRequestBody caps Request bodies before buffering", async () => {
const request = new Request("https://example.test/", {
method: "POST",
body: "too large",
});
await assert.rejects(
() =>
normalizeRequestBody(undefined, {
request,
fromRequest: true,
maxBytes: 4,
}),
/exceeds 4 bytes/i,
);
});
test("normalizeRequestBody enforces the cap for every replayable type", async () => {
const bodies = [
"too large",
Buffer.from("too large"),
new TextEncoder().encode("too large"),
new TextEncoder().encode("too large").buffer,
new Blob(["too large"]),
];
for (const body of bodies) {
await assert.rejects(
() => normalizeRequestBody(body, { maxBytes: 4 }),
/exceeds 4 bytes/i,
);
}
});
test("normalizeRequestBody reads each Request stream chunk shape", async () => {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode("a"));
controller.enqueue(
new TextEncoder().encode("b").buffer,
);
controller.enqueue(new DataView(new TextEncoder().encode("c").buffer));
controller.close();
},
});
const request = new Request("https://example.test/", {
method: "POST",
body: stream,
duplex: "half",
});
const normalized = await normalizeRequestBody(undefined, {
request,
fromRequest: true,
});
assert.equal(new TextDecoder().decode(normalized.body), "abc");
});
test("includeStreamUsage only fills an absent Chat Completions option", () => {
const absent = JSON.stringify({ model: "m", stream: true });
const added = includeStreamUsage(absent, {
pathname: "/v1/chat/completions",
contentType: "application/json",
});
assert.equal(added.changed, true);
assert.equal(JSON.parse(added.body).stream_options.include_usage, true);
for (const body of [
JSON.stringify({ model: "m", stream: false }),
JSON.stringify({
model: "m",
stream: true,
stream_options: { include_usage: false },
}),
absent,
]) {
const result = includeStreamUsage(body, {
pathname: body === absent ? "/v1/embeddings" : "/v1/chat/completions",
contentType: "application/json",
});
assert.equal(result.changed, false);
assert.equal(result.body, body);
}
const nonJson = includeStreamUsage(absent, {
pathname: "/v1/chat/completions",
contentType: "text/plain",
});
assert.equal(nonJson.changed, false);
for (const body of [
"not json",
"null",
JSON.stringify({ stream: true, stream_options: null }),
JSON.stringify({ stream: true, stream_options: [] }),
]) {
const result = includeStreamUsage(body, {
pathname: "/v1/chat/completions",
contentType: "application/json",
});
assert.equal(result.changed, false);
assert.equal(result.body, body);
}
});