125 lines
4.3 KiB
TypeScript
125 lines
4.3 KiB
TypeScript
/**
|
|||
|
|
* Command palette search ranking/filter helpers (node:test).
|
||
|
|
*
|
||
|
|
* Run from apps/web:
|
||
|
|
* node --experimental-strip-types --test src/lib/command-palette-search.test.ts
|
||
|
|
*/
|
||
|
|
import assert from "node:assert/strict";
|
||
|
|
import { describe, it } from "node:test";
|
||
|
|
|
||
|
|
import {
|
||
|
|
commandPaletteShortcutLabel,
|
||
|
|
filterAndRankPaletteItems,
|
||
|
|
normalizePaletteQuery,
|
||
|
|
scorePaletteItem,
|
||
|
|
scorePaletteToken,
|
||
|
|
type PaletteSearchItem
|
||
|
|
} from "./command-palette-search.ts";
|
||
|
|
|
||
|
|
const ITEMS: PaletteSearchItem[] = [
|
||
|
|
{ id: "products", label: "Products", keywords: "catalog items sku" },
|
||
|
|
{ id: "processing", label: "Jobs", keywords: "processing queue tasks" },
|
||
|
|
{ id: "feeds", label: "Feeds", keywords: "import sources" },
|
||
|
|
{ id: "seo", label: "SEO", keywords: "search optimization meta" },
|
||
|
|
{ id: "settings", label: "Settings", keywords: "account preferences" },
|
||
|
|
{ id: "stores", label: "Stores", keywords: "shopify woocommerce connections" }
|
||
|
|
];
|
||
|
|
|
||
|
|
describe("normalizePaletteQuery", () => {
|
||
|
|
it("trims and lowercases", () => {
|
||
|
|
assert.equal(normalizePaletteQuery(" Products "), "products");
|
||
|
|
assert.equal(normalizePaletteQuery(""), "");
|
||
|
|
assert.equal(normalizePaletteQuery(" "), "");
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("scorePaletteToken", () => {
|
||
|
|
it("ranks exact label highest, then prefix, word, includes, keywords", () => {
|
||
|
|
assert.equal(scorePaletteToken("products", "Products", "catalog"), 100);
|
||
|
|
assert.equal(scorePaletteToken("prod", "Products", "catalog"), 80);
|
||
|
|
assert.equal(scorePaletteToken("duct", "Products", "catalog"), 50);
|
||
|
|
assert.equal(scorePaletteToken("catalog", "Products", "catalog items"), 40);
|
||
|
|
assert.equal(scorePaletteToken("items", "Products", "catalog items"), 40);
|
||
|
|
assert.equal(scorePaletteToken("talog", "Products", "catalog items"), 20);
|
||
|
|
assert.equal(scorePaletteToken("zzz", "Products", "catalog"), 0);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("scores word-prefix inside multi-word labels", () => {
|
||
|
|
assert.equal(scorePaletteToken("fields", "Standard fields", "mapping"), 70);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("scorePaletteItem", () => {
|
||
|
|
it("returns 1 for empty query so idle lists keep order", () => {
|
||
|
|
assert.equal(scorePaletteItem("", ITEMS[0]!), 1);
|
||
|
|
assert.equal(scorePaletteItem(" ", ITEMS[0]!), 1);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("requires every token to match (all tokens)", () => {
|
||
|
|
const item = ITEMS.find((i) => i.id === "stores")!;
|
||
|
|
assert.ok(scorePaletteItem("shopify", item) > 0);
|
||
|
|
assert.equal(scorePaletteItem("shopify billing", item), 0);
|
||
|
|
assert.ok(scorePaletteItem("shopify stores", item) > scorePaletteItem("shopify", item));
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("filterAndRankPaletteItems", () => {
|
||
|
|
it("returns original order when query is empty", () => {
|
||
|
|
assert.deepEqual(
|
||
|
|
filterAndRankPaletteItems("", ITEMS).map((i) => i.id),
|
||
|
|
ITEMS.map((i) => i.id)
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("filters non-matches", () => {
|
||
|
|
const out = filterAndRankPaletteItems("billing", ITEMS);
|
||
|
|
assert.deepEqual(out, []);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("ranks label prefix above keyword substring", () => {
|
||
|
|
const out = filterAndRankPaletteItems("search", ITEMS);
|
||
|
|
assert.deepEqual(
|
||
|
|
out.map((i) => i.id),
|
||
|
|
["seo"]
|
||
|
|
);
|
||
|
|
assert.equal(
|
||
|
|
out.some((i) => i.id === "products"),
|
||
|
|
false
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("prefers exact/label hits over weaker keyword hits", () => {
|
||
|
|
const mixed: PaletteSearchItem[] = [
|
||
|
|
{ id: "kw", label: "Marketing", keywords: "product launch" },
|
||
|
|
{ id: "label", label: "Products", keywords: "catalog" }
|
||
|
|
];
|
||
|
|
const out = filterAndRankPaletteItems("product", mixed);
|
||
|
|
assert.equal(out[0]?.id, "label");
|
||
|
|
assert.equal(out[1]?.id, "kw");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("matches Jobs via processing keyword", () => {
|
||
|
|
const out = filterAndRankPaletteItems("processing", ITEMS);
|
||
|
|
assert.equal(out.length, 1);
|
||
|
|
assert.equal(out[0]?.id, "processing");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("matches multi-token queries across label and keywords", () => {
|
||
|
|
const out = filterAndRankPaletteItems("shopify store", ITEMS);
|
||
|
|
assert.equal(out.length, 1);
|
||
|
|
assert.equal(out[0]?.id, "stores");
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("commandPaletteShortcutLabel", () => {
|
||
|
|
it("shows CmdK on Apple platforms", () => {
|
||
|
|
assert.equal(commandPaletteShortcutLabel("MacIntel"), "⌘K");
|
||
|
|
assert.equal(commandPaletteShortcutLabel("", "Mozilla/5.0 (iPhone)"), "⌘K");
|
||
|
|
});
|
||
|
|
|
||
|
|
it("shows Ctrl+K elsewhere (Windows/Linux)", () => {
|
||
|
|
assert.equal(commandPaletteShortcutLabel("Win32"), "Ctrl+K");
|
||
|
|
assert.equal(commandPaletteShortcutLabel("Linux x86_64"), "Ctrl+K");
|
||
|
|
assert.equal(commandPaletteShortcutLabel(null, null), "Ctrl+K");
|
||
|
|
});
|
||
|
|
});
|