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

threadFindText.test.ts

130 lines4.8 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("excludes structured context chips without joining text across them", () => {
25 expect(
26 searchableMessageSegments({
27 role: "user",
28 streaming: false,
29 text: "before[hidden label](t3-context://v1/terminal/terminal_1)after",
30 context: { version: 1, records: [] },
31 }),
32 ).toEqual(["before", "after"]);
33});
34​
35it("excludes the hidden prompt of an attachment-only message", () => {
36 expect(
37 searchableMessageSegments({
38 role: "user",
39 streaming: false,
40 text: ATTACHMENT_ONLY_BOOTSTRAP_PROMPT,
41 }),
42 ).toEqual([]);
43});
44​
45it("does not upgrade literal legacy tags in messages with structured context", () => {
46 const text =
47 "<terminal_context>\n- Terminal 1 line 12:\n 12 | visible output\n</terminal_context>";
48 expect(
49 searchableMessageSegments({
50 role: "user",
51 streaming: false,
52 text,
53 context: { version: 1, records: [] },
54 })?.join("\n"),
55 ).toContain("visible output");
56 expect(searchableMessageSegments({ role: "user", streaming: false, text })).toEqual([]);
57});
58​
59it("excludes repeated legacy attachments containing literal context tags", () => {
60 const context =
61 "<terminal_context>\n- Terminal 1 line 12:\n 12 | <terminal_context>literal</terminal_context>\n</terminal_context>";
62 expect(
63 searchableMessageSegments({
64 role: "user",
65 streaming: false,
66 text: `Fix this\n\n${context}\n\n${context}`,
67 }),
68 ).toEqual(["Fix this"]);
69});
70​
71const assistantSegments = (text: string, cwd?: string) =>
72 searchableMessageSegments({ role: "assistant", text, streaming: false }, cwd);
73​
74it("searches displayed prose and file-chip labels without hidden paths", () => {
75 expect(
76 assistantSegments("[important description](/tmp/actual.ts). `/tmp/inline-example.ts:42`"),
77 ).toEqual(["important description actual.ts. inline-example.ts · L42"]);
78 expect(assistantSegments("[label](src/main.ts#L3C2)", "/workspace/repo")).toEqual([
79 "label main.ts · L3:C2",
80 ]);
81 expect(assistantSegments("[label](src/main.ts#L3C2)")).toEqual(["label"]);
82});
83​
84it("keeps fence paths literal and indexes file-chip labels in user messages", () => {
85 expect(assistantSegments("```text\n/tmp/file.ts:42\n```")).toEqual(["/tmp/file.ts:42\n"]);
86 expect(
87 searchableMessageSegments({ role: "user", text: "`/tmp/file.ts:42`", streaming: false }),
88 ).toEqual(["file.ts · L42"]);
89});
90​
91it("indexes nested disclosure summaries and bodies in rendered order", () => {
92 expect(
93 assistantSegments(
94 "<details><summary>Outer</summary><p>first</p><details><summary>Inner</summary><p>second</p></details></details>",
95 ),
96 ).toEqual(["Outer", "first", "Inner", "second"]);
97});
98​
99it("indexes a citation by its comment, not its quote or link text", () => {
100 const citation = {
101 version: 1 as const,
102 environmentId: EnvironmentId.make("environment"),
103 threadId: ThreadId.make("thread"),
104 messageId: MessageId.make("message"),
105 text: "cited needle",
106 start: 0,
107 end: 14,
108 prefix: "",
109 suffix: "",
110 };
111 const text = `Please fix ${serializeAssistantCitation(citation)} thanks`;
112 expect(searchableMessageSegments({ role: "user", text, streaming: false })).toEqual([
113 "Please fix ",
114 " thanks",
115 ]);
116 const commented = serializeAssistantCitation({ ...citation, comment: "my\nnote" });
117 expect(
118 searchableMessageSegments({ role: "user", text: `fix ${commented} thanks`, streaming: false }),
119 ).toEqual(["fix ", "my note thanks"]);
120});
121​
122it("indexes insight bodies without hidden fences and keeps user and plan text literal", () => {
123 const text = "★ Insight ─────\nVisible body.\n─────";
124 expect(assistantSegments(text)).toEqual(["Visible body."]);
125 expect(searchableMessageSegments({ role: "user", text, streaming: false })?.join("\n")).toContain(
126 "★ Insight",
127 );
128 expect(searchablePlanSegments(text).join("\n")).toContain("★ Insight");
129 expect(assistantSegments(`\`\`\`text\n${text}\n\`\`\``)?.join("\n")).toContain("★ Insight");
130});