|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { normalizeWhitespace, createContextSnippet } from './textNormalization' |
| 3 | + |
| 4 | +describe('normalizeWhitespace', () => { |
| 5 | + describe('HTML entity normalization', () => { |
| 6 | + it('should normalize to regular space', () => { |
| 7 | + expect(normalizeWhitespace('Hello World')).toBe('Hello World') |
| 8 | + expect(normalizeWhitespace('Hello World')).toBe('Hello World') |
| 9 | + }) |
| 10 | + |
| 11 | + it('should normalize Unicode non-breaking space to regular space', () => { |
| 12 | + expect(normalizeWhitespace('Hello\u00A0World')).toBe('Hello World') |
| 13 | + }) |
| 14 | + |
| 15 | + it('should normalize " to double quotes', () => { |
| 16 | + expect(normalizeWhitespace('"Hello"')).toBe('"Hello"') |
| 17 | + expect(normalizeWhitespace('"Hello"')).toBe('"Hello"') |
| 18 | + }) |
| 19 | + |
| 20 | + it('should normalize ' and ' to single quotes', () => { |
| 21 | + expect(normalizeWhitespace(''Hello'')).toBe("'Hello'") |
| 22 | + expect(normalizeWhitespace(''Hello'')).toBe("'Hello'") |
| 23 | + }) |
| 24 | + |
| 25 | + it('should normalize £ to £', () => { |
| 26 | + expect(normalizeWhitespace('£100')).toBe('£100') |
| 27 | + expect(normalizeWhitespace('£100')).toBe('£100') |
| 28 | + }) |
| 29 | + |
| 30 | + it('should normalize € to €', () => { |
| 31 | + expect(normalizeWhitespace('€50')).toBe('€50') |
| 32 | + expect(normalizeWhitespace('€50')).toBe('€50') |
| 33 | + }) |
| 34 | + |
| 35 | + it('should normalize & to &', () => { |
| 36 | + expect(normalizeWhitespace('Fish & Chips')).toBe('Fish & Chips') |
| 37 | + expect(normalizeWhitespace('Fish & Chips')).toBe('Fish & Chips') |
| 38 | + }) |
| 39 | + |
| 40 | + it('should normalize < and > to < and >', () => { |
| 41 | + expect(normalizeWhitespace('<div>')).toBe('<div>') |
| 42 | + expect(normalizeWhitespace('<div>')).toBe('<div>') |
| 43 | + }) |
| 44 | + |
| 45 | + it('should normalize – and — to regular dash', () => { |
| 46 | + expect(normalizeWhitespace('2020–2021')).toBe('2020-2021') |
| 47 | + expect(normalizeWhitespace('Hello—World')).toBe('Hello-World') |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + describe('Unicode character normalization', () => { |
| 52 | + it('should normalize fancy single quotes to regular apostrophe', () => { |
| 53 | + const input = '\u2018Hello\u2019' |
| 54 | + const result = normalizeWhitespace(input) |
| 55 | + expect(result).toBe("'Hello'") |
| 56 | + // Verify it's a regular apostrophe (U+0027), not fancy quotes |
| 57 | + expect(result.charCodeAt(0)).toBe(0x0027) |
| 58 | + expect(result.charCodeAt(result.length - 1)).toBe(0x0027) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should normalize fancy double quotes to regular quotes', () => { |
| 62 | + const input = '\u201CHello\u201D' |
| 63 | + const result = normalizeWhitespace(input) |
| 64 | + expect(result).toBe('"Hello"') |
| 65 | + // Verify it's a regular quote (U+0022), not fancy quotes |
| 66 | + expect(result.charCodeAt(0)).toBe(0x0022) |
| 67 | + expect(result.charCodeAt(result.length - 1)).toBe(0x0022) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should normalize en-dash and em-dash to regular dash', () => { |
| 71 | + expect(normalizeWhitespace('2020\u20132021')).toBe('2020-2021') // en-dash |
| 72 | + expect(normalizeWhitespace('Hello\u2014World')).toBe('Hello-World') // em-dash |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + describe('Whitespace collapsing', () => { |
| 77 | + it('should collapse multiple spaces to single space', () => { |
| 78 | + expect(normalizeWhitespace('Hello World')).toBe('Hello World') |
| 79 | + }) |
| 80 | + |
| 81 | + it('should collapse multiple whitespace types to single space', () => { |
| 82 | + expect(normalizeWhitespace('Hello\n\t World')).toBe('Hello World') |
| 83 | + }) |
| 84 | + |
| 85 | + it('should handle mixed whitespace and HTML entities', () => { |
| 86 | + expect(normalizeWhitespace('Hello World')).toBe('Hello World') |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + describe('Combined normalizations', () => { |
| 91 | + it('should normalize complex mixed content', () => { |
| 92 | + const input = '"Hello World"—£100' |
| 93 | + const expected = '"Hello World"-£100' |
| 94 | + expect(normalizeWhitespace(input)).toBe(expected) |
| 95 | + }) |
| 96 | + |
| 97 | + it('should handle empty string', () => { |
| 98 | + expect(normalizeWhitespace('')).toBe('') |
| 99 | + }) |
| 100 | + |
| 101 | + it('should handle string with no entities or special chars', () => { |
| 102 | + expect(normalizeWhitespace('Hello World')).toBe('Hello World') |
| 103 | + }) |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +describe('createContextSnippet', () => { |
| 108 | + it('should create snippet with context around match', () => { |
| 109 | + const text = 'The quick brown fox jumps over the lazy dog' |
| 110 | + const matchIndex = text.indexOf('fox') |
| 111 | + const result = createContextSnippet(text, matchIndex, 3, 10) |
| 112 | + expect(result).toBe('...ick brown fox jumps ove...') |
| 113 | + }) |
| 114 | + |
| 115 | + it('should not add leading ellipsis when match is near start', () => { |
| 116 | + const text = 'The quick brown fox jumps over the lazy dog' |
| 117 | + const matchIndex = text.indexOf('quick') |
| 118 | + const result = createContextSnippet(text, matchIndex, 5, 10) |
| 119 | + expect(result).toBe('The quick brown fox...') |
| 120 | + }) |
| 121 | + |
| 122 | + it('should not add trailing ellipsis when match is near end', () => { |
| 123 | + const text = 'The quick brown fox jumps over the lazy dog' |
| 124 | + const matchIndex = text.indexOf('lazy') |
| 125 | + const result = createContextSnippet(text, matchIndex, 4, 10) |
| 126 | + expect(result).toBe('... over the lazy dog') |
| 127 | + }) |
| 128 | + |
| 129 | + it('should handle match at start of text', () => { |
| 130 | + const text = 'The quick brown fox' |
| 131 | + const matchIndex = 0 |
| 132 | + const result = createContextSnippet(text, matchIndex, 3, 10) |
| 133 | + expect(result).toBe('The quick bro...') |
| 134 | + }) |
| 135 | + |
| 136 | + it('should handle match at end of text', () => { |
| 137 | + const text = 'The quick brown fox' |
| 138 | + const matchIndex = text.indexOf('fox') |
| 139 | + const result = createContextSnippet(text, matchIndex, 3, 10) |
| 140 | + expect(result).toBe('...ick brown fox') |
| 141 | + }) |
| 142 | + |
| 143 | + it('should include entire text if shorter than context size', () => { |
| 144 | + const text = 'Short text' |
| 145 | + const matchIndex = text.indexOf('text') |
| 146 | + const result = createContextSnippet(text, matchIndex, 4, 100) |
| 147 | + expect(result).toBe('Short text') |
| 148 | + }) |
| 149 | + |
| 150 | + it('should use default context size of 100', () => { |
| 151 | + const text = 'a'.repeat(300) |
| 152 | + const matchIndex = 150 |
| 153 | + const result = createContextSnippet(text, matchIndex, 1) |
| 154 | + // Should be: 100 chars before + 1 match + 100 chars after = 201 chars |
| 155 | + // Plus ellipsis on both ends = 207 chars |
| 156 | + expect(result).toHaveLength(207) |
| 157 | + expect(result.startsWith('...')).toBe(true) |
| 158 | + expect(result.endsWith('...')).toBe(true) |
| 159 | + }) |
| 160 | + |
| 161 | + it('should handle custom context size', () => { |
| 162 | + const text = 'a'.repeat(100) |
| 163 | + const matchIndex = 50 |
| 164 | + const result = createContextSnippet(text, matchIndex, 1, 20) |
| 165 | + // Should be: 20 chars before + 1 match + 20 chars after = 41 chars |
| 166 | + // Plus ellipsis on both ends = 47 chars |
| 167 | + expect(result).toHaveLength(47) |
| 168 | + }) |
| 169 | +}) |
0 commit comments