-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathterminal-link-manager.test.ts
More file actions
140 lines (117 loc) · 3.95 KB
/
Copy pathterminal-link-manager.test.ts
File metadata and controls
140 lines (117 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { describe, expect, it, mock } from "bun:test";
import type { ILinkProvider, Terminal as XTerm } from "@xterm/xterm";
import { TerminalLinkManager } from "./terminal-link-manager";
function createMockTerminal() {
const registeredProviders: ILinkProvider[] = [];
const disposedProviders: ILinkProvider[] = [];
const selectionState = { hasSelection: false };
const terminal = {
options: {
linkHandler: null,
},
registerLinkProvider: (provider: ILinkProvider) => {
registeredProviders.push(provider);
return {
dispose: () => {
disposedProviders.push(provider);
},
};
},
buffer: {
active: {
getLine: () => null,
},
},
cols: 80,
hasSelection: () => selectionState.hasSelection,
} as unknown as XTerm;
return { terminal, registeredProviders, disposedProviders, selectionState };
}
describe("TerminalLinkManager", () => {
it("routes OSC 8 hyperlinks through the terminal URL handler", () => {
const { terminal } = createMockTerminal();
const manager = new TerminalLinkManager(terminal);
const onUrlClick = mock();
const onLinkHover = mock();
const onLinkLeave = mock();
manager.setHandlers({
stat: async () => null,
onUrlClick,
onLinkHover,
onLinkLeave,
});
const linkHandler = terminal.options.linkHandler;
expect(linkHandler).toBeTruthy();
expect(linkHandler?.allowNonHttpProtocols).toBe(false);
const event = {} as MouseEvent;
linkHandler?.activate(event, "https://example.com", {
start: { x: 1, y: 1 },
end: { x: 20, y: 1 },
});
linkHandler?.hover?.(event, "https://example.com", {
start: { x: 1, y: 1 },
end: { x: 20, y: 1 },
});
linkHandler?.leave?.(event, "https://example.com", {
start: { x: 1, y: 1 },
end: { x: 20, y: 1 },
});
expect(onUrlClick).toHaveBeenCalledWith(event, "https://example.com");
expect(onLinkHover).toHaveBeenCalledWith(event, { kind: "url" });
expect(onLinkLeave).toHaveBeenCalled();
});
it("skips plain-click activation while text is selected, but not modifier clicks", () => {
const { terminal, selectionState } = createMockTerminal();
const manager = new TerminalLinkManager(terminal);
const onUrlClick = mock();
manager.setHandlers({
stat: async () => null,
onUrlClick,
});
const linkHandler = terminal.options.linkHandler;
const range = { start: { x: 1, y: 1 }, end: { x: 20, y: 1 } };
// Tail of a double-click word-select / intra-link drag: suppressed.
selectionState.hasSelection = true;
linkHandler?.activate({} as MouseEvent, "https://example.com", range);
expect(onUrlClick).not.toHaveBeenCalled();
// Shift-click extends the selection as a side effect but must still
// activate, else the shift binding would be unreachable.
linkHandler?.activate(
{ shiftKey: true } as MouseEvent,
"https://example.com",
range,
);
expect(onUrlClick).toHaveBeenCalledTimes(1);
// Plain click without a selection activates.
selectionState.hasSelection = false;
linkHandler?.activate({} as MouseEvent, "https://example.com", range);
expect(onUrlClick).toHaveBeenCalledTimes(2);
});
it("clears only the OSC link handler it installed", () => {
const { terminal, disposedProviders } = createMockTerminal();
const manager = new TerminalLinkManager(terminal);
manager.setHandlers({
stat: async () => null,
onUrlClick: mock(),
});
const installedHandler = terminal.options.linkHandler;
expect(installedHandler).toBeTruthy();
manager.dispose();
expect(terminal.options.linkHandler).toBeNull();
expect(disposedProviders.length).toBe(2);
});
it("does not clear a link handler installed by another owner", () => {
const { terminal } = createMockTerminal();
const manager = new TerminalLinkManager(terminal);
manager.setHandlers({
stat: async () => null,
onUrlClick: mock(),
});
const replacementHandler = {
activate: mock(),
};
terminal.options.linkHandler = replacementHandler;
manager.dispose();
expect(terminal.options.linkHandler).toBe(replacementHandler);
});
});