Repository navigation
Expand file tree
/
Copy pathai-history.test.ts
More file actions
76 lines (71 loc) · 2.58 KB
/
Copy pathai-history.test.ts
File metadata and controls
76 lines (71 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { describe, expect, it } from "vitest";
import type { EvenStorage } from "./live-cache";
import {
type AiConversationExcerpt,
appendAiConversationExcerpt,
clearAiConversationHistory,
resolveAiConversationHistory,
writeAiConversationHistory,
} from "./ai-history";
class TestStorage implements EvenStorage {
readonly values = new Map<string, string>();
async getLocalStorage(key: string) { return this.values.get(key) ?? ""; }
async setLocalStorage(key: string, value: string) {
this.values.set(key, value);
return true;
}
}
describe("AI conversation excerpts", () => {
it("keeps only three newest bounded excerpts", () => {
let history: readonly AiConversationExcerpt[] = [];
for (let index = 0; index < 5; index += 1) {
history = appendAiConversationExcerpt(history, {
id: `session-${index}`,
endedAt: `2026-08-0${index + 1}T00:00:00.000Z`,
user: ` User ${index} ${"u".repeat(300)} `,
assistant: ` Answer ${index} ${"a".repeat(300)} `,
sources: [{
title: "Reference",
url: `https://example.com/${index}`,
}],
});
}
expect(history.map(({ id }) => id)).toEqual([
"session-4",
"session-3",
"session-2",
]);
expect(history[0].user.length).toBeLessThanOrEqual(160);
expect(history[0].assistant.length).toBeLessThanOrEqual(160);
expect(history[0].sources).toEqual([{
title: "Reference",
url: "https://example.com/4",
}]);
});
it("persists and clears local excerpts", async () => {
const storage = new TestStorage();
const history = appendAiConversationExcerpt([], {
id: "session-1",
endedAt: "2026-08-01T00:00:00.000Z",
user: "Hello",
assistant: "Hi",
});
await expect(writeAiConversationHistory(storage, history)).resolves.toBe(true);
await expect(resolveAiConversationHistory(storage)).resolves.toEqual(history);
await expect(clearAiConversationHistory(storage)).resolves.toBe(true);
await expect(resolveAiConversationHistory(storage)).resolves.toEqual([]);
});
it("sanitizes cached citation URLs again when reading local history", async () => {
const storage = new TestStorage();
storage.values.set("sandevistan:ai-history:v1", JSON.stringify([{
id: "session-unsafe",
endedAt: "2026-08-01T00:00:00.000Z",
user: "Question",
assistant: "Answer",
sources: [{ title: "Unsafe", url: "javascript:alert(1)" }],
}]));
await expect(resolveAiConversationHistory(storage)).resolves.toEqual([
expect.objectContaining({ sources: [] }),
]);
});
});