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

threadFindText.test.ts

271 lines10.4 KB

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 &#32;b")).toEqual(["a", " b"]);
46 expect(indexed("a \n\t&Tab;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("[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 expect(
108 assistantSegments(
109 "[![a](https://example.com/a.png)](src/index.ts) [![b](https://example.com/b.png)](lib/index.ts)",
110 "/workspace/repo",
111 ),
112 ).toEqual(["index.ts · repo/src index.ts · repo/lib"]);
113 for (const text of [
114 "[a](/repo/app/(a)/page.tsx) and [b](/repo/app/(b)/page.tsx)",
115 String.raw`[a](/repo/app/\(a\)/page.tsx) and [b](/repo/app/\(b\)/page.tsx)`,
116 ]) {
117 expect(assistantSegments(text, "/repo")).toEqual([
118 "a page.tsx · app/(a) and b page.tsx · app/(b)",
119 ]);
120 }
121 expect(
122 assistantSegments(
123 String.raw`[a](C:\repo\.a\page.tsx) and [b](C:\repo\.b\page.tsx)`,
124 String.raw`C:\repo`,
125 ),
126 ).toEqual(["a page.tsx · repo/.a and b page.tsx · repo/.b"]);
127 expect(
128 assistantSegments(
129 String.raw`[a](C:\repo\.a\x&amp;y\page.tsx) and [b](C:\repo\.b\x&amp;y\page.tsx)`,
130 String.raw`C:\repo`,
131 ),
132 ).toEqual(["a page.tsx · .a/x&y and b page.tsx · .b/x&y"]);
133 expect(
134 assistantSegments("[a](/repo/a&amp;b/page.tsx) and [b](/repo/c&amp;d/page.tsx)", "/repo"),
135 ).toEqual(["a page.tsx · repo/a&b and b page.tsx · repo/c&d"]);
136 expect(
137 assistantSegments("[a](</repo/a&b)/page.tsx>) and [b](</repo/c&d)/page.tsx>)", "/repo"),
138 ).toEqual(["a page.tsx · repo/a&b) and b page.tsx · repo/c&d)"]);
139 expect(
140 assistantSegments("[a](/repo/a&b>c/page.tsx) and [b](/repo/c&d>e/page.tsx)", "/repo"),
141 ).toEqual(["a page.tsx · repo/a&b>c and b page.tsx · repo/c&d>e"]);
142 expect(
143 assistantSegments(
144 String.raw`[a](</repo/a\<b &amp;c/page.tsx>) and [b](</repo/d\<e &amp;f/page.tsx>)`,
145 "/repo",
146 ),
147 ).toEqual(["a page.tsx · repo/a<b &c and b page.tsx · repo/d<e &f"]);
148});
149​
150it("keeps fence paths literal and indexes file-chip labels in user messages", () => {
151 expect(assistantSegments("```text\n/tmp/file.ts:42\n```")).toEqual(["/tmp/file.ts:42\n"]);
152 expect(
153 searchableMessageSegments({ role: "user", text: "`/tmp/file.ts:42`", streaming: false }),
154 ).toEqual(["file.ts · L42"]);
155});
156​
157it.each([
158 {
159 source: "user",
160 cwd: "/workspace/repo",
161 text: "See [src/notes.ts](src/notes.ts#L42-L64) and [src/notes.ts](src/notes.ts#L42-L70)",
162 expected: ["See notes.ts · L42–64 and notes.ts · L42–70"],
163 },
164 {
165 source: "assistant",
166 cwd: "/workspace/repo",
167 text: "[Read notes](src/notes.ts:42-64) and `test/notes.ts:42-64`",
168 expected: ["Read notes notes.ts · repo/src · L42–64 and notes.ts · repo/test · L42–64"],
169 },
170 {
171 source: "assistant",
172 cwd: undefined,
173 text: [
174 "[notes.ts](file:///tmp/notes.ts#L42-64) [relative](src/notes.ts#L42-L64)",
175 "[web](https://example.com/notes.ts#L42-L64) [reverse](/tmp/notes.ts#L64-L42)",
176 "`port:42-64`",
177 "\n```text\n/tmp/notes.ts:42-64\n```",
178 ].join(" "),
179 expected: [
180 "notes.ts · L42–64 relative web reverse notes.ts · L64 port:42-64",
181 "/tmp/notes.ts:42-64\n",
182 ],
183 },
184 {
185 source: "plan",
186 cwd: String.raw`\\server\share\repo`,
187 text: "[notes.ts](file://server/share/repo/notes.ts#L42-L64) [other](file://other/share/notes.ts#L42-L64)",
188 expected: ["Proposed plan", "notes.ts · L42–64 other"],
189 },
190] as const)(
191 "indexes the displayed file range in $source text",
192 ({ source, cwd, text, expected }) => {
193 const segments =
194 source === "plan"
195 ? searchablePlanSegments(text, cwd)
196 : searchableMessageSegments({ role: source, text, streaming: false }, cwd);
197 expect(segments).toEqual(expected);
198 },
199);
200​
201it("indexes nested disclosure summaries and bodies in rendered order", () => {
202 expect(
203 assistantSegments(
204 "<details><summary>Outer</summary><p>first</p><details><summary>Inner</summary><p>second</p></details></details>",
205 ),
206 ).toEqual(["Outer", "first", "Inner", "second"]);
207});
208​
209it("indexes a citation by its comment, not its quote or link text", () => {
210 const citation = {
211 version: 1 as const,
212 environmentId: EnvironmentId.make("environment"),
213 threadId: ThreadId.make("thread"),
214 messageId: MessageId.make("message"),
215 text: "cited needle",
216 start: 0,
217 end: 14,
218 prefix: "",
219 suffix: "",
220 };
221 const text = `Please fix ${serializeAssistantCitation(citation)} thanks`;
222 expect(searchableMessageSegments({ role: "user", text, streaming: false })).toEqual([
223 "Please fix ",
224 " thanks",
225 ]);
226 const commented = serializeAssistantCitation({ ...citation, comment: "my\nnote" });
227 expect(
228 searchableMessageSegments({ role: "user", text: `fix ${commented} thanks`, streaming: false }),
229 ).toEqual(["fix ", "my note thanks"]);
230});
231​
232it("indexes insight bodies without hidden fences and keeps user and plan text literal", () => {
233 const text = "★ Insight ─────\nVisible body.\n─────";
234 expect(assistantSegments(text)).toEqual(["Visible body."]);
235 expect(searchableMessageSegments({ role: "user", text, streaming: false })?.join("\n")).toContain(
236 "★ Insight",
237 );
238 expect(searchablePlanSegments(text).join("\n")).toContain("★ Insight");
239 expect(assistantSegments(`\`\`\`text\n${text}\n\`\`\``)?.join("\n")).toContain("★ Insight");
240});
241​
242it("does not index the hidden marker of an alert written with CR-only line endings", () => {
243 expect(assistantSegments("> [!NOTE]\r> hi")).toEqual(["hi"]);
244});
245​
246it("keeps insight line breaks for backticked openers on their own line, not in prose", () => {
247 expect(assistantSegments("`★ Insight ─────`\nfirst line\nsecond line\n`─────`")).toEqual([
248 "first line",
249 " second line",
250 ]);
251 expect(assistantSegments("`★ Insight ─────` opens a callout.\nfirst line\nsecond line")).toEqual([
252 "★ Insight ───── opens a callout. first line second line",
253 ]);
254 expect(
255 assistantSegments("`★ Insight ─────`\u2028opens a callout.\nfirst line\nsecond line"),
256 ).toEqual(["★ Insight ─────\u2028opens a callout. first line second line"]);
257});
258​
259it("indexes image destinations rendered as file chips", () => {
260 expect(
261 assistantSegments(
262 "![note](src/notes.ts#L42-L64) ![screenshot](src/screenshot.png#L42-L64)",
263 "/workspace/repo",
264 ),
265 ).toEqual(["notes.ts · L42–64 "]);
266});
267​
268it("projects carriage-return soft line breaks to a space outside code blocks", () => {
269 expect(assistantSegments("First\rsecond")).toEqual(["First second"]);
270 expect(assistantSegments("```text\nFirst\rsecond\n```")).toEqual(["First\rsecond\n"]);
271});