@@ -0,0 +1,62 @@
1+import { describe, expect, test } from "bun:test";
2+import { normalizeItems, prefixFilter } from "./filter.ts";
3+
4+describe("normalizeItems", () => {
5+ test("normalizes string items", () => {
6+ const result = normalizeItems(["foo", "bar"]);
7+ expect(result).toEqual([
8+ { label: "foo", value: "foo", filterText: "foo" },
9+ { label: "bar", value: "bar", filterText: "bar" },
10+ ]);
11+ });
12+
13+ test("normalizes {label, value} items, filterText defaults to label", () => {
14+ const result = normalizeItems([{ label: "Foo Label", value: "foo" }]);
15+ expect(result).toEqual([
16+ { label: "Foo Label", value: "foo", filterText: "Foo Label" },
17+ ]);
18+ });
19+
20+ test("preserves explicit filterText", () => {
21+ const result = normalizeItems([
22+ { label: "colored", value: "v", filterText: "plain" },
23+ ]);
24+ expect(result[0]!.filterText).toBe("plain");
25+ });
26+});
27+
28+describe("prefixFilter", () => {
29+ const items = normalizeItems(["apple", "apricot", "banana", "Avocado"]);
30+
31+ test("empty query returns all items", () => {
32+ expect(prefixFilter(items, "")).toEqual(items);
33+ });
34+
35+ test("prefix match", () => {
36+ const result = prefixFilter(items, "ap");
37+ expect(result.map((i) => i.value)).toEqual(["apple", "apricot"]);
38+ });
39+
40+ test("case insensitive", () => {
41+ const result = prefixFilter(items, "AV");
42+ expect(result.map((i) => i.value)).toEqual(["Avocado"]);
43+ });
44+
45+ test("no match returns empty", () => {
46+ expect(prefixFilter(items, "xyz")).toEqual([]);
47+ });
48+
49+ test("matches against filterText, not label", () => {
50+ const rich = normalizeItems([
51+ {
52+ label: "\x1b[31mClaude\x1b[0m",
53+ value: "claude",
54+ filterText: "claude-sonnet-4",
55+ },
56+ ]);
57+ expect(prefixFilter(rich, "claude-s").map((i) => i.value)).toEqual([
58+ "claude",
59+ ]);
60+ expect(prefixFilter(rich, "\x1b[31m")).toEqual([]);
61+ });
62+});