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

composerPromptHistory.test.ts

176 lines7.1 KB

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