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

externalLinkContextMenu.test.ts

195 lines6.4 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import { resolveExternalWebLinkHost, showExternalLinkContextMenu } from "./externalLinkContextMenu";
4​
5function createHarness(
6 selection:
7 | "open-in-preview"
8 | "open-external"
9 | "copy-link"
10 | "link-to-thread"
11 | "unlink-from-thread"
12 | null,
13) {
14 const showContextMenu = vi.fn().mockResolvedValue(selection);
15 const openInPreview = vi.fn().mockResolvedValue(undefined);
16 const openExternal = vi.fn().mockResolvedValue(undefined);
17 const copyLink = vi.fn().mockResolvedValue(undefined);
18 const updateThreadLink = vi.fn().mockResolvedValue(undefined);
19 const reportFailure = vi.fn();
20​
21 return {
22 showContextMenu,
23 openInPreview,
24 openExternal,
25 copyLink,
26 updateThreadLink,
27 reportFailure,
28 };
30​
31describe("external chat link context menu", () => {
32 it("offers both open actions and Copy Link", async () => {
33 const harness = createHarness(null);
34​
35 await showExternalLinkContextMenu({
36 href: "https://example.com/docs?topic=menus#copy",
37 position: { x: 12, y: 24 },
38 ...harness,
39 });
40​
41 expect(harness.showContextMenu).toHaveBeenCalledWith(
42 [
43 { id: "open-in-preview", label: "Open in integrated browser" },
44 { id: "open-external", label: "Open in system browser" },
45 { id: "copy-link", label: "Copy Link" },
46 ],
47 { x: 12, y: 24 },
48 );
49 expect(harness.openInPreview).not.toHaveBeenCalled();
50 expect(harness.openExternal).not.toHaveBeenCalled();
51 expect(harness.copyLink).not.toHaveBeenCalled();
52 });
53​
54 it("still offers the link's own actions where the integrated browser cannot be opened", async () => {
55 const harness = createHarness(null);
56​
57 await showExternalLinkContextMenu({
58 href: "https://github.com/pingdotgg/t3code/pull/6169",
59 canOpenInPreview: false,
60 position: { x: 4, y: 8 },
61 ...harness,
62 });
63​
64 expect(harness.showContextMenu).toHaveBeenCalledWith(
65 [
66 { id: "open-external", label: "Open in system browser" },
67 { id: "copy-link", label: "Copy Link" },
68 ],
69 { x: 4, y: 8 },
70 );
71 });
72​
73 it("copies the exact destination without opening it", async () => {
74 const harness = createHarness("copy-link");
75 const href = "https://example.com/docs?topic=menus#copy";
76​
77 await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });
78​
79 expect(harness.copyLink).toHaveBeenCalledWith(href);
80 expect(harness.openInPreview).not.toHaveBeenCalled();
81 expect(harness.openExternal).not.toHaveBeenCalled();
82 });
83​
84 it.each([
85 ["link-to-thread", "Link to thread", true],
86 ["unlink-from-thread", "Unlink from thread", false],
87 ] as const)("offers and runs the %s action", async (action, label, linked) => {
88 const harness = createHarness(action);
89 const href = "https://github.com/pingdotgg/t3code/pull/42";
90​
91 await showExternalLinkContextMenu({
92 href,
93 threadLinkAction: action,
94 position: { x: 1, y: 2 },
95 ...harness,
96 });
97​
98 expect(harness.showContextMenu).toHaveBeenCalledWith(
99 expect.arrayContaining([{ id: action, label }]),
100 { x: 1, y: 2 },
101 );
102 expect(harness.updateThreadLink).toHaveBeenCalledWith(href, linked);
103 });
104​
105 it.each([
106 ["open-in-preview" as const, "openInPreview" as const],
107 ["open-external" as const, "openExternal" as const],
108 ])("preserves the %s action", async (selection, expectedCallback) => {
109 const harness = createHarness(selection);
110 const href = "https://example.com/docs";
111​
112 await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });
113​
114 expect(harness[expectedCallback]).toHaveBeenCalledWith(href);
115 expect(harness.copyLink).not.toHaveBeenCalled();
116 });
117​
118 it("reports the selected action when it fails", async () => {
119 const harness = createHarness("copy-link");
120 const cause = new Error("clipboard denied");
121 harness.copyLink.mockRejectedValue(cause);
122​
123 await showExternalLinkContextMenu({
124 href: "https://example.com/docs",
125 position: { x: 1, y: 2 },
126 ...harness,
127 });
128​
129 expect(harness.reportFailure).toHaveBeenCalledWith("copy-link", cause);
130 });
131​
132 it("reports the menu operation when the native menu cannot be shown", async () => {
133 const harness = createHarness(null);
134 const cause = new Error("menu unavailable");
135 harness.showContextMenu.mockRejectedValue(cause);
136​
137 await showExternalLinkContextMenu({
138 href: "https://example.com/docs",
139 position: { x: 1, y: 2 },
140 ...harness,
141 });
142​
143 expect(harness.reportFailure).toHaveBeenCalledWith("show-link-context-menu", cause);
144 expect(harness.openInPreview).not.toHaveBeenCalled();
145 expect(harness.openExternal).not.toHaveBeenCalled();
146 expect(harness.copyLink).not.toHaveBeenCalled();
147 });
148​
149 it.each([
150 ["open-in-preview" as const, "openInPreview" as const, "open-link-in-preview"],
151 ["open-external" as const, "openExternal" as const, "open-link-external"],
152 ])("reports a failed %s action", async (selection, callback, operation) => {
153 const harness = createHarness(selection);
154 const cause = new Error("open failed");
155 harness[callback].mockRejectedValue(cause);
156​
157 await showExternalLinkContextMenu({
158 href: "https://example.com/docs",
159 position: { x: 1, y: 2 },
160 ...harness,
161 });
162​
163 expect(harness.reportFailure).toHaveBeenCalledWith(operation, cause);
164 });
165​
166 it("reports a failed thread link action", async () => {
167 const harness = createHarness("link-to-thread");
168 const cause = new Error("thread update failed");
169 harness.updateThreadLink.mockRejectedValue(cause);
170​
171 await showExternalLinkContextMenu({
172 href: "https://github.com/pingdotgg/t3code/pull/42",
173 threadLinkAction: "link-to-thread",
174 position: { x: 1, y: 2 },
175 ...harness,
176 });
177​
178 expect(harness.reportFailure).toHaveBeenCalledWith("link-pull-request-to-thread", cause);
179 });
180​
181 it.each([
182 ["https://example.com", "example.com"],
183 ["http://localhost:3000/path", "localhost"],
184 ["//cdn.example.com/clip.mp4?signature=abc#t=2", "cdn.example.com"],
185 ["//", null],
186 ["#details", null],
187 ["mailto:hello@example.com", null],
188 ["file:///tmp/example.txt", null],
189 ["javascript:void(0)", null],
190 ["not a URL", null],
191 [undefined, null],
192 ])("resolves the external web-link host for %s as %s", (href, expected) => {
193 expect(resolveExternalWebLinkHost(href)).toBe(expected);
194 });
195});