Open History to see the last commit
1import { describe, expect, it, vi } from "vite-plus/test";
2
3import { resolveExternalWebLinkHost, showExternalLinkContextMenu } from "./externalLinkContextMenu";
4
5function createHarness(selection: "open-in-preview" | "open-external" | "copy-link" | null) {
6 const showContextMenu = vi.fn().mockResolvedValue(selection);
7 const openInPreview = vi.fn().mockResolvedValue(undefined);
8 const openExternal = vi.fn().mockResolvedValue(undefined);
9 const copyLink = vi.fn().mockResolvedValue(undefined);
10 const reportFailure = vi.fn();
11
12 return {
13 showContextMenu,
14 openInPreview,
15 openExternal,
16 copyLink,
17 reportFailure,
18 };
19}
20
21describe("external chat link context menu", () => {
22 it("offers both open actions and Copy Link", async () => {
23 const harness = createHarness(null);
24
25 await showExternalLinkContextMenu({
26 href: "https://example.com/docs?topic=menus#copy",
27 position: { x: 12, y: 24 },
28 ...harness,
29 });
30
31 expect(harness.showContextMenu).toHaveBeenCalledWith(
32 [
33 { id: "open-in-preview", label: "Open in integrated browser" },
34 { id: "open-external", label: "Open in system browser" },
35 { id: "copy-link", label: "Copy Link" },
36 ],
37 { x: 12, y: 24 },
38 );
39 expect(harness.openInPreview).not.toHaveBeenCalled();
40 expect(harness.openExternal).not.toHaveBeenCalled();
41 expect(harness.copyLink).not.toHaveBeenCalled();
42 });
43
44 it("copies the exact destination without opening it", async () => {
45 const harness = createHarness("copy-link");
46 const href = "https://example.com/docs?topic=menus#copy";
47
48 await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });
49
50 expect(harness.copyLink).toHaveBeenCalledWith(href);
51 expect(harness.openInPreview).not.toHaveBeenCalled();
52 expect(harness.openExternal).not.toHaveBeenCalled();
53 });
54
55 it.each([
56 ["open-in-preview" as const, "openInPreview" as const],
57 ["open-external" as const, "openExternal" as const],
58 ])("preserves the %s action", async (selection, expectedCallback) => {
59 const harness = createHarness(selection);
60 const href = "https://example.com/docs";
61
62 await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });
63
64 expect(harness[expectedCallback]).toHaveBeenCalledWith(href);
65 expect(harness.copyLink).not.toHaveBeenCalled();
66 });
67
68 it("reports the selected action when it fails", async () => {
69 const harness = createHarness("copy-link");
70 const cause = new Error("clipboard denied");
71 harness.copyLink.mockRejectedValue(cause);
72
73 await showExternalLinkContextMenu({
74 href: "https://example.com/docs",
75 position: { x: 1, y: 2 },
76 ...harness,
77 });
78
79 expect(harness.reportFailure).toHaveBeenCalledWith("copy-link", cause);
80 });
81
82 it("reports the menu operation when the native menu cannot be shown", async () => {
83 const harness = createHarness(null);
84 const cause = new Error("menu unavailable");
85 harness.showContextMenu.mockRejectedValue(cause);
86
87 await showExternalLinkContextMenu({
88 href: "https://example.com/docs",
89 position: { x: 1, y: 2 },
90 ...harness,
91 });
92
93 expect(harness.reportFailure).toHaveBeenCalledWith("show-link-context-menu", cause);
94 expect(harness.openInPreview).not.toHaveBeenCalled();
95 expect(harness.openExternal).not.toHaveBeenCalled();
96 expect(harness.copyLink).not.toHaveBeenCalled();
97 });
98
99 it.each([
100 ["open-in-preview" as const, "openInPreview" as const, "open-link-in-preview"],
101 ["open-external" as const, "openExternal" as const, "open-link-external"],
102 ])("reports a failed %s action", async (selection, callback, operation) => {
103 const harness = createHarness(selection);
104 const cause = new Error("open failed");
105 harness[callback].mockRejectedValue(cause);
106
107 await showExternalLinkContextMenu({
108 href: "https://example.com/docs",
109 position: { x: 1, y: 2 },
110 ...harness,
111 });
112
113 expect(harness.reportFailure).toHaveBeenCalledWith(operation, cause);
114 });
115
116 it.each([
117 ["https://example.com", "example.com"],
118 ["http://localhost:3000/path", "localhost"],
119 ["#details", null],
120 ["mailto:hello@example.com", null],
121 ["file:///tmp/example.txt", null],
122 ["javascript:void(0)", null],
123 ["not a URL", null],
124 [undefined, null],
125 ])("resolves the external web-link host for %s as %s", (href, expected) => {
126 expect(resolveExternalWebLinkHost(href)).toBe(expected);
127 });
128});