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

terminal-links.test.ts

139 lines3.3 KB

1import { describe, expect, it } from "vitest";
2​
3import {
4 extractTerminalLinks,
5 isTerminalLinkActivation,
6 resolvePathLinkTarget,
7} from "./terminal-links";
8​
9describe("extractTerminalLinks", () => {
10 it("finds http urls and path tokens", () => {
11 const line =
12 "failed at https://example.com/docs and src/components/ThreadTerminalDrawer.tsx:42";
13 expect(extractTerminalLinks(line)).toEqual([
14 {
15 kind: "url",
16 text: "https://example.com/docs",
17 start: 10,
18 end: 34,
19 },
20 {
21 kind: "path",
22 text: "src/components/ThreadTerminalDrawer.tsx:42",
23 start: 39,
24 end: 81,
25 },
26 ]);
27 });
28​
29 it("trims trailing punctuation from links", () => {
30 const line = "(https://example.com/docs), ./src/main.ts:12.";
31 expect(extractTerminalLinks(line)).toEqual([
32 {
33 kind: "url",
34 text: "https://example.com/docs",
35 start: 1,
36 end: 25,
37 },
38 {
39 kind: "path",
40 text: "./src/main.ts:12",
41 start: 28,
42 end: 44,
43 },
44 ]);
45 });
46​
47 it("finds Windows absolute paths with forward slashes", () => {
48 const line = "see C:/Users/someone/project/src/file.ts:42 for details";
49 const path = "C:/Users/someone/project/src/file.ts:42";
50 const start = line.indexOf(path);
51 expect(extractTerminalLinks(line)).toEqual([
52 {
53 kind: "path",
54 text: path,
55 start,
56 end: start + path.length,
57 },
58 ]);
59 });
60​
61 it("trims trailing punctuation from Windows forward-slash paths", () => {
62 const line = "(C:/tmp/x.ts).";
63 expect(extractTerminalLinks(line)).toEqual([
64 {
65 kind: "path",
66 text: "C:/tmp/x.ts",
67 start: 1,
68 end: 12,
69 },
70 ]);
71 });
72});
73​
74describe("resolvePathLinkTarget", () => {
75 it("resolves relative paths against cwd", () => {
76 expect(
77 resolvePathLinkTarget(
78 "src/components/ThreadTerminalDrawer.tsx:42:7",
79 "/Users/julius/project",
80 ),
81 ).toBe("/Users/julius/project/src/components/ThreadTerminalDrawer.tsx:42:7");
82 });
83​
84 it("keeps absolute paths unchanged", () => {
85 expect(
86 resolvePathLinkTarget("/Users/julius/project/src/main.ts:12", "/Users/julius/project"),
87 ).toBe("/Users/julius/project/src/main.ts:12");
88 });
89​
90 it("keeps Windows absolute paths with forward slashes unchanged", () => {
91 expect(
92 resolvePathLinkTarget("C:/Users/julius/project/src/main.ts:12", "C:\\Users\\julius\\project"),
93 ).toBe("C:/Users/julius/project/src/main.ts:12");
94 });
95});
96​
97describe("isTerminalLinkActivation", () => {
98 it("requires cmd on macOS", () => {
99 expect(
100 isTerminalLinkActivation(
101 {
102 metaKey: true,
103 ctrlKey: false,
104 },
105 "MacIntel",
106 ),
107 ).toBe(true);
108 expect(
109 isTerminalLinkActivation(
110 {
111 metaKey: false,
112 ctrlKey: true,
113 },
114 "MacIntel",
115 ),
116 ).toBe(false);
117 });
118​
119 it("requires ctrl on non-macOS", () => {
120 expect(
121 isTerminalLinkActivation(
122 {
123 metaKey: false,
124 ctrlKey: true,
125 },
126 "Win32",
127 ),
128 ).toBe(true);
129 expect(
130 isTerminalLinkActivation(
131 {
132 metaKey: true,
133 ctrlKey: false,
134 },
135 "Linux",
136 ),
137 ).toBe(false);
138 });
139});