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

terminal-links.test.ts

295 lines10.6 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 "src/app/[id]/page.tsx:3:10",
129 "src/routes/(app)/+page.svelte:12:4",
130 "node_modules/.pnpm/react@18.3.1/node_modules/react/index.js:12:5",
131 "app/routes/posts.$id.tsx:5",
132 ])("keeps the complete relative path %s", (text) => {
133 expect(extractTerminalLinks(`see ${text} for details`)).toEqual([
134 { kind: "path", text, start: 4, end: 4 + text.length },
135 ]);
136 });
137​
138 it("finds Windows absolute paths with forward slashes", () => {
139 const line = "see C:/Users/someone/project/src/file.ts:42 for details";
140 const path = "C:/Users/someone/project/src/file.ts:42";
141 const start = line.indexOf(path);
142 expect(extractTerminalLinks(line)).toEqual([
143 {
144 kind: "path",
145 text: path,
146 start,
147 end: start + path.length,
148 },
149 ]);
150 });
151​
152 it("keeps a trailing colon on URLs", () => {
153 expect(extractTerminalLinks("GET https://example.test/items/foo:")).toEqual([
154 { kind: "url", text: "https://example.test/items/foo:", start: 4, end: 35 },
155 ]);
156 });
157​
158 it.each([
159 ["./main.go:10:5: undefined: x", "./main.go:10:5"],
160 ["/home/dev/app/src/main.c:10:5: error: expected ';'", "/home/dev/app/src/main.c:10:5"],
161 ["C:\\dev\\app\\src\\main.c:10:5: error: expected ';'", "C:\\dev\\app\\src\\main.c:10:5"],
162 ["wrote ./out/report.txt:", "./out/report.txt"],
163 ])("drops the colon that ends a compiler diagnostic location in %s", (line, text) => {
164 const start = line.indexOf(text);
165 expect(extractTerminalLinks(line)).toEqual([
166 { kind: "path", text, start, end: start + text.length },
167 ]);
168 });
169​
170 it.each([
171 " GET /api/trpc/post.list,user.me?batch=1&input=%7B%220%22%3A%7B%22json%22%3Anull%7D%7D 200 in 35ms",
172 "GET /api?+debug 200",
173 'Started GET "/posts?page=2" for ::1',
174 "proxying api/trpc/post.list?batch=1 to the backend",
175 ])("does not turn a logged request route into an editor link: %s", (line) => {
176 expect(extractTerminalLinks(line)).toEqual([]);
177 });
178​
179 it("keeps file and URL links before and after a logged request route", () => {
180 const before = "src/server.ts:12";
181 const after = "/app/bin/server:3:7";
182 const url = "https://example.com/api/users?id=42";
183 const line = `${before} GET /api/users?id=42 200 ${after} ${url}`;
184 const afterStart = line.indexOf(after);
185 const urlStart = line.indexOf(url);
186​
187 expect(extractTerminalLinks(line)).toEqual([
188 { kind: "path", text: before, start: 0, end: before.length },
189 { kind: "path", text: after, start: afterStart, end: afterStart + after.length },
190 { kind: "url", text: url, start: urlStart, end: urlStart + url.length },
191 ]);
192 });
193​
194 it.each([
195 "connected to redis://127.0.0.1:6379/0",
196 "connected to postgres://localhost:5432/app",
197 "uploaded s3://bucket/reports/today.csv",
198 " at main (file:///Users/jane/app/src/index.mjs:3:7)",
199 "+redis://localhost:6379/0",
200 "-redis://localhost:6379/0",
201 ])("does not turn part of a non-web URL into an editor link: %s", (line) => {
202 expect(extractTerminalLinks(line)).toEqual([]);
203 });
204​
205 it("keeps file links beside a non-web URL", () => {
206 const line = "src/db.ts:4 redis://localhost:6379/0 C:/app/src/db.ts:9";
207 expect(extractTerminalLinks(line)).toEqual([
208 { kind: "path", text: "src/db.ts:4", start: 0, end: 11 },
209 { kind: "path", text: "C:/app/src/db.ts:9", start: 37, end: 55 },
210 ]);
211 });
212​
213 it("keeps a question mark inside a filesystem path", () => {
214 const path = "/tmp/report?draft";
215 expect(extractTerminalLinks(`wrote ${path}`)).toEqual([
216 { kind: "path", text: path, start: 6, end: 6 + path.length },
217 ]);
218 });
219});
220​
221describe("collectWrappedTerminalLinkLine", () => {
222 it("reconstructs a wrapped line from any physical row", () => {
223 const firstSegment = "see https://example.com/a";
224 const secondSegment = "/bc?x=1";
225 const lines = [
226 createBufferLine("prompt> "),
227 createBufferLine(firstSegment),
228 createBufferLine(secondSegment, true),
229 createBufferLine("done"),
230 ];
231​
232 const fromFirstRow = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
233 const fromWrappedRow = collectWrappedTerminalLinkLine(3, (index) => lines[index]);
234​
235 expect(fromFirstRow).toEqual({
236 text: `${firstSegment}${secondSegment}`,
237 segments: [
238 {
239 bufferLineNumber: 2,
240 text: firstSegment,
241 startIndex: 0,
242 endIndex: firstSegment.length,
243 },
244 {
245 bufferLineNumber: 3,
246 text: secondSegment,
247 startIndex: firstSegment.length,
248 endIndex: firstSegment.length + secondSegment.length,
249 },
250 ],
251 });
252 expect(fromWrappedRow).toEqual(fromFirstRow);
253 });
254​
255 it("preserves trailing spaces on continued segments for downstream offsets", () => {
256 const firstSegment = "prefix ";
257 const secondSegment = "https://example.com/path";
258 const lines = [createBufferLine(firstSegment), createBufferLine(secondSegment, true)];
259​
260 const wrappedLine = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
261​
262 expect(wrappedLine?.text).toBe(`${firstSegment}${secondSegment}`);
263 expect(extractTerminalLinks(wrappedLine?.text ?? "")).toEqual([
264 {
265 kind: "url",
266 text: secondSegment,
267 start: firstSegment.length,
268 end: firstSegment.length + secondSegment.length,
269 },
270 ]);
271 });
272});
273​
274describe("resolvePathLinkTarget", () => {
275 it("resolves a quoted terminal link against a Windows environment", () => {
276 const [link] = extractTerminalLinks(String.raw`error at "src\my folder\index.ts":12:7`);
277 expect(resolvePathLinkTarget(link?.text ?? "", String.raw`C:\Users\Jane Doe\project`)).toBe(
278 String.raw`C:\Users\Jane Doe\project\src\my folder\index.ts:12:7`,
279 );
280 });
281​
282 it("keeps Windows absolute paths with forward slashes unchanged", () => {
283 expect(
284 resolvePathLinkTarget("C:/Users/julius/project/src/main.ts:12", "C:\\Users\\julius\\project"),
285 ).toBe("C:/Users/julius/project/src/main.ts:12");
286 });
287​
288 // regression: #13408 terminal links kept a trailing colon
289 it("keeps the line and column of a compiler diagnostic", () => {
290 const [link] = extractTerminalLinks("/Users/julius/project/main.c:10:5: error: expected ';'");
291 expect(resolvePathLinkTarget(link?.text ?? "", "/Users/julius/project")).toBe(
292 "/Users/julius/project/main.c:10:5",
293 );
294 });
295});