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

composerPromptHistory.test.ts

214 lines7.9 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { appendElementContextsToPrompt } from "../../lib/elementContext";
4import {
5 appendTerminalContextsToPrompt,
6 materializeInlineTerminalContextPrompt,
7} from "../../lib/terminalContext";
8import { appendReviewCommentsToPrompt, buildFileReviewComment } from "../../reviewCommentContext";
9import { buildPlanImplementationPrompt } from "../../proposedPlan";
10import {
11 ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
12 buildComposerPromptHistoryEntries,
13 recallableComposerPrompt,
14 stepComposerPromptHistory,
15 type ComposerPromptHistoryPosition,
16} from "./composerPromptHistory";
17​
18const entries = buildComposerPromptHistoryEntries([
19 { id: "m1", role: "user", text: "first" },
20 { id: "a1", role: "assistant", text: "reply" },
21 { id: "m2", role: "user", text: "second" },
22 { id: "m3", role: "user", text: "third" },
23]);
24​
25function backward(position: ComposerPromptHistoryPosition | null, currentPrompt: string) {
26 return stepComposerPromptHistory({ direction: "backward", entries, position, currentPrompt });
28​
29function forward(position: ComposerPromptHistoryPosition | null, currentPrompt: string) {
30 return stepComposerPromptHistory({ direction: "forward", entries, position, currentPrompt });
32​
33describe("recallableComposerPrompt", () => {
34 it("strips send-time context blocks and the ultrathink prefix", () => {
35 const withTerminal = appendTerminalContextsToPrompt("Investigate this", [
36 {
37 terminalId: "default",
38 terminalLabel: "Terminal 1",
39 lineStart: 12,
40 lineEnd: 13,
41 text: "git status\nOn branch main",
42 },
43 ]);
44 const withElement = appendElementContextsToPrompt(withTerminal, [
45 {
46 pageUrl: "https://example.com",
47 pageTitle: "Example",
48 tagName: "button",
49 selector: "button.submit",
50 htmlPreview: "<button>Save</button>",
51 componentName: null,
52 source: null,
53 styles: "",
54 },
55 ]);
56 expect(recallableComposerPrompt(`Ultrathink:\n${withElement}`)).toBe("Investigate this");
57 });
58​
59 it("removes inline terminal labels along with their trailing block", () => {
60 const context = {
61 terminalId: "default",
62 terminalLabel: "Terminal 1",
63 lineStart: 12,
64 lineEnd: 13,
65 text: "git status",
66 };
67 const typed = materializeInlineTerminalContextPrompt("Look at \uFFFC please", [context]);
68 expect(typed).toBe("Look at @terminal-1:12-13 please");
69 const sent = appendTerminalContextsToPrompt(typed, [context]);
70 expect(recallableComposerPrompt(sent)).toBe("Look at please");
71 });
72​
73 it("removes one label per chip and leaves other whitespace alone", () => {
74 const context = {
75 terminalId: "default",
76 terminalLabel: "Terminal 1",
77 lineStart: 4,
78 lineEnd: 4,
79 text: "ls",
80 };
81 const typed = "@terminal-1:4 typed twice: @terminal-1:4\n indented code";
82 const sent = appendTerminalContextsToPrompt(typed, [context]);
83 expect(recallableComposerPrompt(sent)).toBe("typed twice: @terminal-1:4\n indented code");
84 });
85​
86 it("does not strip a typed label that only starts with the chip label", () => {
87 const context = {
88 terminalId: "default",
89 terminalLabel: "Terminal 1",
90 lineStart: 4,
91 lineEnd: 4,
92 text: "ls",
93 };
94 const typed = "see @terminal-1:40 and @terminal-1:4-12 then @terminal-1:4";
95 const sent = appendTerminalContextsToPrompt(typed, [context]);
96 expect(recallableComposerPrompt(sent)).toBe("see @terminal-1:40 and @terminal-1:4-12 then");
97 });
98​
99 it("strips only the review comments appended at the end", () => {
100 const comment = buildFileReviewComment({
101 id: "comment-1",
102 filePath: "src/app.ts",
103 startLine: 2,
104 endLine: 3,
105 text: "Keep this configurable.",
106 contents: "one\ntwo\nthree",
107 });
108 const sent = appendReviewCommentsToPrompt("Please update this.", [comment]);
109 expect(recallableComposerPrompt(sent)).toBe("Please update this.");
110 const midPrompt = appendReviewCommentsToPrompt("Before", [comment]) + "\n\nAfter";
111 expect(recallableComposerPrompt(midPrompt)).toBe(midPrompt);
112 // A typed block earlier in the prompt survives when the trailing one goes.
113 const both = appendReviewCommentsToPrompt(midPrompt, [comment]);
114 expect(recallableComposerPrompt(both)).toBe(midPrompt);
115 });
116​
117 it("returns an empty string for app-composed sends", () => {
118 expect(recallableComposerPrompt(" ")).toBe("");
119 expect(recallableComposerPrompt(ATTACHMENT_ONLY_BOOTSTRAP_PROMPT)).toBe("");
120 expect(recallableComposerPrompt(buildPlanImplementationPrompt("# Plan\n1. do it"))).toBe("");
121 });
122});
123​
124describe("buildComposerPromptHistoryEntries", () => {
125 it("keeps user messages with text, oldest first", () => {
126 expect(entries.map((entry) => entry.prompt)).toEqual(["first", "second", "third"]);
127 });
128​
129 it("collapses consecutive duplicates onto the newest message id", () => {
130 const collapsed = buildComposerPromptHistoryEntries([
131 { id: "m1", role: "user", text: "same" },
132 { id: "m2", role: "user", text: "same" },
133 { id: "m3", role: "user", text: "other" },
134 { id: "m4", role: "user", text: "same" },
135 ]);
136 expect(collapsed).toEqual([
137 { id: "m2", prompt: "same" },
138 { id: "m3", prompt: "other" },
139 { id: "m4", prompt: "same" },
140 ]);
141 });
142});
143​
144describe("stepComposerPromptHistory", () => {
145 it("does not start browsing from a non-empty draft", () => {
146 expect(backward(null, "typing")).toBeNull();
147 });
148​
149 it("walks back from the newest entry", () => {
150 const first = backward(null, "");
151 expect(first).toEqual({ position: { entryId: "m3", recalled: "third" }, prompt: "third" });
152 expect(backward(first!.position, "third")?.prompt).toBe("second");
153 });
154​
155 it("is a no-op at the oldest entry so the caret keeps moving", () => {
156 expect(backward({ entryId: "m1", recalled: "first" }, "first")).toBeNull();
157 });
158​
159 it("walks forward and empties the composer past the newest entry", () => {
160 const newer = forward({ entryId: "m2", recalled: "second" }, "second");
161 expect(newer?.prompt).toBe("third");
162 expect(forward(newer!.position, "third")).toEqual({ position: null, prompt: "" });
163 });
164​
165 it("treats an edited recall as a fresh draft", () => {
166 const position: ComposerPromptHistoryPosition = { entryId: "m3", recalled: "third" };
167 expect(backward(position, "third edited")).toBeNull();
168 expect(forward(position, "third edited")).toBeNull();
169 // Sent and cleared: ArrowUp starts over from the newest entry.
170 expect(backward(position, "")?.position).toEqual({ entryId: "m3", recalled: "third" });
171 });
172​
173 it("does nothing on forward when not browsing", () => {
174 expect(forward(null, "")).toBeNull();
175 });
176​
177 it("follows the entry by id when the list changes under it", () => {
178 const grown = buildComposerPromptHistoryEntries([
179 { id: "m0", role: "user", text: "zeroth" },
180 { id: "m1", role: "user", text: "A" },
181 { id: "m2", role: "user", text: "B" },
182 { id: "m3", role: "user", text: "A" },
183 ]);
184 const older = stepComposerPromptHistory({
185 direction: "backward",
186 entries: grown,
187 position: { entryId: "m1", recalled: "A" },
188 currentPrompt: "A",
189 });
190 expect(older?.prompt).toBe("zeroth");
191 // Unknown id with no matching text: browsing is over.
192 const missing = stepComposerPromptHistory({
193 direction: "forward",
194 entries: grown,
195 position: { entryId: "gone", recalled: "not sent" },
196 currentPrompt: "not sent",
197 });
198 expect(missing).toBeNull();
199 });
200​
201 it("falls back to matching text when a duplicate collapse retires the id", () => {
202 const collapsed = buildComposerPromptHistoryEntries([
203 { id: "m1", role: "user", text: "first" },
204 { id: "m3", role: "user", text: "A" },
205 ]);
206 const step = stepComposerPromptHistory({
207 direction: "backward",
208 entries: collapsed,
209 position: { entryId: "m2", recalled: "A" },
210 currentPrompt: "A",
211 });
212 expect(step?.prompt).toBe("first");
213 });
214});