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): Record { const init = fetchMock.mock.calls[0][1] as RequestInit; return JSON.parse(String(init.body)) as Record; } 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, ); }); });