A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

dbbasic-browser/apps/browser/test/context-menu.test.ts
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
context-menu.test.ts · 5.3 KB · 81 lines
1import { describe, expect, it } from "vitest";
2import { clipLabel, contextMenuItems, type ClickedOn, type MenuContext } from "../src/context-menu.js";
3
4const page: MenuContext = { page: true, canGoBack: true, canGoForward: false, newTabs: true };
5const ids = (p: ClickedOn, c: MenuContext = page) => contextMenuItems(p, c).map((it) => ("id" in it ? it.id : "—"));
6
7describe("right-click menu", () => {
8 it("on a link: open in a new tab and copy its address", () => {
9 expect(ids({ linkURL: "https://example.com/a" })).toEqual(["open-link-tab", "copy-link", "—", "inspect"]);
10 });
11
12 it("a word of the link selected by the right-click itself is not offered as a selection", () => {
13 expect(ids({ linkURL: "https://example.com/a", linkText: "a link to open", selectionText: "link" })).toEqual(["open-link-tab", "copy-link", "—", "inspect"]);
14 expect(ids({ linkURL: "https://example.com/a", linkText: "a link", selectionText: "text chosen beforehand" })).toEqual(["open-link-tab", "copy-link", "—", "copy", "search-selection", "ask-selection", "—", "inspect"]);
15 });
16
17 it("a link that is not a web address can be copied but not opened", () => {
18 expect(ids({ linkURL: "mailto:a@example.com" })).toEqual(["copy-link", "—", "inspect"]);
19 expect(ids({ linkURL: "javascript:alert(1)" })).toEqual(["back", "forward", "reload", "—", "print", "save-pdf", "—", "inspect"]);
20 });
21
22 it("no new tabs while new tabs are off", () => {
23 expect(ids({ linkURL: "https://example.com/" }, { ...page, newTabs: false })).toEqual(["copy-link", "—", "inspect"]);
24 expect(ids({ selectionText: "hello" }, { ...page, newTabs: false })).toEqual(["copy", "ask-selection", "—", "inspect"]);
25 });
26
27 it("on an image: open, copy, copy address, save", () => {
28 expect(ids({ mediaType: "image", srcURL: "https://example.com/p.png" })).toEqual(["open-image-tab", "copy-image", "copy-image-address", "save-image", "—", "inspect"]);
29 expect(ids({ mediaType: "image", srcURL: "data:image/png;base64,AAAA" })).toEqual(["copy-image", "—", "inspect"]);
30 });
31
32 it("an image inside a link gets both", () => {
33 expect(ids({ linkURL: "https://example.com/", mediaType: "image", srcURL: "https://example.com/p.png" })).toEqual(["open-link-tab", "copy-link", "—", "open-image-tab", "copy-image", "copy-image-address", "save-image", "—", "inspect"]);
34 });
35
36 it("on selected text: copy, search for it, ask the assistant", () => {
37 const items = contextMenuItems({ selectionText: " the quick brown fox jumps over the lazy dog " }, page);
38 expect(items.map((it) => ("id" in it ? it.id : "—"))).toEqual(["copy", "search-selection", "ask-selection", "—", "inspect"]);
39 expect(items.find((it) => "id" in it && it.id === "search-selection")).toMatchObject({ label: "Search askrobots for “the quick brown fox jum…”" });
40 });
41
42 it("in a text field: the edit items, enabled as the field says", () => {
43 const items = contextMenuItems({ isEditable: true, editFlags: { canUndo: false, canRedo: false, canCut: false, canCopy: false, canPaste: true, canSelectAll: true } }, page);
44 expect(items.map((it) => ("id" in it ? `${it.id}${it.enabled === false ? "(off)" : ""}` : "—"))).toEqual(["undo(off)", "redo(off)", "—", "cut(off)", "copy(off)", "paste", "select-all", "—", "inspect"]);
45 });
46
47 it("a misspelled word offers its suggestions first", () => {
48 expect(ids({ isEditable: true, misspelledWord: "teh", dictionarySuggestions: ["the", "ten", "tea", "tech", "teak", "team"] })[0]).toBe("spell:0");
49 expect(ids({ isEditable: true, misspelledWord: "teh", dictionarySuggestions: ["the", "ten", "tea", "tech", "teak", "team"] }).filter((i) => i.startsWith("spell:"))).toHaveLength(5);
50 expect(contextMenuItems({ isEditable: true, misspelledWord: "zxqv", dictionarySuggestions: [] }, page)[0]).toMatchObject({ id: "no-suggestions", enabled: false });
51 });
52
53 it("anywhere else on a page: back, forward, reload, then print and save as PDF", () => {
54 const items = contextMenuItems({}, page);
55 expect(items.map((it) => ("id" in it ? `${it.id}${it.enabled === false ? "(off)" : ""}` : "—"))).toEqual(["back", "forward(off)", "reload", "—", "print", "save-pdf", "—", "inspect"]);
56 });
57
58 it("the browser's own fields get only the text items", () => {
59 const own: MenuContext = { page: false, canGoBack: false, canGoForward: false, newTabs: true };
60 expect(ids({ isEditable: true, linkURL: "https://example.com/" }, own)).toEqual(["undo", "redo", "—", "cut", "copy", "paste", "select-all"]);
61 expect(ids({ selectionText: "a label" }, own)).toEqual(["copy"]);
62 expect(ids({}, own)).toEqual([]);
63 });
64
65 it("never starts or ends with a separator, or has two in a row", () => {
66 for (const p of [{}, { linkURL: "https://x.example/" }, { selectionText: "x" }, { isEditable: true }, { mediaType: "image", srcURL: "https://x.example/i.png" }]) {
67 for (const c of [page, { ...page, page: false }]) {
68 const s = ids(p, c).join(",");
69 expect(s.startsWith("—")).toBe(false);
70 expect(s.endsWith("—")).toBe(false);
71 expect(s.includes("—,—")).toBe(false);
72 }
73 }
74 });
75
76 it("clips long selections for the label", () => {
77 expect(clipLabel("short")).toBe("short");
78 expect(clipLabel("a\nb\tc")).toBe("a b c");
79 expect(clipLabel("x".repeat(40), 10)).toBe("xxxxxxxxx…");
80 });
81});