From 8977441355b8aae09fdc284afaf436f00a0b268b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 26 Apr 2026 19:33:45 +0000 Subject: [PATCH] Fix validateSong instrument fallback logic The validateSong function was checking the original instrument array's length before filtering, which led to an empty array being returned if the input array contained only invalid instrument codes. This change modifies validateSong to check the length of the filtered instrument array, ensuring it falls back to the default ['g'] when no valid instruments are provided. Verified with updated tests in tests/run-tests.js. Co-authored-by: julesklord <801266+julesklord@users.noreply.github.com> --- js/songs.js | 8 +++++--- tests/run-tests.js | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) 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']); });