|
| 1 | +import { describe, expect, test } from 'bun:test' |
| 2 | +import { validateLyricsMatch } from './validator' |
| 3 | + |
| 4 | +const result = (artist: string, title: string) => ({ artist, title }) |
| 5 | + |
| 6 | +describe('validateLyricsMatch', () => { |
| 7 | + describe('exact matches', () => { |
| 8 | + test('exact artist + song → valid', () => { |
| 9 | + const r = validateLyricsMatch( |
| 10 | + 'Taylor Swift', |
| 11 | + 'Shake It Off', |
| 12 | + result('Taylor Swift', 'Shake It Off'), |
| 13 | + ) |
| 14 | + expect(r.valid).toBe(true) |
| 15 | + expect(r.artist_match).toBeCloseTo(1.0, 2) |
| 16 | + expect(r.song_match).toBeCloseTo(1.0, 2) |
| 17 | + expect(r.script_mismatch).toBe(false) |
| 18 | + }) |
| 19 | + |
| 20 | + test('case insensitive match → valid', () => { |
| 21 | + const r = validateLyricsMatch( |
| 22 | + 'taylor swift', |
| 23 | + 'shake it off', |
| 24 | + result('TAYLOR SWIFT', 'SHAKE IT OFF'), |
| 25 | + ) |
| 26 | + expect(r.valid).toBe(true) |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + describe('no-metadata fallbacks', () => { |
| 31 | + test('no title in result → trusting fetcher', () => { |
| 32 | + const r = validateLyricsMatch('Artist', 'Song', { artist: 'Artist' }) |
| 33 | + expect(r.valid).toBe(true) |
| 34 | + expect(r.reason).toContain('trusting fetcher') |
| 35 | + }) |
| 36 | + |
| 37 | + test('no artist metadata → song-only match', () => { |
| 38 | + const r = validateLyricsMatch('Taylor Swift', 'Shake It Off', { title: 'Shake It Off' }) |
| 39 | + expect(r.valid).toBe(true) |
| 40 | + expect(r.reason).toContain('No artist metadata') |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + describe('cross-script', () => { |
| 45 | + // normalizeString strips non-Latin chars via /[^\w\s]/gu so Korean/Arabic title |
| 46 | + // becomes empty → hits "trusting fetcher" path (valid: true, script_mismatch: false) |
| 47 | + test('CJK title stripped by normalizeString → trusting fetcher fallback', () => { |
| 48 | + const r = validateLyricsMatch('BTS', 'Dynamite', { artist: 'BTS', title: '다이너마이트' }) |
| 49 | + expect(r.valid).toBe(true) |
| 50 | + expect(r.reason).toContain('trusting fetcher') |
| 51 | + expect(r.script_mismatch).toBe(false) |
| 52 | + }) |
| 53 | + |
| 54 | + test('same script (both Latin) → no script_mismatch', () => { |
| 55 | + const r = validateLyricsMatch( |
| 56 | + 'Taylor Swift', |
| 57 | + 'Shake It Off', |
| 58 | + result('Taylor Swift', 'Shake It Off'), |
| 59 | + ) |
| 60 | + expect(r.script_mismatch).toBe(false) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + describe('mismatches', () => { |
| 65 | + test('wrong artist → invalid', () => { |
| 66 | + const r = validateLyricsMatch( |
| 67 | + 'Taylor Swift', |
| 68 | + 'Shake It Off', |
| 69 | + result('Ed Sheeran', 'Shake It Off'), |
| 70 | + ) |
| 71 | + expect(r.valid).toBe(false) |
| 72 | + expect(r.reason).toContain('artist score') |
| 73 | + }) |
| 74 | + |
| 75 | + test('wrong song → invalid', () => { |
| 76 | + const r = validateLyricsMatch( |
| 77 | + 'Taylor Swift', |
| 78 | + 'Shake It Off', |
| 79 | + result('Taylor Swift', 'Blank Space'), |
| 80 | + ) |
| 81 | + expect(r.valid).toBe(false) |
| 82 | + expect(r.reason).toContain('song score') |
| 83 | + }) |
| 84 | + |
| 85 | + test('both wrong → reason mentions both', () => { |
| 86 | + const r = validateLyricsMatch( |
| 87 | + 'Taylor Swift', |
| 88 | + 'Shake It Off', |
| 89 | + result('Ed Sheeran', 'Blank Space'), |
| 90 | + ) |
| 91 | + expect(r.valid).toBe(false) |
| 92 | + expect(r.reason).toContain('artist score') |
| 93 | + expect(r.reason).toContain('song score') |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + describe('artist matching strategies', () => { |
| 98 | + test('featured artist in returned song title → valid', () => { |
| 99 | + const r = validateLyricsMatch('Nicki Minaj', 'Monster', { |
| 100 | + artist: 'Kanye West', |
| 101 | + title: 'Monster featuring Nicki Minaj', |
| 102 | + }) |
| 103 | + expect(r.valid).toBe(true) |
| 104 | + }) |
| 105 | + |
| 106 | + test('feat. in requested artist, exact match on primary → valid', () => { |
| 107 | + const r = validateLyricsMatch('Kanye West feat. Jay-Z', 'Otis', result('Kanye West', 'Otis')) |
| 108 | + expect(r.valid).toBe(true) |
| 109 | + }) |
| 110 | + |
| 111 | + test('artist array field → valid', () => { |
| 112 | + const r = validateLyricsMatch('Taylor Swift', 'Shake It Off', { |
| 113 | + artists: ['Taylor Swift'], |
| 114 | + title: 'Shake It Off', |
| 115 | + }) |
| 116 | + expect(r.valid).toBe(true) |
| 117 | + }) |
| 118 | + |
| 119 | + test('trackArtist field → valid', () => { |
| 120 | + const r = validateLyricsMatch('Taylor Swift', 'Shake It Off', { |
| 121 | + trackArtist: 'Taylor Swift', |
| 122 | + trackName: 'Shake It Off', |
| 123 | + }) |
| 124 | + expect(r.valid).toBe(true) |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + describe('extension suffixes', () => { |
| 129 | + test('remix suffix → valid', () => { |
| 130 | + const r = validateLyricsMatch( |
| 131 | + 'Taylor Swift', |
| 132 | + 'Shake It Off', |
| 133 | + result('Taylor Swift', 'Shake It Off Remix'), |
| 134 | + ) |
| 135 | + expect(r.valid).toBe(true) |
| 136 | + }) |
| 137 | + |
| 138 | + test('live suffix → valid', () => { |
| 139 | + const r = validateLyricsMatch('Adele', 'Hello', result('Adele', 'Hello Live')) |
| 140 | + expect(r.valid).toBe(true) |
| 141 | + }) |
| 142 | + |
| 143 | + test('acoustic suffix → valid', () => { |
| 144 | + const r = validateLyricsMatch( |
| 145 | + 'Ed Sheeran', |
| 146 | + 'Shape of You', |
| 147 | + result('Ed Sheeran', 'Shape of You Acoustic'), |
| 148 | + ) |
| 149 | + expect(r.valid).toBe(true) |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + describe('adaptive threshold', () => { |
| 154 | + test('lenient threshold accepts near-miss', () => { |
| 155 | + const strict = validateLyricsMatch( |
| 156 | + 'Radiohead', |
| 157 | + 'Karma Police', |
| 158 | + result('Radio Head', 'Karma Police'), |
| 159 | + 0.99, |
| 160 | + ) |
| 161 | + const lenient = validateLyricsMatch( |
| 162 | + 'Radiohead', |
| 163 | + 'Karma Police', |
| 164 | + result('Radio Head', 'Karma Police'), |
| 165 | + 0.5, |
| 166 | + ) |
| 167 | + if (!strict.valid) { |
| 168 | + expect(lenient.valid).toBe(true) |
| 169 | + } |
| 170 | + }) |
| 171 | + |
| 172 | + test('short song name uses lower adaptive threshold', () => { |
| 173 | + const r = validateLyricsMatch('Jay-Z', 'Run', result('Jay-Z', 'Run')) |
| 174 | + expect(r.valid).toBe(true) |
| 175 | + }) |
| 176 | + }) |
| 177 | + |
| 178 | + describe('returned fields', () => { |
| 179 | + test('returned_artists is normalized array', () => { |
| 180 | + const r = validateLyricsMatch( |
| 181 | + 'Taylor Swift', |
| 182 | + 'Shake It Off', |
| 183 | + result('Taylor Swift', 'Shake It Off'), |
| 184 | + ) |
| 185 | + expect(Array.isArray(r.returned_artists)).toBe(true) |
| 186 | + expect(r.returned_artists).toContain('taylor swift') |
| 187 | + }) |
| 188 | + |
| 189 | + test('returned_song is normalized', () => { |
| 190 | + const r = validateLyricsMatch( |
| 191 | + 'Taylor Swift', |
| 192 | + 'Shake It Off', |
| 193 | + result('Taylor Swift', 'Shake It Off'), |
| 194 | + ) |
| 195 | + expect(r.returned_song).toBe('shake it off') |
| 196 | + }) |
| 197 | + |
| 198 | + test('scores are rounded to 3 decimal places', () => { |
| 199 | + const r = validateLyricsMatch( |
| 200 | + 'Taylor Swift', |
| 201 | + 'Shake It Off', |
| 202 | + result('Taylor Swift', 'Shake It Off'), |
| 203 | + ) |
| 204 | + expect(r.artist_match * 1000).toBe(Math.round(r.artist_match * 1000)) |
| 205 | + expect(r.song_match * 1000).toBe(Math.round(r.song_match * 1000)) |
| 206 | + }) |
| 207 | + }) |
| 208 | +}) |
0 commit comments