Files
siftlode/frontend/src/lib/globalKeymap.dom.test.tsx
T
peter 8fb276aa90 feat(keys): j/k walk the feed, s finds the search box, g jumps to a module
A feed card cost ~10 Tab stops, so the tenth card sat ~135 Tabs from the top of the page, and the
only keyboard focus ring (a 60% accent outline on the thumbnail) all but vanished on the dark glass.
First slice of the E11 keyboard layer (S4a); Home, Plex and the tables follow in S4b/S4c.

- `lib/listNav`: one window listener over every registered list, walked in document order. `j`/`k`
  step item to item (on into the next list — owner decision D4), the arrows move around a grid, Enter
  clicks the item's own `data-nav-open` element, so it opens exactly like a click. An item root is
  never a Tab stop: it gets `tabindex=-1` only while the keyboard is on it, and keeps it until the next
  item takes it, so the player's focus restore lands back on the card. The first `j` takes the first
  card you can see, not the top of the list.
- `VirtualGrid` takes part with `keyboardNav` (column count + scrolling an unmounted row in);
  `VirtualFeed` turns it on, so the feed, the library and the channel page all have it.
- Keymap: two-step sequences (`g h`) whose pending step belongs to the scope that started it; the
  page-level scopes (`page`, `search`, `list`) are quiet while a layer that owns the keyboard is open,
  enforced in `dispatch`. A test pins the owner's rule that none of them needs a modifier on the
  Hungarian layout.
- `g h/f/c/p/x/n/m/d/s/,` jump to a module this account can reach; `s` focuses the header search box
  and selects the old query; Escape in it hands the keyboard back to the page (Plex's typeahead spends
  its first Escape on its panel).
- Hover popovers are passive layers: an open tooltip still takes Escape first but no longer silences
  the page keys.
- Focus ring (owner decision D2 = B): solid 2px accent around the whole item; the app-wide button/
  link/row ring goes from 60% to solid too. The item keeps a transparent resting outline because the
  video card transitions `all` — without it every `j` flashed a 3px white ring (measured in Chrome).
- Help sheet: the new keys, in a fixed order after the surface's own; a `g` chord only for a reachable
  module; opened over a player or dialog it leaves out the page-level keys and the context-menu key,
  which nothing can reach there (the latter pre-existing since E10).
- CLAUDE.md: a canonical-patterns row for list/grid keys (`useListNav` + `navItemAttrs` +
  `data-nav-open`, not a private arrow-key handler).
2026-09-13 04:16:20 +02:00

175 lines
6.2 KiB
TypeScript

// @vitest-environment jsdom
//
// The `global` scope's LIVE behaviour, as opposed to `matches`/`dispatch` in isolation (keymap.test.ts
// covers those against a handler map the test supplies itself, which cannot see the real handlers).
//
// Every assertion here pins a fix that E10 review round 2 proved was un-pinned: reverting the guard
// each one covers left the whole suite green, which is how a fix round quietly becomes the next
// round's source of regressions.
import { useEffect } from "react";
import { describe, it, expect, afterEach, vi } from "vitest";
import { render, cleanup, act } from "@testing-library/react";
import {
closeKeyboardHelp,
closePalette,
useGlobalKeymap,
useKeyboardHelpOpen,
usePaletteOpen,
} from "./keymap";
vi.mock("react-i18next", async (importOriginal) => {
const actual = await importOriginal<typeof import("react-i18next")>();
return { ...actual, useTranslation: () => ({ t: (k: string) => k }) };
});
afterEach(() => {
cleanup();
closeKeyboardHelp();
closePalette();
});
/** Renders the live stores so an assertion reads what a real consumer would render. */
function Host({
enabled = true,
onState,
onHome = () => {},
}: {
enabled?: boolean;
onState: (s: string) => void;
onHome?: () => void;
}) {
useGlobalKeymap(enabled, (page) => page === "home" && onHome(), ["home"]);
const help = useKeyboardHelpOpen();
const palette = usePaletteOpen();
useEffect(() => {
onState(`${help ? "help" : ""}${palette ? "palette" : ""}` || "none");
}, [help, palette, onState]);
return null;
}
const key = (init: KeyboardEventInit) =>
act(() => {
window.dispatchEvent(
new KeyboardEvent("keydown", { bubbles: true, cancelable: true, ...init }),
);
});
describe("useGlobalKeymap", () => {
it("opens the cheat sheet on h and the palette on Mod+K", () => {
let state = "";
render(<Host onState={(s) => (state = s)} />);
key({ key: "h" });
expect(state).toBe("help");
key({ key: "h" });
expect(state).toBe("none");
key({ key: "k", ctrlKey: true });
expect(state).toBe("palette");
});
// Mod+K is written as a toggle, and the palette autofocuses its own search field — so without
// `allowInInput` on the row the second press was eaten by the typing guard and Chrome took the
// chord instead (round 1). The FIRST press must still be routed from a field, too.
it("toggles the palette shut from inside a text field", () => {
let state = "";
render(<Host onState={(s) => (state = s)} />);
const field = document.createElement("input");
document.body.appendChild(field);
field.focus();
act(() => {
field.dispatchEvent(
new KeyboardEvent("keydown", { key: "k", ctrlKey: true, bubbles: true, cancelable: true }),
);
});
expect(state).toBe("palette");
act(() => {
field.dispatchEvent(
new KeyboardEvent("keydown", { key: "k", ctrlKey: true, bubbles: true, cancelable: true }),
);
});
expect(state).toBe("none");
field.remove();
});
// `h` still belongs to whatever is being typed.
it("leaves a plain letter to the text field it was typed into", () => {
let state = "";
render(<Host onState={(s) => (state = s)} />);
const field = document.createElement("input");
document.body.appendChild(field);
field.focus();
act(() => {
field.dispatchEvent(
new KeyboardEvent("keydown", { key: "h", bubbles: true, cancelable: true }),
);
});
expect(state).toBe("none");
field.remove();
});
// Each toggle mounts/unmounts an overlay that costs a real history entry, so ~30 of them a second
// desyncs the overlay bookkeeping from the browser's actual stack. Holding a key is not a press.
it("ignores auto-repeat", () => {
let state = "";
render(<Host onState={(s) => (state = s)} />);
key({ key: "h", repeat: true });
expect(state).toBe("none");
key({ key: "k", ctrlKey: true, repeat: true });
expect(state).toBe("none");
});
// E24. The chord is the point: the Plex player binds bare letters and `w` sits on top of `s`, so a
// bare `w` would yank a viewer out of the film on a blind reach for the subtitle key.
it("jumps Home on Shift+W and ignores a bare w", () => {
const onHome = vi.fn();
render(<Host onState={() => {}} onHome={onHome} />);
key({ key: "w" });
expect(onHome).not.toHaveBeenCalled();
key({ key: "w", shiftKey: true });
expect(onHome).toHaveBeenCalledTimes(1);
});
it("leaves Shift+W to the text field it was typed into", () => {
const onHome = vi.fn();
render(<Host onState={() => {}} onHome={onHome} />);
const field = document.createElement("input");
document.body.appendChild(field);
field.focus();
act(() => {
field.dispatchEvent(
new KeyboardEvent("keydown", { key: "w", shiftKey: true, bubbles: true, cancelable: true }),
);
});
expect(onHome).not.toHaveBeenCalled();
field.remove();
});
// The handler is App's `() => setPage("home")` — a fresh closure every render. It is held in a ref
// rather than in the effect's deps, so the listener is bound once; this pins that the ref is the
// CURRENT one and not the closure captured at mount.
it("calls the latest handler after a re-render, without rebinding", () => {
const first = vi.fn();
const second = vi.fn();
const { rerender } = render(<Host onState={() => {}} onHome={first} />);
rerender(<Host onState={() => {}} onHome={second} />);
key({ key: "w", shiftKey: true });
expect(first).not.toHaveBeenCalled();
expect(second).toHaveBeenCalledTimes(1);
});
// Bound above the branch that mounts the overlays, the keys were swallowed on the loading/Welcome
// screens and the flipped store then sprang the overlay open by itself once the shell mounted.
it("binds nothing, and clears a stale store, while disabled", () => {
let state = "";
const { rerender } = render(<Host onState={(s) => (state = s)} />);
key({ key: "h" });
expect(state).toBe("help");
rerender(<Host enabled={false} onState={(s) => (state = s)} />);
expect(state).toBe("none"); // the stale `true` is cleared, not carried into the next mount
key({ key: "h" });
expect(state).toBe("none"); // and the listener is gone
});
});