Files
feedBack/tests/js/song_close.test.js
T
d2b2a7e9f7 fix(tests): re-green the JS suite — 18 stale source-shape tests + 1 real seek-reason violation (#740)
main's JS suite has been red since the recent v3-library and player
refactors landed. 17 of 18 failures were test harnesses/regexes that
went stale behind real, intentional code changes; one was a genuine
contract violation in the code.

Code fix:
- session-resume seek passed 'resume' as its _audioSeek reason; the
  documented contract (enforced by song_seek.test.js) requires
  multi-word kebab-case. Renamed to 'session-resume' — no consumer
  string-matches specific reasons, so this is rename-safe.

Test updates (each pins the CURRENT contract):
- highway_colors_facade: inject HWC_PRESETS + applyHighwayStringPreset
  (new preset feature); lock presets/applyPreset into the surface test
- loop_api: stub _updateEditRegionBtn (new edit-region UI hook)
- song_close: sandbox gets window.feedBack.playQueue; assert a real
  close abandons the queue (the new queue-aware behavior)
- v3_keep_practicing: the shelf moved from client-side /api/stats/recent
  dedupe+gating to the server-side practice-suggestions recommender —
  tests now pin that (fetch, arrangement-aware card click, Promise.all)
- v3_songs_tuning: card row variable renamed song → shown (grouped cards)
- live_guitar_tone_source: accept literal ’ where ’ drifted in copy
- legacy_shim_hits: normalize CRLF before fixed-width region() slicing
  (Windows-only failure; char windows shrank by one char per line)

Suite: 987/987 locally (Windows), previously 968/987 (and 18 red on CI).

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 14:35:07 +02:00

106 lines
4.2 KiB
JavaScript

// Verify closeCurrentSong() exits via origin-aware showScreen without
// restart, seek, playSong reload, or direct audio mutation.
//
// Same isolation strategy as song_restart.test.js — extract the function
// from app.js by brace-matching and run it in a vm sandbox.
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const { extractFunction } = require('./test_utils');
const APP_JS = path.join(__dirname, '..', '..', 'static', 'app.js');
function buildSandbox({ playerOriginScreen = 'home' } = {}) {
const sandbox = {
_playerOriginScreen: playerOriginScreen,
__showScreenCalls: [],
__restartCalls: 0,
__seekCalls: 0,
__playSongCalls: 0,
__clearLoopCalls: 0,
__audioCurrentTimeSets: [],
audio: {
_t: 42,
get currentTime() { return sandbox.audio._t; },
set currentTime(v) { sandbox.__audioCurrentTimeSets.push(v); sandbox.audio._t = v; },
},
};
vm.createContext(sandbox);
return sandbox;
}
function loadClose(sandbox, src) {
const closeSrc = extractFunction(src, 'function closeCurrentSong(');
const code = `
var _playerOriginScreen = ${JSON.stringify(sandbox._playerOriginScreen)};
globalThis.__showScreenCalls = [];
globalThis.__restartCalls = 0;
globalThis.__seekCalls = 0;
globalThis.__playSongCalls = 0;
globalThis.__clearLoopCalls = 0;
globalThis.__queueClearCalls = 0;
globalThis.__audioCurrentTimeSets = [];
// closeCurrentSong abandons any play-queue before leaving the player.
var window = { feedBack: { playQueue: { clear() { globalThis.__queueClearCalls++; } } } };
var audio = {
_t: 42,
get currentTime() { return this._t; },
set currentTime(v) { globalThis.__audioCurrentTimeSets.push(v); this._t = v; }
};
function showScreen(id) {
globalThis.__showScreenCalls.push(id);
return Promise.resolve();
}
function restartCurrentSong() { globalThis.__restartCalls++; }
async function _audioSeek() { globalThis.__seekCalls++; }
function playSong() { globalThis.__playSongCalls++; }
function clearLoop() { globalThis.__clearLoopCalls++; }
${closeSrc}
globalThis.__closeCurrentSong = closeCurrentSong;
`;
vm.runInContext(code, sandbox);
}
test('closeCurrentSong is exported on window and window.feedBack', () => {
const src = fs.readFileSync(APP_JS, 'utf8');
assert.match(src, /window\.closeCurrentSong\s*=\s*closeCurrentSong/);
assert.match(src, /window\.feedBack\.closeCurrentSong\s*=\s*closeCurrentSong/);
});
test('closeCurrentSong uses _playerOriginScreen when set', async () => {
const src = fs.readFileSync(APP_JS, 'utf8');
const sandbox = buildSandbox({ playerOriginScreen: 'favorites' });
loadClose(sandbox, src);
await sandbox.__closeCurrentSong();
assert.equal(sandbox.__showScreenCalls.length, 1);
assert.equal(sandbox.__showScreenCalls[0], 'favorites');
assert.equal(sandbox.__queueClearCalls, 1, 'a real close abandons the play-queue');
assert.equal(sandbox.__restartCalls, 0);
assert.equal(sandbox.__seekCalls, 0);
assert.equal(sandbox.__playSongCalls, 0);
assert.equal(sandbox.__clearLoopCalls, 0);
assert.equal(sandbox.__audioCurrentTimeSets.length, 0);
});
test('closeCurrentSong falls back to home when origin missing', async () => {
const src = fs.readFileSync(APP_JS, 'utf8');
const sandbox = buildSandbox({ playerOriginScreen: null });
loadClose(sandbox, src);
await sandbox.__closeCurrentSong();
assert.equal(sandbox.__showScreenCalls.length, 1);
assert.equal(sandbox.__showScreenCalls[0], 'home');
});
test('closeCurrentSong falls back to home when origin is empty string', async () => {
const src = fs.readFileSync(APP_JS, 'utf8');
const sandbox = buildSandbox({ playerOriginScreen: '' });
loadClose(sandbox, src);
await sandbox.__closeCurrentSong();
assert.equal(sandbox.__showScreenCalls.length, 1);
assert.equal(sandbox.__showScreenCalls[0], 'home');
});