Lutra/ui/tests/commands/rank.test.ts
danny 184b8a3468 M1.8: command palette, global keymap, UI commands
- Engine: ui.registerCommands (validated, replaces the previous set),
  UI commands in listCommands with ui: true and labelKey, running one
  through the engine emits uiCommand {id, params}.
- Frontend: ui/src/commands/ (keymap by key code, fuzzy ranking, Pinia
  commands store with handlers, recents and error toasts), the command
  palette (Mod+Shift+P), UI commands palette.open, settings.open (Mod+,,
  disabled until M1.9b) and view.toggleProjectTree (Mod+1). The editor's
  shortcuts.ts moved into the global keymap.
- Tests: engine and control-channel tests for registration and
  uiCommand, Vitest for keymap, ranking, store, palette and App wiring.
- Docs: architecture, ui-parity, target-app-parity, gaps, roadmap,
  ROADMAP, README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 16:59:43 +02:00

94 lines
3.4 KiB
TypeScript

/**
* Tests of the palette ranking: the match tiers (prefix, word prefix,
* word starts, substring, in-order characters), matching on the id as
* well as the label, recently run commands first, and the order without a
* query.
*/
import { describe, expect, it } from "vitest";
import { Tier, matchText, rank, words } from "../../src/commands/rank";
/** Commands as the palette ranks them. */
const COMMANDS = [
{ id: "file.save", label: "Save" },
{ id: "file.saveAll", label: "Save All" },
{ id: "project.openDialog", label: "Open Folder…" },
{ id: "project.close", label: "Close Project" },
{ id: "buffer.close", label: "Close Tab" },
{ id: "view.toggleProjectTree", label: "Show or Hide Project" },
{ id: "palette.open", label: "Command Palette" },
{ id: "app.quit", label: "Quit" },
];
/**
* The ids of the ranking.
*
* @param query - the query.
* @param recent - recently run ids.
*/
function ids(query: string, recent: string[] = []): string[] {
return rank(COMMANDS, query, recent).map((c) => c.id);
}
describe("words", () => {
it("splits at punctuation, spaces and camelCase", () => {
expect(words("view.toggleProjectTree")).toEqual(["view", "toggle", "project", "tree"]);
expect(words("Open Folder…")).toEqual(["open", "folder"]);
});
});
describe("matchText", () => {
it("sorts matches into tiers", () => {
expect(matchText("sav", "Save All")?.tier).toBe(Tier.Prefix);
expect(matchText("all", "Save All")?.tier).toBe(Tier.WordPrefix);
expect(matchText("hide proj", "Show or Hide Project")?.tier).toBe(Tier.WordPrefix);
expect(matchText("shp", "Show or Hide Project")?.tier).toBe(Tier.WordStarts);
expect(matchText("togpro", "view.toggleProjectTree")?.tier).toBe(Tier.WordStarts);
expect(matchText("ave", "Save All")?.tier).toBe(Tier.Substring);
expect(matchText("sel", "Save All")?.tier).toBe(Tier.Fuzzy);
expect(matchText("xyz", "Save All")).toBeNull();
});
it("ignores case and surrounding spaces", () => {
expect(matchText(" SAVE ", "save all")?.tier).toBe(Tier.Prefix);
});
});
describe("rank", () => {
it("puts prefix and word-start matches before in-order characters", () => {
expect(ids("clo")).toEqual(["buffer.close", "project.close", "project.openDialog"]);
expect(ids("pro").slice(0, 3)).toEqual(["project.close", "project.openDialog", "view.toggleProjectTree"]);
expect(ids("sa")[0]).toBe("file.save");
});
it("matches the id too", () => {
expect(ids("togglepro")).toEqual(["view.toggleProjectTree"]);
expect(ids("app.quit")).toEqual(["app.quit"]);
});
it("puts recent commands first within a tier", () => {
expect(ids("clo", ["project.close"]).slice(0, 2)).toEqual(["project.close", "buffer.close"]);
expect(ids("sav", ["file.saveAll"])).toEqual(["file.saveAll", "file.save"]);
expect(ids("sav", ["project.close"])).toEqual(["file.save", "file.saveAll"]);
});
it("does not let recency beat a better tier", () => {
expect(ids("sa", ["view.toggleProjectTree"])[0]).toBe("file.save");
});
it("lists everything without a query, recent first, then by label", () => {
expect(ids("", ["app.quit", "file.save"])).toEqual([
"app.quit",
"file.save",
"project.close",
"buffer.close",
"palette.open",
"project.openDialog",
"file.saveAll",
"view.toggleProjectTree",
]);
});
it("returns nothing for a query that matches nothing", () => {
expect(ids("zzz")).toEqual([]);
});
});