Файли
openidea/src/platform/openUrl.test.ts
T
l0ng-ai 04666f5980 fix(app): open middle-clicked links in the browser (#5005)
A middle click on a link (an agent reply, any rendered content) navigated the IDE window to it: link handlers listen to click, and the middle button fires auxclick, whose default action the webview carried out. A window-wide auxclick handler now cancels it for every link and opens a web link in the default browser, as a left click does; a link into the app itself does nothing.

Fixes #4992
2026-11-21 19:33:15 +08:00

62 рядки
2.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const openUrl = vi.fn(async (_url: string) => {});
vi.mock("./ipc", () => ({ isTauri: () => true }));
vi.mock("@tauri-apps/plugin-opener", () => ({ openUrl }));
import { installMiddleClickLinks, openExternalUrl } from "./openUrl";
describe("openExternalUrl", () => {
beforeEach(() => openUrl.mockClear());
it.each(["https://example.com/a?b=1", "http://localhost:3000/", "mailto:[email protected]"])("opens %s", async (url) => {
await openExternalUrl(url);
expect(openUrl).toHaveBeenCalledWith(url);
});
it.each([
"file:///C:/Windows/win.ini",
"ms-msdt:/id x",
"search-ms:query=x",
"javascript:alert(1)",
"data:text/html,x",
"//evil.example",
" https://example.com",
"example.com",
"",
])("refuses %j", async (url) => {
await openExternalUrl(url);
expect(openUrl).not.toHaveBeenCalled();
});
});
describe("middle click on a link", () => {
beforeEach(() => openUrl.mockClear());
const middleClick = (target: Element) => {
const event = new MouseEvent("auxclick", { button: 1, bubbles: true, cancelable: true });
target.dispatchEvent(event);
return event;
};
it("opens a web link in the browser instead of letting the webview navigate (#4992)", async () => {
const off = installMiddleClickLinks();
document.body.innerHTML = '<p><a href="https://example.com/pr/1"><code>#1</code></a><a href="/local">x</a></p>';
const event = middleClick(document.querySelector("code")!);
expect(event.defaultPrevented).toBe(true);
await vi.waitFor(() => expect(openUrl).toHaveBeenCalledWith("https://example.com/pr/1"));
// A link into the app itself is not handed to the browser, nor followed.
expect(middleClick(document.querySelector('a[href="/local"]')!).defaultPrevented).toBe(true);
// Left clicks and other targets are left alone.
const left = new MouseEvent("auxclick", { button: 2, bubbles: true, cancelable: true });
document.querySelector("code")!.dispatchEvent(left);
expect(left.defaultPrevented).toBe(false);
expect(middleClick(document.querySelector("p")!).defaultPrevented).toBe(false);
off();
expect(middleClick(document.querySelector("code")!).defaultPrevented).toBe(false);
expect(openUrl).toHaveBeenCalledTimes(1);
});
});