+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
224 lines
7.2 KiB
TypeScript
224 lines
7.2 KiB
TypeScript
import { describe, expect, it, vi } from "vite-plus/test";
|
|
|
|
import {
|
|
resolveExternalWebLinkHost,
|
|
resolveExternalWebLinkHref,
|
|
showExternalLinkContextMenu,
|
|
} from "./externalLinkContextMenu";
|
|
|
|
function createHarness(
|
|
selection:
|
|
| "open-in-preview"
|
|
| "open-external"
|
|
| "copy-link"
|
|
| "link-to-thread"
|
|
| "unlink-from-thread"
|
|
| null,
|
|
) {
|
|
const showContextMenu = vi.fn().mockResolvedValue(selection);
|
|
const openInPreview = vi.fn().mockResolvedValue(undefined);
|
|
const openExternal = vi.fn().mockResolvedValue(undefined);
|
|
const copyLink = vi.fn().mockResolvedValue(undefined);
|
|
const updateThreadLink = vi.fn().mockResolvedValue(undefined);
|
|
const reportFailure = vi.fn();
|
|
|
|
return {
|
|
showContextMenu,
|
|
openInPreview,
|
|
openExternal,
|
|
copyLink,
|
|
updateThreadLink,
|
|
reportFailure,
|
|
};
|
|
}
|
|
|
|
describe("external chat link context menu", () => {
|
|
it("offers both open actions and Copy Link", async () => {
|
|
const harness = createHarness(null);
|
|
|
|
await showExternalLinkContextMenu({
|
|
href: "https://example.com/docs?topic=menus#copy",
|
|
position: { x: 12, y: 24 },
|
|
...harness,
|
|
});
|
|
|
|
expect(harness.showContextMenu).toHaveBeenCalledWith(
|
|
[
|
|
{ id: "open-in-preview", label: "Open in integrated browser" },
|
|
{ id: "open-external", label: "Open in system browser" },
|
|
{ id: "copy-link", label: "Copy Link" },
|
|
],
|
|
{ x: 12, y: 24 },
|
|
);
|
|
expect(harness.openInPreview).not.toHaveBeenCalled();
|
|
expect(harness.openExternal).not.toHaveBeenCalled();
|
|
expect(harness.copyLink).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("still offers the link's own actions where the integrated browser cannot be opened", async () => {
|
|
const harness = createHarness(null);
|
|
|
|
await showExternalLinkContextMenu({
|
|
href: "https://github.com/pingdotgg/t3code/pull/6169",
|
|
canOpenInPreview: false,
|
|
position: { x: 4, y: 8 },
|
|
...harness,
|
|
});
|
|
|
|
expect(harness.showContextMenu).toHaveBeenCalledWith(
|
|
[
|
|
{ id: "open-external", label: "Open in system browser" },
|
|
{ id: "copy-link", label: "Copy Link" },
|
|
],
|
|
{ x: 4, y: 8 },
|
|
);
|
|
});
|
|
|
|
it("copies the exact destination without opening it", async () => {
|
|
const harness = createHarness("copy-link");
|
|
const href = "https://example.com/docs?topic=menus#copy";
|
|
|
|
await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });
|
|
|
|
expect(harness.copyLink).toHaveBeenCalledWith(href);
|
|
expect(harness.openInPreview).not.toHaveBeenCalled();
|
|
expect(harness.openExternal).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each([
|
|
["link-to-thread", "Link to thread", true],
|
|
["unlink-from-thread", "Unlink from thread", false],
|
|
] as const)("offers and runs the %s action", async (action, label, linked) => {
|
|
const harness = createHarness(action);
|
|
const href = "https://github.com/pingdotgg/t3code/pull/42";
|
|
|
|
await showExternalLinkContextMenu({
|
|
href,
|
|
threadLinkAction: action,
|
|
position: { x: 1, y: 2 },
|
|
...harness,
|
|
});
|
|
|
|
expect(harness.showContextMenu).toHaveBeenCalledWith(
|
|
expect.arrayContaining([{ id: action, label }]),
|
|
{ x: 1, y: 2 },
|
|
);
|
|
expect(harness.updateThreadLink).toHaveBeenCalledWith(href, linked);
|
|
});
|
|
|
|
it.each([
|
|
["open-in-preview" as const, "openInPreview" as const],
|
|
["open-external" as const, "openExternal" as const],
|
|
])("preserves the %s action", async (selection, expectedCallback) => {
|
|
const harness = createHarness(selection);
|
|
const href = "https://example.com/docs";
|
|
|
|
await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });
|
|
|
|
expect(harness[expectedCallback]).toHaveBeenCalledWith(href);
|
|
expect(harness.copyLink).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("reports the selected action when it fails", async () => {
|
|
const harness = createHarness("copy-link");
|
|
const cause = new Error("clipboard denied");
|
|
harness.copyLink.mockRejectedValue(cause);
|
|
|
|
await showExternalLinkContextMenu({
|
|
href: "https://example.com/docs",
|
|
position: { x: 1, y: 2 },
|
|
...harness,
|
|
});
|
|
|
|
expect(harness.reportFailure).toHaveBeenCalledWith("copy-link", cause);
|
|
});
|
|
|
|
it("reports the menu operation when the native menu cannot be shown", async () => {
|
|
const harness = createHarness(null);
|
|
const cause = new Error("menu unavailable");
|
|
harness.showContextMenu.mockRejectedValue(cause);
|
|
|
|
await showExternalLinkContextMenu({
|
|
href: "https://example.com/docs",
|
|
position: { x: 1, y: 2 },
|
|
...harness,
|
|
});
|
|
|
|
expect(harness.reportFailure).toHaveBeenCalledWith("show-link-context-menu", cause);
|
|
expect(harness.openInPreview).not.toHaveBeenCalled();
|
|
expect(harness.openExternal).not.toHaveBeenCalled();
|
|
expect(harness.copyLink).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each([
|
|
["open-in-preview" as const, "openInPreview" as const, "open-link-in-preview"],
|
|
["open-external" as const, "openExternal" as const, "open-link-external"],
|
|
])("reports a failed %s action", async (selection, callback, operation) => {
|
|
const harness = createHarness(selection);
|
|
const cause = new Error("open failed");
|
|
harness[callback].mockRejectedValue(cause);
|
|
|
|
await showExternalLinkContextMenu({
|
|
href: "https://example.com/docs",
|
|
position: { x: 1, y: 2 },
|
|
...harness,
|
|
});
|
|
|
|
expect(harness.reportFailure).toHaveBeenCalledWith(operation, cause);
|
|
});
|
|
|
|
it("reports a failed thread link action", async () => {
|
|
const harness = createHarness("link-to-thread");
|
|
const cause = new Error("thread update failed");
|
|
harness.updateThreadLink.mockRejectedValue(cause);
|
|
|
|
await showExternalLinkContextMenu({
|
|
href: "https://github.com/pingdotgg/t3code/pull/42",
|
|
threadLinkAction: "link-to-thread",
|
|
position: { x: 1, y: 2 },
|
|
...harness,
|
|
});
|
|
|
|
expect(harness.reportFailure).toHaveBeenCalledWith("link-pull-request-to-thread", cause);
|
|
});
|
|
|
|
it.each([
|
|
["https://example.com", "example.com"],
|
|
["http://localhost:3000/path", "localhost"],
|
|
["//cdn.example.com/clip.mp4?signature=abc#t=2", "cdn.example.com"],
|
|
["//", null],
|
|
["#details", null],
|
|
["mailto:[email protected]", null],
|
|
["file:///tmp/example.txt", null],
|
|
["javascript:void(0)", null],
|
|
["not a URL", null],
|
|
[undefined, null],
|
|
])("resolves the external web-link host for %s as %s", (href, expected) => {
|
|
expect(resolveExternalWebLinkHost(href)).toBe(expected);
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
"https://example.com/docs?topic=security#links",
|
|
"https://example.com/docs?topic=security#links",
|
|
],
|
|
["HTTP://EXAMPLE.COM", "http://example.com/"],
|
|
["//example.com/path", "https://example.com/path"],
|
|
])("resolves the safe external web-link href for %s as %s", (href, expected) => {
|
|
expect(resolveExternalWebLinkHref(href)).toBe(expected);
|
|
});
|
|
|
|
it.each([
|
|
"javascript:alert(document.domain)",
|
|
" \nJaVaScRiPt:alert(document.domain)",
|
|
"data:text/html,<script>alert(document.domain)</script>",
|
|
"file:///tmp/example.txt",
|
|
"mailto:[email protected]",
|
|
"/relative/path",
|
|
"not a URL",
|
|
"",
|
|
])("rejects unsafe external web-link href %s", (href) => {
|
|
expect(resolveExternalWebLinkHref(href)).toBeNull();
|
|
});
|
|
});
|