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

threadFindText.test.ts

285 lines10.9 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: "assistant",
160 cwd: undefined,
161 text: "**已返回总部工作。**据美联社报道 and ~~删除。~~后续",
162 expected: ["已返回总部工作。据美联社报道 and 删除。后续"],
163 },
164 {
165 source: "user",
166 cwd: "/workspace/repo",
167 text: "See [src/notes.ts](src/notes.ts#L42-L64) and [src/notes.ts](src/notes.ts#L42-L70)",
168 expected: ["See notes.ts · L42–64 and notes.ts · L42–70"],
169 },
170 {
171 source: "assistant",
172 cwd: "/workspace/repo",
173 text: "[Read notes](src/notes.ts:42-64) and `test/notes.ts:42-64`",
174 expected: ["Read notes notes.ts · repo/src · L42–64 and notes.ts · repo/test · L42–64"],
175 },
176 {
177 source: "assistant",
178 cwd: undefined,
179 text: [
180 "[notes.ts](file:///tmp/notes.ts#L42-64) [relative](src/notes.ts#L42-L64)",
181 "[web](https://example.com/notes.ts#L42-L64) [reverse](/tmp/notes.ts#L64-L42)",
182 "`port:42-64`",
183 "\n```text\n/tmp/notes.ts:42-64\n```",
184 ].join(" "),
185 expected: [
186 "notes.ts · L42–64 relative web reverse notes.ts · L64 port:42-64",
187 "/tmp/notes.ts:42-64\n",
188 ],
189 },
190 {
191 source: "plan",
192 cwd: String.raw`\\server\share\repo`,
193 text: "[notes.ts](file://server/share/repo/notes.ts#L42-L64) [other](file://other/share/notes.ts#L42-L64)",
194 expected: ["Proposed plan", "notes.ts · L42–64 other"],
195 },
196] as const)(
197 "indexes the displayed file range in $source text",
198 ({ source, cwd, text, expected }) => {
199 const segments =
200 source === "plan"
201 ? searchablePlanSegments(text, cwd)
202 : searchableMessageSegments({ role: source, text, streaming: false }, cwd);
203 expect(segments).toEqual(expected);
204 },
205);
206​
207it("indexes nested disclosure summaries and bodies in rendered order", () => {
208 expect(
209 assistantSegments(
210 "<details><summary>Outer</summary><p>first</p><details><summary>Inner</summary><p>second</p></details></details>",
211 ),
212 ).toEqual(["Outer", "first", "Inner", "second"]);
213});
214​
215it("indexes a citation by its comment, not its quote or link text", () => {
216 const citation = {
217 version: 1 as const,
218 environmentId: EnvironmentId.make("environment"),
219 threadId: ThreadId.make("thread"),
220 messageId: MessageId.make("message"),
221 text: "cited needle",
222 start: 0,
223 end: 14,
224 prefix: "",
225 suffix: "",
226 };
227 const text = `Please fix ${serializeAssistantCitation(citation)} thanks`;
228 expect(searchableMessageSegments({ role: "user", text, streaming: false })).toEqual([
229 "Please fix ",
230 " thanks",
231 ]);
232 const commented = serializeAssistantCitation({ ...citation, comment: "my\nnote" });
233 expect(
234 searchableMessageSegments({ role: "user", text: `fix ${commented} thanks`, streaming: false }),
235 ).toEqual(["fix ", "my note thanks"]);
236});
237​
238it("indexes insight bodies without hidden fences and keeps user and plan text literal", () => {
239 const text = "★ Insight ─────\nVisible body.\n─────";
240 expect(assistantSegments(text)).toEqual(["Visible body."]);
241 expect(searchableMessageSegments({ role: "user", text, streaming: false })?.join("\n")).toContain(
242 "★ Insight",
243 );
244 expect(searchablePlanSegments(text).join("\n")).toContain("★ Insight");
245 expect(assistantSegments(`\`\`\`text\n${text}\n\`\`\``)?.join("\n")).toContain("★ Insight");
246});
247​
248it("does not index the hidden marker of an alert written with CR-only line endings", () => {
249 expect(assistantSegments("> [!NOTE]\r> hi")).toEqual(["hi"]);
250});
251​
252it("keeps insight line breaks for backticked openers on their own line, not in prose", () => {
253 expect(assistantSegments("`★ Insight ─────`\nfirst line\nsecond line\n`─────`")).toEqual([
254 "first line",
255 " second line",
256 ]);
257 expect(assistantSegments("`★ Insight ─────` opens a callout.\nfirst line\nsecond line")).toEqual([
258 "★ Insight ───── opens a callout. first line second line",
259 ]);
260 expect(
261 assistantSegments("`★ Insight ─────`\u2028opens a callout.\nfirst line\nsecond line"),
262 ).toEqual(["★ Insight ─────\u2028opens a callout. first line second line"]);
263});
264​
265it("indexes image destinations rendered as file chips", () => {
266 expect(
267 assistantSegments(
268 "![note](src/notes.ts#L42-L64) ![screenshot](src/screenshot.png#L42-L64)",
269 "/workspace/repo",
270 ),
271 ).toEqual(["notes.ts · L42–64 "]);
272});
273​
274it.each(["\n", "\r\n", "\r"])(
275 "does not search across a standalone image with %j line endings",
276 (ending) => {
277 const text = ["Caption", "![pic](https://x.test/pic.png)", "After"].join(ending);
278 expect(assistantSegments(text)).toEqual(["Caption", " After"]);
279 },
280);
281​
282it("projects carriage-return soft line breaks to a space outside code blocks", () => {
283 expect(assistantSegments("First\rsecond")).toEqual(["First second"]);
284 expect(assistantSegments("```text\nFirst\rsecond\n```")).toEqual(["First\rsecond\n"]);
285});