fuzzy.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import assert from "node:assert";
  2. import { describe, it } from "node:test";
  3. import { fuzzyFilter, fuzzyMatch } from "../src/fuzzy.ts";
  4. describe("fuzzyMatch", () => {
  5. it("empty query matches everything with score 0", () => {
  6. const result = fuzzyMatch("", "anything");
  7. assert.strictEqual(result.matches, true);
  8. assert.strictEqual(result.score, 0);
  9. });
  10. it("query longer than text does not match", () => {
  11. const result = fuzzyMatch("longquery", "short");
  12. assert.strictEqual(result.matches, false);
  13. });
  14. it("exact match has good score", () => {
  15. const result = fuzzyMatch("test", "test");
  16. assert.strictEqual(result.matches, true);
  17. assert.ok(result.score < 0); // Should be negative due to consecutive bonuses
  18. });
  19. it("characters must appear in order", () => {
  20. const matchInOrder = fuzzyMatch("abc", "aXbXc");
  21. assert.strictEqual(matchInOrder.matches, true);
  22. const matchOutOfOrder = fuzzyMatch("abc", "cba");
  23. assert.strictEqual(matchOutOfOrder.matches, false);
  24. });
  25. it("case insensitive matching", () => {
  26. const result = fuzzyMatch("ABC", "abc");
  27. assert.strictEqual(result.matches, true);
  28. const result2 = fuzzyMatch("abc", "ABC");
  29. assert.strictEqual(result2.matches, true);
  30. });
  31. it("consecutive matches score better than scattered matches", () => {
  32. const consecutive = fuzzyMatch("foo", "foobar");
  33. const scattered = fuzzyMatch("foo", "f_o_o_bar");
  34. assert.strictEqual(consecutive.matches, true);
  35. assert.strictEqual(scattered.matches, true);
  36. assert.ok(consecutive.score < scattered.score);
  37. });
  38. it("word boundary matches score better", () => {
  39. const atBoundary = fuzzyMatch("fb", "foo-bar");
  40. const notAtBoundary = fuzzyMatch("fb", "afbx");
  41. assert.strictEqual(atBoundary.matches, true);
  42. assert.strictEqual(notAtBoundary.matches, true);
  43. assert.ok(atBoundary.score < notAtBoundary.score);
  44. });
  45. it("matches swapped alpha numeric tokens", () => {
  46. const result = fuzzyMatch("codex52", "gpt-5.2-codex");
  47. assert.strictEqual(result.matches, true);
  48. });
  49. });
  50. describe("fuzzyFilter", () => {
  51. it("empty query returns all items unchanged", () => {
  52. const items = ["apple", "banana", "cherry"];
  53. const result = fuzzyFilter(items, "", (x: string) => x);
  54. assert.deepStrictEqual(result, items);
  55. });
  56. it("filters out non-matching items", () => {
  57. const items = ["apple", "banana", "cherry"];
  58. const result = fuzzyFilter(items, "an", (x: string) => x);
  59. assert.ok(result.includes("banana"));
  60. assert.ok(!result.includes("apple"));
  61. assert.ok(!result.includes("cherry"));
  62. });
  63. it("sorts results by match quality", () => {
  64. const items = ["a_p_p", "app", "application"];
  65. const result = fuzzyFilter(items, "app", (x: string) => x);
  66. // "app" should be first (exact consecutive match at start)
  67. assert.strictEqual(result[0], "app");
  68. });
  69. it("prioritizes exact matches over longer prefix matches", () => {
  70. const items = ["clone", "cl"];
  71. const result = fuzzyFilter(items, "cl", (x: string) => x);
  72. assert.deepStrictEqual(result, ["cl", "clone"]);
  73. });
  74. it("works with custom getText function", () => {
  75. const items = [
  76. { name: "foo", id: 1 },
  77. { name: "bar", id: 2 },
  78. { name: "foobar", id: 3 },
  79. ];
  80. const result = fuzzyFilter(items, "foo", (item: { name: string; id: number }) => item.name);
  81. assert.strictEqual(result.length, 2);
  82. assert.ok(result.map((r) => r.name).includes("foo"));
  83. assert.ok(result.map((r) => r.name).includes("foobar"));
  84. });
  85. });