Files
openidea/src/ui/selectableText.test.ts
T
Mr0Bread 31016ee684 fix(agents): add Copy Link Address to the context menu of links in readable text (#3371)
Right-clicking a link in Agent Task chat (and other readable panes using the shared Copy menu) only offered Copy, which copied the selected label. Links now also get Copy Link Address, as in browsers; Copy keeps copying the selection, and in-page anchors get no link item.

Co-authored-by: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com>
2026-11-14 22:05:29 +08:00

131 lines
5.5 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { copyMenuFor, copyText, selectableText } from "./selectableText";
function mount(html: string) {
document.body.innerHTML = html;
return document.body;
}
function select(node: Node) {
const range = document.createRange();
range.selectNodeContents(node);
const sel = window.getSelection()!;
sel.removeAllRanges();
sel.addRange(range);
}
afterEach(() => {
window.getSelection()?.removeAllRanges();
document.body.innerHTML = "";
vi.restoreAllMocks();
});
describe("selectableText", () => {
it("is a real class name", () => {
expect(selectableText).toBeTruthy();
});
});
describe("copyMenuFor", () => {
it("offers Copy of the selection in a selectable pane (issue #36: Markdown preview)", () => {
mount(`<div class="${selectableText}" role="document"><p id="p">Hello <b id="b">world</b></p></div>`);
select(document.getElementById("p")!);
const items = copyMenuFor(document.getElementById("b"));
expect(items).toHaveLength(1);
expect(items![0]).toMatchObject({ label: "Copy", disabled: false });
});
it.each([
["link", "none"],
["nested", "link"],
["text", "elsewhere"],
])("offers Copy Link Address on a %s target with %s selected", async (target, selection) => {
const writeText = vi.fn().mockResolvedValue(undefined);
Object.defineProperty(navigator, "clipboard", { value: { writeText }, configurable: true });
const url = "https://example.com/pull/42?tab=files#diff";
mount(
`<div class="${selectableText}"><a id="link" href="${url}"><strong id="nested">Review changes</strong></a><p id="elsewhere">Other text</p></div>`,
);
if (selection !== "none") select(document.getElementById(selection)!);
const node = target === "text" ? document.getElementById("nested")!.firstChild : document.getElementById(target);
const items = copyMenuFor(node)!;
expect(items.map((i) => ("label" in i ? i.label : null))).toEqual(["Copy", "Copy Link Address"]);
const link = items[1]!;
if ("run" in link) link.run?.();
await Promise.resolve();
expect(writeText).toHaveBeenCalledWith(url);
});
it("keeps Copy on a link copying the selected text", async () => {
const writeText = vi.fn().mockResolvedValue(undefined);
Object.defineProperty(navigator, "clipboard", { value: { writeText }, configurable: true });
mount(`<div class="${selectableText}"><a id="link" href="https://example.com">Review changes</a></div>`);
select(document.getElementById("link")!);
const copy = copyMenuFor(document.getElementById("link"))![0]!;
if ("run" in copy) copy.run?.();
await Promise.resolve();
expect(writeText).toHaveBeenCalledWith("Review changes");
});
it("offers no Copy Link Address on an anchor without a destination or with an in-page one", () => {
mount(`<div class="${selectableText}"><a id="anchor">Section</a><a id="local" href="#top">Top</a></div>`);
for (const id of ["anchor", "local"]) expect(copyMenuFor(document.getElementById(id))).toHaveLength(1);
});
it("offers a disabled Copy in a selectable pane with nothing selected", () => {
mount(`<div class="${selectableText}"><p id="p">Hello</p></div>`);
expect(copyMenuFor(document.getElementById("p"))![0]).toMatchObject({ label: "Copy", disabled: true });
});
it("resolves text-node targets", () => {
mount(`<div class="${selectableText}"><p id="p">Hello</p></div>`);
expect(copyMenuFor(document.getElementById("p")!.firstChild)).not.toBeNull();
});
it("offers Copy on a selection in any pane that opted in with its own CSS", () => {
mount(`<pre id="console">line 1</pre>`);
select(document.getElementById("console")!);
expect(copyMenuFor(document.getElementById("console"))![0]).toMatchObject({ disabled: false });
});
it("stays out of chrome without a selection", () => {
mount(`<button id="b">Run</button><div id="row">tree row</div>`);
expect(copyMenuFor(document.getElementById("b"))).toBeNull();
expect(copyMenuFor(document.getElementById("row"))).toBeNull();
});
it("ignores a selection elsewhere when right-clicking chrome", () => {
mount(`<div class="${selectableText}"><p id="p">Hello</p></div><div id="row">tree row</div>`);
select(document.getElementById("p")!);
expect(copyMenuFor(document.getElementById("row"))).toBeNull();
});
it.each(["<input id='t'>", "<textarea id='t'></textarea>", "<div id='t' contenteditable='true'>x</div>"])(
"leaves editable targets alone: %s",
(html) => {
mount(`<div class="${selectableText}">${html}</div>`);
expect(copyMenuFor(document.getElementById("t"))).toBeNull();
},
);
it("copies the text captured when the menu opened", async () => {
const writeText = vi.fn().mockResolvedValue(undefined);
Object.defineProperty(navigator, "clipboard", { value: { writeText }, configurable: true });
mount(`<div class="${selectableText}"><p id="p">Hello</p></div>`);
select(document.getElementById("p")!);
const items = copyMenuFor(document.getElementById("p"))!;
window.getSelection()!.removeAllRanges();
(items[0] as { run: () => void }).run();
await Promise.resolve();
expect(writeText).toHaveBeenCalledWith("Hello");
});
it("falls back to execCommand when the clipboard API is missing", async () => {
Object.defineProperty(navigator, "clipboard", { value: undefined, configurable: true });
const exec = vi.fn().mockReturnValue(true);
document.execCommand = exec;
await copyText("x");
expect(exec).toHaveBeenCalledWith("copy");
});
});