70 lines
2.6 KiB
JavaScript
70 lines
2.6 KiB
JavaScript
import { describe, expect, it } from "bun:test";
|
|
import {
|
|
MAX_SAVED_SESSIONS,
|
|
buildTurnHistoryPayload,
|
|
createCopilotStore,
|
|
deriveSessionTitle,
|
|
} from "./copilot-store.js";
|
|
|
|
describe("copilot-store session persistence, history serialization, and memory notes", () => {
|
|
it("strips <think> reasoning tags and keeps only completed turns in buildTurnHistoryPayload", () => {
|
|
const history = buildTurnHistoryPayload([
|
|
{
|
|
status: "done",
|
|
question: "第一问",
|
|
rawText: "<think>hidden thought</think>### 第一答\n\n事实清晰。",
|
|
rawReasoning: "structured reasoning",
|
|
},
|
|
{ status: "error", question: "失败轮次", rawText: "不应进入历史" },
|
|
{
|
|
status: "done",
|
|
question: "第二问",
|
|
reply: { answer_markdown: "第二答正文" },
|
|
},
|
|
]);
|
|
|
|
expect(history).toEqual([
|
|
{ role: "user", content: "第一问" },
|
|
{ role: "assistant", content: "### 第一答\n\n事实清晰。" },
|
|
{ role: "user", content: "第二问" },
|
|
{ role: "assistant", content: "第二答正文" },
|
|
]);
|
|
});
|
|
|
|
it("saves/loads/evicts up to MAX_SAVED_SESSIONS and manages deduplicated memory notes", async () => {
|
|
const store = createCopilotStore({ useIndexedDb: false });
|
|
expect(deriveSessionTitle([{ question: "这是一个超过二十八个字符的地球全图层综合态势对比测试标题问题" }])).toContain("…");
|
|
|
|
for (let i = 1; i <= MAX_SAVED_SESSIONS + 3; i += 1) {
|
|
await store.saveSession({
|
|
id: `sess-${i}`,
|
|
domain: "global",
|
|
turns: [{ id: `t-${i}`, status: "done", question: `问题 ${i}`, rawText: `回答 ${i}` }],
|
|
updatedAt: 1000 + i,
|
|
});
|
|
}
|
|
|
|
const sessions = await store.listSessions();
|
|
expect(sessions).toHaveLength(MAX_SAVED_SESSIONS);
|
|
expect(sessions[0].id).toBe(`sess-${MAX_SAVED_SESSIONS + 3}`);
|
|
expect(sessions.some((s) => s.id === "sess-1")).toBe(false);
|
|
|
|
await store.setActiveSessionId(sessions[0].id);
|
|
expect(await store.getActiveSessionId()).toBe(sessions[0].id);
|
|
|
|
await store.deleteSession(sessions[0].id);
|
|
expect(await store.getActiveSessionId()).toBeNull();
|
|
|
|
const savedNotes = await store.saveMemoryNotes([
|
|
{ id: "m1", text: "关注亚太海底光缆" },
|
|
{ id: "m2", text: "关注亚太海底光缆" },
|
|
{ id: "m3", text: "算力按统一口径比较" },
|
|
]);
|
|
expect(savedNotes).toHaveLength(2);
|
|
expect((await store.getMemoryNotes()).map((n) => n.text)).toEqual([
|
|
"关注亚太海底光缆",
|
|
"算力按统一口径比较",
|
|
]);
|
|
});
|
|
});
|