import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import Fastify from "fastify"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { aiRoutes } from "./ai.js"; import { getTask } from "../task-queue.js"; const originalDataDir = process.env.LPT_AI_DATA_DIR; const originalKey = process.env.LLM_API_KEY; const originalRateLimit = process.env.LPT_AI_RATE_LIMIT; let workDir: string; beforeEach(() => { // 面板日志会落盘,必须指向临时目录,否则会清掉开发者真实的面板数据 workDir = mkdtempSync(join(tmpdir(), "lpt-ai-routes-")); process.env.LPT_AI_DATA_DIR = workDir; // 不配 Key:任务会以「LLM 不可用」快速失败,用例不需要真调 LLM delete process.env.LLM_API_KEY; process.env.LPT_AI_RATE_LIMIT = "10000"; }); afterEach(() => { for (const [key, value] of [ ["LPT_AI_DATA_DIR", originalDataDir], ["LLM_API_KEY", originalKey], ["LPT_AI_RATE_LIMIT", originalRateLimit], ] as const) { if (value === undefined) { delete process.env[key]; } else { process.env[key] = value; } } rmSync(workDir, { recursive: true, force: true }); }); async function buildApp() { const app = Fastify(); await app.register(aiRoutes); await app.ready(); return app; } function submit(app: Awaited>, body: unknown) { return app.inject({ method: "POST", url: "/ai/tasks", payload: body as Record, }); } describe("POST /ai/tasks 的 callerRef(用量归属)", () => { it("带归属提交时落到任务记录上,供结算写进用量记录", async () => { const app = await buildApp(); const response = await submit(app, { type: "guidance", params: { taskName: "t", concept: "c", history: [] }, callerRef: "U-123", }); expect(response.statusCode).toBe(201); const { taskId } = JSON.parse(response.body) as { taskId: string }; expect(getTask(taskId)?.callerRef).toBe("U-123"); await app.close(); }); it("不传归属时任务记录上没有该字段", async () => { const app = await buildApp(); const response = await submit(app, { type: "guidance", params: { taskName: "t", concept: "c", history: [] }, }); const { taskId } = JSON.parse(response.body) as { taskId: string }; expect(getTask(taskId)?.callerRef).toBeUndefined(); await app.close(); }); it("空白归属视为未传,不写空字符串", async () => { const app = await buildApp(); const response = await submit(app, { type: "guidance", params: { taskName: "t", concept: "c", history: [] }, callerRef: " ", }); const { taskId } = JSON.parse(response.body) as { taskId: string }; expect(getTask(taskId)?.callerRef).toBeUndefined(); await app.close(); }); it("超过 64 字符被拒:它会落进日志与数据库列,限长比让库截断安全", async () => { const app = await buildApp(); const response = await submit(app, { type: "guidance", params: { taskName: "t", concept: "c", history: [] }, callerRef: "x".repeat(65), }); expect(response.statusCode).toBe(400); expect(JSON.parse(response.body).error).toContain("64"); await app.close(); }); it("非字符串被拒,避免把对象写进归属列", async () => { const app = await buildApp(); const response = await submit(app, { type: "guidance", params: { taskName: "t", concept: "c", history: [] }, callerRef: { userId: "U-1" }, }); expect(response.statusCode).toBe(400); await app.close(); }); it("归属不影响参数校验:type 非法仍然优先报错", async () => { const app = await buildApp(); const response = await submit(app, { type: "nope", params: {}, callerRef: "U-1" }); expect(response.statusCode).toBe(400); expect(JSON.parse(response.body).error).toContain("无效的 type"); await app.close(); }); });