/** * 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([]); }); });