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 isTerminalUrl,
8 type TerminalBufferLineLike,
9} from "./terminal-links";
10
11function createBufferLine(text: string, isWrapped = false): TerminalBufferLineLike {
12 return {
13 isWrapped,
14 translateToString: (trimRight = false) => (trimRight ? text.replace(/\s+$/u, "") : text),
15 };
16}
17
18describe("extractTerminalLinks", () => {
19 it.each([
20 String.raw`src\index.ts:12`,
21 String.raw`.\src\index.ts:12:7`,
22 String.raw`..\src\index.ts:12`,
23 String.raw`C:\Users\Jane\project\index.ts:12`,
24 String.raw`\\server\share\index.ts:12`,
25 ])("finds Windows path %s", (text) => {
26 const line = `error at ${text}: failed`;
27 expect(extractTerminalLinks(line)).toEqual([
28 { kind: "path", text, start: 9, end: 9 + text.length },
29 ]);
30 });
31
32 it.each(['"', "'", "`"])("finds paths containing spaces enclosed in %s", (quote) => {
33 const text = String.raw`C:\Users\Jane Doe\project\index.ts:12:7`;
34 expect(extractTerminalLinks(`see ${quote}${text}${quote}.`)).toEqual([
35 { kind: "path", text, start: 5, end: 5 + text.length },
36 ]);
37 });
38
39 it.each([
40 String.raw`src\my folder\index.ts`,
41 "./my folder/index.ts",
42 "/home/Jane Doe/index.ts",
43 "/home/Jane Doe/file)",
44 String.raw`\\server\my share\index.ts`,
45 ])("keeps positions outside quotes on %s", (path) => {
46 expect(extractTerminalLinks(`"${path}":12:7: error`)).toEqual([
47 { kind: "path", text: `${path}:12:7`, start: 1, end: path.length + 7 },
48 ]);
49 });
50
51 it("does not treat quoted prose as a path", () => {
52 expect(extractTerminalLinks('error: "something went wrong"')).toEqual([]);
53 expect(extractTerminalLinks('error: "something went wrong."')).toEqual([]);
54 });
55
56 it("keeps quoted paths and URLs separate without duplicate partial paths", () => {
57 const path = String.raw`C:\Jane Doe\index.ts:12`;
58 const url = "https://example.com/docs";
59 const line = `"${path}" ${url} src/index.ts`;
60 expect(extractTerminalLinks(line)).toEqual([
61 { kind: "path", text: path, start: 1, end: 1 + path.length },
62 { kind: "url", text: url, start: path.length + 3, end: path.length + 3 + url.length },
63 { kind: "path", text: "src/index.ts", start: line.length - 12, end: line.length },
64 ]);
65 });
66
67 it("classifies uppercase schemes as URLs at activation time too", () => {
68 expect(isTerminalUrl("HTTPS://example.com/docs")).toBe(true);
69 expect(isTerminalUrl("Http://example.com")).toBe(true);
70 expect(isTerminalUrl("src/components/main.ts")).toBe(false);
71 expect(isTerminalUrl("httpsdocs/readme.md")).toBe(false);
72 });
73
74 it("trims trailing punctuation from links", () => {
75 const line = "(https://example.com/docs), ./src/main.ts:12.";
76 expect(extractTerminalLinks(line)).toEqual([
77 {
78 kind: "url",
79 text: "https://example.com/docs",
80 start: 1,
81 end: 25,
82 },
83 {
84 kind: "path",
85 text: "./src/main.ts:12",
86 start: 28,
87 end: 44,
88 },
89 ]);
90 });
91
92 it.each([
93 "src/żółć.ts:12:7",
94 "żółć/main.ts:12",
95 String.raw`żółć\main.ts:12`,
96 "src/cafe\u0301.ts:12",
97 "src/𐐀.ts:12",
98 ])("keeps the complete Unicode relative path %s", (text) => {
99 expect(extractTerminalLinks(`see ${text} for details`)).toEqual([
100 { kind: "path", text, start: 4, end: 4 + text.length },
101 ]);
102 });
103
104 it("finds Windows absolute paths with forward slashes", () => {
105 const line = "see C:/Users/someone/project/src/file.ts:42 for details";
106 const path = "C:/Users/someone/project/src/file.ts:42";
107 const start = line.indexOf(path);
108 expect(extractTerminalLinks(line)).toEqual([
109 {
110 kind: "path",
111 text: path,
112 start,
113 end: start + path.length,
114 },
115 ]);
116 });
117
118 it("keeps a trailing colon on URLs", () => {
119 expect(extractTerminalLinks("GET https://example.test/items/foo:")).toEqual([
120 { kind: "url", text: "https://example.test/items/foo:", start: 4, end: 35 },
121 ]);
122 });
123
124 it.each([
125 ["./main.go:10:5: undefined: x", "./main.go:10:5"],
126 ["/home/dev/app/src/main.c:10:5: error: expected ';'", "/home/dev/app/src/main.c:10:5"],
127 ["C:\\dev\\app\\src\\main.c:10:5: error: expected ';'", "C:\\dev\\app\\src\\main.c:10:5"],
128 ["wrote ./out/report.txt:", "./out/report.txt"],
129 ])("drops the colon that ends a compiler diagnostic location in %s", (line, text) => {
130 const start = line.indexOf(text);
131 expect(extractTerminalLinks(line)).toEqual([
132 { kind: "path", text, start, end: start + text.length },
133 ]);
134 });
135
136 it.each([
137 " GET /api/trpc/post.list,user.me?batch=1&input=%7B%220%22%3A%7B%22json%22%3Anull%7D%7D 200 in 35ms",
138 "GET /api?+debug 200",
139 'Started GET "/posts?page=2" for ::1',
140 "proxying api/trpc/post.list?batch=1 to the backend",
141 ])("does not turn a logged request route into an editor link: %s", (line) => {
142 expect(extractTerminalLinks(line)).toEqual([]);
143 });
144
145 it("keeps file and URL links before and after a logged request route", () => {
146 const before = "src/server.ts:12";
147 const after = "/app/bin/server:3:7";
148 const url = "https://example.com/api/users?id=42";
149 const line = `${before} GET /api/users?id=42 200 ${after} ${url}`;
150 const afterStart = line.indexOf(after);
151 const urlStart = line.indexOf(url);
152
153 expect(extractTerminalLinks(line)).toEqual([
154 { kind: "path", text: before, start: 0, end: before.length },
155 { kind: "path", text: after, start: afterStart, end: afterStart + after.length },
156 { kind: "url", text: url, start: urlStart, end: urlStart + url.length },
157 ]);
158 });
159
160 it("keeps a question mark inside a filesystem path", () => {
161 const path = "/tmp/report?draft";
162 expect(extractTerminalLinks(`wrote ${path}`)).toEqual([
163 { kind: "path", text: path, start: 6, end: 6 + path.length },
164 ]);
165 });
166});
167
168describe("collectWrappedTerminalLinkLine", () => {
169 it("reconstructs a wrapped line from any physical row", () => {
170 const firstSegment = "see https://example.com/a";
171 const secondSegment = "/bc?x=1";
172 const lines = [
173 createBufferLine("prompt> "),
174 createBufferLine(firstSegment),
175 createBufferLine(secondSegment, true),
176 createBufferLine("done"),
177 ];
178
179 const fromFirstRow = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
180 const fromWrappedRow = collectWrappedTerminalLinkLine(3, (index) => lines[index]);
181
182 expect(fromFirstRow).toEqual({
183 text: `${firstSegment}${secondSegment}`,
184 segments: [
185 {
186 bufferLineNumber: 2,
187 text: firstSegment,
188 startIndex: 0,
189 endIndex: firstSegment.length,
190 },
191 {
192 bufferLineNumber: 3,
193 text: secondSegment,
194 startIndex: firstSegment.length,
195 endIndex: firstSegment.length + secondSegment.length,
196 },
197 ],
198 });
199 expect(fromWrappedRow).toEqual(fromFirstRow);
200 });
201
202 it("preserves trailing spaces on continued segments for downstream offsets", () => {
203 const firstSegment = "prefix ";
204 const secondSegment = "https://example.com/path";
205 const lines = [createBufferLine(firstSegment), createBufferLine(secondSegment, true)];
206
207 const wrappedLine = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
208
209 expect(wrappedLine?.text).toBe(`${firstSegment}${secondSegment}`);
210 expect(extractTerminalLinks(wrappedLine?.text ?? "")).toEqual([
211 {
212 kind: "url",
213 text: secondSegment,
214 start: firstSegment.length,
215 end: firstSegment.length + secondSegment.length,
216 },
217 ]);
218 });
219});
220
221describe("resolvePathLinkTarget", () => {
222 it("resolves a quoted terminal link against a Windows environment", () => {
223 const [link] = extractTerminalLinks(String.raw`error at "src\my folder\index.ts":12:7`);
224 expect(resolvePathLinkTarget(link?.text ?? "", String.raw`C:\Users\Jane Doe\project`)).toBe(
225 String.raw`C:\Users\Jane Doe\project\src\my folder\index.ts:12:7`,
226 );
227 });
228
229 it("keeps Windows absolute paths with forward slashes unchanged", () => {
230 expect(
231 resolvePathLinkTarget("C:/Users/julius/project/src/main.ts:12", "C:\\Users\\julius\\project"),
232 ).toBe("C:/Users/julius/project/src/main.ts:12");
233 });
234
235 // regression: #13408 terminal links kept a trailing colon
236 it("keeps the line and column of a compiler diagnostic", () => {
237 const [link] = extractTerminalLinks("/Users/julius/project/main.c:10:5: error: expected ';'");
238 expect(resolvePathLinkTarget(link?.text ?? "", "/Users/julius/project")).toBe(
239 "/Users/julius/project/main.c:10:5",
240 );
241 });
242});