- 客户端解析上游 usage(含 DeepSeek 缓存命中/未命中),跨重试累加 - 新增计价表(三档单价 + 峰谷时段),费用在结算时冻结 - 新增用量上报与本地 outbox 重试;4xx 判定为确定性拒绝,不无限重试 - 面板补充 Token/费用/调用次数与计价快照 - 顺带修复面板内联脚本转义错误(模板字面量吃掉 \n 与 \',面板一直空白)
163 lines
4.7 KiB
TypeScript
163 lines
4.7 KiB
TypeScript
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||
import { tmpdir } from "node:os";
|
||
import { join } from "node:path";
|
||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||
|
||
import { logStore, LogStore, resetLogStoreForTest, type LogEntry } from "./store.js";
|
||
|
||
const originalDataDir = process.env.LPT_AI_DATA_DIR;
|
||
let workDir: string;
|
||
|
||
// 必须指向临时目录:单例默认写 process.cwd()/data,
|
||
// 不隔离就会在跑测试时把开发者真实的面板数据清掉。
|
||
beforeEach(() => {
|
||
workDir = mkdtempSync(join(tmpdir(), "lpt-ai-store-"));
|
||
process.env.LPT_AI_DATA_DIR = workDir;
|
||
});
|
||
|
||
afterEach(() => {
|
||
if (originalDataDir === undefined) {
|
||
delete process.env.LPT_AI_DATA_DIR;
|
||
} else {
|
||
process.env.LPT_AI_DATA_DIR = originalDataDir;
|
||
}
|
||
rmSync(workDir, { recursive: true, force: true });
|
||
});
|
||
|
||
function logFile(): string {
|
||
return join(workDir, "logs.json");
|
||
}
|
||
|
||
function entry(overrides: Partial<LogEntry> = {}): Omit<LogEntry, "id" | "recordId"> {
|
||
return {
|
||
timestamp: "2026-10-10T09:00:00.000Z",
|
||
endpoint: "/ai/tasks#guidance",
|
||
taskType: "guidance",
|
||
model: "deepseek-flash",
|
||
attempts: 1,
|
||
isLlmCall: true,
|
||
durationMs: 100,
|
||
status: 200,
|
||
requestBody: {},
|
||
responseBody: {},
|
||
...overrides,
|
||
};
|
||
}
|
||
|
||
describe("LogStore 落盘", () => {
|
||
it("写入临时文件后原子替换,不留下 .tmp 残渣", () => {
|
||
const store = new LogStore();
|
||
store.add(entry());
|
||
|
||
expect(existsSync(logFile())).toBe(true);
|
||
expect(existsSync(`${logFile()}.tmp`)).toBe(false);
|
||
|
||
const persisted = JSON.parse(readFileSync(logFile(), "utf-8")) as { entries: LogEntry[] };
|
||
expect(persisted.entries).toHaveLength(1);
|
||
expect(persisted.entries[0].recordId).toBeTruthy();
|
||
});
|
||
|
||
it("落盘到 LPT_AI_DATA_DIR,而不是 process.cwd()/data", () => {
|
||
const store = new LogStore();
|
||
store.add(entry());
|
||
expect(existsSync(logFile())).toBe(true);
|
||
});
|
||
|
||
it("重新打开时能读回自己写的数据", () => {
|
||
new LogStore().add(entry({ endpoint: "/ai/tasks#compare-recall" }));
|
||
|
||
const reopened = new LogStore();
|
||
expect(reopened.list()).toHaveLength(1);
|
||
expect(reopened.list()[0].endpoint).toBe("/ai/tasks#compare-recall");
|
||
});
|
||
|
||
it("超过上限时丢弃最旧的记录", () => {
|
||
const store = new LogStore();
|
||
for (let i = 0; i < 501; i += 1) {
|
||
store.add(entry({ status: i }));
|
||
}
|
||
expect(store.list()).toHaveLength(500);
|
||
});
|
||
});
|
||
|
||
describe("LogStore 历史数据迁移", () => {
|
||
it("旧条目缺 isLlmCall 时按端点补齐,避免历史 LLM 调用被当成非 LLM 而静默消失", () => {
|
||
// 旧格式:没有 isLlmCall / recordId / usage
|
||
writeFileSync(
|
||
logFile(),
|
||
JSON.stringify({
|
||
nextId: 3,
|
||
entries: [
|
||
{
|
||
id: 1,
|
||
timestamp: "2026-10-09T09:00:00.000Z",
|
||
endpoint: "/ai/tasks#guidance",
|
||
model: "deepseek-flash",
|
||
durationMs: 100,
|
||
status: 200,
|
||
requestBody: {},
|
||
responseBody: {},
|
||
},
|
||
{
|
||
id: 2,
|
||
timestamp: "2026-10-09T09:01:00.000Z",
|
||
endpoint: "/fetch-title",
|
||
model: "",
|
||
durationMs: 5,
|
||
status: 200,
|
||
requestBody: {},
|
||
responseBody: {},
|
||
},
|
||
],
|
||
}),
|
||
"utf-8",
|
||
);
|
||
|
||
const summaries = new LogStore().list();
|
||
|
||
expect(summaries).toHaveLength(2);
|
||
expect(summaries.find((s) => s.endpoint === "/ai/tasks#guidance")?.isLlmCall).toBe(true);
|
||
// 抓标题不过 LLM,不该被计入用量分母
|
||
expect(summaries.find((s) => s.endpoint === "/fetch-title")?.isLlmCall).toBe(false);
|
||
});
|
||
|
||
it("旧条目没有用量时保持未上报,不用 0 冒充", () => {
|
||
writeFileSync(
|
||
logFile(),
|
||
JSON.stringify({
|
||
nextId: 2,
|
||
entries: [
|
||
{
|
||
id: 1,
|
||
timestamp: "2026-10-09T09:00:00.000Z",
|
||
endpoint: "/ai/tasks#guidance",
|
||
model: "deepseek-flash",
|
||
durationMs: 100,
|
||
status: 200,
|
||
requestBody: {},
|
||
responseBody: {},
|
||
},
|
||
],
|
||
}),
|
||
"utf-8",
|
||
);
|
||
|
||
const summary = new LogStore().list()[0];
|
||
expect(summary.usage).toBeUndefined();
|
||
expect(summary.costUsd).toBeUndefined();
|
||
});
|
||
|
||
it("文件损坏时从空存储开始,不让面板整体起不来", () => {
|
||
writeFileSync(logFile(), "{ not json", "utf-8");
|
||
expect(new LogStore().list()).toEqual([]);
|
||
});
|
||
});
|
||
|
||
describe("resetLogStoreForTest", () => {
|
||
it("清空单例,避免用例之间互相污染", () => {
|
||
logStore.add(entry());
|
||
resetLogStoreForTest();
|
||
expect(logStore.list()).toEqual([]);
|
||
});
|
||
});
|