Open History to see the last commit
1import { describe, expect, it } from "vitest";
2
3import { buildBootstrapInput } from "./historyBootstrap";
4
5describe("buildBootstrapInput", () => {
6 it("includes full transcript when under budget", () => {
7 const result = buildBootstrapInput(
8 [
9 {
10 id: "u-1",
11 role: "user",
12 text: "hello",
13 createdAt: "2026-02-09T00:00:00.000Z",
14 streaming: false,
15 },
16 {
17 id: "a-1",
18 role: "assistant",
19 text: "world",
20 createdAt: "2026-02-09T00:00:01.000Z",
21 streaming: false,
22 },
23 ],
24 "what's next?",
25 1_500,
26 );
27
28 expect(result.includedCount).toBe(2);
29 expect(result.omittedCount).toBe(0);
30 expect(result.truncated).toBe(false);
31 expect(result.text).toContain("USER:\nhello");
32 expect(result.text).toContain("ASSISTANT:\nworld");
33 expect(result.text).toContain("Latest user request (answer this now):");
34 expect(result.text).toContain("what's next?");
35 });
36
37 it("truncates older transcript messages when over budget", () => {
38 const result = buildBootstrapInput(
39 [
40 {
41 id: "u-1",
42 role: "user",
43 text: "first question with details",
44 createdAt: "2026-02-09T00:00:00.000Z",
45 streaming: false,
46 },
47 {
48 id: "a-1",
49 role: "assistant",
50 text: "first answer with details",
51 createdAt: "2026-02-09T00:00:01.000Z",
52 streaming: false,
53 },
54 {
55 id: "u-2",
56 role: "user",
57 text: "second question with details",
58 createdAt: "2026-02-09T00:00:02.000Z",
59 streaming: false,
60 },
61 ],
62 "final request",
63 320,
64 );
65
66 expect(result.truncated).toBe(true);
67 expect(result.omittedCount).toBeGreaterThan(0);
68 expect(result.includedCount).toBeLessThan(3);
69 expect(result.text).toContain("omitted to stay within input limits");
70 expect(result.text.length).toBeLessThanOrEqual(320);
71 });
72
73 it("preserves the latest prompt when prompt-only fallback is required", () => {
74 const latestPrompt = "Please keep this exact latest prompt.";
75 const result = buildBootstrapInput(
76 [
77 {
78 id: "u-1",
79 role: "user",
80 text: "old context",
81 createdAt: "2026-02-09T00:00:00.000Z",
82 streaming: false,
83 },
84 ],
85 latestPrompt,
86 latestPrompt.length + 3,
87 );
88
89 expect(result.text).toBe(latestPrompt);
90 expect(result.includedCount).toBe(0);
91 expect(result.omittedCount).toBe(1);
92 expect(result.truncated).toBe(true);
93 });
94
95 it("captures user image attachment context in transcript blocks", () => {
96 const result = buildBootstrapInput(
97 [
98 {
99 id: "u-image",
100 role: "user",
101 text: "",
102 attachments: [
103 {
104 type: "image",
105 id: "img-1",
106 name: "screenshot.png",
107 mimeType: "image/png",
108 sizeBytes: 2_048,
109 },
110 ],
111 createdAt: "2026-02-09T00:00:00.000Z",
112 streaming: false,
113 },
114 ],
115 "What does this error mean?",
116 1_500,
117 );
118
119 expect(result.text).toContain("Attached image");
120 expect(result.text).toContain("screenshot.png");
121 });
122});