import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { formatShortcut, initializeShortcut, renderStaticShortcuts } from "../options/shortcut.js"; function setup(commands, os) { const elements = Object.fromEntries([ "shortcut-intro", "shortcut-intro-key", "shortcut-keycaps", "shortcut-current", "change-shortcut", ].map((id) => [id, Object.assign(new EventTarget(), { textContent: "", hidden: true, children: [], replaceChildren(...children) { this.children = children; }, })])); const document = Object.assign(new EventTarget(), { visibilityState: "visible", getElementById: (id) => elements[id], createElement: (tagName) => ({ tagName, className: "", textContent: "" }), }); const window = new EventTarget(); const ready = initializeShortcut({ commands, document, window, os }); return { elements, document, window, ready }; } const flush = () => new Promise((resolve) => setImmediate(resolve)); const command = (shortcut) => ({ name: "ask-chatgpt", description: "Ask ChatGPT", shortcut }); // The drawing's key caps and the "+" text between them, in order. const drawn = (element) => element.children.map((child) => child.textContent ?? child); test("shows the configured command rather than another command or the default", async () => { const { elements, ready } = setup({ getAll: async () => [ { name: "other", description: "Other", shortcut: "Alt+O" }, command("Ctrl+Shift+Y"), ] }); await ready; assert.equal(elements["shortcut-intro"].hidden, false); assert.equal(elements["shortcut-intro-key"].textContent, "++Y"); assert.equal(elements["shortcut-current"].textContent, "Current shortcut: ++Y"); assert.deepEqual(drawn(elements["shortcut-keycaps"]), ["Ctrl", "+", "Shift", "+", "Y"]); assert.ok(elements["shortcut-keycaps"].children .filter((child) => typeof child !== "string") .every(({ tagName, className }) => tagName === "kbd" && className === "drawing-key")); }); test("writes keys the way the page does, with Mac names on a Mac", () => { assert.equal(formatShortcut("Alt+Shift+G", "linux"), "++G"); assert.equal(formatShortcut("Ctrl+L", "win"), "+L"); assert.equal(formatShortcut("Enter", undefined), ""); assert.equal(formatShortcut("Ctrl+Shift+F5", "win"), "++"); // Firefox names punctuation and media keys rather than using the character. assert.equal(formatShortcut("Ctrl+Shift+Comma", "win"), "++"); assert.equal(formatShortcut("MediaPlayPause", "linux"), ""); assert.equal(formatShortcut("Alt+Shift+G", "mac"), "