guanghu-ice-heart/server-tools/bingshuo-tcs-controller/model-client.test.mjs

49 lines
1.5 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import {
DeepSeekJsonClient,
completionEndpoint,
extractJson,
} from "./model-client.mjs";
test("extractJson accepts strict JSON and fenced JSON", () => {
assert.deepEqual(extractJson('{"ok":true}'), { ok: true });
assert.deepEqual(extractJson('```json\n{"ok":true}\n```'), { ok: true });
});
test("completionEndpoint accepts either a base URL or a complete registered endpoint", () => {
assert.equal(
completionEndpoint("https://api.deepseek.com/v1"),
"https://api.deepseek.com/v1/chat/completions",
);
assert.equal(
completionEndpoint("https://api.deepseek.com/chat/completions"),
"https://api.deepseek.com/chat/completions",
);
});
test("DeepSeek client sends the secret only as authorization and returns JSON", async () => {
let observed;
const client = new DeepSeekJsonClient({
apiKey: "test-secret",
apiUrl: "https://model.invalid/v1",
model: "fixture-model",
fetchImpl: async (url, options) => {
observed = { url, options };
return {
ok: true,
async json() {
return { choices: [{ message: { content: '{"decision":"ok"}' } }] };
},
};
},
});
const result = await client.generate({
role: "fixture",
instruction: "return json",
input: { event: "test" },
});
assert.deepEqual(result, { decision: "ok" });
assert.equal(observed.options.headers.authorization, "Bearer test-secret");
assert.equal(observed.options.body.includes("test-secret"), false);
});