|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { |
| 3 | + compileFileSearchPattern, |
| 4 | + escapeFileSearchLikePattern, |
| 5 | + FileSearchPatternError, |
| 6 | + isFileSearchCaseSensitive, |
| 7 | +} from '@/lib/workspace-files/search/pattern' |
| 8 | + |
| 9 | +describe('compileFileSearchPattern', () => { |
| 10 | + describe('shared query rules', () => { |
| 11 | + it.each(['exact', 'regex'] as const)('bounds and screens the query in %s mode', (mode) => { |
| 12 | + expect(() => compileFileSearchPattern('ab', mode)).toThrow(/at least 3 characters/) |
| 13 | + expect(() => compileFileSearchPattern('a'.repeat(513), mode)).toThrow(/at most 512/) |
| 14 | + expect(() => compileFileSearchPattern('abc\0def', mode)).toThrow(/NUL/) |
| 15 | + }) |
| 16 | + }) |
| 17 | + |
| 18 | + describe('exact mode', () => { |
| 19 | + it('implements Unicode smart-case and escapes LIKE metacharacters', () => { |
| 20 | + expect(isFileSearchCaseSensitive('résumé')).toBe(false) |
| 21 | + expect(isFileSearchCaseSensitive('Résumé')).toBe(true) |
| 22 | + expect(isFileSearchCaseSensitive('東京A')).toBe(true) |
| 23 | + expect(escapeFileSearchLikePattern('100%_done\\')).toBe('100\\%\\_done\\\\') |
| 24 | + }) |
| 25 | + |
| 26 | + it('wraps the escaped query for LIKE and keeps the raw text for ranking', () => { |
| 27 | + const pattern = compileFileSearchPattern('100%_done', 'exact') |
| 28 | + |
| 29 | + expect(pattern).toMatchObject({ |
| 30 | + mode: 'exact', |
| 31 | + caseSensitive: false, |
| 32 | + sqlPattern: '%100\\%\\_done%', |
| 33 | + literalText: '100%_done', |
| 34 | + wholeLineOnly: false, |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + it('reads regex metacharacters as text', () => { |
| 39 | + const pattern = compileFileSearchPattern('a.c', 'exact') |
| 40 | + |
| 41 | + expect(pattern.sqlPattern).toBe('%a.c%') |
| 42 | + expect(pattern.findMatchRange('xxabcxx')).toEqual({ start: 0, end: 3 }) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + describe('regex mode', () => { |
| 47 | + it('matches through PostgreSQL spelling while ranking has no fixed literal', () => { |
| 48 | + const pattern = compileFileSearchPattern('\\bTODO\\b', 'regex') |
| 49 | + |
| 50 | + expect(pattern).toMatchObject({ |
| 51 | + mode: 'regex', |
| 52 | + sqlPattern: '\\yTODO\\y', |
| 53 | + literalText: null, |
| 54 | + caseSensitive: true, |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + it('derives smart case from literals, not from metacharacters', () => { |
| 59 | + expect(compileFileSearchPattern('error \\d+', 'regex').caseSensitive).toBe(false) |
| 60 | + expect(compileFileSearchPattern('error \\D+', 'regex').caseSensitive).toBe(false) |
| 61 | + expect(compileFileSearchPattern('[A-Z]+ error', 'regex').caseSensitive).toBe(false) |
| 62 | + expect(compileFileSearchPattern('Error \\d+', 'regex').caseSensitive).toBe(true) |
| 63 | + }) |
| 64 | + |
| 65 | + it('restricts an anchored pattern to segments that hold a whole line', () => { |
| 66 | + expect(compileFileSearchPattern('^import x', 'regex').wholeLineOnly).toBe(true) |
| 67 | + expect(compileFileSearchPattern('import x;$', 'regex').wholeLineOnly).toBe(true) |
| 68 | + expect(compileFileSearchPattern('import x', 'regex').wholeLineOnly).toBe(false) |
| 69 | + }) |
| 70 | + |
| 71 | + it('requires a literal run long enough for the trigram index to be used', () => { |
| 72 | + expect(() => compileFileSearchPattern('\\w+ \\d+', 'regex')).toThrow(FileSearchPatternError) |
| 73 | + expect(() => compileFileSearchPattern('\\w+ \\d+', 'regex')).toThrow( |
| 74 | + /at least 3 consecutive literal characters/ |
| 75 | + ) |
| 76 | + expect(() => compileFileSearchPattern('\\d{4}-\\d{2}-\\d{2}', 'regex')).toThrow( |
| 77 | + /at least 3 consecutive literal characters/ |
| 78 | + ) |
| 79 | + expect(() => compileFileSearchPattern('error \\d+', 'regex')).not.toThrow() |
| 80 | + }) |
| 81 | + |
| 82 | + it('locates the match so a long line previews around it', () => { |
| 83 | + const pattern = compileFileSearchPattern('needle\\d+', 'regex') |
| 84 | + |
| 85 | + expect(pattern.findMatchRange('xxx needle42 yyy')).toEqual({ start: 4, end: 12 }) |
| 86 | + expect(pattern.findMatchRange('no match here')).toBeNull() |
| 87 | + }) |
| 88 | + |
| 89 | + it('never returns a range that splits a surrogate pair', () => { |
| 90 | + const pattern = compileFileSearchPattern('.needle', 'regex') |
| 91 | + const line = `a🙂needle` |
| 92 | + const range = pattern.findMatchRange(line) |
| 93 | + |
| 94 | + expect(range).not.toBeNull() |
| 95 | + expect([...line.slice(range?.start, range?.end)].join('')).not.toContain('�') |
| 96 | + expect(line.slice(range?.start, range?.end)).toBe('🙂needle') |
| 97 | + }) |
| 98 | + |
| 99 | + it('does not carry match state between calls', () => { |
| 100 | + const pattern = compileFileSearchPattern('needle', 'regex') |
| 101 | + |
| 102 | + expect(pattern.findMatchRange('a needle')).toEqual({ start: 2, end: 8 }) |
| 103 | + expect(pattern.findMatchRange('a needle')).toEqual({ start: 2, end: 8 }) |
| 104 | + }) |
| 105 | + }) |
| 106 | +}) |
0 commit comments