diff --git a/js/songs.js b/js/songs.js index 62114c1..3fd630d 100644 --- a/js/songs.js +++ b/js/songs.js @@ -360,9 +360,11 @@ function validateSong(s) { prog: String(s.prog || 'I-IV-V').trim().slice(0, 40), energy: Math.min(5, Math.max(1, parseInt(s.energy) || 3)), effort: Math.min(5, Math.max(1, parseInt(s.effort) || 2)), - instr: Array.isArray(s.instr) && s.instr.length - ? s.instr.filter(i => ['eg','ag','b','dr','k','sx','tp','tb','vo','bv','pc'].includes(i)) - : ['g'], + instr: (() => { + if (!Array.isArray(s.instr)) return ['g']; + const filtered = s.instr.filter(i => ['eg','ag','b','dr','k','sx','tp','tb','vo','bv','pc'].includes(i)); + return filtered.length > 0 ? filtered : ['g']; + })(), note: String(s.note || '').trim() }; } diff --git a/tests/run-tests.js b/tests/run-tests.js index 8e43fe4..4f971d4 100644 --- a/tests/run-tests.js +++ b/tests/run-tests.js @@ -318,8 +318,8 @@ runTest('Defaults to ["g"] if no valid instruments provided', () => { // If instr: ['invalid'], filter returns [], but .length check was on ORIGINAL s.instr. // Wait: s.instr.length is 1. filter returns []. So it returns []. // This looks like a bug in validateSong implementation if it wants to ensure at least one valid instrument. - // But I must test the CURRENT code. - assertArrayEqual(validateSong({instr: ['invalid']}).instr, []); + // Fix: Check filtered array length instead. + assertArrayEqual(validateSong({instr: ['invalid']}).instr, ['g']); // If instr: 'not-an-array', Array.isArray is false, returns ['g'] assertArrayEqual(validateSong({instr: 'not-an-array'}).instr, ['g']); });