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

terminal-links.test.ts

585 lines22.8 KB

1import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 collectWrappedTerminalLinkLine,
6 extractTerminalLinks,
7 isTerminalUrl,
8 resolveTerminalLinkTarget,
9 type TerminalBufferLineLike,
10} from "./terminal-links";
11​
12function createBufferLine(text: string, isWrapped = false): TerminalBufferLineLike {
13 return {
14 isWrapped,
15 translateToString: (trimRight = false) => (trimRight ? text.replace(/\s+$/u, "") : text),
16 };
18​
19describe("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 "run.sh: line 3",
92 "my run.sh: line 3: foo: command not found",
93 "main.cs(42,7): error CS1002: expected semicolon",
94 "Connecting to api.example.com:443...",
95 "dial tcp: lookup db.internal:5432: no such host",
96 "redis.local:6379> PING",
97 "postgres@db.internal:5432",
98 ])("does not treat prose or unsupported locations as a path: %s", (line) => {
99 expect(extractTerminalLinks(line)).toEqual([]);
100 });
101​
102 it("keeps quoted paths and URLs separate without duplicate partial paths", () => {
103 const path = String.raw`C:\Jane Doe\index.ts:12`;
104 const url = "https://example.com/docs";
105 const line = `"${path}" ${url} src/index.ts`;
106 expect(extractTerminalLinks(line)).toEqual([
107 { kind: "path", text: path, start: 1, end: 1 + path.length },
108 { kind: "url", text: url, start: path.length + 3, end: path.length + 3 + url.length },
109 { kind: "path", text: "src/index.ts", start: line.length - 12, end: line.length },
110 ]);
111 });
112​
113 it("classifies uppercase schemes as URLs at activation time too", () => {
114 expect(isTerminalUrl("HTTPS://example.com/docs")).toBe(true);
115 expect(isTerminalUrl("Http://example.com")).toBe(true);
116 expect(isTerminalUrl("src/components/main.ts")).toBe(false);
117 expect(isTerminalUrl("httpsdocs/readme.md")).toBe(false);
118 });
119​
120 it("trims trailing punctuation from links", () => {
121 const line = "(https://example.com/docs), ./src/main.ts:12.";
122 expect(extractTerminalLinks(line)).toEqual([
123 {
124 kind: "url",
125 text: "https://example.com/docs",
126 start: 1,
127 end: 25,
128 },
129 {
130 kind: "path",
131 text: "./src/main.ts:12",
132 start: 28,
133 end: 44,
134 },
135 ]);
136 });
137​
138 it.each([
139 ["[(https://example.com/path)]", "https://example.com/path", 2],
140 ["[(https://example.com/wiki/Dune_(2021))]", "https://example.com/wiki/Dune_(2021)", 2],
141 ["{[(/tmp/docs)]}", "/tmp/docs", 3],
142 ])("trims mixed nested wrapper delimiters from %s", (raw, text, start) => {
143 expect(extractTerminalLinks(raw)).toEqual([
144 { kind: isTerminalUrl(text) ? "url" : "path", text, start, end: start + text.length },
145 ]);
146 });
147​
148 it.each([
149 ["https://example.com/a(b)c)", "https://example.com/a(b)c"],
150 ["https://example.com/(docs)", "https://example.com/(docs)"],
151 ["https://example.com/wiki/Dune_(2021)", "https://example.com/wiki/Dune_(2021)"],
152 ["https://example.com/docs)}])", "https://example.com/docs"],
153 ["https://example.com/docs):", "https://example.com/docs):"],
154 ["https://example.com/docs.)", "https://example.com/docs"],
155 ["/tmp/a[b]c]", "/tmp/a[b]c"],
156 ["/tmp/docs)}])", "/tmp/docs"],
157 ["/tmp/docs):", "/tmp/docs"],
158 ["./src/index.ts:12.)", "./src/index.ts:12"],
159 ["/(60)", "/(60)"],
160 ["/tmp/:[60]", "/tmp/:[60]"],
161 ["https://example.com/docs。", "https://example.com/docs"],
162 ["/tmp/a.txt:12,然后", "/tmp/a.txt:12"],
163 ["https://example.com/文档。", "https://example.com/文档。"],
164 ])("trims only unbalanced closing delimiters from %s", (raw, text) => {
165 expect(extractTerminalLinks(`see ${raw}`)).toEqual([
166 { kind: isTerminalUrl(text) ? "url" : "path", text, start: 4, end: 4 + text.length },
167 ]);
168 });
169​
170 it.each([
171 "src/żółć.ts:12:7",
172 "żółć/main.ts:12",
173 String.raw`żółć\main.ts:12`,
174 "src/cafe\u0301.ts:12",
175 "src/𐐀.ts:12",
176 "src/app/[id]/page.tsx:3:10",
177 "src/routes/(app)/+page.svelte:12:4",
178 "src/routes/(app)/+page@.svelte:12:3",
179 "src/contact@support.vue:12:3",
180 String.raw`src\contact@support.ts:12:3`,
181 "src/équipe@home.ts:12:3",
182 "src/foo$contact@support.ts:12:3",
183 "node_modules/.pnpm/react@18.3.1/node_modules/react/index.js:12:5",
184 "app/routes/posts.$id.tsx:5",
185 "main.c:1:25",
186 "page.test.tsx:5:3",
187 "foo$bar.ts:12:3",
188 "+page.svelte:12:4",
189 "contact@support.ts:12:3",
190 ])("keeps the complete relative path %s", (text) => {
191 expect(extractTerminalLinks(`see ${text} for details`)).toEqual([
192 { kind: "path", text, start: 4, end: 4 + text.length },
193 ]);
194 });
195​
196 it.each([
197 ["./src/a.js:1:function inner() {", "./src/a.js:1:function", "./src/a.js:1"],
198 ["./src/a.js:4:1:function outer() {", "./src/a.js:4:1:function", "./src/a.js:4:1"],
199 ["/app/top.rb:2:in `f': boom (RuntimeError)", "/app/top.rb:2:in", "/app/top.rb:2"],
200 ["./data.csv:5:2024-01-01 ok", "./data.csv:5:2024-01-01", "./data.csv:5"],
201 ["./src/a.js:7:process.exit(1)", "./src/a.js:7:process.exit(1)", "./src/a.js:7"],
202 [
203 "/var/lib/bluetooth/AA:BB:CC:11:22:DD",
204 "/var/lib/bluetooth/AA:BB:CC:11:22:DD",
205 "/var/lib/bluetooth/AA:BB:CC:11:22",
206 ],
207 ["./notes.txt:3:App.java:[27,49]", "./notes.txt:3:App.java:[27,49]", "./notes.txt:3"],
208 ["./notes.txt:3:/app/ok.py(3)f()", "./notes.txt:3:/app/ok.py(3)f()", "./notes.txt:3"],
209 ])("keeps %s whole and offers its location as the fallback", (line, text, fallback) => {
210 expect(extractTerminalLinks(line)).toEqual([
211 {
212 kind: "path",
213 text,
214 start: 0,
215 end: text.length,
216 fallback: { text: fallback, end: fallback.length },
217 },
218 ]);
219 });
220​
221 it.each([
222 [
223 "/app/src/Program.cs(12,5): error CS1002: ; expected",
224 "/app/src/Program.cs(12,5)",
225 "/app/src/Program.cs:12:5",
226 ],
227 [
228 "src/Program.cs(12,5): error CS1002: ; expected",
229 "src/Program.cs(12,5)",
230 "src/Program.cs:12:5",
231 ],
232 [
233 String.raw`C:\app\UnitTest1.cs(12): error VSTEST1: Assert.True() Failure`,
234 String.raw`C:\app\UnitTest1.cs(12)`,
235 String.raw`C:\app\UnitTest1.cs:12`,
236 ],
237 [
238 "#0 /var/www/html/index.php(110): handle()",
239 "/var/www/html/index.php(110)",
240 "/var/www/html/index.php:110",
241 ],
242 [
243 "[ERROR] /app/src/main/java/App.java:[27,49] cannot find symbol",
244 "/app/src/main/java/App.java:[27,49]",
245 "/app/src/main/java/App.java:27:49",
246 ],
247 [
248 "[ERROR] /app/src/main/java/App.java:[99] Type mismatch: cannot convert from Integer to String",
249 "/app/src/main/java/App.java:[99]",
250 "/app/src/main/java/App.java:99",
251 ],
252 [
253 " at Demo.Program.Main(String[] args) in /home/dev/app/Program.cs:line 29",
254 "/home/dev/app/Program.cs:line",
255 "/home/dev/app/Program.cs:29",
256 ],
257 [
258 String.raw` at Demo.Program.Main(String[] args) in C:\app\Program.cs:line 29`,
259 String.raw`C:\app\Program.cs:line`,
260 String.raw`C:\app\Program.cs:29`,
261 ],
262 ])("offers the closing location in %s as the fallback", (line, text, fallback) => {
263 const start = line.indexOf(text);
264 const end = start + text.length;
265 expect(extractTerminalLinks(line)).toEqual([
266 { kind: "path", text, start, end, fallback: { text: fallback, end } },
267 ]);
268 });
269​
270 it.each([
271 ["> /home/dev/app/ok.py(3)f()", "/home/dev/app/ok.py(3)f()", "/home/dev/app/ok.py:3"],
272 ["> /home/dev/app/ok.py(3)f()->42", "/home/dev/app/ok.py(3)f()-", "/home/dev/app/ok.py:3"],
273 [String.raw`> c:\app\ok.py(3)f()`, String.raw`c:\app\ok.py(3)f()`, String.raw`c:\app\ok.py:3`],
274 ])(
275 "offers the location before the function name in %s as the fallback",
276 (line, text, fallback) => {
277 const start = line.indexOf(text);
278 expect(extractTerminalLinks(line)).toEqual([
279 {
280 kind: "path",
281 text,
282 start,
283 end: start + text.length,
284 fallback: { text: fallback, end: start + text.indexOf(")") + 1 },
285 },
286 ]);
287 },
288 );
289​
290 it.each([
291 [' File "/home/dev/app/x.py", line 12, in f', "/home/dev/app/x.py"],
292 [String.raw` File "C:\app\x.py", line 12, in <module>`, String.raw`C:\app\x.py`],
293 ["Build file '/home/dev/app/build.gradle' line: 12", "/home/dev/app/build.gradle"],
294 ])("opens the quoted path at the line that follows it in %s", (line, path) => {
295 const start = line.indexOf(path);
296 expect(extractTerminalLinks(line)).toEqual([
297 { kind: "path", text: `${path}:12`, start, end: start + path.length },
298 ]);
299 });
300​
301 it.each([
302 ["/home/dev/app/run.sh: line 3: foo: command not found", "/home/dev/app/run.sh"],
303 ["run.sh: line 3: foo: command not found", "run.sh"],
304 ["PHP Warning: x in /home/dev/app/index.php on line 12", "/home/dev/app/index.php"],
305 ['syntax error at /home/dev/app/job.pl line 7, near "}"', "/home/dev/app/job.pl"],
306 ])("opens the path at the line the words after it name in %s", (line, path) => {
307 const start = line.indexOf(path);
308 const row = /line (\d+)/.exec(line)?.[1];
309 expect(extractTerminalLinks(line)).toEqual([
310 { kind: "path", text: `${path}:${row}`, start, end: start + path.length },
311 ]);
312 });
313​
314 it("keeps the position a quoted traceback path already carries", () => {
315 expect(extractTerminalLinks(' File "/home/dev/app/x.py:5", line 12')).toEqual([
316 { kind: "path", text: "/home/dev/app/x.py:5", start: 8, end: 28 },
317 ]);
318 });
319​
320 it.each([
321 ["see /home/dev/app/notes:line for details", "/home/dev/app/notes:line"],
322 ["see /home/dev/app/notes:line 0x1d", "/home/dev/app/notes:line"],
323 ["see /home/dev/app/ok.py(3)f(x) for details", "/home/dev/app/ok.py(3)f(x)"],
324 ])("offers no line number as the fallback in %s", (line, text) => {
325 const start = line.indexOf(text);
326 expect(extractTerminalLinks(line)).toEqual([
327 { kind: "path", text, start, end: start + text.length },
328 ]);
329 });
330​
331 it("finds Windows absolute paths with forward slashes", () => {
332 const line = "see C:/Users/someone/project/src/file.ts:42 for details";
333 const path = "C:/Users/someone/project/src/file.ts:42";
334 const start = line.indexOf(path);
335 expect(extractTerminalLinks(line)).toEqual([
336 {
337 kind: "path",
338 text: path,
339 start,
340 end: start + path.length,
341 },
342 ]);
343 });
344​
345 it.each([
346 ["**https://example.com/docs**", "https://example.com/docs", 2],
347 ["see **https://example.com/docs**.", "https://example.com/docs", 6],
348 ["https://example.com/search?q=*", "https://example.com/search?q=*", 0],
349 ["**https://example.com/files/***", "https://example.com/files/*", 2],
350 ["(**https://example.com/docs**)", "https://example.com/docs", 3],
351 ["**https://example.com/search?q=**)**", "https://example.com/search?q=**", 2],
352 ["**(https://example.com/docs)**", "https://example.com/docs", 3],
353 ["**[https://example.com/docs]**.", "https://example.com/docs", 3],
354 ["**see (https://example.com/docs)**", "https://example.com/docs", 7],
355 ["**see https://example.com**", "https://example.com", 6],
356 ["**see https://example.com.**", "https://example.com", 6],
357 ["(**see https://example.com**)", "https://example.com", 7],
358 ["*see `https://example.com/?q=*`*", "https://example.com/?q=*", 6],
359 ["a ` stray **see (https://example.com)**", "https://example.com", 17],
360 ["https://example.com/search?q=)*", "https://example.com/search?q=)*", 0],
361 ["**see** (https://example.com/q=)*", "https://example.com/q=)*", 9],
362 ["**see**: https://example.com/search?q=)**", "https://example.com/search?q=)**", 9],
363 ["`**`: https://example.com/search?q=)**", "https://example.com/search?q=)**", 6],
364 ["2*3 https://example.com/search?q=)*", "https://example.com/search?q=)*", 4],
365 ["a*b (https://example.com/q=)*", "https://example.com/q=)*", 5],
366 ])(
367 "ends a URL before the Markdown emphasis marks around it, and only those, in %s",
368 (line, text, start) => {
369 expect(extractTerminalLinks(line)).toEqual([
370 { kind: "url", text, start, end: start + text.length },
371 ]);
372 },
373 );
374​
375 it.each([
376 ["see **/etc/hosts** now", "/etc/hosts", 6],
377 ["(**./src/main.ts:12:3**)", "./src/main.ts:12:3", 3],
378 ["**~/notes/todo.md**.", "~/notes/todo.md", 2],
379 ["**see ./src/app.ts**", "./src/app.ts", 6],
380 ["**(see ./src/app.ts)**", "./src/app.ts", 7],
381 ["(**see ./src/app.ts**)", "./src/app.ts", 7],
382 ])("ends a path before the Markdown emphasis marks around it in %s", (line, text, start) => {
383 expect(extractTerminalLinks(line)).toEqual([
384 { kind: "path", text, start, end: start + text.length },
385 ]);
386 });
387​
388 it("keeps a trailing colon on URLs", () => {
389 expect(extractTerminalLinks("GET https://example.test/items/foo:")).toEqual([
390 { kind: "url", text: "https://example.test/items/foo:", start: 4, end: 35 },
391 ]);
392 });
393​
394 it.each([
395 ["./main.go:10:5: undefined: x", "./main.go:10:5"],
396 ["/home/dev/app/src/main.c:10:5: error: expected ';'", "/home/dev/app/src/main.c:10:5"],
397 ["C:\\dev\\app\\src\\main.c:10:5: error: expected ';'", "C:\\dev\\app\\src\\main.c:10:5"],
398 ["wrote ./out/report.txt:", "./out/report.txt"],
399 ])("drops the colon that ends a compiler diagnostic location in %s", (line, text) => {
400 const start = line.indexOf(text);
401 expect(extractTerminalLinks(line)).toEqual([
402 { kind: "path", text, start, end: start + text.length },
403 ]);
404 });
405​
406 it.each([
407 " GET /api/trpc/post.list,user.me?batch=1&input=%7B%220%22%3A%7B%22json%22%3Anull%7D%7D 200 in 35ms",
408 "GET /api?+debug 200",
409 'Started GET "/posts?page=2" for ::1',
410 "proxying api/trpc/post.list?batch=1 to the backend",
411 ])("does not turn a logged request route into an editor link: %s", (line) => {
412 expect(extractTerminalLinks(line)).toEqual([]);
413 });
414​
415 it("keeps file and URL links before and after a logged request route", () => {
416 const before = "src/server.ts:12";
417 const after = "/app/bin/server:3:7";
418 const url = "https://example.com/api/users?id=42";
419 const line = `${before} GET /api/users?id=42 200 ${after} ${url}`;
420 const afterStart = line.indexOf(after);
421 const urlStart = line.indexOf(url);
422​
423 expect(extractTerminalLinks(line)).toEqual([
424 { kind: "path", text: before, start: 0, end: before.length },
425 { kind: "path", text: after, start: afterStart, end: afterStart + after.length },
426 { kind: "url", text: url, start: urlStart, end: urlStart + url.length },
427 ]);
428 });
429​
430 it.each([
431 "connected to redis://127.0.0.1:6379/0",
432 "connected to postgres://localhost:5432/app",
433 "uploaded s3://bucket/reports/today.csv",
434 " at main (file:///Users/jane/app/src/index.mjs:3:7)",
435 "+redis://localhost:6379/0",
436 "-redis://localhost:6379/0",
437 "git clone git@github.com:acme/widgets.git",
438 "remote: git@gitlab.com:group/sub/project.git",
439 ])("does not turn part of a non-web URL into an editor link: %s", (line) => {
440 expect(extractTerminalLinks(line)).toEqual([]);
441 });
442​
443 it("keeps file links beside a non-web URL", () => {
444 const line = "src/db.ts:4 redis://localhost:6379/0 C:/app/src/db.ts:9";
445 expect(extractTerminalLinks(line)).toEqual([
446 { kind: "path", text: "src/db.ts:4", start: 0, end: 11 },
447 { kind: "path", text: "C:/app/src/db.ts:9", start: 37, end: 55 },
448 ]);
449 });
450​
451 it("keeps a question mark inside a filesystem path", () => {
452 const path = "/tmp/report?draft";
453 expect(extractTerminalLinks(`wrote ${path}`)).toEqual([
454 { kind: "path", text: path, start: 6, end: 6 + path.length },
455 ]);
456 });
457});
458​
459describe("collectWrappedTerminalLinkLine", () => {
460 it("reconstructs a wrapped line from any physical row", () => {
461 const firstSegment = "see https://example.com/a";
462 const secondSegment = "/bc?x=1";
463 const lines = [
464 createBufferLine("prompt> "),
465 createBufferLine(firstSegment),
466 createBufferLine(secondSegment, true),
467 createBufferLine("done"),
468 ];
469​
470 const fromFirstRow = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
471 const fromWrappedRow = collectWrappedTerminalLinkLine(3, (index) => lines[index]);
472​
473 expect(fromFirstRow).toEqual({
474 text: `${firstSegment}${secondSegment}`,
475 segments: [
476 {
477 bufferLineNumber: 2,
478 text: firstSegment,
479 startIndex: 0,
480 endIndex: firstSegment.length,
481 },
482 {
483 bufferLineNumber: 3,
484 text: secondSegment,
485 startIndex: firstSegment.length,
486 endIndex: firstSegment.length + secondSegment.length,
487 },
488 ],
489 });
490 expect(fromWrappedRow).toEqual(fromFirstRow);
491 });
492​
493 it("preserves trailing spaces on continued segments for downstream offsets", () => {
494 const firstSegment = "prefix ";
495 const secondSegment = "https://example.com/path";
496 const lines = [createBufferLine(firstSegment), createBufferLine(secondSegment, true)];
497​
498 const wrappedLine = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
499​
500 expect(wrappedLine?.text).toBe(`${firstSegment}${secondSegment}`);
501 expect(extractTerminalLinks(wrappedLine?.text ?? "")).toEqual([
502 {
503 kind: "url",
504 text: secondSegment,
505 start: firstSegment.length,
506 end: firstSegment.length + secondSegment.length,
507 },
508 ]);
509 });
510});
511​
512describe("resolvePathLinkTarget", () => {
513 it("resolves a quoted terminal link against a Windows environment", () => {
514 const [link] = extractTerminalLinks(String.raw`error at "src\my folder\index.ts":12:7`);
515 expect(resolvePathLinkTarget(link?.text ?? "", String.raw`C:\Users\Jane Doe\project`)).toBe(
516 String.raw`C:\Users\Jane Doe\project\src\my folder\index.ts:12:7`,
517 );
518 });
519​
520 // regression: #13408 terminal links kept a trailing colon
521 it("keeps the line and column of a compiler diagnostic", () => {
522 const [link] = extractTerminalLinks("/Users/julius/project/main.c:10:5: error: expected ';'");
523 expect(resolvePathLinkTarget(link?.text ?? "", "/Users/julius/project")).toBe(
524 "/Users/julius/project/main.c:10:5",
525 );
526 });
527​
528 it.each([
529 ["src/main.ts", "/repo/src/main.ts"],
530 ["/repo/src/main.ts", "/repo/src/main.ts"],
531 [String.raw`C:\repo\src\main.ts`, String.raw`C:\repo\src\main.ts`],
532 ["app/(auth)/page.tsx", "/repo/app/(auth)/page.tsx"],
533 ])("preserves a TypeScript compiler location on %s", (path, target) => {
534 const text = `${path}(42,7)`;
535 const links = extractTerminalLinks(`${text}: error TS2322: Type mismatch`);
536 expect(links).toEqual([{ kind: "path", text: `${path}:42:7`, start: 0, end: text.length }]);
537 expect(resolveTerminalLinkTarget(links[0]?.text ?? "", "/repo")).toBe(`${target}:42:7`);
538 });
539​
540 it.each([
541 [String.raw`PS C:\dev\folder (42,7)> `, String.raw`C:\dev\folder (42,7)`],
542 ["/tmp/report(42,7)", "/tmp/report(42,7)"],
543 ["/tmp/report:[42,7]", "/tmp/report:[42,7]"],
544 ["/tmp/notes:line 29", "/tmp/notes:line"],
545 ["/tmp/ok.py(3)f()", "/tmp/ok.py(3)f()"],
546 ["src/Program.cs(3,10): error CS1002: x", "/repo/src/Program.cs(3,10)"],
547 ])("preserves existing path targets in %s", (line, target) => {
548 const [link] = extractTerminalLinks(line);
549 expect(resolveTerminalLinkTarget(link?.text ?? "", "/repo")).toBe(target);
550 });
551​
552 // regression: #36005 OSC 8 file URIs were joined onto the cwd as relative paths
553 it("does not resolve an OSC 8 file URI target against the cwd", () => {
554 expect(resolveTerminalLinkTarget("file:///var/tmp/report.txt", "/workspace/project")).toBe(
555 "file:///var/tmp/report.txt",
556 );
557 expect(
558 resolveTerminalLinkTarget("file://remote.invalid/var/tmp/report.txt", "/workspace/project"),
559 ).not.toMatch(/^\/workspace\/project\//);
560 expect(
561 resolveTerminalLinkTarget(
562 "file://myhost/var/tmp/report.txt:12#L5",
563 "/workspace/project",
564 "myhost",
565 ),
566 ).toBe("file:///var/tmp/report.txt:12#L5");
567 expect(
568 resolveTerminalLinkTarget(
569 "file://foreign/var/tmp/report.txt:12#L5",
570 "/workspace/project",
571 "myhost",
572 ),
573 ).toBe("file://foreign/var/tmp/report.txt:12#L5");
574 for (const name of ["report.txt:12-64", "report.txt:0", "report.txt:12"]) {
575 const uri = `file:///var/tmp/${encodeURIComponent(name)}`;
576 expect(resolveTerminalLinkTarget(uri, "/workspace/project")).toBe(uri);
577 }
578 expect(
579 resolveTerminalLinkTarget("file:///var/tmp/report.txt:12#L5", "/workspace/project"),
580 ).toBe("file:///var/tmp/report.txt:12#L5");
581 expect(resolvePathLinkTarget("file:notes.md", "/workspace/project")).toBe(
582 "/workspace/project/file:notes.md",
583 );
584 });
585});