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("[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&y\page.tsx) and [b](C:\repo\.b\x&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&b/page.tsx) and [b](/repo/c&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 &c/page.tsx>) and [b](</repo/d\<e &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
144it("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
151it("indexes nested disclosure summaries and bodies in rendered order", () => {
152 expect(
153 assistantSegments(
154 "<details><summary>Outer</summary><p>first</p><details><summary>Inner</summary><p>second</p></details></details>",
155 ),
156 ).toEqual(["Outer", "first", "Inner", "second"]);
157});
158
159it("indexes a citation by its comment, not its quote or link text", () => {
160 const citation = {
161 version: 1 as const,
162 environmentId: EnvironmentId.make("environment"),
163 threadId: ThreadId.make("thread"),
164 messageId: MessageId.make("message"),
165 text: "cited needle",
166 start: 0,
167 end: 14,
168 prefix: "",
169 suffix: "",
170 };
171 const text = `Please fix ${serializeAssistantCitation(citation)} thanks`;
172 expect(searchableMessageSegments({ role: "user", text, streaming: false })).toEqual([
173 "Please fix ",
174 " thanks",
175 ]);
176 const commented = serializeAssistantCitation({ ...citation, comment: "my\nnote" });
177 expect(
178 searchableMessageSegments({ role: "user", text: `fix ${commented} thanks`, streaming: false }),
179 ).toEqual(["fix ", "my note thanks"]);
180});
181
182it("indexes insight bodies without hidden fences and keeps user and plan text literal", () => {
183 const text = "★ Insight ─────\nVisible body.\n─────";
184 expect(assistantSegments(text)).toEqual(["Visible body."]);
185 expect(searchableMessageSegments({ role: "user", text, streaming: false })?.join("\n")).toContain(
186 "★ Insight",
187 );
188 expect(searchablePlanSegments(text).join("\n")).toContain("★ Insight");
189 expect(assistantSegments(`\`\`\`text\n${text}\n\`\`\``)?.join("\n")).toContain("★ Insight");
190});