// @vitest-environment jsdom import { describe, it, expect } from "vitest"; import { DEFAULT_BINDINGS, findConflict, formatKeybinding, getActiveBindings, keybindingFromEvent, matchKeybinding, } from "./shortcuts"; function ev(key: string, mods: { ctrl?: boolean; meta?: boolean; shift?: boolean; alt?: boolean } = {}): KeyboardEvent { return new KeyboardEvent("keydown", { key, ctrlKey: Boolean(mods.ctrl), metaKey: Boolean(mods.meta), shiftKey: Boolean(mods.shift), altKey: Boolean(mods.alt), }); } describe("matchKeybinding", () => { it("matches case-insensitively on the key letter", () => { expect(matchKeybinding(ev("c"), { key: "c" })).toBe(true); expect(matchKeybinding(ev("C", { shift: true }), { key: "c", shift: true })).toBe(true); }); it("requires every modifier to match exactly", () => { expect(matchKeybinding(ev("l", { ctrl: true }), { key: "l", ctrl: true })).toBe(true); expect(matchKeybinding(ev("l"), { key: "l", ctrl: true })).toBe(false); expect(matchKeybinding(ev("l", { ctrl: true, shift: true }), { key: "l", ctrl: true })).toBe(false); }); it("rejects when key differs", () => { expect(matchKeybinding(ev("c"), { key: "n" })).toBe(false); }); }); describe("getActiveBindings", () => { it("returns full default map when no overrides", () => { const active = getActiveBindings(undefined); expect(active.copy_password).toEqual(DEFAULT_BINDINGS.copy_password); expect(Object.keys(active).length).toBe(Object.keys(DEFAULT_BINDINGS).length); }); it("user override replaces default for that action only", () => { const active = getActiveBindings({ copy_password: { key: "p", ctrl: true } }); expect(active.copy_password).toEqual({ key: "p", ctrl: true }); expect(active.copy_username).toEqual(DEFAULT_BINDINGS.copy_username); }); }); describe("keybindingFromEvent", () => { it("returns null for bare modifier presses", () => { expect(keybindingFromEvent(ev("Shift"))).toBeNull(); expect(keybindingFromEvent(ev("Control"))).toBeNull(); expect(keybindingFromEvent(ev("Meta"))).toBeNull(); expect(keybindingFromEvent(ev("Alt"))).toBeNull(); }); it("captures key + modifiers from the event", () => { const b = keybindingFromEvent(ev("k", { ctrl: true, shift: true })); expect(b).toEqual({ key: "k", ctrl: true, shift: true }); }); it("omits unset modifier flags rather than storing false", () => { const b = keybindingFromEvent(ev("a")); expect(b).toEqual({ key: "a" }); }); }); describe("formatKeybinding", () => { it("uses Ctrl+K style on non-mac", () => { expect(formatKeybinding({ key: "k", ctrl: true }, false)).toBe("Ctrl+K"); }); it("uses ⌘ glyph on mac", () => { expect(formatKeybinding({ key: "k", meta: true }, true)).toBe("⌘K"); }); it("renames named keys (arrows, escape, enter)", () => { expect(formatKeybinding({ key: "ArrowUp" }, false)).toBe("↑"); expect(formatKeybinding({ key: "Escape" }, false)).toBe("Esc"); expect(formatKeybinding({ key: "Enter" }, false)).toBe("↵"); expect(formatKeybinding({ key: " " }, false)).toBe("Space"); }); it("orders mac modifiers ⌃⌥⇧⌘ then key", () => { expect(formatKeybinding({ key: "k", ctrl: true, alt: true, shift: true, meta: true }, true)) .toBe("⌃⌥⇧⌘K"); }); }); describe("findConflict", () => { it("returns the conflicting action when binding collides", () => { const bindings = getActiveBindings({}); const conflict = findConflict(bindings, { key: "c" }); expect(conflict).toBe("copy_password"); }); it("excludes the action being rebound from conflict checks", () => { const bindings = getActiveBindings({}); const conflict = findConflict(bindings, { key: "c" }, "copy_password"); expect(conflict).toBeNull(); }); it("returns null when binding is unique", () => { const bindings = getActiveBindings({}); const conflict = findConflict(bindings, { key: "z", ctrl: true, shift: true }); expect(conflict).toBeNull(); }); });