fix(ui): the palette ranks what you typed, and lib stops leaning on components

Typing "theme" put the channel "The Meltzone Podcast" (matched only through its handle
`@themeltzone`) above every "Theme: …" row, and Enter opened the channel — the palette listed
matches in pure group order and always highlighted the first one.

- CommandPalette ranks each word: label starts with it > elsewhere in the label > only the hint,
  keywords or group name; an item ranks by its worst word, then the sum. The group order stays
  fixed, rows are ranked inside each group, and the best row overall starts highlighted. The
  highlight is held by id and follows the best match until the arrows or the pointer pick a row,
  so a channel list landing mid-typing no longer shifts it.
- A11y: result groups are `role="group"` named by their heading; `aria-expanded` is false with
  nothing to choose; the listbox stays mounted (hidden) so `aria-controls` never names a missing
  element, and "Nothing matches" sits outside it; a polite status announces the count once the
  typing pauses.
- `Column`/`FilterMap`/`filterRows`/`isActive` move from `ui/tableHeader` to `lib/tableColumns`,
  and the player's auto/loop modes from `playerModalControls` to `lib/playerModes` — three lib
  helpers imported the column contract from a ui component, and `playerModalHooks` its types from a
  feature component.
- eslint: `lib/` may not import from `components/` at any depth, type-only and re-exports included;
  the boundary contract test proves it fires. CLAUDE.md and the ui README say so.
- keymap: `dispatch`'s docblock sat stacked on `matchesShortcut`'s; moved onto `dispatch`.
This commit is contained in:
2026-09-13 02:14:07 +02:00
parent 8a50538275
commit 78a9324381
25 changed files with 418 additions and 167 deletions
@@ -5,7 +5,7 @@
// (no i18n, no domain), so this asserts the shell only. i18n is key-passthrough.
import { useState } from "react";
import { describe, it, expect, vi, afterEach, beforeAll } from "vitest";
import { render, screen, fireEvent, cleanup } from "@testing-library/react";
import { render, screen, fireEvent, cleanup, waitFor } from "@testing-library/react";
import CommandPalette, { type CommandGroup } from "./CommandPalette";
vi.mock("react-i18next", async (importOriginal) => {
@@ -160,3 +160,105 @@ describe("CommandPalette", () => {
expect(ran).toEqual(["closed"]);
});
});
// The owner's case (E11 S3): typing "theme" put a channel matched only through its handle above every
// "Theme: …" row, and Enter opened the channel.
describe("CommandPalette · ranking", () => {
const noop = () => {};
const ranked = (): CommandGroup[] => [
{
id: "channels",
label: "Channels",
items: [
{ id: "ch:melt", label: "The Meltzone Podcast", hint: "@themeltzone", run: noop },
{ id: "ch:talk", label: "Talk about themes", run: noop },
],
},
{
id: "actions",
label: "Actions",
items: [
{ id: "theme:midnight", label: "Theme: Midnight", run: noop },
{ id: "theme:forest", label: "Theme: Forest", run: noop },
],
},
];
it("ranks rows inside each group, keeps the group order, and starts on the best match", () => {
render(<CommandPalette open onClose={noop} groups={ranked()} />);
type("theme");
// label elsewhere (talk) > handle only (melt); label start (midnight = forest, group order kept)
expect(rows()).toEqual(["ch:talk", "ch:melt", "theme:midnight", "theme:forest"]);
// The highlight is what Enter takes (same `activeIndex`); the full close→run cycle cannot run in
// this file — see the note on "chooses the selected row on Enter".
expect(selected()).toBe("theme:midnight");
});
it("ranks by the worst word first, so every word in the label beats a label start plus a keyword", () => {
const groups: CommandGroup[] = [
{
id: "channels",
label: "Channels",
items: [
{ id: "weekly", label: "Podcast Weekly", keywords: "melt", run: noop },
{ id: "melt", label: "The Meltzone Podcast", run: noop },
],
},
];
render(<CommandPalette open onClose={noop} groups={groups} />);
type("podcast melt");
expect(rows()).toEqual(["melt", "weekly"]);
});
// The channel and playlist lists load after the palette opens — often while you are already typing.
it("follows the best match as a list lands, but keeps a row the arrows picked", () => {
const early: CommandGroup[] = [ranked()[0]!];
const r = render(<CommandPalette open onClose={noop} groups={early} />);
type("theme");
expect(selected()).toBe("ch:talk");
r.rerender(<CommandPalette open onClose={noop} groups={ranked()} />);
expect(selected()).toBe("theme:midnight");
press("ArrowDown"); // theme:forest, picked
r.rerender(<CommandPalette open onClose={noop} groups={[...ranked()]} />);
expect(selected()).toBe("theme:forest");
type("them"); // a new query goes back to following the best match
expect(selected()).toBe("theme:midnight");
});
});
describe("CommandPalette · accessibility", () => {
it("names each group by its heading", () => {
open();
const channels = screen.getByRole("group", { name: "Channels" });
expect(
[...channels.querySelectorAll("[role='option']")].map((o) => o.getAttribute("data-cmd-id")),
).toEqual(["ch:1", "ch:2"]);
expect(screen.getByRole("group", { name: "Modules" })).toBeTruthy();
});
it("is expanded only while there is something to choose, and says 'nothing' outside the listbox", () => {
open();
expect(field().getAttribute("aria-expanded")).toBe("true");
type("zzzz");
expect(field().getAttribute("aria-expanded")).toBe("false");
const listbox = screen.getByRole("listbox", { hidden: true });
expect(listbox.hidden).toBe(true);
// aria-controls still names a real element
expect(document.getElementById(field().getAttribute("aria-controls")!)).toBe(listbox);
expect(listbox.contains(screen.getByText("palette.empty"))).toBe(false);
});
it("announces the result count politely, once the typing pauses", async () => {
open();
const status = screen.getByRole("status");
expect(status.textContent).toBe(""); // nothing to announce on a blank field
type("the");
expect(status.textContent).toBe(""); // not per keystroke
await waitFor(() => expect(status.textContent).toBe("palette.results"));
type("zzzz");
await waitFor(() => expect(status.textContent).toBe("palette.empty"));
type("");
expect(status.textContent).toBe("");
});
});