2026-08-05 11:09:08 +08:00
|
|
|
import assert from "node:assert/strict";
|
|
|
|
|
import test from "node:test";
|
2026-08-05 11:14:10 +08:00
|
|
|
import {
|
|
|
|
|
DeepSeekJsonClient,
|
|
|
|
|
completionEndpoint,
|
|
|
|
|
extractJson,
|
|
|
|
|
} from "./model-client.mjs";
|
2026-08-05 11:09:08 +08:00
|
|
|
|
|
|
|
|
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 });
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-05 11:14:10 +08:00
|
|
|
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",
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-05 11:09:08 +08:00
|
|
|
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);
|
|
|
|
|
});
|