The best way to control Claude Code, Codex and more from any computer or phone

main
ts 243 lines 9.11 KB
Raw
1 import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
2 import { expect, it } from "vite-plus/test";
3 import { serializeAssistantCitation } from "./assistantCitations.ts";
4 import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "./composerContextReferences.ts";
5 import { searchableMessageSegments, searchablePlanSegments } from "./threadFindText.ts";
6
7 it("excludes review attachments rendered as context chips", () => {
8 const text = [
9 "Before **review**",
10 '<review_comment sectionId="turn:2" sectionTitle="Turn 2" filePath="hidden.ts" startIndex="3" endIndex="14" rangeLabel="L4">',
11 "Keep **this literal** comment.",
12 "```diff",
13 "+ hidden patch content",
14 "```",
15 "</review_comment>",
16 "After review",
17 ].join("\n");
18 expect(searchableMessageSegments({ role: "user", text, streaming: false })).toEqual([
19 "Before review",
20 " After review",
21 ]);
22 });
23
24 it("indexes single tildes in user paths as typed", () => {
25 const text = "Install as ~/a/x, then read (~/a/guide.md).";
26 expect(searchableMessageSegments({ role: "user", text, streaming: false })?.join("")).toBe(text);
27 expect(
28 searchableMessageSegments({ role: "user", text: "Drop ~~old~~", streaming: false })?.join(""),
29 ).toBe("Drop old");
30 });
31
32 it("indexes the spaces typed at the start of a user's lines, as the bubble shows them", () => {
33 const text = "Plan\n Step one\n\tDetail";
34 // Each line ending reads as one space, as the text after a rendered line break does.
35 expect(searchableMessageSegments({ role: "user", text, streaming: false })).toEqual([
36 "Plan",
37 " Step one",
38 " \tDetail",
39 ]);
40 const indexed = (text: string) =>
41 searchableMessageSegments({ role: "user", text, streaming: false });
42 // A task's checkbox does not move where its continuation lines start.
43 expect(indexed("- [ ] task\n detail")).toEqual([" task", " detail"]);
44 // A space or tab written as a character reference is dropped there, as without the indentation.
45 expect(indexed("a\n &#32;b")).toEqual(["a", " b"]);
46 expect(indexed("a \n\t&Tab;b")).toEqual(["a", " \tb"]);
47 });
48
49 it("excludes structured context chips without joining text across them", () => {
50 expect(
51 searchableMessageSegments({
52 role: "user",
53 streaming: false,
54 text: "before[hidden label](t3-context://v1/terminal/terminal_1)after",
55 context: { version: 1, records: [] },
56 }),
57 ).toEqual(["before", "after"]);
58 });
59
60 it("excludes the hidden prompt of an attachment-only message", () => {
61 expect(
62 searchableMessageSegments({
63 role: "user",
64 streaming: false,
65 text: ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
66 }),
67 ).toEqual([]);
68 });
69
70 it("does not upgrade literal legacy tags in messages with structured context", () => {
71 const text =
72 "<terminal_context>\n- Terminal 1 line 12:\n 12 | visible output\n</terminal_context>";
73 expect(
74 searchableMessageSegments({
75 role: "user",
76 streaming: false,
77 text,
78 context: { version: 1, records: [] },
79 })?.join("\n"),
80 ).toContain("visible output");
81 expect(searchableMessageSegments({ role: "user", streaming: false, text })).toEqual([]);
82 });
83
84 it("excludes repeated legacy attachments containing literal context tags", () => {
85 const context =
86 "<terminal_context>\n- Terminal 1 line 12:\n 12 | <terminal_context>literal</terminal_context>\n</terminal_context>";
87 expect(
88 searchableMessageSegments({
89 role: "user",
90 streaming: false,
91 text: `Fix this\n\n${context}\n\n${context}`,
92 }),
93 ).toEqual(["Fix this"]);
94 });
95
96 const assistantSegments = (text: string, cwd?: string) =>
97 searchableMessageSegments({ role: "assistant", text, streaming: false }, cwd);
98
99 it("searches displayed prose and file-chip labels without hidden paths", () => {
100 expect(
101 assistantSegments("[important description](/tmp/actual.ts). `/tmp/inline-example.ts:42`"),
102 ).toEqual(["important description actual.ts. inline-example.ts · L42"]);
103 expect(assistantSegments("[label](src/main.ts#L3C2)", "/workspace/repo")).toEqual([
104 "label main.ts · L3:C2",
105 ]);
106 expect(assistantSegments("[label](src/main.ts#L3C2)")).toEqual(["label"]);
107 for (const text of [
108 "[a](/repo/app/(a)/page.tsx) and [b](/repo/app/(b)/page.tsx)",
109 String.raw`[a](/repo/app/\(a\)/page.tsx) and [b](/repo/app/\(b\)/page.tsx)`,
110 ]) {
111 expect(assistantSegments(text, "/repo")).toEqual([
112 "a page.tsx · app/(a) and b page.tsx · app/(b)",
113 ]);
114 }
115 expect(
116 assistantSegments(
117 String.raw`[a](C:\repo\.a\page.tsx) and [b](C:\repo\.b\page.tsx)`,
118 String.raw`C:\repo`,
119 ),
120 ).toEqual(["a page.tsx · repo/.a and b page.tsx · repo/.b"]);
121 expect(
122 assistantSegments(
123 String.raw`[a](C:\repo\.a\x&amp;y\page.tsx) and [b](C:\repo\.b\x&amp;y\page.tsx)`,
124 String.raw`C:\repo`,
125 ),
126 ).toEqual(["a page.tsx · .a/x&y and b page.tsx · .b/x&y"]);
127 expect(
128 assistantSegments("[a](/repo/a&amp;b/page.tsx) and [b](/repo/c&amp;d/page.tsx)", "/repo"),
129 ).toEqual(["a page.tsx · repo/a&b and b page.tsx · repo/c&d"]);
130 expect(
131 assistantSegments("[a](</repo/a&b)/page.tsx>) and [b](</repo/c&d)/page.tsx>)", "/repo"),
132 ).toEqual(["a page.tsx · repo/a&b) and b page.tsx · repo/c&d)"]);
133 expect(
134 assistantSegments("[a](/repo/a&b>c/page.tsx) and [b](/repo/c&d>e/page.tsx)", "/repo"),
135 ).toEqual(["a page.tsx · repo/a&b>c and b page.tsx · repo/c&d>e"]);
136 expect(
137 assistantSegments(
138 String.raw`[a](</repo/a\<b &amp;c/page.tsx>) and [b](</repo/d\<e &amp;f/page.tsx>)`,
139 "/repo",
140 ),
141 ).toEqual(["a page.tsx · repo/a<b &c and b page.tsx · repo/d<e &f"]);
142 });
143
144 it("keeps fence paths literal and indexes file-chip labels in user messages", () => {
145 expect(assistantSegments("```text\n/tmp/file.ts:42\n```")).toEqual(["/tmp/file.ts:42\n"]);
146 expect(
147 searchableMessageSegments({ role: "user", text: "`/tmp/file.ts:42`", streaming: false }),
148 ).toEqual(["file.ts · L42"]);
149 });
150
151 it.each([
152 {
153 source: "user",
154 cwd: "/workspace/repo",
155 text: "See [src/notes.ts](src/notes.ts#L42-L64) and [src/notes.ts](src/notes.ts#L42-L70)",
156 expected: ["See notes.ts · L42–64 and notes.ts · L42–70"],
157 },
158 {
159 source: "assistant",
160 cwd: "/workspace/repo",
161 text: "[Read notes](src/notes.ts:42-64) and `test/notes.ts:42-64`",
162 expected: ["Read notes notes.ts · repo/src · L42–64 and notes.ts · repo/test · L42–64"],
163 },
164 {
165 source: "assistant",
166 cwd: undefined,
167 text: [
168 "[notes.ts](file:///tmp/notes.ts#L42-64) [relative](src/notes.ts#L42-L64)",
169 "[web](https://example.com/notes.ts#L42-L64) [reverse](/tmp/notes.ts#L64-L42)",
170 "`port:42-64`",
171 "\n```text\n/tmp/notes.ts:42-64\n```",
172 ].join(" "),
173 expected: [
174 "notes.ts · L42–64 relative web reverse notes.ts · L64 port:42-64",
175 "/tmp/notes.ts:42-64\n",
176 ],
177 },
178 {
179 source: "plan",
180 cwd: String.raw`\\server\share\repo`,
181 text: "[notes.ts](file://server/share/repo/notes.ts#L42-L64) [other](file://other/share/notes.ts#L42-L64)",
182 expected: ["Proposed plan", "notes.ts · L42–64 other"],
183 },
184 ] as const)(
185 "indexes the displayed file range in $source text",
186 ({ source, cwd, text, expected }) => {
187 const segments =
188 source === "plan"
189 ? searchablePlanSegments(text, cwd)
190 : searchableMessageSegments({ role: source, text, streaming: false }, cwd);
191 expect(segments).toEqual(expected);
192 },
193 );
194
195 it("indexes nested disclosure summaries and bodies in rendered order", () => {
196 expect(
197 assistantSegments(
198 "<details><summary>Outer</summary><p>first</p><details><summary>Inner</summary><p>second</p></details></details>",
199 ),
200 ).toEqual(["Outer", "first", "Inner", "second"]);
201 });
202
203 it("indexes a citation by its comment, not its quote or link text", () => {
204 const citation = {
205 version: 1 as const,
206 environmentId: EnvironmentId.make("environment"),
207 threadId: ThreadId.make("thread"),
208 messageId: MessageId.make("message"),
209 text: "cited needle",
210 start: 0,
211 end: 14,
212 prefix: "",
213 suffix: "",
214 };
215 const text = `Please fix ${serializeAssistantCitation(citation)} thanks`;
216 expect(searchableMessageSegments({ role: "user", text, streaming: false })).toEqual([
217 "Please fix ",
218 " thanks",
219 ]);
220 const commented = serializeAssistantCitation({ ...citation, comment: "my\nnote" });
221 expect(
222 searchableMessageSegments({ role: "user", text: `fix ${commented} thanks`, streaming: false }),
223 ).toEqual(["fix ", "my note thanks"]);
224 });
225
226 it("indexes insight bodies without hidden fences and keeps user and plan text literal", () => {
227 const text = "★ Insight ─────\nVisible body.\n─────";
228 expect(assistantSegments(text)).toEqual(["Visible body."]);
229 expect(searchableMessageSegments({ role: "user", text, streaming: false })?.join("\n")).toContain(
230 "★ Insight",
231 );
232 expect(searchablePlanSegments(text).join("\n")).toContain("★ Insight");
233 expect(assistantSegments(`\`\`\`text\n${text}\n\`\`\``)?.join("\n")).toContain("★ Insight");
234 });
235
236 it("indexes image destinations rendered as file chips", () => {
237 expect(
238 assistantSegments(
239 "![note](src/notes.ts#L42-L64) ![screenshot](src/screenshot.png#L42-L64)",
240 "/workspace/repo",
241 ),
242 ).toEqual(["notes.ts · L42–64 "]);
243 });