Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions apps/web/src/command-palette/command-groups.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, expect, test } from "bun:test";

import { buildCommandPaletteGroups, type PaletteSource } from "./command-groups";

const workflows = { id: "route:/workflows", title: "Workflows" };
const settings = { id: "route:/settings", title: "Settings" };
const pages: PaletteSource = {
id: "pages",
heading: "Pages",
kind: "pages",
items: [workflows, settings],
};

describe("buildCommandPaletteGroups", () => {
test("keeps Recent first and omits later duplicates without reordering other results", () => {
const groups = buildCommandPaletteGroups({
query: "",
recents: [workflows],
sources: [pages, { id: "duplicates", heading: "Duplicates", items: [workflows, settings] }],
});

expect(groups).toEqual([
{ id: "recents", heading: "Recent", items: [workflows] },
{ id: "pages", heading: "Pages", items: [settings] },
]);
});

test("deduplicates scoped results after excluding Recent and out-of-scope sources", () => {
const workbench = { id: "entity:workbenches:one", title: "One" };
const groups = buildCommandPaletteGroups({
query: "#",
recents: [workbench],
sources: [
{ id: "unscoped", heading: "Unscoped", items: [workbench] },
{ id: "pages", heading: "Pages", kind: "pages", items: [workbench] },
{
id: "workbenches",
heading: "Workbenches",
kind: "workbenches",
items: [workbench],
},
{
id: "duplicates",
heading: "Duplicates",
kind: "workbenches",
items: [workbench],
},
],
});

expect(groups).toEqual([{ id: "workbenches", heading: "Workbenches", items: [workbench] }]);
});

test("keeps the first matching result even when an earlier copy does not match", () => {
const groups = buildCommandPaletteGroups({
query: "work",
recents: [workflows],
sources: [
{
id: "nonmatching",
heading: "Nonmatching",
items: [{ id: workflows.id, title: "Other" }],
},
pages,
{ id: "duplicates", heading: "Duplicates", items: [workflows] },
],
});

expect(groups).toEqual([{ id: "pages", heading: "Pages", items: [workflows] }]);
});
});
5 changes: 4 additions & 1 deletion apps/web/src/command-palette/command-groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export function buildCommandPaletteGroups(
} else if (scope !== null && scope.kind !== source.kind) {
continue;
}
const items = filterItems(source.items, query);
// Scan the small palette; use a Set if result counts grow.
const items = filterItems(source.items, query).filter(
(item) => !groups.some((group) => group.items.some((existing) => existing.id === item.id)),
);
if (items.length > 0) {
groups.push({ id: source.id, heading: source.heading, items });
}
Expand Down
Loading