From 032e99f06587596176a6b129e22ed93a7a7296d1 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:34:02 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20[testing]=20add=20unit=20tests?= =?UTF-8?q?=20for=20normalizeInstr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Export `normalizeInstr` from `js/app.js` for testing. - Implement comprehensive unit tests in `tests/run-tests.js` covering: - Non-array and empty inputs (defaulting to ['g']). - Instrument name mapping to internal codes. - Case-insensitivity and whitespace handling. - Duplicate removal. - Filtering of invalid instruments. - Verified all tests pass with `npm test`. Co-authored-by: julesklord <801266+julesklord@users.noreply.github.com> --- js/app.js | 3 ++- tests/run-tests.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/js/app.js b/js/app.js index a630427..f20acd7 100644 --- a/js/app.js +++ b/js/app.js @@ -1623,6 +1623,7 @@ if (typeof module !== 'undefined' && module.exports) { cleanJSON, encryptApiKey, decryptApiKey, - parseCSVLine + parseCSVLine, + normalizeInstr }; } diff --git a/tests/run-tests.js b/tests/run-tests.js index 8e43fe4..482e062 100644 --- a/tests/run-tests.js +++ b/tests/run-tests.js @@ -333,6 +333,58 @@ runTest('Truncates prog field to 40 characters', () => { console.groupEnd(); +// 6. Tests for normalizeInstr +console.group('\n✅ Test Group: normalizeInstr'); + +runTest('Returns ["g"] for non-array input', () => { + assertArrayEqual(normalizeInstr(null), ['g']); + assertArrayEqual(normalizeInstr(undefined), ['g']); + assertArrayEqual(normalizeInstr('guitar'), ['g']); + assertArrayEqual(normalizeInstr(123), ['g']); + assertArrayEqual(normalizeInstr({}), ['g']); +}); + +runTest('Returns ["g"] for empty array', () => { + assertArrayEqual(normalizeInstr([]), ['g']); +}); + +runTest('Maps instrument names to internal codes', () => { + assertArrayEqual(normalizeInstr(['guitar']), ['g']); + assertArrayEqual(normalizeInstr(['guitarra']), ['g']); + assertArrayEqual(normalizeInstr(['piano', 'keyboards', 'keys']), ['p']); + assertArrayEqual(normalizeInstr(['winds', 'wind', 'brass', 'horns']), ['v']); + assertArrayEqual(normalizeInstr(['voice', 'vocals', 'vocal', 'voz', 'singing']), ['o']); +}); + +runTest('Accepts existing codes', () => { + assertArrayEqual(normalizeInstr(['g', 'p', 'v', 'o']), ['g', 'p', 'v', 'o']); +}); + +runTest('Handles case-insensitivity and trimming', () => { + assertArrayEqual(normalizeInstr([' GUITAR ', 'Vocals ']), ['g', 'o']); +}); + +runTest('Removes duplicates in the resulting codes', () => { + assertArrayEqual(normalizeInstr(['guitar', 'guitarra', 'g']), ['g']); + assertArrayEqual(normalizeInstr(['piano', 'keys', 'p', 'vocals', 'o']), ['p', 'o']); +}); + +runTest('Filters out invalid instruments', () => { + assertArrayEqual(normalizeInstr(['guitar', 'invalid-instrument', 'piano']), ['g', 'p']); +}); + +runTest('Returns ["g"] if all instruments in array are invalid', () => { + assertArrayEqual(normalizeInstr(['invalid1', 'invalid2']), ['g']); +}); + +runTest('Handles non-string elements by converting to string', () => { + // Map expects strings. String(null) is "null", String(1) is "1". + // None of these are in the MAP, so it should filter them out and return ['g'] + assertArrayEqual(normalizeInstr([1, null, true]), ['g']); +}); + +console.groupEnd(); + console.log(`\n=============================================`); console.log(`Test Summary: ${passed} passed, ${failed} failed`); console.log(`=============================================`);