|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { filterCommandsByPrefix } from "@/utils/commandPaletteFiltering"; |
| 3 | +import { CommandIds, CommandIdMatchers } from "@/utils/commandIds"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Tests for command palette filtering logic |
| 7 | + * Property-based tests that verify behavior regardless of specific command data |
| 8 | + */ |
| 9 | + |
| 10 | +describe("CommandPalette filtering", () => { |
| 11 | + describe("property: default mode shows only ws:switch:* commands", () => { |
| 12 | + test("all results start with ws:switch:", () => { |
| 13 | + const actions = [ |
| 14 | + { id: CommandIds.workspaceSwitch("1") }, |
| 15 | + { id: CommandIds.workspaceSwitch("2") }, |
| 16 | + { id: CommandIds.workspaceNew() }, |
| 17 | + { id: CommandIds.navToggleSidebar() }, |
| 18 | + ]; |
| 19 | + |
| 20 | + const result = filterCommandsByPrefix("", actions); |
| 21 | + |
| 22 | + expect(result.every((a) => CommandIdMatchers.isWorkspaceSwitch(a.id))).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + test("excludes all non-switching commands", () => { |
| 26 | + const actions = [ |
| 27 | + { id: CommandIds.workspaceSwitch("1") }, |
| 28 | + { id: CommandIds.workspaceNew() }, |
| 29 | + { id: CommandIds.workspaceRemove() }, |
| 30 | + { id: CommandIds.navToggleSidebar() }, |
| 31 | + ]; |
| 32 | + |
| 33 | + const result = filterCommandsByPrefix("", actions); |
| 34 | + |
| 35 | + expect(result.some((a) => !CommandIdMatchers.isWorkspaceSwitch(a.id))).toBe(false); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("property: > mode shows all EXCEPT ws:switch:* commands", () => { |
| 40 | + test("no results start with ws:switch:", () => { |
| 41 | + const actions = [ |
| 42 | + { id: CommandIds.workspaceSwitch("1") }, |
| 43 | + { id: CommandIds.workspaceNew() }, |
| 44 | + { id: CommandIds.navToggleSidebar() }, |
| 45 | + { id: CommandIds.chatClear() }, |
| 46 | + ]; |
| 47 | + |
| 48 | + const result = filterCommandsByPrefix(">", actions); |
| 49 | + |
| 50 | + expect(result.every((a) => !CommandIdMatchers.isWorkspaceSwitch(a.id))).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + test("includes all non-switching commands", () => { |
| 54 | + const actions = [ |
| 55 | + { id: CommandIds.workspaceSwitch("1") }, |
| 56 | + { id: CommandIds.workspaceNew() }, |
| 57 | + { id: CommandIds.workspaceRemove() }, |
| 58 | + { id: CommandIds.navToggleSidebar() }, |
| 59 | + ]; |
| 60 | + |
| 61 | + const result = filterCommandsByPrefix(">", actions); |
| 62 | + |
| 63 | + // Should include workspace mutations |
| 64 | + expect(result.some((a) => a.id === CommandIds.workspaceNew())).toBe(true); |
| 65 | + expect(result.some((a) => a.id === CommandIds.workspaceRemove())).toBe(true); |
| 66 | + // Should include navigation |
| 67 | + expect(result.some((a) => a.id === CommandIds.navToggleSidebar())).toBe(true); |
| 68 | + // Should NOT include switching |
| 69 | + expect(result.some((a) => a.id === CommandIds.workspaceSwitch("1"))).toBe(false); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("property: modes partition the command space", () => { |
| 74 | + test("default + > modes cover all commands (no overlap, no gaps)", () => { |
| 75 | + const actions = [ |
| 76 | + { id: CommandIds.workspaceSwitch("1") }, |
| 77 | + { id: CommandIds.workspaceSwitch("2") }, |
| 78 | + { id: CommandIds.workspaceNew() }, |
| 79 | + { id: CommandIds.workspaceRemove() }, |
| 80 | + { id: CommandIds.navToggleSidebar() }, |
| 81 | + { id: CommandIds.chatClear() }, |
| 82 | + ]; |
| 83 | + |
| 84 | + const defaultResult = filterCommandsByPrefix("", actions); |
| 85 | + const commandResult = filterCommandsByPrefix(">", actions); |
| 86 | + |
| 87 | + // No overlap - disjoint sets |
| 88 | + const defaultIds = new Set(defaultResult.map((a) => a.id)); |
| 89 | + const commandIds = new Set(commandResult.map((a) => a.id)); |
| 90 | + const intersection = [...defaultIds].filter((id) => commandIds.has(id)); |
| 91 | + expect(intersection).toHaveLength(0); |
| 92 | + |
| 93 | + // No gaps - covers everything |
| 94 | + expect(defaultResult.length + commandResult.length).toBe(actions.length); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("property: / prefix always returns empty", () => { |
| 99 | + test("returns empty array regardless of actions", () => { |
| 100 | + const actions = [ |
| 101 | + { id: CommandIds.workspaceSwitch("1") }, |
| 102 | + { id: CommandIds.workspaceNew() }, |
| 103 | + { id: CommandIds.navToggleSidebar() }, |
| 104 | + ]; |
| 105 | + |
| 106 | + expect(filterCommandsByPrefix("/", actions)).toHaveLength(0); |
| 107 | + expect(filterCommandsByPrefix("/help", actions)).toHaveLength(0); |
| 108 | + expect(filterCommandsByPrefix("/ ", actions)).toHaveLength(0); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("property: query with > prefix applies to all non-switching", () => { |
| 113 | + test(">text shows same set as > (cmdk filters further)", () => { |
| 114 | + const actions = [ |
| 115 | + { id: CommandIds.workspaceSwitch("1") }, |
| 116 | + { id: CommandIds.workspaceNew() }, |
| 117 | + { id: CommandIds.navToggleSidebar() }, |
| 118 | + ]; |
| 119 | + |
| 120 | + // Our filter doesn't care about text after >, just the prefix |
| 121 | + const resultEmpty = filterCommandsByPrefix(">", actions); |
| 122 | + const resultWithText = filterCommandsByPrefix(">abc", actions); |
| 123 | + |
| 124 | + expect(resultEmpty).toEqual(resultWithText); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments