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

terminal-links.test.ts

308 lines9.5 KB

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 };
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 ])("finds a quoted basename in %s", (line, text, end) => {
62 expect(extractTerminalLinks(line)).toEqual([{ kind: "path", text, start: 1, end }]);
63 });
64​
65 it("keeps quoted paths and URLs separate without duplicate partial paths", () => {
66 const path = String.raw`C:\Jane Doe\index.ts:12`;
67 const url = "https://example.com/docs";
68 const line = `"${path}" ${url} src/index.ts`;
69 expect(extractTerminalLinks(line)).toEqual([
70 { kind: "path", text: path, start: 1, end: 1 + path.length },
71 { kind: "url", text: url, start: path.length + 3, end: path.length + 3 + url.length },
72 { kind: "path", text: "src/index.ts", start: line.length - 12, end: line.length },
73 ]);
74 });
75​
76 it("finds http urls and path tokens", () => {
77 const line =
78 "failed at https://example.com/docs and src/components/ThreadTerminalDrawer.tsx:42";
79 expect(extractTerminalLinks(line)).toEqual([
80 {
81 kind: "url",
82 text: "https://example.com/docs",
83 start: 10,
84 end: 34,
85 },
86 {
87 kind: "path",
88 text: "src/components/ThreadTerminalDrawer.tsx:42",
89 start: 39,
90 end: 81,
91 },
92 ]);
93 });
94​
95 it("classifies uppercase schemes as URLs at activation time too", () => {
96 expect(isTerminalUrl("HTTPS://example.com/docs")).toBe(true);
97 expect(isTerminalUrl("Http://example.com")).toBe(true);
98 expect(isTerminalUrl("src/components/main.ts")).toBe(false);
99 expect(isTerminalUrl("httpsdocs/readme.md")).toBe(false);
100 });
101​
102 it("finds URLs regardless of scheme casing", () => {
103 expect(extractTerminalLinks("open HTTPS://example.com/docs")).toEqual([
104 {
105 kind: "url",
106 text: "HTTPS://example.com/docs",
107 start: 5,
108 end: 29,
109 },
110 ]);
111 });
112​
113 it("trims trailing punctuation from links", () => {
114 const line = "(https://example.com/docs), ./src/main.ts:12.";
115 expect(extractTerminalLinks(line)).toEqual([
116 {
117 kind: "url",
118 text: "https://example.com/docs",
119 start: 1,
120 end: 25,
121 },
122 {
123 kind: "path",
124 text: "./src/main.ts:12",
125 start: 28,
126 end: 44,
127 },
128 ]);
129 });
130​
131 it("finds Windows absolute paths with forward slashes", () => {
132 const line = "see C:/Users/someone/project/src/file.ts:42 for details";
133 const path = "C:/Users/someone/project/src/file.ts:42";
134 const start = line.indexOf(path);
135 expect(extractTerminalLinks(line)).toEqual([
136 {
137 kind: "path",
138 text: path,
139 start,
140 end: start + path.length,
141 },
142 ]);
143 });
144​
145 it("trims trailing punctuation from Windows forward-slash paths", () => {
146 const line = "(C:/tmp/x.ts).";
147 expect(extractTerminalLinks(line)).toEqual([
148 {
149 kind: "path",
150 text: "C:/tmp/x.ts",
151 start: 1,
152 end: 12,
153 },
154 ]);
155 });
156​
157 it("keeps a trailing colon on URLs", () => {
158 expect(extractTerminalLinks("GET https://example.test/items/foo:")).toEqual([
159 { kind: "url", text: "https://example.test/items/foo:", start: 4, end: 35 },
160 ]);
161 });
162​
163 it.each([
164 ["./main.go:10:5: undefined: x", "./main.go:10:5"],
165 ["/home/dev/app/src/main.c:10:5: error: expected ';'", "/home/dev/app/src/main.c:10:5"],
166 ["C:\\dev\\app\\src\\main.c:10:5: error: expected ';'", "C:\\dev\\app\\src\\main.c:10:5"],
167 ["wrote ./out/report.txt:", "./out/report.txt"],
168 ])("drops the colon that ends a compiler diagnostic location in %s", (line, text) => {
169 const start = line.indexOf(text);
170 expect(extractTerminalLinks(line)).toEqual([
171 { kind: "path", text, start, end: start + text.length },
172 ]);
173 });
174});
175​
176describe("collectWrappedTerminalLinkLine", () => {
177 it("reconstructs a wrapped line from any physical row", () => {
178 const firstSegment = "see https://example.com/a";
179 const secondSegment = "/bc?x=1";
180 const lines = [
181 createBufferLine("prompt> "),
182 createBufferLine(firstSegment),
183 createBufferLine(secondSegment, true),
184 createBufferLine("done"),
185 ];
186​
187 const fromFirstRow = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
188 const fromWrappedRow = collectWrappedTerminalLinkLine(3, (index) => lines[index]);
189​
190 expect(fromFirstRow).toEqual({
191 text: `${firstSegment}${secondSegment}`,
192 segments: [
193 {
194 bufferLineNumber: 2,
195 text: firstSegment,
196 startIndex: 0,
197 endIndex: firstSegment.length,
198 },
199 {
200 bufferLineNumber: 3,
201 text: secondSegment,
202 startIndex: firstSegment.length,
203 endIndex: firstSegment.length + secondSegment.length,
204 },
205 ],
206 });
207 expect(fromWrappedRow).toEqual(fromFirstRow);
208 });
209​
210 it("preserves trailing spaces on continued segments for downstream offsets", () => {
211 const firstSegment = "prefix ";
212 const secondSegment = "https://example.com/path";
213 const lines = [createBufferLine(firstSegment), createBufferLine(secondSegment, true)];
214​
215 const wrappedLine = collectWrappedTerminalLinkLine(2, (index) => lines[index]);
216​
217 expect(wrappedLine?.text).toBe(`${firstSegment}${secondSegment}`);
218 expect(extractTerminalLinks(wrappedLine?.text ?? "")).toEqual([
219 {
220 kind: "url",
221 text: secondSegment,
222 start: firstSegment.length,
223 end: firstSegment.length + secondSegment.length,
224 },
225 ]);
226 });
227});
228​
229describe("resolvePathLinkTarget", () => {
230 it("resolves a quoted terminal link against a Windows environment", () => {
231 const [link] = extractTerminalLinks(String.raw`error at "src\my folder\index.ts":12:7`);
232 expect(resolvePathLinkTarget(link?.text ?? "", String.raw`C:\Users\Jane Doe\project`)).toBe(
233 String.raw`C:\Users\Jane Doe\project\src\my folder\index.ts:12:7`,
234 );
235 });
236​
237 it("resolves relative paths against cwd", () => {
238 expect(
239 resolvePathLinkTarget(
240 "src/components/ThreadTerminalDrawer.tsx:42:7",
241 "/Users/julius/project",
242 ),
243 ).toBe("/Users/julius/project/src/components/ThreadTerminalDrawer.tsx:42:7");
244 });
245​
246 it("keeps absolute paths unchanged", () => {
247 expect(
248 resolvePathLinkTarget("/Users/julius/project/src/main.ts:12", "/Users/julius/project"),
249 ).toBe("/Users/julius/project/src/main.ts:12");
250 });
251​
252 it("keeps Windows absolute paths with forward slashes unchanged", () => {
253 expect(
254 resolvePathLinkTarget("C:/Users/julius/project/src/main.ts:12", "C:\\Users\\julius\\project"),
255 ).toBe("C:/Users/julius/project/src/main.ts:12");
256 });
257​
258 it("keeps the line and column of a compiler diagnostic", () => {
259 const [link] = extractTerminalLinks("/Users/julius/project/main.c:10:5: error: expected ';'");
260 expect(resolvePathLinkTarget(link?.text ?? "", "/Users/julius/project")).toBe(
261 "/Users/julius/project/main.c:10:5",
262 );
263 });
264});
265​
266describe("isTerminalLinkActivation", () => {
267 it("requires cmd on macOS", () => {
268 expect(
269 isTerminalLinkActivation(
270 {
271 metaKey: true,
272 ctrlKey: false,
273 },
274 "MacIntel",
275 ),
276 ).toBe(true);
277 expect(
278 isTerminalLinkActivation(
279 {
280 metaKey: false,
281 ctrlKey: true,
282 },
283 "MacIntel",
284 ),
285 ).toBe(false);
286 });
287​
288 it("requires ctrl on non-macOS", () => {
289 expect(
290 isTerminalLinkActivation(
291 {
292 metaKey: false,
293 ctrlKey: true,
294 },
295 "Win32",
296 ),
297 ).toBe(true);
298 expect(
299 isTerminalLinkActivation(
300 {
301 metaKey: true,
302 ctrlKey: false,
303 },
304 "Linux",
305 ),
306 ).toBe(false);
307 });
308});