Files
feedBack/tests/js/song_restart.test.js
T
5fb28d5c5a refactor(app): lift the shared player state onto a container (R3a) (#889)
static/js/player-state.js — one exported object, two fields. app.js's 70 reference
sites rewritten. Provably a no-op; nothing shrinks.

WHY NOW. Every slice carved out of app.js so far only ever READ the state it shared
(loopA/loopB, _audioSeekGen, currentFilename), so a read-only getter hook was enough
and no container was needed — twice I checked and twice I got away with it. That
runs out at count-in: it genuinely WRITES `isPlaying` (it starts and stops playback,
4 sites) and `lastAudioTime` (2). `import { isPlaying }` then `isPlaying = true`
THROWS — an imported binding cannot be assigned to. So the state has to live on an
object: `S.isPlaying = true` is a property write, and works from any module holding
the same S. Same shape stems, studio, and editor all converged on.

DELIBERATELY SMALL. app.js has ~104 top-level `let` scalars; lifting all of them is
a ~977-site rewrite for no benefit, because most are private to one cluster and
travel with it. Only what a carved module must WRITE goes here. Add on demand.

THE REWRITE IS AST-DRIVEN, NOT TEXTUAL — and that is not fussiness. Of 100 textual
occurrences of these two names, only 70 resolve to the module binding:
  * 22 are member accesses (`someObj.isPlaying`, `window.feedBack.isPlaying`)
  * 4 are the LOCAL PARAMETER of `function setPlayButtonState(isPlaying)` — a blind
    replace yields `function setPlayButtonState(S.isPlaying)`
  * 1 is an object key
  * 2 are shorthand properties `{ isPlaying }`, which must become
    `{ isPlaying: S.isPlaying }` — and acorn gives a shorthand's key and value the
    SAME range, so rewriting both produced `isPlaying: S.isPlaying: S.isPlaying`
    until I deduped by range
A find-and-replace corrupts all 29. The rewrite walks the AST, skips shadows, member
properties and keys, and replaces identifier RANGES.

`window.feedBack.isPlaying` — the PUBLIC mirror — is a different thing and is
untouched. Two test sandboxes stub it; those were left alone deliberately.

VERIFIED WITH REAL PLAYBACK. A/B against origin/main in two browsers, real song:
togglePlay -> the public mirror goes true -> false -> true across two toggles, the
audio element's paused state follows, seekBy works — IDENTICAL, zero page errors.

Harnesses: 8 vm-sandbox suites slice playback code out of app.js and now see
S.isPlaying — juce_engine_reroute, loop_restart, play_button_reroute_guard,
playback_app_adapter, song_restart, song_seek, speed_reset, and the python
idempotence source-assert. Each gets the same container in its sandbox; every
assertion is unchanged.

pytest 2396, node 1040/1040, ESLint 0, tailwind clean, Codex 0.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 21:52:51 +02:00

142 lines
5.6 KiB
JavaScript

// Verify restartCurrentSong() uses the canonical _audioSeek / togglePlay /
// startCountIn paths without clearing loops or reloading the song.
//
// Same isolation strategy as song_seek.test.js — extract the function from
// app.js by brace-matching and run it in a vm sandbox with stubbed deps.
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');
const V3_HTML = path.join(__dirname, '..', '..', 'static', 'v3', 'index.html');
function buildSandbox({ loopA = null, loopB = null, isPlaying = false } = {}) {
const sandbox = {
loopA,
loopB,
// isPlaying moved onto the shared player-state container so a carved module can
// WRITE it (an imported binding is read-only). Same value, same assertions.
S: { isPlaying, lastAudioTime: 0 },
__cancelCountInCalls: 0,
__seekCalls: [],
__startCountInCalls: [],
__togglePlayCalls: 0,
__clearLoopCalls: 0,
window: {
feedBack: {
getLoop() {
return { loopA: sandbox.loopA, loopB: sandbox.loopB };
},
},
},
__audioSeek(s, reason) {
sandbox.__seekCalls.push({ s, reason });
return Promise.resolve({ completed: true, from: 30, to: s });
},
__startCountIn(opts) {
sandbox.__startCountInCalls.push(opts);
return Promise.resolve();
},
__togglePlay() {
sandbox.__togglePlayCalls++;
sandbox.S.isPlaying = true;
return Promise.resolve();
},
};
vm.createContext(sandbox);
return sandbox;
}
function loadRestart(sandbox, src, { audioSeekImpl } = {}) {
const restartSrc = extractFunction(src, 'async function restartCurrentSong(');
const code = `
var S = { isPlaying: ${sandbox.S.isPlaying}, lastAudioTime: 0 };
function _cancelCountIn() { __cancelCountInCalls++; }
async function _audioSeek(s, reason) {
return (${audioSeekImpl || '__audioSeek'})(s, reason);
}
async function startCountIn(opts) { return __startCountIn(opts); }
async function togglePlay() { return __togglePlay(); }
function clearLoop() { __clearLoopCalls++; }
${restartSrc}
globalThis.__restartCurrentSong = restartCurrentSong;
`;
vm.runInContext(code, sandbox);
}
test('restartCurrentSong is exported on window and window.feedBack', () => {
const src = fs.readFileSync(APP_JS, 'utf8');
assert.match(src, /window\.restartCurrentSong\s*=\s*restartCurrentSong/);
assert.match(src, /window\.feedBack\.restartCurrentSong\s*=\s*restartCurrentSong/);
});
test('no loop: seeks to 0 with song-restart and starts playback when stopped', async () => {
const src = fs.readFileSync(APP_JS, 'utf8');
const sandbox = buildSandbox({ isPlaying: false });
loadRestart(sandbox, src);
const ok = await sandbox.__restartCurrentSong();
assert.equal(ok, true);
assert.equal(sandbox.__cancelCountInCalls, 1);
assert.equal(sandbox.__seekCalls.length, 1);
assert.equal(sandbox.__seekCalls[0].s, 0);
assert.equal(sandbox.__seekCalls[0].reason, 'song-restart');
assert.equal(sandbox.__togglePlayCalls, 1);
assert.equal(sandbox.__startCountInCalls.length, 0);
assert.equal(sandbox.__clearLoopCalls, 0);
});
test('already playing, no loop: seeks to 0 and does not toggle play', async () => {
const src = fs.readFileSync(APP_JS, 'utf8');
const sandbox = buildSandbox({ isPlaying: true });
loadRestart(sandbox, src);
const ok = await sandbox.__restartCurrentSong();
assert.equal(ok, true);
assert.equal(sandbox.__seekCalls[0].s, 0);
assert.equal(sandbox.__togglePlayCalls, 0);
assert.equal(sandbox.__clearLoopCalls, 0);
});
test('loop armed: seeks to loopA, preserves loop, re-enters via startCountIn immediate', async () => {
const src = fs.readFileSync(APP_JS, 'utf8');
const sandbox = buildSandbox({ loopA: 12.5, loopB: 48, isPlaying: false });
loadRestart(sandbox, src);
const ok = await sandbox.__restartCurrentSong();
assert.equal(ok, true);
assert.equal(sandbox.__seekCalls.length, 1);
assert.equal(sandbox.__seekCalls[0].s, 12.5);
assert.equal(sandbox.__seekCalls[0].reason, 'song-restart');
assert.equal(sandbox.__startCountInCalls.length, 1);
assert.equal(sandbox.__startCountInCalls[0].immediate, true);
assert.equal(sandbox.__togglePlayCalls, 0);
assert.equal(sandbox.__clearLoopCalls, 0);
assert.equal(sandbox.loopB, 48, 'loopB must be preserved');
});
test('failed/incomplete seek: does not start playback or count-in', async () => {
const src = fs.readFileSync(APP_JS, 'utf8');
const sandbox = buildSandbox({ isPlaying: false });
loadRestart(sandbox, src, {
audioSeekImpl: '(s, reason) => Promise.resolve({ completed: false, from: NaN, to: NaN })',
});
const ok = await sandbox.__restartCurrentSong();
assert.equal(ok, false);
assert.equal(sandbox.__togglePlayCalls, 0);
assert.equal(sandbox.__startCountInCalls.length, 0);
});
test('V3 transport restart button exists with correct attributes', () => {
const html = fs.readFileSync(V3_HTML, 'utf8');
assert.match(html, /v3-transport-mid[\s\S]*onclick="restartCurrentSong\(\)"/);
assert.match(html, /title="Restart song"/);
assert.match(html, /aria-label="Restart song"/);
});