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

183 lines
6.6 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import {
DEFAULT_BASE_URL,
DEFAULT_CONTEXT,
DEFAULT_OUTPUT,
normalizeCodingPlanModel,
sanitizeModelKey,
toConfigModel,
toProviderModel,
trustedModelBaseURL,
} from "../models.mjs";
/**
* `base_url` 来自上游 `models-v2`(服务端可控数据)。这些用例钉住白名单:
* 只有 https、无内嵌凭证、且落在 atomgit 域上的地址才被采纳,其余一律回退
* 官方网关——否则上游响应就能决定这个账号的 bearer token 发往哪里。
*/
test("trustedModelBaseURL:只采纳 loader 实际允许的网关 origin", () => {
assert.equal(trustedModelBaseURL("https://llm-api.atomgit.com/v1"), "https://llm-api.atomgit.com/v1");
assert.equal(trustedModelBaseURL("https://raw.atomgit.com/x"), undefined);
assert.equal(trustedModelBaseURL("https://atomgit.com"), undefined);
assert.equal(trustedModelBaseURL("https://llm-api.atomgit.com:8443/v1"), undefined);
assert.equal(trustedModelBaseURL(" https://llm-api.atomgit.com/v1 "), "https://llm-api.atomgit.com/v1");
});
test("trustedModelBaseURL:域名不在白名单 → 拒绝", () => {
assert.equal(trustedModelBaseURL("https://evil.example/v1"), undefined);
assert.equal(trustedModelBaseURL("https://evil-atomgit.com/v1"), undefined, "相近域名不得蒙混过关");
assert.equal(
trustedModelBaseURL("https://llm-api.atomgit.com.evil.example/v1"),
undefined,
"把白名单域名当前缀同样不行",
);
});
test("trustedModelBaseURL:明文降级与内嵌凭证 → 拒绝", () => {
assert.equal(trustedModelBaseURL("http://llm-api.atomgit.com/v1"), undefined);
assert.equal(trustedModelBaseURL("https://user:pass@llm-api.atomgit.com/v1"), undefined);
});
test("trustedModelBaseURL:非 URL 与非法类型 → 拒绝", () => {
assert.equal(trustedModelBaseURL("not a url"), undefined);
assert.equal(trustedModelBaseURL(""), undefined);
assert.equal(trustedModelBaseURL(" "), undefined);
assert.equal(trustedModelBaseURL(undefined), undefined);
assert.equal(trustedModelBaseURL(null), undefined);
assert.equal(trustedModelBaseURL(42), undefined);
assert.equal(trustedModelBaseURL({}), undefined);
});
test("normalizeCodingPlanModel:非法 base_url 被丢弃并上报原始值", () => {
const rejected = [];
const model = normalizeCodingPlanModel(
{ display_model_name: "m1", base_url: "https://evil.example/v1" },
{ onReject: (value) => rejected.push(value) },
);
assert.equal(model.baseURL, undefined, "非法地址不得进入模型描述");
assert.deepEqual(rejected, ["https://evil.example/v1"]);
});
test("normalizeCodingPlanModel:合法 base_url 原样保留,且不触发上报", () => {
let rejected = 0;
const model = normalizeCodingPlanModel(
{ display_model_name: "m1", base_url: "https://llm-api.atomgit.com/v1" },
{
onReject: () => {
rejected += 1;
},
},
);
assert.equal(model.baseURL, "https://llm-api.atomgit.com/v1");
assert.equal(rejected, 0);
});
test("normalizeCodingPlanModel:没有 base_url 时静默,不当作被拒", () => {
let rejected = 0;
const model = normalizeCodingPlanModel(
{ display_model_name: "m1" },
{
onReject: () => {
rejected += 1;
},
},
);
assert.equal(model.baseURL, undefined);
assert.equal(rejected, 0);
});
test("normalizeCodingPlanModel:上报回调自己抛错也不影响模型", () => {
const model = normalizeCodingPlanModel(
{ display_model_name: "m1", base_url: "https://evil.example/v1" },
{
onReject: () => {
throw new Error("logger exploded");
},
},
);
assert.equal(model.id, "m1");
assert.equal(model.baseURL, undefined);
});
test("toProviderModel:模型没有可用 base_url 时落到调用方给的官方网关", () => {
const model = normalizeCodingPlanModel({ display_model_name: "m1", base_url: "https://evil.example" });
const providerModel = toProviderModel(model, "atomcode", { baseURL: DEFAULT_BASE_URL });
assert.equal(providerModel.api.url, DEFAULT_BASE_URL);
assert.equal(providerModel.api.id, "m1");
});
test("reasoning variants carry the request option agents merge", () => {
const m = normalizeCodingPlanModel({
display_model_name: "qwen3.8-27b",
context_window: 262144,
reasoning_effort_levels: ["low", "medium", "xhigh"],
});
assert.deepEqual(toConfigModel(m).variants, {
low: { reasoningEffort: "low" },
medium: { reasoningEffort: "medium" },
xhigh: { reasoningEffort: "xhigh" },
});
assert.deepEqual(toProviderModel(m, "atomcode").variants, {
low: { reasoningEffort: "low" },
medium: { reasoningEffort: "medium" },
xhigh: { reasoningEffort: "xhigh" },
});
});
test("model output limit honours upstream declarations before the plugin fallback", () => {
const declared = normalizeCodingPlanModel(
{ display_model_name: "declared", max_output_tokens: 64000 },
{ output: 32768 },
);
const fallback = normalizeCodingPlanModel({ display_model_name: "fallback" }, { output: 32768 });
assert.equal(declared.output, 64000);
assert.equal(fallback.output, 32768);
});
test("catalog rows tolerate malformed optional fields", () => {
assert.equal(normalizeCodingPlanModel(), null);
assert.equal(normalizeCodingPlanModel({ display_model_name: 42 }), null);
const model = normalizeCodingPlanModel({
display_model_name: " unsafe/model ",
reasoning_effort_levels: [" low ", "", null, 7, "high"],
context_window: "not-a-number",
max_output_tokens: null,
max_completion_tokens: null,
output_token_limit: "invalid",
base_url: 42,
supports_vision: false,
type: "",
});
assert.deepEqual(model, {
id: "unsafe/model",
key: "unsafe_model",
context: DEFAULT_CONTEXT,
output: DEFAULT_OUTPUT,
levels: ["low", "high"],
vision: false,
type: "openai",
baseURL: undefined,
});
});
test("model factories provide stable defaults for empty rows", () => {
assert.equal(sanitizeModelKey(), "");
const configModel = toConfigModel();
assert.equal(configModel.name, undefined);
assert.equal(configModel.reasoning, false);
assert.equal(configModel.limit.context, DEFAULT_CONTEXT);
assert.equal(configModel.limit.output, DEFAULT_OUTPUT);
assert.equal(Object.hasOwn(configModel, "variants"), false);
const providerModel = toProviderModel();
assert.equal(providerModel.id, undefined);
assert.equal(providerModel.name, undefined);
assert.equal(providerModel.api.url, DEFAULT_BASE_URL);
assert.equal(providerModel.capabilities.reasoning, false);
assert.deepEqual(providerModel.variants, {});
});