Files
siftlode/frontend/src/components/ui/KeyboardHelp.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

183 lines
6.0 KiB
TypeScript

// @vitest-environment jsdom
//
// Contract of the app-wide cheat sheet: `h` opens it, it lists the SCOPES that are live (innermost
// first, global last), and it hides a `when` row whose condition the surface has not published.
// i18n is key-passthrough, so the asserted text is the i18n key.
import { describe, it, expect, vi, afterEach } from "vitest";
import { render, screen, fireEvent, cleanup, act } from "@testing-library/react";
import KeyboardHelp from "./KeyboardHelp";
import { useContextMenu } from "./useContextMenu";
import {
closeKeyboardHelp,
useGlobalKeymap,
useHelpConditions,
useKeyScope,
type KeyScope,
} from "../../lib/keymap";
import { pushLayer, removeLayer } from "../../lib/layerStack";
vi.mock("react-i18next", async (importOriginal) => {
const actual = await importOriginal<typeof import("react-i18next")>();
return { ...actual, useTranslation: () => ({ t: (k: string) => k }) };
});
// The open flag is module state shared by the whole app (one sheet, opened from anywhere), so a
// test that leaves it open would flip the NEXT test's toggle into a close.
afterEach(() => {
cleanup();
closeKeyboardHelp();
});
/** App's job: it binds the `global` scope (h / Mod+K / Shift+W) and the `page` scope (the `g` chords)
* once for the whole tree. Home is the one module this stand-in account reaches, so the page block
* is the single `G H` chord, sharing Shift+W's line. */
function Host({ children }: { children?: React.ReactNode }) {
useGlobalKeymap(true, () => {}, ["home"]);
return (
<>
{children}
<KeyboardHelp />
</>
);
}
/** A stand-in for a surface that registers a scope while mounted, the way the players do. */
function Surface({ episode, scope = "plexPlayer" }: { episode?: boolean; scope?: KeyScope }) {
useKeyScope(scope);
useHelpConditions({ episode: !!episode });
return null;
}
const pressH = () =>
act(() => {
fireEvent.keyDown(document.body, { key: "h" });
});
const descriptions = () => screen.getAllByRole("definition").map((el) => el.textContent);
describe("KeyboardHelp", () => {
it("opens and closes on the global h", () => {
render(<Host />);
expect(screen.queryByText("keys.title")).toBeNull();
pressH();
expect(screen.getByText("keys.title")).toBeTruthy();
pressH();
expect(screen.queryByText("keys.title")).toBeNull();
});
it("does not open while a field has focus", () => {
render(<Host />);
const input = document.createElement("input");
document.body.append(input);
act(() => {
fireEvent.keyDown(input, { key: "h" });
});
expect(screen.queryByText("keys.title")).toBeNull();
input.remove();
});
it("leads with the live surface and trails with the app-wide keys", () => {
render(
<Host>
<Surface />
</Host>,
);
pressH();
const rows = descriptions();
// A plexPlayer row first, the app-wide rows last — the reading order the reader wants. (Home's
// line comes from the page block, which sits between the two; Shift+W joins it there.)
expect(rows[0]).toBe("keys.playPause");
expect(rows.slice(-3)).toEqual(["keys.home", "keys.palette", "keys.help"]);
});
it("lists only the app-wide keys when no surface is registered", () => {
render(<Host />);
pressH();
expect(descriptions()).toEqual(["keys.home", "keys.palette", "keys.help"]);
});
// The context menu is NOT app-wide: it has its own scope, registered by `useContextMenu`, so the
// sheet only offers Shift+F10 where something actually answers it. Advertising it globally was a
// promise the feed, Plex, Downloads and Settings did not keep (E10 review round 1) — the test above
// pins the other half, that a bare app lists the two global keys and nothing else.
// Through the REAL hook, not a scope stub: round 2 showed that commenting out
// `useKeyScope("contextMenu")` inside `useContextMenu` left the whole suite green, because the
// only coverage registered the scope by hand. This binds the sheet to the thing that owns it.
it("offers the context-menu key on a surface that has one, and not otherwise", () => {
function WithMenu() {
useContextMenu<string>();
return null;
}
const { unmount } = render(
<Host>
<WithMenu />
</Host>,
);
pressH();
expect(descriptions()).toContain("keys.contextMenu");
unmount();
cleanup();
closeKeyboardHelp();
render(<Host />);
pressH();
expect(descriptions()).not.toContain("keys.contextMenu");
});
// Opened over a player, Shift+F10 has nothing to reach — focus is inside the player, not on a row.
it("drops the context-menu key from a sheet opened over a keyboard-owning layer", () => {
function WithMenu() {
useContextMenu<string>();
return null;
}
render(
<Host>
<WithMenu />
</Host>,
);
const id = pushLayer();
pressH();
removeLayer(id);
expect(descriptions()).not.toContain("keys.contextMenu");
expect(descriptions()).toContain("keys.help");
});
// Two live context menus (the Playlists page mounts one, its rail another) must not double the row.
it("lists the context-menu chords once however many surfaces register", () => {
function WithMenu() {
useContextMenu<string>();
return null;
}
render(
<Host>
<WithMenu />
<WithMenu />
</Host>,
);
pressH();
expect(descriptions().filter((d) => d === "keys.contextMenu")).toHaveLength(1);
expect(screen.getAllByText("⇧ F10")).toHaveLength(1);
});
// "Previous / next episode" inside a movie would be a promise the player does not keep.
it("hides a conditional row until its surface says it applies", () => {
const { unmount } = render(
<Host>
<Surface />
</Host>,
);
pressH();
expect(descriptions()).not.toContain("keys.episode");
unmount();
closeKeyboardHelp(); // the sheet outlives the tree it was opened from
render(
<Host>
<Surface episode />
</Host>,
);
pressH();
expect(descriptions()).toContain("keys.episode");
});
});