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

composerPromptHistory.test.ts

206 lines8.4 KB

1import { ScheduledTaskId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "@t3tools/shared/composerContextReferences";
5import { buildPlanImplementationPrompt } from "../../proposedPlan";
6import {
7 buildComposerPromptHistoryEntries,
8 recallableComposerPrompt,
9 stepComposerPromptHistory,
10 type ComposerPromptHistoryPosition,
11} from "./composerPromptHistory";
12​
13const entries = buildComposerPromptHistoryEntries([
14 { id: "m1", role: "user", text: "first" },
15 { id: "a1", role: "assistant", text: "reply" },
16 { id: "m2", role: "user", text: "second" },
17 { id: "m3", role: "user", text: "third" },
18]);
19​
20function backward(position: ComposerPromptHistoryPosition | null, currentPrompt: string) {
21 return stepComposerPromptHistory({ direction: "backward", entries, position, currentPrompt });
23​
24function forward(position: ComposerPromptHistoryPosition | null, currentPrompt: string) {
25 return stepComposerPromptHistory({ direction: "forward", entries, position, currentPrompt });
27​
28describe("recallableComposerPrompt", () => {
29 it.each([
30 "Render <preview_annotation>",
31 "Render\n<preview_annotation>\nhere",
32 "Render &lt;preview_annotation&gt;",
33 ])("strips an annotation whose comment contains a literal opening tag: %s", (comment) => {
34 expect(
35 recallableComposerPrompt(
36 `Prompt\n<preview_annotation>\nComment: ${comment}\n</preview_annotation>`,
37 ),
38 ).toBe("Prompt");
39 });
40​
41 it("preserves a malformed annotation containing a nested opening tag", () => {
42 const text =
43 "Prompt\n<preview_annotation>\nouter literal\n<preview_annotation>\ninner\n</preview_annotation>";
44 expect(recallableComposerPrompt(text)).toBe(text);
45 });
46 it("does not strip a terminal label embedded in ordinary prose", () => {
47 expect(
48 recallableComposerPrompt(
49 "email@build:7\n<terminal_context>\n- Build line 7:\n output\n</terminal_context>",
50 ),
51 ).toBe("email@build:7");
52 });
53 it("strips legacy send-time context blocks and the ultrathink prefix", () => {
54 const sent =
55 "Ultrathink:\nInvestigate this\n\n<terminal_context>\n- Terminal 1 lines 12-13:\n 12 | git status\n 13 | On branch main\n</terminal_context>\n\n<element_context>\n- <button>:\n url: https://example.com\n</element_context>";
56 expect(recallableComposerPrompt(sent)).toBe("Investigate this");
57 });
58​
59 it.each([
60 ["Look at @terminal-1:4 please", "Look at please"],
61 [
62 "@terminal-1:4 typed twice: @terminal-1:4\n indented code",
63 "typed twice: @terminal-1:4\n indented code",
64 ],
65 [
66 "see @terminal-1:40 and @terminal-1:4-12 then @terminal-1:4",
67 "see @terminal-1:40 and @terminal-1:4-12 then",
68 ],
69 ])("removes only the matching legacy terminal label from %s", (typed, expected) => {
70 const sent =
71 typed + "\n\n<terminal_context>\n- Terminal 1 line 4:\n 4 | ls\n</terminal_context>";
72 expect(recallableComposerPrompt(sent)).toBe(expected);
73 });
74​
75 it("strips only review comments appended at the end", () => {
76 const block = '<review_comment filePath="src/app.ts">Keep this configurable.</review_comment>';
77 expect(recallableComposerPrompt("Please update this.\n\n" + block)).toBe("Please update this.");
78 const midPrompt = "Before\n\n" + block + "\n\nAfter";
79 expect(recallableComposerPrompt(midPrompt)).toBe(midPrompt);
80 expect(recallableComposerPrompt(midPrompt + "\n\n" + block)).toBe(midPrompt);
81 });
82​
83 it("strips a trailing preview annotation including its nested element context", () => {
84 const sent =
85 "Fix this\n\n<preview_annotation>\nPage: Example\n<element_context>\n- <button>:\n html: Save\n</element_context>\n</preview_annotation>";
86 expect(recallableComposerPrompt(sent)).toBe("Fix this");
87 });
88​
89 it("removes canonical references without restoring dangling chips", () => {
90 const sent =
91 "Look at [Terminal](t3-context://v1/terminal/term-1) please\n indented code\n![image](t3-context://v1/image/img-1)";
92 expect(recallableComposerPrompt(sent)).toBe("Look at please\n indented code");
93 expect(recallableComposerPrompt("![image](t3-context://v1/image/img-1)")).toBe("");
94 });
95​
96 it("returns an empty string for app-composed sends", () => {
97 expect(recallableComposerPrompt(" ")).toBe("");
98 expect(recallableComposerPrompt(ATTACHMENT_ONLY_BOOTSTRAP_PROMPT)).toBe("");
99 expect(recallableComposerPrompt(buildPlanImplementationPrompt("# Plan\n1. do it"))).toBe("");
100 });
101});
102​
103describe("buildComposerPromptHistoryEntries", () => {
104 it("keeps user messages with text, oldest first", () => {
105 expect(entries.map((entry) => entry.prompt)).toEqual(["first", "second", "third"]);
106 });
107​
108 it("collapses consecutive duplicates onto the newest message id", () => {
109 const collapsed = buildComposerPromptHistoryEntries([
110 { id: "m1", role: "user", text: "same" },
111 { id: "m2", role: "user", text: "same" },
112 { id: "m3", role: "user", text: "other" },
113 { id: "m4", role: "user", text: "same" },
114 ]);
115 expect(collapsed).toEqual([
116 { id: "m2", prompt: "same" },
117 { id: "m3", prompt: "other" },
118 { id: "m4", prompt: "same" },
119 ]);
120 });
121​
122 it("skips messages sent by automations, agents, and T3 Code", () => {
123 const typed = buildComposerPromptHistoryEntries([
124 { id: "m1", role: "user", text: "typed" },
125 { id: "m2", role: "user", text: "scheduled", scheduledTaskId: ScheduledTaskId.make("task") },
126 {
127 id: "scheduled-task-message:task:1:scheduled",
128 role: "user",
129 text: "[Triggered by schedule task: Daily]\n\nlegacy scheduled",
130 },
131 { id: "m3", role: "user", text: "from agent", createdBy: "agent", creationSource: "mcp" },
132 { id: "m4", role: "user", text: "from server", createdBy: "agent", creationSource: "server" },
133 ]);
134 expect(typed).toEqual([{ id: "m1", prompt: "typed" }]);
135 });
136});
137​
138describe("stepComposerPromptHistory", () => {
139 it("does not start browsing from a non-empty draft", () => {
140 expect(backward(null, "typing")).toBeNull();
141 });
142​
143 it("walks back from the newest entry", () => {
144 const first = backward(null, "");
145 expect(first).toEqual({ position: { entryId: "m3", recalled: "third" }, prompt: "third" });
146 expect(backward(first!.position, "third")?.prompt).toBe("second");
147 });
148​
149 it("is a no-op at the oldest entry so the caret keeps moving", () => {
150 expect(backward({ entryId: "m1", recalled: "first" }, "first")).toBeNull();
151 });
152​
153 it("walks forward and empties the composer past the newest entry", () => {
154 const newer = forward({ entryId: "m2", recalled: "second" }, "second");
155 expect(newer?.prompt).toBe("third");
156 expect(forward(newer!.position, "third")).toEqual({ position: null, prompt: "" });
157 });
158​
159 it("treats an edited recall as a fresh draft", () => {
160 const position: ComposerPromptHistoryPosition = { entryId: "m3", recalled: "third" };
161 expect(backward(position, "third edited")).toBeNull();
162 expect(forward(position, "third edited")).toBeNull();
163 expect(backward(position, "")?.position).toEqual({ entryId: "m3", recalled: "third" });
164 });
165​
166 it("does nothing on forward when not browsing", () => {
167 expect(forward(null, "")).toBeNull();
168 });
169​
170 it("follows the entry by id when the list changes under it", () => {
171 const grown = buildComposerPromptHistoryEntries([
172 { id: "m0", role: "user", text: "zeroth" },
173 { id: "m1", role: "user", text: "A" },
174 { id: "m2", role: "user", text: "B" },
175 { id: "m3", role: "user", text: "A" },
176 ]);
177 const older = stepComposerPromptHistory({
178 direction: "backward",
179 entries: grown,
180 position: { entryId: "m1", recalled: "A" },
181 currentPrompt: "A",
182 });
183 expect(older?.prompt).toBe("zeroth");
184 const missing = stepComposerPromptHistory({
185 direction: "forward",
186 entries: grown,
187 position: { entryId: "gone", recalled: "not sent" },
188 currentPrompt: "not sent",
189 });
190 expect(missing).toBeNull();
191 });
192​
193 it("falls back to matching text when a duplicate collapse retires the id", () => {
194 const collapsed = buildComposerPromptHistoryEntries([
195 { id: "m1", role: "user", text: "first" },
196 { id: "m3", role: "user", text: "A" },
197 ]);
198 const step = stepComposerPromptHistory({
199 direction: "backward",
200 entries: collapsed,
201 position: { entryId: "m2", recalled: "A" },
202 currentPrompt: "A",
203 });
204 expect(step?.prompt).toBe("first");
205 });
206});