Files
siftlode/frontend/src/lib/keymap.sequence.test.ts
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

167 lines
6.8 KiB
TypeScript

// @vitest-environment jsdom
//
// Two-step sequences (`g h`) and the page-level quiet rule, in their own file: the pending first step
// is module state, and a test that left one pending would turn the NEXT test's first key into its
// second step. Every test here starts a fake clock far past the previous test's, so nothing carries.
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { SHORTCUTS, dispatch, formatChord, helpLines, type ShortcutId } from "./keymap";
import { pushLayer, removeLayer } from "./layerStack";
let clock = Date.UTC(2030, 0, 1);
beforeEach(() => {
vi.useFakeTimers({ toFake: ["Date"] });
clock += 3_600_000;
vi.setSystemTime(clock);
});
afterEach(() => vi.useRealTimers());
function press(init: KeyboardEventInit, on?: HTMLElement): KeyboardEvent {
const e = new KeyboardEvent("keydown", { bubbles: true, cancelable: true, ...init });
(on ?? document.body).dispatchEvent(e);
return e;
}
/** Every page row's handler, recording which one ran. */
function pageHandlers(calls: string[]) {
return Object.fromEntries(
SHORTCUTS.filter((s) => s.scope === "page").map((s) => [s.id, () => void calls.push(s.id)]),
) as { readonly [K in ShortcutId<"page">]: () => void };
}
const listHandlers = (calls: string[]) =>
({
next: () => calls.push("next"),
prev: () => calls.push("prev"),
left: () => {},
right: () => {},
up: () => {},
down: () => {},
open: () => {},
}) as const;
describe("two-step sequences", () => {
it("runs the row once both steps arrive, and not on the first alone", () => {
const calls: string[] = [];
expect(dispatch(press({ key: "g" }), "page", pageHandlers(calls))).toBe(true);
expect(calls).toEqual([]);
expect(dispatch(press({ key: "f" }), "page", pageHandlers(calls))).toBe(true);
expect(calls).toEqual(["goFeed"]);
});
// Every scope has its own window listener, so the `h` completing `g h` reaches the global one too.
it("keeps the completing key from every other scope, whichever listener runs first", () => {
const calls: string[] = [];
const global = {
help: () => calls.push("help"),
palette: () => {},
home: () => {},
};
// another scope BEFORE the owner: the pending step belongs to `page`, so it stands aside
dispatch(press({ key: "g" }), "page", pageHandlers(calls));
const h = press({ key: "h" });
expect(dispatch(h, "global", global)).toBe(false);
expect(dispatch(h, "page", pageHandlers(calls))).toBe(true);
expect(calls).toEqual(["goHome"]);
// another scope AFTER the owner: the owner stamped the event
dispatch(press({ key: "g" }), "page", pageHandlers(calls));
const j = press({ key: "j" }); // breaks the sequence: no `g j`
expect(dispatch(j, "page", pageHandlers(calls))).toBe(false);
expect(dispatch(j, "list", listHandlers(calls))).toBe(false);
expect(calls).toEqual(["goHome"]);
});
it("gives up after the window, so a late key is an ordinary key again", () => {
const calls: string[] = [];
dispatch(press({ key: "g" }), "page", pageHandlers(calls));
vi.setSystemTime(clock + 1_600);
expect(dispatch(press({ key: "j" }), "list", listHandlers(calls))).toBe(true);
expect(dispatch(press({ key: "f" }), "page", pageHandlers(calls))).toBe(false);
expect(calls).toEqual(["next"]);
});
it("is not broken by reaching for a modifier between the steps", () => {
const calls: string[] = [];
dispatch(press({ key: "g" }), "page", pageHandlers(calls));
dispatch(press({ key: "Shift", shiftKey: true }), "page", pageHandlers(calls));
dispatch(press({ key: "d" }), "page", pageHandlers(calls));
expect(calls).toEqual(["goDownloads"]);
});
it("does not start or finish inside a text field", () => {
const calls: string[] = [];
const input = document.createElement("input");
document.body.append(input);
expect(dispatch(press({ key: "g" }, input), "page", pageHandlers(calls))).toBe(false);
dispatch(press({ key: "g" }), "page", pageHandlers(calls));
expect(dispatch(press({ key: "f" }, input), "page", pageHandlers(calls))).toBe(false);
expect(calls).toEqual([]);
input.remove();
});
});
describe("page-level scopes under a layer", () => {
it("are quiet while a keyboard-owning layer is open", () => {
const calls: string[] = [];
const id = pushLayer();
expect(dispatch(press({ key: "j" }), "list", listHandlers(calls))).toBe(false);
expect(dispatch(press({ key: "s" }), "search", { focus: () => calls.push("s") })).toBe(false);
dispatch(press({ key: "g" }), "page", pageHandlers(calls));
dispatch(press({ key: "f" }), "page", pageHandlers(calls));
removeLayer(id);
expect(calls).toEqual([]);
});
// The layer's OWN keys must keep working — that is the player the page keys are standing aside for.
it("leave the layer's own scope alone", () => {
const calls: string[] = [];
const id = pushLayer();
expect(
dispatch(press({ key: "k" }), "plexPlayer", { playPause: () => calls.push("pp") } as never),
).toBe(true);
removeLayer(id);
expect(calls).toEqual(["pp"]);
});
it("keep working under a passive layer (an open tooltip)", () => {
const calls: string[] = [];
const id = pushLayer({ passive: true });
expect(dispatch(press({ key: "j" }), "list", listHandlers(calls))).toBe(true);
removeLayer(id);
expect(calls).toEqual(["next"]);
});
});
describe("the page-level rows", () => {
// Owner rule D1, enforced rather than commented: on the Hungarian layout the owner types on, a
// chord with a modifier (or `/`, `?`, which ARE Shift chords there) is not a quick key.
it("need no modifier and no Shift-only character", () => {
for (const s of SHORTCUTS) {
if (!["page", "search", "list"].includes(s.scope)) continue;
for (const chord of s.keys) {
for (const step of chord.split(" ")) {
expect(step.includes("+"), `${s.scope}:${s.id} ${chord}`).toBe(false);
expect(/^[a-z0-9,.-]$|^(Enter|Escape|Arrow(Up|Down|Left|Right))$/.test(step), chord).toBe(
true,
);
}
}
}
});
it("render as the keys to press, step by step", () => {
expect(formatChord("g h")).toBe("G H");
expect(formatChord("g ,")).toBe("G ,");
});
// `g h` shares Home's line with Shift+W; leaving out an unreachable chord must not take that with it.
it("drop an unreachable module's chord but keep the line it shares", () => {
const lines = helpLines(["page", "global"], ["home", "feed"]);
expect(lines.find((l) => l.labelKey === "keys.home")?.chords).toEqual(["g h", "Shift+w"]);
expect(lines.some((l) => l.labelKey === "keys.goFeed")).toBe(true);
expect(lines.some((l) => l.labelKey === "keys.goPlex")).toBe(false);
expect(
helpLines(["page", "global"], []).find((l) => l.labelKey === "keys.home")?.chords,
).toEqual(["Shift+w"]);
});
});