askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
context-menu.test.ts · 5.2 KB · 81 lines
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { clipLabel, contextMenuItems, type ClickedOn, type MenuContext } from "../src/context-menu.js"; |
| 3 | |
| 4 | const page: MenuContext = { page: true, canGoBack: true, canGoForward: false, newTabs: true }; |
| 5 | const ids = (p: ClickedOn, c: MenuContext = page) => contextMenuItems(p, c).map((it) => ("id" in it ? it.id : "—")); |
| 6 | |
| 7 | describe("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", "—", "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", () => { |
| 54 | const items = contextMenuItems({}, page); |
| 55 | expect(items.map((it) => ("id" in it ? `${it.id}${it.enabled === false ? "(off)" : ""}` : "—"))).toEqual(["back", "forward(off)", "reload", "—", "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 | }); |