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

terminal-links.test.ts

107 lines2.4 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​
48describe("resolvePathLinkTarget", () => {
49 it("resolves relative paths against cwd", () => {
50 expect(
51 resolvePathLinkTarget(
52 "src/components/ThreadTerminalDrawer.tsx:42:7",
53 "/Users/julius/project",
54 ),
55 ).toBe("/Users/julius/project/src/components/ThreadTerminalDrawer.tsx:42:7");
56 });
57​
58 it("keeps absolute paths unchanged", () => {
59 expect(
60 resolvePathLinkTarget("/Users/julius/project/src/main.ts:12", "/Users/julius/project"),
61 ).toBe("/Users/julius/project/src/main.ts:12");
62 });
63});
64​
65describe("isTerminalLinkActivation", () => {
66 it("requires cmd on macOS", () => {
67 expect(
68 isTerminalLinkActivation(
69 {
70 metaKey: true,
71 ctrlKey: false,
72 },
73 "MacIntel",
74 ),
75 ).toBe(true);
76 expect(
77 isTerminalLinkActivation(
78 {
79 metaKey: false,
80 ctrlKey: true,
81 },
82 "MacIntel",
83 ),
84 ).toBe(false);
85 });
86​
87 it("requires ctrl on non-macOS", () => {
88 expect(
89 isTerminalLinkActivation(
90 {
91 metaKey: false,
92 ctrlKey: true,
93 },
94 "Win32",
95 ),
96 ).toBe(true);
97 expect(
98 isTerminalLinkActivation(
99 {
100 metaKey: true,
101 ctrlKey: false,
102 },
103 "Linux",
104 ),
105 ).toBe(false);
106 });
107});