Open History to see the last commit
1import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
2import { expect, it } from "vite-plus/test";
3import { serializeAssistantCitation } from "./assistantCitations.ts";
4import { ATTACHMENT_ONLY_BOOTSTRAP_PROMPT } from "./composerContextReferences.ts";
5import { searchableMessageSegments, searchablePlanSegments } from "./threadFindText.ts";
6
7it("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
24it("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
32it("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  b")).toEqual(["a", " b"]);
46 expect(indexed("a \n\t	b")).toEqual(["a", " \tb"]);
47});
48
49it("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
60it("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
70it("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
84it("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
96const assistantSegments = (text: string, cwd?: string) =>
97 searchableMessageSegments({ role: "assistant", text, streaming: false }, cwd);
98
99it("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("See <code>./src/index.ts:42</code>.", "/work")).toEqual([
104 "See index.ts · L42.",
105 ]);
106 expect(
107 assistantSegments(
108 "[See <code>./src/index.ts:42</code>](https://example.com)\n\n<pre><code>./src/index.ts:42</code></pre>",
109 "/work",
110 ),
111 ).toEqual(["See ./src/index.ts:42", "./src/index.ts:42"]);
112 expect(assistantSegments("[label](src/main.ts#L3C2)", "/workspace/repo")).toEqual([
113 "label main.ts · L3:C2",
114 ]);
115 expect(assistantSegments("[label](src/main.ts#L3C2)")).toEqual(["label"]);
116 expect(
117 assistantSegments(
118 "[](src/index.ts) [](lib/index.ts)",
119 "/workspace/repo",
120 ),
121 ).toEqual(["index.ts · repo/src index.ts · repo/lib"]);
122 for (const text of [
123 "[a](/repo/app/(a)/page.tsx) and [b](/repo/app/(b)/page.tsx)",
124 String.raw`[a](/repo/app/\(a\)/page.tsx) and [b](/repo/app/\(b\)/page.tsx)`,
125 ]) {
126 expect(assistantSegments(text, "/repo")).toEqual([
127 "a page.tsx · app/(a) and b page.tsx · app/(b)",
128 ]);
129 }
130 expect(
131 assistantSegments(
132 String.raw`[a](C:\repo\.a\page.tsx) and [b](C:\repo\.b\page.tsx)`,
133 String.raw`C:\repo`,
134 ),
135 ).toEqual(["a page.tsx · repo/.a and b page.tsx · repo/.b"]);
136 expect(
137 assistantSegments(
138 String.raw`[a](C:\repo\.a\x&y\page.tsx) and [b](C:\repo\.b\x&y\page.tsx)`,
139 String.raw`C:\repo`,
140 ),
141 ).toEqual(["a page.tsx · .a/x&y and b page.tsx · .b/x&y"]);
142 expect(
143 assistantSegments("[a](/repo/a&b/page.tsx) and [b](/repo/c&d/page.tsx)", "/repo"),
144 ).toEqual(["a page.tsx · repo/a&b and b page.tsx · repo/c&d"]);
145 expect(
146 assistantSegments("[a](</repo/a&b)/page.tsx>) and [b](</repo/c&d)/page.tsx>)", "/repo"),
147 ).toEqual(["a page.tsx · repo/a&b) and b page.tsx · repo/c&d)"]);
148 expect(
149 assistantSegments("[a](/repo/a&b>c/page.tsx) and [b](/repo/c&d>e/page.tsx)", "/repo"),
150 ).toEqual(["a page.tsx · repo/a&b>c and b page.tsx · repo/c&d>e"]);
151 expect(
152 assistantSegments(
153 String.raw`[a](</repo/a\<b &c/page.tsx>) and [b](</repo/d\<e &f/page.tsx>)`,
154 "/repo",
155 ),
156 ).toEqual(["a page.tsx · repo/a<b &c and b page.tsx · repo/d<e &f"]);
157});
158
159it("indexes the folders of angle-bracket file links that end in a literal space", () => {
160 const encoded = assistantSegments("[first](/tmp/one/a.ts%20) and [second](/tmp/two/a.ts%20)");
161 expect(encoded).toEqual(["first a.ts · tmp/one and second a.ts · tmp/two"]);
162 expect(assistantSegments("[first](</tmp/one/a.ts >) and [second](</tmp/two/a.ts >)")).toEqual(
163 encoded,
164 );
165 expect(assistantSegments("[a](</tmp/one/a%.ts >) and [b](/tmp/two/a%25.ts%2520)")).toEqual(
166 ["a a%.ts and b a%.ts%20"],
167 );
168});
169
170it("keeps fence paths literal and indexes file-chip labels in user messages", () => {
171 expect(assistantSegments("```text\n/tmp/file.ts:42\n```")).toEqual(["/tmp/file.ts:42\n"]);
172 expect(
173 searchableMessageSegments({ role: "user", text: "`/tmp/file.ts:42`", streaming: false }),
174 ).toEqual(["file.ts · L42"]);
175 expect(
176 searchableMessageSegments(
177 { role: "user", text: "<code>./src/index.ts:42</code>", streaming: false },
178 "/work",
179 ),
180 ).toEqual(["<code>./src/index.ts:42</code>"]);
181});
182
183it.each([
184 {
185 source: "assistant",
186 cwd: undefined,
187 text: "**已返回总部工作。**据美联社报道 and ~~删除。~~后续",
188 expected: ["已返回总部工作。据美联社报道 and 删除。后续"],
189 },
190 {
191 source: "user",
192 cwd: "/workspace/repo",
193 text: "See [src/notes.ts](src/notes.ts#L42-L64) and [src/notes.ts](src/notes.ts#L42-L70)",
194 expected: ["See notes.ts · L42–64 and notes.ts · L42–70"],
195 },
196 {
197 source: "assistant",
198 cwd: "/workspace/repo",
199 text: "[Read notes](src/notes.ts:42-64) and `test/notes.ts:42-64`",
200 expected: ["Read notes notes.ts · repo/src · L42–64 and notes.ts · repo/test · L42–64"],
201 },
202 {
203 source: "assistant",
204 cwd: undefined,
205 text: [
206 "[notes.ts](file:///tmp/notes.ts#L42-64) [relative](src/notes.ts#L42-L64)",
207 "[web](https://example.com/notes.ts#L42-L64) [reverse](/tmp/notes.ts#L64-L42)",
208 "`port:42-64`",
209 "\n```text\n/tmp/notes.ts:42-64\n```",
210 ].join(" "),
211 expected: [
212 "notes.ts · L42–64 relative web reverse notes.ts · L64 port:42-64",
213 "/tmp/notes.ts:42-64\n",
214 ],
215 },
216 {
217 source: "plan",
218 cwd: String.raw`\\server\share\repo`,
219 text: "[notes.ts](file://server/share/repo/notes.ts#L42-L64) [other](file://other/share/notes.ts#L42-L64)",
220 expected: ["Proposed plan", "notes.ts · L42–64 other"],
221 },
222] as const)(
223 "indexes the displayed file range in $source text",
224 ({ source, cwd, text, expected }) => {
225 const segments =
226 source === "plan"
227 ? searchablePlanSegments(text, cwd)
228 : searchableMessageSegments({ role: source, text, streaming: false }, cwd);
229 expect(segments).toEqual(expected);
230 },
231);
232
233it("indexes nested disclosure summaries and bodies in rendered order", () => {
234 expect(
235 assistantSegments(
236 "<details><summary>Outer</summary><p>first</p><details><summary>Inner</summary><p>second</p></details></details>",
237 ),
238 ).toEqual(["Outer", "first", "Inner", "second"]);
239});
240
241it("indexes a citation by its comment, not its quote or link text", () => {
242 const citation = {
243 version: 1 as const,
244 environmentId: EnvironmentId.make("environment"),
245 threadId: ThreadId.make("thread"),
246 messageId: MessageId.make("message"),
247 text: "cited needle",
248 start: 0,
249 end: 14,
250 prefix: "",
251 suffix: "",
252 };
253 const text = `Please fix ${serializeAssistantCitation(citation)} thanks`;
254 expect(searchableMessageSegments({ role: "user", text, streaming: false })).toEqual([
255 "Please fix ",
256 " thanks",
257 ]);
258 const commented = serializeAssistantCitation({ ...citation, comment: "my\nnote" });
259 expect(
260 searchableMessageSegments({ role: "user", text: `fix ${commented} thanks`, streaming: false }),
261 ).toEqual(["fix ", "my note thanks"]);
262});
263
264it("indexes insight bodies without hidden fences and keeps user and plan text literal", () => {
265 const text = "★ Insight ─────\nVisible body.\n─────";
266 expect(assistantSegments(text)).toEqual(["Visible body."]);
267 expect(searchableMessageSegments({ role: "user", text, streaming: false })?.join("\n")).toContain(
268 "★ Insight",
269 );
270 expect(searchablePlanSegments(text).join("\n")).toContain("★ Insight");
271 expect(assistantSegments(`\`\`\`text\n${text}\n\`\`\``)?.join("\n")).toContain("★ Insight");
272});
273
274it("does not index the hidden marker of an alert written with CR-only line endings", () => {
275 expect(assistantSegments("> [!NOTE]\r> hi")).toEqual(["hi"]);
276});
277
278it("keeps insight line breaks for backticked openers on their own line, not in prose", () => {
279 expect(assistantSegments("`★ Insight ─────`\nfirst line\nsecond line\n`─────`")).toEqual([
280 "first line",
281 " second line",
282 ]);
283 expect(assistantSegments("`★ Insight ─────` opens a callout.\nfirst line\nsecond line")).toEqual([
284 "★ Insight ───── opens a callout. first line second line",
285 ]);
286 expect(
287 assistantSegments("`★ Insight ─────`\u2028opens a callout.\nfirst line\nsecond line"),
288 ).toEqual(["★ Insight ─────\u2028opens a callout. first line second line"]);
289});
290
291it("indexes image destinations rendered as file chips", () => {
292 expect(
293 assistantSegments(
294 " ",
295 "/workspace/repo",
296 ),
297 ).toEqual(["notes.ts · L42–64 "]);
298});
299
300it.each(["\n", "\r\n", "\r"])(
301 "does not search across a standalone image with %j line endings",
302 (ending) => {
303 const text = ["Caption", "", "After"].join(ending);
304 expect(assistantSegments(text)).toEqual(["Caption", " After"]);
305 },
306);
307
308it("projects carriage-return soft line breaks to a space outside code blocks", () => {
309 expect(assistantSegments("First\rsecond")).toEqual(["First second"]);
310 expect(assistantSegments("```text\nFirst\rsecond\n```")).toEqual(["First\rsecond\n"]);
311});