mirror of
https://github.com/zadam/trilium.git
synced 2025-10-19 22:58:52 +02:00
65 lines
2.8 KiB
TypeScript
65 lines
2.8 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { calculateOptimizedEditDistance, validateFuzzySearchTokens, fuzzyMatchWord } from './text_utils.js';
|
|
|
|
describe('Fuzzy Search Core', () => {
|
|
describe('calculateOptimizedEditDistance', () => {
|
|
it('calculates edit distance for common typos', () => {
|
|
expect(calculateOptimizedEditDistance('hello', 'helo')).toBe(1);
|
|
expect(calculateOptimizedEditDistance('world', 'wrold')).toBe(2);
|
|
expect(calculateOptimizedEditDistance('cafe', 'café')).toBe(1);
|
|
expect(calculateOptimizedEditDistance('identical', 'identical')).toBe(0);
|
|
});
|
|
|
|
it('handles performance safety with oversized input', () => {
|
|
const longString = 'a'.repeat(2000);
|
|
const result = calculateOptimizedEditDistance(longString, 'short');
|
|
expect(result).toBeGreaterThan(2); // Should use fallback heuristic
|
|
});
|
|
});
|
|
|
|
describe('validateFuzzySearchTokens', () => {
|
|
it('validates minimum length requirements for fuzzy operators', () => {
|
|
const result1 = validateFuzzySearchTokens(['ab'], '~=');
|
|
expect(result1.isValid).toBe(false);
|
|
expect(result1.error).toContain('at least 3 characters');
|
|
|
|
const result2 = validateFuzzySearchTokens(['hello'], '~=');
|
|
expect(result2.isValid).toBe(true);
|
|
|
|
const result3 = validateFuzzySearchTokens(['ok'], '=');
|
|
expect(result3.isValid).toBe(true); // Non-fuzzy operators allow short tokens
|
|
});
|
|
|
|
it('validates token types and empty arrays', () => {
|
|
expect(validateFuzzySearchTokens([], '=')).toEqual({
|
|
isValid: false,
|
|
error: 'Invalid tokens: at least one token is required'
|
|
});
|
|
|
|
expect(validateFuzzySearchTokens([''], '=')).toEqual({
|
|
isValid: false,
|
|
error: 'Invalid tokens: empty or whitespace-only tokens are not allowed'
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('fuzzyMatchWord', () => {
|
|
it('matches words with diacritics normalization', () => {
|
|
expect(fuzzyMatchWord('cafe', 'café')).toBe(true);
|
|
expect(fuzzyMatchWord('naive', 'naïve')).toBe(true);
|
|
});
|
|
|
|
it('matches with typos within distance threshold', () => {
|
|
expect(fuzzyMatchWord('hello', 'helo')).toBe(true);
|
|
expect(fuzzyMatchWord('world', 'wrold')).toBe(true);
|
|
expect(fuzzyMatchWord('test', 'tset')).toBe(true);
|
|
expect(fuzzyMatchWord('test', 'xyz')).toBe(false);
|
|
});
|
|
|
|
it('handles edge cases safely', () => {
|
|
expect(fuzzyMatchWord('', 'test')).toBe(false);
|
|
expect(fuzzyMatchWord('test', '')).toBe(false);
|
|
expect(fuzzyMatchWord('a', 'b')).toBe(false); // Very short tokens
|
|
});
|
|
});
|
|
}); |