|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | + |
| 3 | +import { fuzzyMatch, fuzzyMatchScore, bigramCoverage } from '@/landings/lib/fuzzy-match' |
| 4 | + |
| 5 | +describe('fuzzyMatch', () => { |
| 6 | + test('matches exact substrings', () => { |
| 7 | + expect(fuzzyMatch('GitHub Copilot agents', 'agent')).toBe(true) |
| 8 | + expect(fuzzyMatch('GitHub Copilot agents', 'copilot')).toBe(true) |
| 9 | + }) |
| 10 | + |
| 11 | + test('matches singular vs plural via bigrams', () => { |
| 12 | + expect(fuzzyMatch('GitHub Copilot agent', 'agents')).toBe(true) |
| 13 | + expect(fuzzyMatch('Managing your repository', 'repositories')).toBe(true) |
| 14 | + }) |
| 15 | + |
| 16 | + test('is case insensitive', () => { |
| 17 | + expect(fuzzyMatch('GitHub Copilot', 'COPILOT')).toBe(true) |
| 18 | + expect(fuzzyMatch('AGENTS', 'agents')).toBe(true) |
| 19 | + }) |
| 20 | + |
| 21 | + test('returns false for non-matching text', () => { |
| 22 | + expect(fuzzyMatch('GitHub Copilot', 'xyz')).toBe(false) |
| 23 | + expect(fuzzyMatch('Repository settings', 'workflow')).toBe(false) |
| 24 | + }) |
| 25 | + |
| 26 | + test('matches multi-word queries via bigram coverage', () => { |
| 27 | + expect(fuzzyMatch('About GitHub Copilot agent features', 'copilot agent')).toBe(true) |
| 28 | + expect(fuzzyMatch('Using agent in Copilot', 'copilot agent')).toBe(true) |
| 29 | + }) |
| 30 | + |
| 31 | + test('multi-word queries require sufficient bigram overlap', () => { |
| 32 | + expect(fuzzyMatch('xyz abc', 'copilot agents')).toBe(false) |
| 33 | + }) |
| 34 | + |
| 35 | + test('handles edge cases gracefully', () => { |
| 36 | + // Empty strings |
| 37 | + expect(fuzzyMatch('GitHub Copilot', '')).toBe(true) // empty search matches anything |
| 38 | + expect(fuzzyMatch('', 'copilot')).toBe(false) |
| 39 | + expect(fuzzyMatch('', '')).toBe(true) |
| 40 | + |
| 41 | + // Whitespace-only queries |
| 42 | + expect(fuzzyMatch('GitHub Copilot', ' ')).toBe(false) |
| 43 | + |
| 44 | + // Multiple consecutive spaces in query |
| 45 | + expect(fuzzyMatch('GitHub Copilot agent', 'copilot agent')).toBe(true) |
| 46 | + }) |
| 47 | +}) |
| 48 | + |
| 49 | +describe('fuzzyMatchScore', () => { |
| 50 | + test('returns 1 for exact substring match', () => { |
| 51 | + expect(fuzzyMatchScore('GitHub Copilot agents', 'copilot')).toBe(1) |
| 52 | + }) |
| 53 | + |
| 54 | + test('returns -1 for no match', () => { |
| 55 | + expect(fuzzyMatchScore('GitHub Copilot', 'xyz')).toBe(-1) |
| 56 | + }) |
| 57 | + |
| 58 | + test('returns bigram coverage score for fuzzy matches', () => { |
| 59 | + // Bigram coverage should give a score between 0.6 and 1 |
| 60 | + const score = fuzzyMatchScore('About Copilot memory features', 'memory copilot') |
| 61 | + expect(score).toBeGreaterThan(0.6) |
| 62 | + expect(score).toBeLessThan(1) |
| 63 | + }) |
| 64 | + |
| 65 | + test('matches singular vs plural via bigrams', () => { |
| 66 | + // "agents" bigrams: ag, ge, en, nt, ts (5) |
| 67 | + // "agent" in text has: ag, ge, en, nt (4) |
| 68 | + // Coverage: 4/5 = 0.8, which is > 0.6 threshold |
| 69 | + const score = fuzzyMatchScore('GitHub Copilot agent', 'agents') |
| 70 | + expect(score).toBeGreaterThan(0.6) |
| 71 | + }) |
| 72 | + |
| 73 | + test('exact substring matches score higher than fuzzy matches', () => { |
| 74 | + const exactScore = fuzzyMatchScore('copilot agent guide', 'copilot agent') |
| 75 | + const fuzzyScore = fuzzyMatchScore('About Copilot memory features', 'memory copilot') |
| 76 | + expect(exactScore).toBe(1) |
| 77 | + expect(fuzzyScore).toBeLessThan(1) |
| 78 | + }) |
| 79 | +}) |
| 80 | + |
| 81 | +describe('bigramCoverage', () => { |
| 82 | + test('returns 1.0 when all search bigrams are found in text', () => { |
| 83 | + expect(bigramCoverage('copilot agent', 'agent')).toBe(1) |
| 84 | + }) |
| 85 | + |
| 86 | + test('returns 0 for completely different texts', () => { |
| 87 | + expect(bigramCoverage('xyz', 'abc')).toBe(0) |
| 88 | + }) |
| 89 | + |
| 90 | + test('returns 0 for empty search string', () => { |
| 91 | + expect(bigramCoverage('some text', '')).toBe(0) |
| 92 | + }) |
| 93 | + |
| 94 | + test('handles singular vs plural with high coverage', () => { |
| 95 | + // "agents" bigrams: ag, ge, en, nt, ts (5) |
| 96 | + // "agent" in text has: ag, ge, en, nt (4) |
| 97 | + // Coverage: 4/5 = 0.8 |
| 98 | + const coverage = bigramCoverage('agent', 'agents') |
| 99 | + expect(coverage).toBeCloseTo(4 / 5, 2) |
| 100 | + }) |
| 101 | + |
| 102 | + test('calculates partial coverage correctly', () => { |
| 103 | + // Text "hello" has bigrams: he, el, ll, lo |
| 104 | + // Search "help" has bigrams: he, el, lp |
| 105 | + // Found: he, el (2 of 3) = 0.67 |
| 106 | + const coverage = bigramCoverage('hello', 'help') |
| 107 | + expect(coverage).toBeCloseTo(2 / 3, 2) |
| 108 | + }) |
| 109 | + |
| 110 | + test('is case insensitive', () => { |
| 111 | + expect(bigramCoverage('COPILOT', 'copilot')).toBe(1) |
| 112 | + expect(bigramCoverage('copilot', 'COPILOT')).toBe(1) |
| 113 | + }) |
| 114 | + |
| 115 | + test('ignores whitespace in both text and search', () => { |
| 116 | + expect(bigramCoverage('co pi lot', 'copilot')).toBe(1) |
| 117 | + expect(bigramCoverage('copilot', 'co pi lot')).toBe(1) |
| 118 | + }) |
| 119 | +}) |
0 commit comments