Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

historyBootstrap.test.ts

125 lines3.5 KB

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