Files
lpt-ai/src/llm/client.test.ts
T
cat-shark 935f4781a4 feat: 采集 LLM token 用量并上报 lpt-be
- 客户端解析上游 usage(含 DeepSeek 缓存命中/未命中),跨重试累加
- 新增计价表(三档单价 + 峰谷时段),费用在结算时冻结
- 新增用量上报与本地 outbox 重试;4xx 判定为确定性拒绝,不无限重试
- 面板补充 Token/费用/调用次数与计价快照
- 顺带修复面板内联脚本转义错误(模板字面量吃掉 \n 与 \',面板一直空白)
2026-10-10 18:46:01 +08:00

141 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, describe, expect, it, vi } from "vitest";
import { chat, type LlmConfig } from "./client.js";
/**
* 只验证「请求体契约」:思考强度要真的写进 `reasoning_effort`,
* 不传时不能凭空加上该字段(否则换供应商会被当成非法参数)。
*/
const llmConfig: LlmConfig = {
apiUrl: "https://example.invalid/v1/chat/completions",
apiKey: "test-key",
model: "deepseek-flash",
timeoutMs: 5_000,
};
function mockFetchOnce() {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ choices: [{ message: { content: "ok" } }] }),
headers: new Headers(),
} as unknown as Response);
vi.stubGlobal("fetch", fetchMock);
return fetchMock;
}
function mockFetchJson(payload: unknown, ok = true, status = 200) {
const fetchMock = vi.fn().mockResolvedValue({
ok,
status,
json: async () => payload,
text: async () => JSON.stringify(payload),
headers: new Headers(),
} as unknown as Response);
vi.stubGlobal("fetch", fetchMock);
return fetchMock;
}
function bodyOf(fetchMock: ReturnType<typeof mockFetchOnce>): Record<string, unknown> {
const init = fetchMock.mock.calls[0][1] as RequestInit;
return JSON.parse(String(init.body)) as Record<string, unknown>;
}
afterEach(() => {
vi.unstubAllGlobals();
});
describe("chat 请求体", () => {
it("传入 reasoningEffort 时写进 reasoning_effort", async () => {
const fetchMock = mockFetchOnce();
await chat(llmConfig, [{ role: "user", content: "hi" }], {
temperature: 0.2,
maxTokens: 1024,
reasoningEffort: "low",
});
expect(bodyOf(fetchMock)).toMatchObject({
model: "deepseek-flash",
temperature: 0.2,
max_tokens: 1024,
stream: false,
reasoning_effort: "low",
});
});
it("none 也要显式发送,用于关闭思考模式", async () => {
const fetchMock = mockFetchOnce();
await chat(llmConfig, [{ role: "user", content: "hi" }], { reasoningEffort: "none" });
expect(bodyOf(fetchMock).reasoning_effort).toBe("none");
});
it("不传 reasoningEffort 时不带该字段,交回供应商默认", async () => {
const fetchMock = mockFetchOnce();
await chat(llmConfig, [{ role: "user", content: "hi" }]);
expect(bodyOf(fetchMock)).not.toHaveProperty("reasoning_effort");
});
});
describe("chat 返回值", () => {
it("带回上游上报的 token 用量与模型名", async () => {
mockFetchJson({
model: "deepseek-flash-0715",
choices: [{ message: { content: "ok" }, finish_reason: "stop" }],
usage: {
prompt_tokens: 1000,
completion_tokens: 200,
total_tokens: 1200,
prompt_cache_hit_tokens: 800,
prompt_cache_miss_tokens: 200,
},
});
const result = await chat(llmConfig, [{ role: "user", content: "hi" }]);
expect(result.content).toBe("ok");
expect(result.model).toBe("deepseek-flash-0715");
expect(result.finishReason).toBe("stop");
expect(result.usageSource).toBe("upstream");
expect(result.usage).toMatchObject({
promptTokens: 1000,
completionTokens: 200,
cacheHitTokens: 800,
cacheMissTokens: 200,
});
});
it("上游没带 usage 时标为 missing,字段全为 null", async () => {
mockFetchJson({ choices: [{ message: { content: "ok" } }] });
const result = await chat(llmConfig, [{ role: "user", content: "hi" }]);
expect(result.usageSource).toBe("missing");
expect(result.usage.promptTokens).toBeNull();
});
it("返回 200 但内容为空时,把已产生的用量挂在错误上带回来", async () => {
mockFetchJson({
choices: [{ message: { content: "" } }],
usage: { prompt_tokens: 500, completion_tokens: 0, total_tokens: 500 },
});
await expect(chat(llmConfig, [{ role: "user", content: "hi" }])).rejects.toMatchObject({
statusCode: 502,
usage: { promptTokens: 500 },
});
});
it("上游报错时错误不带用量(上游没产生可计费的输出)", async () => {
mockFetchJson({ error: "rate limited" }, false, 429);
await expect(chat(llmConfig, [{ role: "user", content: "hi" }])).rejects.toMatchObject({
statusCode: 429,
});
await expect(chat(llmConfig, [{ role: "user", content: "hi" }])).rejects.toHaveProperty(
"usage",
undefined,
);
});
});