Open History to see the last commit
1import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 collectWrappedTerminalLinkLine,
6 extractTerminalLinks,
7 isTerminalLinkActivation,
8 isTerminalUrl,
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 };
17}
18
19describe("extractTerminalLinks", () => {
20 it.each([
21 String.raw`src\index.ts:12`,
22 String.raw`.\src\index.ts:12:7`,
23 String.raw`..\src\index.ts:12`,
24 String.raw`C:\Users\Jane\project\index.ts:12`,
25 String.raw`\\server\share\index.ts:12`,
26 ])("finds Windows path %s", (text) => {
27 const line = `error at ${text}: failed`;
28 expect(extractTerminalLinks(line)).toEqual([
29 { kind: "path", text, start: 9, end: 9 + text.length },
30 ]);
31 });
32
33 it.each(['"', "'", "`"])("finds paths containing spaces enclosed in %s", (quote) => {
34 const text = String.raw`C:\Users\Jane Doe\project\index.ts:12:7`;
35 expect(extractTerminalLinks(`see ${quote}${text}${quote}.`)).toEqual([
36 { kind: "path", text, start: 5, end: 5 + text.length },
37 ]);
38 });
39
40 it.each([
41 String.raw`src\my folder\index.ts`,
42 "./my folder/index.ts",
43 "/home/Jane Doe/index.ts",
44 "/home/Jane Doe/file)",
45 String.raw`\\server\my share\index.ts`,
46 ])("keeps positions outside quotes on %s", (path) => {
47 expect(extractTerminalLinks(`"${path}":12:7: error`)).toEqual([
48 { kind: "path", text: `${path}:12:7`, start: 1, end: path.length + 7 },
49 ]);
50 });
51
52 it("does not treat quoted prose as a path", () => {
53 expect(extractTerminalLinks('error: "something went wrong"')).toEqual([]);
54 expect(extractTerminalLinks('error: "something went wrong."')).toEqual([]);
55 });
56
57 it.each([
58 ['"my file.ts":12:7', "my file.ts:12:7", 17],
59 ["'my file.ts:12:7'", "my file.ts:12:7", 16],
60 ["`my file.ts`", "my file.ts", 11],
61 ['"mój plik.ts":12', "mój plik.ts:12", 16],
62 ])("finds a quoted basename in %s", (line, text, end) => {
63 expect(extractTerminalLinks(line)).toEqual([{ kind: "path", text, start: 1, end }]);
64 });
65
66 it("keeps quoted paths and URLs separate without duplicate partial paths", () => {
67 const path = String.raw`C:\Jane Doe\index.ts:12`;
68 const url = "https://example.com/docs";
69 const line = `"${path}" ${url} src/index.ts`;
70 expect(extractTerminalLinks(line)).toEqual([
71 { kind: "path", text: path, start: 1, end: 1 + path.length },
72 { kind: "url", text: url, start: path.length + 3, end: path.length + 3 + url.length },
73 { kind: "path", text: "src/index.ts", start: line.length - 12, end: line.length },
74 ]);
75 });
76
77 it("finds http urls and path tokens", () => {
78 const line =
79 "failed at https://example.com/docs and src/components/ThreadTerminalDrawer.tsx:42";
80 expect(extractTerminalLinks(line)).toEqual([
81 {
82 kind: "url",
83 text: "https://example.com/docs",
84 start: 10,
85 end: 34,
86 },
87 {
88 kind: "path",
89 text: "src/components/ThreadTerminalDrawer.tsx:42",
90 start: 39,
91 end: 81,
92 },
93 ]);
94 });
95
96 it("classifies uppercase schemes as URLs at activation time too", () => {
97 expect(isTerminalUrl("HTTPS://example.com/docs")).toBe(true);
98 expect(isTerminalUrl("Http://example.com")).toBe(true);
99 expect(isTerminalUrl("src/components/main.ts")).toBe(false);
100 expect(isTerminalUrl("httpsdocs/readme.md")).toBe(false);
101 });
102
103 it("finds URLs regardless of scheme casing", () => {
104 expect(extractTerminalLinks("open HTTPS://example.com/docs")).toEqual([
105 {
106 kind: "url",
107 text: "HTTPS://example.com/docs",
108 start: 5,
109 end: 29,
110 },
111 ]);
112 });
113
114 it("trims trailing punctuation from links", () => {
115 const line = "(https://example.com/docs), ./src/main.ts:12.";
116 expect(extractTerminalLinks(line)).toEqual([
117 {
118 kind: "url",
119 text: "https://example.com/docs",
120 start: 1,
121 end: 25,
122 },
123 {
124 kind: "path",
125 text: "./src/main.ts:12",
126 start: 28,
127 end: 44,
128 },
129 ]);
130 });
131
132 it.each([
133 "src/żółć.ts:12:7",
134 "żółć/main.ts:12",
135 String.raw`żółć\main.ts:12`,
136 "src/cafe\u0301.ts:12",
137 "src/𐐀.ts:12",
138 ])("keeps the complete Unicode relative path %s", (text) => {
139 expect(extractTerminalLinks(`see ${text} for details`)).toEqual([
140 { kind: "path", text, start: 4, end: 4 + text.length },
141 ]);
142 });
143
144 it("finds Windows absolute paths with forward slashes", () => {
145 const line = "see C:/Users/someone/project/src/file.ts:42 for details";
146 const path = "C:/Users/someone/project/src/file.ts:42";
147 const start = line.indexOf(path);
148 expect(extractTerminalLinks(line)).toEqual([
149 {
150 kind: "path",
151 text: path,
152 start,
153 end: start + path.length,
154 },
155 ]);
156 });
157
158 it("trims trailing punctuation from Windows forward-slash paths", () => {
159 const line = "(C:/tmp/x.ts).";
160 expect(extractTerminalLinks(line)).toEqual([
161 {
162 kind: "path",
163 text: "C:/tmp/x.ts",
164 start: 1,
165 end: 12,
166 },
167 ]);
168 });
169
170 it("keeps a trailing colon on URLs", () => {
171 expect(extractTerminalLinks("GET https://example.test/items/foo:")).toEqual([
172 { kind: "url", text: "https://example.test/items/foo:", start: 4, end: 35 },
173 ]);
174 });
175
176 it.each([
177 ["./main.go:10:5: undefined: x", "./main.go:10:5"],
178 ["/home/dev/app/src/main.c:10:5: error: expected ';'", "/home/dev/app/src/main.c:10:5"],
179 ["C:\\dev\\app\\src\\main.c:10:5: error: expected ';'", "C:\\dev\\app\\src\\main.c:10:5"],
180 ["wrote ./out/report.txt:", "./out/report.txt"],
181 ])("drops the colon that ends a compiler diagnostic location in %s", (line, text) => {
182 const start = line.indexOf(text);
183 expect(extractTerminalLinks(line)).toEqual([
184 { kind: "path", text, start, end: start + text.length },
185 ]);
186 });
187
188 it.each([
189 " GET /api/trpc/post.list,user.me?batch=1&input=%7B%220%22%3A%7B%22json%22%3Anull%7D%7D 200 in 35ms",
190 "GET /api?+debug 200",
191 'Started GET "/posts?page=2" for ::1',
192 "proxying api/trpc/post.list?batch=1 to the backend",
193 ])("does not turn a logged request route into an editor link: %s", (line) => {
194 expect(extractTerminalLinks(line)).toEqual([]);
195 });
196
197 it("keeps file and URL links before and after a logged request route", () => {
198 const before = "src/server.ts:12";
199 const after = "/app/bin/server:3:7";
200 const url = "https://example.com/api/users?id=42";
201 const line = `${before} GET /api/users?id=42 200 ${after} ${url}`;
202 const afterStart = line.indexOf(after);
203 const urlStart = line.indexOf(url);
204
205 expect(extractTerminalLinks(line)).toEqual([
206 { kind: "path", text: before, start: 0, end: before.length },
207 { kind: "path", text: after, start: afterStart, end: afterStart + after.length },
208 { kind: "url", text: url, start: urlStart, end: urlStart + url.length },
209 ]);
210 });
211
212 it("keeps a question mark inside a filesystem path", () => {
213 const path = "/tmp/report?draft";
214 expect(extractTerminalLinks(`wrote ${path}`)).toEqual([
215 { kind: "path", text: path, start: 6, end: 6 + path.length },
216 ]);
217 });
218});
219
220describe("collectWrappedTerminalLinkLine", () => {
221 it("reconstructs a wrapped line from any physical row", () => {
222 const firstSegment = "see https://example.com/a";
223 const secondSegment = "/bc?x=1";
224 const lines = [
225 createBufferLine("prompt> "),
226 createBufferLine(firstSegment),
227 createBufferLine(secondSegment, true),
228 createBufferLine("done"),
229 ];
230
231 const fromFirstRow = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
232 const fromWrappedRow = collectWrappedTerminalLinkLine(3, (index) => lines[index]);
233
234 expect(fromFirstRow).toEqual({
235 text: `${firstSegment}${secondSegment}`,
236 segments: [
237 {
238 bufferLineNumber: 2,
239 text: firstSegment,
240 startIndex: 0,
241 endIndex: firstSegment.length,
242 },
243 {
244 bufferLineNumber: 3,
245 text: secondSegment,
246 startIndex: firstSegment.length,
247 endIndex: firstSegment.length + secondSegment.length,
248 },
249 ],
250 });
251 expect(fromWrappedRow).toEqual(fromFirstRow);
252 });
253
254 it("preserves trailing spaces on continued segments for downstream offsets", () => {
255 const firstSegment = "prefix ";
256 const secondSegment = "https://example.com/path";
257 const lines = [createBufferLine(firstSegment), createBufferLine(secondSegment, true)];
258
259 const wrappedLine = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
260
261 expect(wrappedLine?.text).toBe(`${firstSegment}${secondSegment}`);
262 expect(extractTerminalLinks(wrappedLine?.text ?? "")).toEqual([
263 {
264 kind: "url",
265 text: secondSegment,
266 start: firstSegment.length,
267 end: firstSegment.length + secondSegment.length,
268 },
269 ]);
270 });
271});
272
273describe("resolvePathLinkTarget", () => {
274 it("resolves a quoted terminal link against a Windows environment", () => {
275 const [link] = extractTerminalLinks(String.raw`error at "src\my folder\index.ts":12:7`);
276 expect(resolvePathLinkTarget(link?.text ?? "", String.raw`C:\Users\Jane Doe\project`)).toBe(
277 String.raw`C:\Users\Jane Doe\project\src\my folder\index.ts:12:7`,
278 );
279 });
280
281 it("resolves relative paths against cwd", () => {
282 expect(
283 resolvePathLinkTarget(
284 "src/components/ThreadTerminalDrawer.tsx:42:7",
285 "/Users/julius/project",
286 ),
287 ).toBe("/Users/julius/project/src/components/ThreadTerminalDrawer.tsx:42:7");
288 });
289
290 it("keeps absolute paths unchanged", () => {
291 expect(
292 resolvePathLinkTarget("/Users/julius/project/src/main.ts:12", "/Users/julius/project"),
293 ).toBe("/Users/julius/project/src/main.ts:12");
294 });
295
296 it("keeps Windows absolute paths with forward slashes unchanged", () => {
297 expect(
298 resolvePathLinkTarget("C:/Users/julius/project/src/main.ts:12", "C:\\Users\\julius\\project"),
299 ).toBe("C:/Users/julius/project/src/main.ts:12");
300 });
301
302 it("keeps the line and column of a compiler diagnostic", () => {
303 const [link] = extractTerminalLinks("/Users/julius/project/main.c:10:5: error: expected ';'");
304 expect(resolvePathLinkTarget(link?.text ?? "", "/Users/julius/project")).toBe(
305 "/Users/julius/project/main.c:10:5",
306 );
307 });
308});
309
310describe("isTerminalLinkActivation", () => {
311 it("requires cmd on macOS", () => {
312 expect(
313 isTerminalLinkActivation(
314 {
315 metaKey: true,
316 ctrlKey: false,
317 },
318 "MacIntel",
319 ),
320 ).toBe(true);
321 expect(
322 isTerminalLinkActivation(
323 {
324 metaKey: false,
325 ctrlKey: true,
326 },
327 "MacIntel",
328 ),
329 ).toBe(false);
330 });
331
332 it("requires ctrl on non-macOS", () => {
333 expect(
334 isTerminalLinkActivation(
335 {
336 metaKey: false,
337 ctrlKey: true,
338 },
339 "Win32",
340 ),
341 ).toBe(true);
342 expect(
343 isTerminalLinkActivation(
344 {
345 metaKey: true,
346 ctrlKey: false,
347 },
348 "Linux",
349 ),
350 ).toBe(false);
351 });
352});