@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 384 lines 14.3 KB
Raw
1 import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
2 import { describe, expect, it } from "vite-plus/test";
3
4 import {
5 collectWrappedTerminalLinkLine,
6 extractTerminalLinks,
7 isTerminalUrl,
8 resolveTerminalLinkTarget,
9 type TerminalBufferLineLike,
10 } from "./terminal-links";
11
12 function createBufferLine(text: string, isWrapped = false): TerminalBufferLineLike {
13 return {
14 isWrapped,
15 translateToString: (trimRight = false) => (trimRight ? text.replace(/\s+$/u, "") : text),
16 };
17 }
18
19 describe("extractTerminalLinks", () => {
20 it.each([
21 String.raw`C:\dev\ai-test\dir 1`,
22 String.raw`C:\dev\ai-test\dir 1\test`,
23 "C:\\",
24 String.raw`\\server\my share\dir 1`,
25 "/home/Jane Doe/project",
26 String.raw`C:\dev\folder (draft)\name!`,
27 ])("keeps the full PowerShell prompt path %s in one link", (path) => {
28 expect(extractTerminalLinks(`PS ${path}> `)).toEqual([
29 { kind: "path", text: path, start: 3, end: 3 + path.length },
30 ]);
31 });
32
33 it("keeps links in a command separate from the PowerShell prompt path", () => {
34 const path = String.raw`C:\dev\dir 1\test`;
35 const prompt = `PS ${path}> `;
36 const url = "https://example.com";
37 expect(extractTerminalLinks(`${prompt}echo ${url}`)).toEqual([
38 { kind: "path", text: path, start: 3, end: 3 + path.length },
39 { kind: "url", text: url, start: prompt.length + 5, end: prompt.length + 5 + url.length },
40 ]);
41 });
42
43 it("does not extend ordinary output paths through trailing prose", () => {
44 const path = String.raw`C:\dev\project`;
45 expect(extractTerminalLinks(`see ${path} for details`)).toEqual([
46 { kind: "path", text: path, start: 4, end: 4 + path.length },
47 ]);
48 });
49
50 it.each([
51 String.raw`src\index.ts:12`,
52 String.raw`.\src\index.ts:12:7`,
53 String.raw`..\src\index.ts:12`,
54 String.raw`C:\Users\Jane\project\index.ts:12`,
55 String.raw`\\server\share\index.ts:12`,
56 ])("finds Windows path %s", (text) => {
57 const line = `error at ${text}: failed`;
58 expect(extractTerminalLinks(line)).toEqual([
59 { kind: "path", text, start: 9, end: 9 + text.length },
60 ]);
61 });
62
63 it.each(['"', "'", "`"])("finds paths containing spaces enclosed in %s", (quote) => {
64 const text = String.raw`C:\Users\Jane Doe\project\index.ts:12:7`;
65 expect(extractTerminalLinks(`see ${quote}${text}${quote}.`)).toEqual([
66 { kind: "path", text, start: 5, end: 5 + text.length },
67 ]);
68 const atText = "src/my contact@support.vue:12:3";
69 expect(extractTerminalLinks(`see ${quote}${atText}${quote}.`)).toEqual([
70 { kind: "path", text: atText, start: 5, end: 5 + atText.length },
71 ]);
72 });
73
74 it.each([
75 String.raw`src\my folder\index.ts`,
76 "./my folder/index.ts",
77 "/home/Jane Doe/index.ts",
78 "/home/Jane Doe/file)",
79 String.raw`\\server\my share\index.ts`,
80 ])("keeps positions outside quotes on %s", (path) => {
81 expect(extractTerminalLinks(`"${path}":12:7: error`)).toEqual([
82 { kind: "path", text: `${path}:12:7`, start: 1, end: path.length + 7 },
83 ]);
84 });
85
86 it.each([
87 'error: "something went wrong"',
88 'error: "something went wrong."',
89 "see README.md, Node.js v22 on 127.0.0.1:3000",
90 "README.md(42,7): see details",
91 "main.cs(42,7): error CS1002: expected semicolon",
92 "Connecting to api.example.com:443...",
93 "dial tcp: lookup db.internal:5432: no such host",
94 "redis.local:6379> PING",
95 "postgres@db.internal:5432",
96 ])("does not treat prose or unsupported locations as a path: %s", (line) => {
97 expect(extractTerminalLinks(line)).toEqual([]);
98 });
99
100 it("keeps quoted paths and URLs separate without duplicate partial paths", () => {
101 const path = String.raw`C:\Jane Doe\index.ts:12`;
102 const url = "https://example.com/docs";
103 const line = `"${path}" ${url} src/index.ts`;
104 expect(extractTerminalLinks(line)).toEqual([
105 { kind: "path", text: path, start: 1, end: 1 + path.length },
106 { kind: "url", text: url, start: path.length + 3, end: path.length + 3 + url.length },
107 { kind: "path", text: "src/index.ts", start: line.length - 12, end: line.length },
108 ]);
109 });
110
111 it("classifies uppercase schemes as URLs at activation time too", () => {
112 expect(isTerminalUrl("HTTPS://example.com/docs")).toBe(true);
113 expect(isTerminalUrl("Http://example.com")).toBe(true);
114 expect(isTerminalUrl("src/components/main.ts")).toBe(false);
115 expect(isTerminalUrl("httpsdocs/readme.md")).toBe(false);
116 });
117
118 it("trims trailing punctuation from links", () => {
119 const line = "(https://example.com/docs), ./src/main.ts:12.";
120 expect(extractTerminalLinks(line)).toEqual([
121 {
122 kind: "url",
123 text: "https://example.com/docs",
124 start: 1,
125 end: 25,
126 },
127 {
128 kind: "path",
129 text: "./src/main.ts:12",
130 start: 28,
131 end: 44,
132 },
133 ]);
134 });
135
136 it.each([
137 ["https://example.com/a(b)c)", "https://example.com/a(b)c"],
138 ["https://example.com/(docs)", "https://example.com/(docs)"],
139 ["https://example.com/docs)}])", "https://example.com/docs)"],
140 ["https://example.com/docs):", "https://example.com/docs):"],
141 ["/tmp/a[b]c]", "/tmp/a[b]c"],
142 ["/tmp/docs)}])", "/tmp/docs)"],
143 ["/tmp/docs):", "/tmp/docs"],
144 ])("trims only unbalanced closing delimiters from %s", (raw, text) => {
145 expect(extractTerminalLinks(`see ${raw}`)).toEqual([
146 { kind: isTerminalUrl(text) ? "url" : "path", text, start: 4, end: 4 + text.length },
147 ]);
148 });
149
150 it.each([
151 "src/żółć.ts:12:7",
152 "żółć/main.ts:12",
153 String.raw`żółć\main.ts:12`,
154 "src/cafe\u0301.ts:12",
155 "src/𐐀.ts:12",
156 "src/app/[id]/page.tsx:3:10",
157 "src/routes/(app)/+page.svelte:12:4",
158 "src/routes/(app)/+page@.svelte:12:3",
159 "src/contact@support.vue:12:3",
160 String.raw`src\contact@support.ts:12:3`,
161 "src/équipe@home.ts:12:3",
162 "src/foo$contact@support.ts:12:3",
163 "node_modules/.pnpm/react@18.3.1/node_modules/react/index.js:12:5",
164 "app/routes/posts.$id.tsx:5",
165 "main.c:1:25",
166 "page.test.tsx:5:3",
167 "foo$bar.ts:12:3",
168 "+page.svelte:12:4",
169 "contact@support.ts:12:3",
170 ])("keeps the complete relative path %s", (text) => {
171 expect(extractTerminalLinks(`see ${text} for details`)).toEqual([
172 { kind: "path", text, start: 4, end: 4 + text.length },
173 ]);
174 });
175
176 it("finds Windows absolute paths with forward slashes", () => {
177 const line = "see C:/Users/someone/project/src/file.ts:42 for details";
178 const path = "C:/Users/someone/project/src/file.ts:42";
179 const start = line.indexOf(path);
180 expect(extractTerminalLinks(line)).toEqual([
181 {
182 kind: "path",
183 text: path,
184 start,
185 end: start + path.length,
186 },
187 ]);
188 });
189
190 it("keeps a trailing colon on URLs", () => {
191 expect(extractTerminalLinks("GET https://example.test/items/foo:")).toEqual([
192 { kind: "url", text: "https://example.test/items/foo:", start: 4, end: 35 },
193 ]);
194 });
195
196 it.each([
197 ["./main.go:10:5: undefined: x", "./main.go:10:5"],
198 ["/home/dev/app/src/main.c:10:5: error: expected ';'", "/home/dev/app/src/main.c:10:5"],
199 ["C:\\dev\\app\\src\\main.c:10:5: error: expected ';'", "C:\\dev\\app\\src\\main.c:10:5"],
200 ["wrote ./out/report.txt:", "./out/report.txt"],
201 ])("drops the colon that ends a compiler diagnostic location in %s", (line, text) => {
202 const start = line.indexOf(text);
203 expect(extractTerminalLinks(line)).toEqual([
204 { kind: "path", text, start, end: start + text.length },
205 ]);
206 });
207
208 it.each([
209 " GET /api/trpc/post.list,user.me?batch=1&input=%7B%220%22%3A%7B%22json%22%3Anull%7D%7D 200 in 35ms",
210 "GET /api?+debug 200",
211 'Started GET "/posts?page=2" for ::1',
212 "proxying api/trpc/post.list?batch=1 to the backend",
213 ])("does not turn a logged request route into an editor link: %s", (line) => {
214 expect(extractTerminalLinks(line)).toEqual([]);
215 });
216
217 it("keeps file and URL links before and after a logged request route", () => {
218 const before = "src/server.ts:12";
219 const after = "/app/bin/server:3:7";
220 const url = "https://example.com/api/users?id=42";
221 const line = `${before} GET /api/users?id=42 200 ${after} ${url}`;
222 const afterStart = line.indexOf(after);
223 const urlStart = line.indexOf(url);
224
225 expect(extractTerminalLinks(line)).toEqual([
226 { kind: "path", text: before, start: 0, end: before.length },
227 { kind: "path", text: after, start: afterStart, end: afterStart + after.length },
228 { kind: "url", text: url, start: urlStart, end: urlStart + url.length },
229 ]);
230 });
231
232 it.each([
233 "connected to redis://127.0.0.1:6379/0",
234 "connected to postgres://localhost:5432/app",
235 "uploaded s3://bucket/reports/today.csv",
236 " at main (file:///Users/jane/app/src/index.mjs:3:7)",
237 "+redis://localhost:6379/0",
238 "-redis://localhost:6379/0",
239 "git clone git@github.com:acme/widgets.git",
240 "remote: git@gitlab.com:group/sub/project.git",
241 ])("does not turn part of a non-web URL into an editor link: %s", (line) => {
242 expect(extractTerminalLinks(line)).toEqual([]);
243 });
244
245 it("keeps file links beside a non-web URL", () => {
246 const line = "src/db.ts:4 redis://localhost:6379/0 C:/app/src/db.ts:9";
247 expect(extractTerminalLinks(line)).toEqual([
248 { kind: "path", text: "src/db.ts:4", start: 0, end: 11 },
249 { kind: "path", text: "C:/app/src/db.ts:9", start: 37, end: 55 },
250 ]);
251 });
252
253 it("keeps a question mark inside a filesystem path", () => {
254 const path = "/tmp/report?draft";
255 expect(extractTerminalLinks(`wrote ${path}`)).toEqual([
256 { kind: "path", text: path, start: 6, end: 6 + path.length },
257 ]);
258 });
259 });
260
261 describe("collectWrappedTerminalLinkLine", () => {
262 it("reconstructs a wrapped line from any physical row", () => {
263 const firstSegment = "see https://example.com/a";
264 const secondSegment = "/bc?x=1";
265 const lines = [
266 createBufferLine("prompt> "),
267 createBufferLine(firstSegment),
268 createBufferLine(secondSegment, true),
269 createBufferLine("done"),
270 ];
271
272 const fromFirstRow = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
273 const fromWrappedRow = collectWrappedTerminalLinkLine(3, (index) => lines[index]);
274
275 expect(fromFirstRow).toEqual({
276 text: `${firstSegment}${secondSegment}`,
277 segments: [
278 {
279 bufferLineNumber: 2,
280 text: firstSegment,
281 startIndex: 0,
282 endIndex: firstSegment.length,
283 },
284 {
285 bufferLineNumber: 3,
286 text: secondSegment,
287 startIndex: firstSegment.length,
288 endIndex: firstSegment.length + secondSegment.length,
289 },
290 ],
291 });
292 expect(fromWrappedRow).toEqual(fromFirstRow);
293 });
294
295 it("preserves trailing spaces on continued segments for downstream offsets", () => {
296 const firstSegment = "prefix ";
297 const secondSegment = "https://example.com/path";
298 const lines = [createBufferLine(firstSegment), createBufferLine(secondSegment, true)];
299
300 const wrappedLine = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
301
302 expect(wrappedLine?.text).toBe(`${firstSegment}${secondSegment}`);
303 expect(extractTerminalLinks(wrappedLine?.text ?? "")).toEqual([
304 {
305 kind: "url",
306 text: secondSegment,
307 start: firstSegment.length,
308 end: firstSegment.length + secondSegment.length,
309 },
310 ]);
311 });
312 });
313
314 describe("resolvePathLinkTarget", () => {
315 it("resolves a quoted terminal link against a Windows environment", () => {
316 const [link] = extractTerminalLinks(String.raw`error at "src\my folder\index.ts":12:7`);
317 expect(resolvePathLinkTarget(link?.text ?? "", String.raw`C:\Users\Jane Doe\project`)).toBe(
318 String.raw`C:\Users\Jane Doe\project\src\my folder\index.ts:12:7`,
319 );
320 });
321
322 // regression: #13408 terminal links kept a trailing colon
323 it("keeps the line and column of a compiler diagnostic", () => {
324 const [link] = extractTerminalLinks("/Users/julius/project/main.c:10:5: error: expected ';'");
325 expect(resolvePathLinkTarget(link?.text ?? "", "/Users/julius/project")).toBe(
326 "/Users/julius/project/main.c:10:5",
327 );
328 });
329
330 it.each([
331 ["src/main.ts", "/repo/src/main.ts"],
332 ["/repo/src/main.ts", "/repo/src/main.ts"],
333 [String.raw`C:\repo\src\main.ts`, String.raw`C:\repo\src\main.ts`],
334 ["app/(auth)/page.tsx", "/repo/app/(auth)/page.tsx"],
335 ])("preserves a TypeScript compiler location on %s", (path, target) => {
336 const text = `${path}(42,7)`;
337 const links = extractTerminalLinks(`${text}: error TS2322: Type mismatch`);
338 expect(links).toEqual([{ kind: "path", text: `${path}:42:7`, start: 0, end: text.length }]);
339 expect(resolveTerminalLinkTarget(links[0]?.text ?? "", "/repo")).toBe(`${target}:42:7`);
340 });
341
342 it.each([
343 [String.raw`PS C:\dev\folder (42,7)> `, String.raw`C:\dev\folder (42,7)`],
344 ["/tmp/report(42,7)", "/tmp/report(42,7)"],
345 ["src/Program.cs(3,10): error CS1002: x", "/repo/src/Program.cs"],
346 ])("preserves existing path targets in %s", (line, target) => {
347 const [link] = extractTerminalLinks(line);
348 expect(resolveTerminalLinkTarget(link?.text ?? "", "/repo")).toBe(target);
349 });
350
351 // regression: #36005 OSC 8 file URIs were joined onto the cwd as relative paths
352 it("does not resolve an OSC 8 file URI target against the cwd", () => {
353 expect(resolveTerminalLinkTarget("file:///var/tmp/report.txt", "/workspace/project")).toBe(
354 "file:///var/tmp/report.txt",
355 );
356 expect(
357 resolveTerminalLinkTarget("file://remote.invalid/var/tmp/report.txt", "/workspace/project"),
358 ).not.toMatch(/^\/workspace\/project\//);
359 expect(
360 resolveTerminalLinkTarget(
361 "file://myhost/var/tmp/report.txt:12#L5",
362 "/workspace/project",
363 "myhost",
364 ),
365 ).toBe("file:///var/tmp/report.txt:12#L5");
366 expect(
367 resolveTerminalLinkTarget(
368 "file://foreign/var/tmp/report.txt:12#L5",
369 "/workspace/project",
370 "myhost",
371 ),
372 ).toBe("file://foreign/var/tmp/report.txt:12#L5");
373 for (const name of ["report.txt:12-64", "report.txt:0", "report.txt:12"]) {
374 const uri = `file:///var/tmp/${encodeURIComponent(name)}`;
375 expect(resolveTerminalLinkTarget(uri, "/workspace/project")).toBe(uri);
376 }
377 expect(
378 resolveTerminalLinkTarget("file:///var/tmp/report.txt:12#L5", "/workspace/project"),
379 ).toBe("file:///var/tmp/report.txt:12#L5");
380 expect(resolvePathLinkTarget("file:notes.md", "/workspace/project")).toBe(
381 "/workspace/project/file:notes.md",
382 );
383 });
384 });