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

terminal-links.test.ts

272 lines9.7 KB

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