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