import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { emptyUsage } from "../llm/usage.js"; import { UsageReporter, loadReporterConfig, toUsageRecord, type ReporterConfig } from "./reporter.js"; import type { LogEntry } from "./store.js"; let workDir: string; beforeEach(() => { workDir = mkdtempSync(join(tmpdir(), "lpt-ai-reporter-")); }); afterEach(() => { rmSync(workDir, { recursive: true, force: true }); }); function outboxFile(): string { return join(workDir, "usage-outbox.json"); } function reporterConfig(overrides: Partial = {}): ReporterConfig { return { url: "http://lpt-be.test", token: "report-token", intervalMs: 15_000, batchSize: 50, maxPending: 5_000, timeoutMs: 5_000, ...overrides, }; } function makeReporter( config: Partial = {}, fetchImpl?: typeof fetch, ): UsageReporter { return new UsageReporter({ loadConfig: () => reporterConfig(config), outboxFile: outboxFile(), ...(fetchImpl ? { fetchImpl } : {}), }); } function entry(overrides: Partial = {}): LogEntry { return { id: 1, recordId: "rec-1", timestamp: "2026-10-10T09:38:23.543Z", endpoint: "/ai/tasks#aggregate-report", taskType: "aggregate-report", taskId: "task-1", model: "deepseek-flash", attempts: 2, isLlmCall: true, usage: { promptTokens: 1234, completionTokens: 567, totalTokens: 1801, cacheHitTokens: 800, cacheMissTokens: 434, reasoningTokens: 0, }, usageSource: "upstream", costUsd: 0.001234, pricing: { version: "hit0.006|miss0.3|out1.2|off0.5", peak: false, effectivePrices: { cacheHitPerM: 0.003, cacheMissPerM: 0.15, outputPerM: 0.6 }, listPrices: { cacheHitPerM: 0.006, cacheMissPerM: 0.3, outputPerM: 1.2 }, }, durationMs: 7466, status: 200, requestBody: {}, responseBody: {}, ...overrides, }; } describe("loadReporterConfig", () => { it("未配置上报地址时视为关闭", () => { expect(loadReporterConfig({}).url).toBe(""); }); it("去掉结尾斜杠,避免拼出双斜杠路径", () => { expect(loadReporterConfig({ LPT_AI_REPORT_URL: "http://be.test/" }).url).toBe("http://be.test"); }); it("非法数值回退到缺省", () => { const config = loadReporterConfig({ LPT_AI_REPORT_INTERVAL_MS: "-5", LPT_AI_REPORT_BATCH_SIZE: "x" }); expect(config.intervalMs).toBe(15_000); expect(config.batchSize).toBe(50); }); }); describe("toUsageRecord", () => { it("把日志映射成上报载荷,null 字段显式保留", () => { const record = toUsageRecord(entry({ usage: emptyUsage(), costUsd: null, usageSource: "missing" })); expect(record).toMatchObject({ recordId: "rec-1", taskType: "aggregate-report", attempts: 2, usageSource: "missing", promptTokens: null, completionTokens: null, cacheHitTokens: null, costUsd: null, }); }); it("非 LLM 请求也上报,由 isLlmCall 区分", () => { const record = toUsageRecord( entry({ isLlmCall: false, taskType: "fetch-title", model: "", usage: undefined }), ); expect(record.isLlmCall).toBe(false); expect(record.taskType).toBe("fetch-title"); expect(record.model).toBe(""); }); it("归属标记原样带上;没传时显式写 null", () => { expect(toUsageRecord(entry({ callerRef: "U-123" })).callerRef).toBe("U-123"); expect(toUsageRecord(entry()).callerRef).toBeNull(); }); it("必填字段永远非空,否则 lpt-be 会把整批上报判 400", () => { // 契约 1 的必填项:lpt-be 侧 @NotBlank 的字段不能是空串,@NotNull 的字段不能缺。 // 尤其是 /fetch-title 的 model——它合法地是空串,所以 lpt-be 那边用的是 @NotNull。 const notBlank = ["recordId", "occurredAt", "taskType", "usageSource"] as const; const notNull = ["model", "attempts", "isLlmCall", "durationMs", "status"] as const; const cases = [ entry(), entry({ isLlmCall: false, taskType: "fetch-title", model: "", usage: undefined, usageSource: undefined }), entry({ usage: emptyUsage(), costUsd: null, usageSource: "missing", status: 500, errorCode: "rate_limited" }), ]; for (const logEntry of cases) { const record = toUsageRecord(logEntry) as unknown as Record; for (const field of notBlank) { expect(typeof record[field], field).toBe("string"); expect(String(record[field]).trim().length, field).toBeGreaterThan(0); } for (const field of notNull) { expect(record[field], field).not.toBeNull(); expect(record[field], field).not.toBeUndefined(); } // lpt-be 用 OffsetDateTime.parse 解析 occurredAt,必须是带时区的 ISO 8601 expect(record.occurredAt).toMatch(/Z$|[+-]\d{2}:\d{2}$/); } }); }); describe("UsageReporter", () => { it("未配置上报地址时不入队、不落盘", async () => { const reporter = makeReporter({ url: "" }); reporter.enqueue(entry()); expect(reporter.isConfigured()).toBe(false); expect(reporter.pendingCount()).toBe(0); expect(existsSync(outboxFile())).toBe(false); await expect(reporter.flush()).resolves.toEqual({ sent: 0, remaining: 0 }); }); it("推送成功后清空队列并落盘", async () => { const fetchImpl = vi.fn().mockResolvedValue({ ok: true, status: 200 } as Response); const reporter = makeReporter({}, fetchImpl as unknown as typeof fetch); reporter.enqueue(entry({ recordId: "rec-1" })); reporter.enqueue(entry({ recordId: "rec-2" })); await expect(reporter.flush()).resolves.toEqual({ sent: 2, remaining: 0 }); expect(reporter.pendingCount()).toBe(0); expect(JSON.parse(readFileSync(outboxFile(), "utf-8"))).toEqual([]); const [url, init] = fetchImpl.mock.calls[0] as [string, RequestInit]; expect(url).toBe("http://lpt-be.test/internal/ai-usage/records"); expect((init.headers as Record)["x-api-token"]).toBe("report-token"); expect(JSON.parse(String(init.body)).records).toHaveLength(2); }); it("上报失败时保留队列,等下次重试", async () => { const fetchImpl = vi.fn().mockResolvedValue({ ok: false, status: 500, text: async () => "boom", } as Response); const reporter = makeReporter({}, fetchImpl as unknown as typeof fetch); reporter.enqueue(entry()); const result = await reporter.flush(); expect(result.sent).toBe(0); expect(result.remaining).toBe(1); expect(result.error).toContain("500"); expect(reporter.pendingCount()).toBe(1); expect(reporter.dropped).toBe(0); }); it("4xx 是确定性拒绝:丢弃这批而不是无限重试把队列堵死", async () => { const fetchImpl = vi.fn().mockResolvedValue({ ok: false, status: 400, text: async () => "model 不能为空", } as Response); const reporter = makeReporter({}, fetchImpl as unknown as typeof fetch); reporter.enqueue(entry({ recordId: "bad" })); reporter.enqueue(entry({ recordId: "good" })); const result = await reporter.flush(); expect(result.error).toContain("400"); expect(reporter.pendingCount()).toBe(0); expect(reporter.dropped).toBe(2); // 后续正常记录不受影响,仍然能上报 fetchImpl.mockResolvedValue({ ok: true, status: 200 } as Response); reporter.enqueue(entry({ recordId: "later" })); await expect(reporter.flush()).resolves.toEqual({ sent: 1, remaining: 0 }); }); it("网络异常时不丢队列", async () => { const fetchImpl = vi.fn().mockRejectedValue(new Error("ECONNREFUSED")); const reporter = makeReporter({}, fetchImpl as unknown as typeof fetch); reporter.enqueue(entry()); const result = await reporter.flush(); expect(result.error).toBe("ECONNREFUSED"); expect(reporter.pendingCount()).toBe(1); }); it("按 batchSize 分批推送,剩余记录留待下一轮", async () => { const fetchImpl = vi.fn().mockResolvedValue({ ok: true, status: 200 } as Response); const reporter = makeReporter({ batchSize: 2 }, fetchImpl as unknown as typeof fetch); for (const id of ["a", "b", "c"]) { reporter.enqueue(entry({ recordId: id })); } await expect(reporter.flush()).resolves.toEqual({ sent: 2, remaining: 1 }); expect(JSON.parse(String((fetchImpl.mock.calls[0][1] as RequestInit).body)).records.map( (r: { recordId: string }) => r.recordId, )).toEqual(["a", "b"]); }); it("超出上限时丢弃最旧的并计数,避免磁盘被写满", () => { const reporter = makeReporter({ maxPending: 2 }); for (const id of ["a", "b", "c", "d"]) { reporter.enqueue(entry({ recordId: id })); } expect(reporter.pendingCount()).toBe(2); expect(reporter.dropped).toBe(2); }); it("新实例从 outbox 文件恢复未上报的记录", () => { const first = makeReporter(); first.enqueue(entry({ recordId: "rec-persisted" })); expect(first.pendingCount()).toBe(1); const second = makeReporter(); expect(second.pendingCount()).toBe(1); }); it("outbox 文件损坏时从空队列开始,不阻塞后续上报", () => { const first = makeReporter(); first.enqueue(entry()); const reporter = new UsageReporter({ loadConfig: () => reporterConfig(), outboxFile: join(workDir, "corrupt.json"), }); expect(reporter.pendingCount()).toBe(0); }); it("并发调用 flush 时第二次直接返回,不重复推送", async () => { let release!: (value: Response) => void; const fetchImpl = vi.fn().mockImplementation( () => new Promise((resolve) => { release = resolve; }), ); const reporter = makeReporter({}, fetchImpl as unknown as typeof fetch); reporter.enqueue(entry()); const first = reporter.flush(); const second = await reporter.flush(); expect(second).toEqual({ sent: 0, remaining: 1 }); release({ ok: true, status: 200 } as Response); await expect(first).resolves.toEqual({ sent: 1, remaining: 0 }); expect(fetchImpl).toHaveBeenCalledTimes(1); }); });