Spaces:
Sleeping
Sleeping
File size: 2,880 Bytes
05c5ed5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import { describe, expect, test } from "vitest";
import { fuzzySearch, type SearchItem } from "./fuzzy-search";
describe("fuzzySearch", () => {
const testItems: SearchItem[] = [
{ id: "item-1", label: "Apple" },
{ id: "item-2", label: "Banana" },
{ id: "item-3", label: "Cherry" },
{ id: "item-4", label: "Dragon fruit" },
{ id: "apple-5", label: "Elderberry" },
{ id: "item-6", label: "Fig" },
];
test("returns all items when query is empty", () => {
expect(fuzzySearch(testItems, "")).toEqual(testItems);
expect(fuzzySearch(testItems, " ")).toEqual(testItems);
});
test("finds exact matches in id", () => {
const result = fuzzySearch(testItems, "apple");
// Should match item-1 (label: Apple) and apple-5 (id: apple-5)
expect(result).toHaveLength(2);
expect(result).toContainEqual(testItems[0]); // Apple
expect(result).toContainEqual(testItems[4]); // apple-5
});
test("finds exact matches in label", () => {
const result = fuzzySearch(testItems, "ban");
// Should match item-2 (label: Banana)
expect(result).toHaveLength(1);
expect(result).toContainEqual(testItems[1]); // Banana
});
test("ignores special characters in query", () => {
const result = fuzzySearch(testItems, "d*r*a*g*o*n");
// Should match item-4 (label: Dragon fruit)
expect(result).toHaveLength(1);
expect(result).toContainEqual(testItems[3]); // Dragon fruit
});
test("matches using bigram similarity for longer queries", () => {
const result = fuzzySearch(testItems, "el");
// Should match item-5 (label: Elderberry)
expect(result).toHaveLength(1);
expect(result).toContainEqual(testItems[4]); // Elderberry
});
test("sorts results by score", () => {
// Add item with partial match to test sorting
const extendedItems: SearchItem[] = [
...testItems,
{ id: "app-test", label: "Application" },
];
const result = fuzzySearch(extendedItems, "app");
// Check that app-related items are returned
expect(result.length).toBeGreaterThanOrEqual(3);
expect(result).toContainEqual(testItems[0]); // Apple
expect(result).toContainEqual(testItems[4]); // apple-5
expect(result).toContainEqual({ id: "app-test", label: "Application" });
// The actual order depends on the scoring implementation
// Don't assert exact order as it can change
});
test("filters items with score below minimum threshold", () => {
const result = fuzzySearch(testItems, "xyz");
// Should not match any items
expect(result).toHaveLength(0);
});
test("ignores case sensitivity", () => {
const result = fuzzySearch(testItems, "CHERRY");
// CHERRY matches both Cherry and potentially other items
expect(result.length).toBeGreaterThanOrEqual(1);
expect(result).toContainEqual(testItems[2]); // Cherry
});
});
|