6615
Yc
yoctoselect / files
yoctoselect / src / filter.test.ts
2.7 KB
1import { describe, expect, test } from "bun:test"; 2import { normalizeItems, prefixFilter } from "./filter.ts"; 3 4describe("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 28describe("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 63 test("matches word prefix in filterText", () => { 64 const models = normalizeItems([ 65 { 66 label: "Claude Sonnet 4", 67 value: "anthropic/claude-sonnet-4", 68 filterText: "anthropic/claude-sonnet-4 Claude Sonnet 4 anthropic", 69 }, 70 { 71 label: "GPT-4o", 72 value: "openai/gpt-4o", 73 filterText: "openai/gpt-4o GPT-4o openai", 74 }, 75 ]); 76 // "cl" matches "claude-sonnet-4" and "Claude" words 77 expect(prefixFilter(models, "cl").map((i) => i.value)).toEqual([ 78 "anthropic/claude-sonnet-4", 79 ]); 80 // "Son" matches "Sonnet" word 81 expect(prefixFilter(models, "Son").map((i) => i.value)).toEqual([ 82 "anthropic/claude-sonnet-4", 83 ]); 84 // "gpt" matches "gpt-4o" word 85 expect(prefixFilter(models, "gpt").map((i) => i.value)).toEqual([ 86 "openai/gpt-4o", 87 ]); 88 }); 89});