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