refactor(playback): move the audio engine into features/playback

Relocate the playback/queue/transport/audio-output engine out of the type-first
store/ + utils/playback/ + utils/audio/ dirs into a cohesive src/features/playback/,
structure-preserving:
  store/<x>                    -> features/playback/store/<x>
  store/audioListenerSetup/<x> -> features/playback/store/audioListenerSetup/<x>
  utils/playback/<x>           -> features/playback/utils/playback/<x>
  utils/audio/<x>              -> features/playback/utils/audio/<x>

184 files moved (107 source + 77 tests), 365 consumers rewritten. Pure move — no
behavior change, no state-split (the playerStore state-split stays a separate M5
question). Enabled by this session's decouple seams (artist/offline/orbit/auth →
core registries), so the engine carries no inbound core->feature inversion: store/
now holds only the 50 cross-cutting global stores (auth family, the seams, library
index, UI/settings stores).

KEPT OUT of the move (would re-create global->engine edges): the 3 pure config
helpers utils/audio/{loudnessPreAnalysisSlider,hiResCrossfadeResample} +
utils/playback/autodjOverlapCap (authStore + settings UI read them — they stay in
utils/). Ambiguous view-state stores (eqStore, queueToolbarStore,
playerBarLayoutStore) stay global (no engine imports).

Consumers use DEEP paths (@/features/playback/...), no barrel — matches the lib/
approach and avoids barrel-mock-collapse across the 140 usePlayerStore consumers.
Two tolerated type-only core->feature edges remain (localPlaybackStore->QueueItemRef,
localPlaybackMigration->HotCacheEntry, both erased).

tsc 0, lint 0, full suite 319/2353 green, iron-rule clean (no runtime store->feature
import). Behavior-touching only via the prerequisite bridge seam (already QA-flagged);
the move itself is pure.
This commit is contained in:
Psychotoxical
2026-06-30 15:00:20 +02:00
parent 6651abbc6f
commit cb1a110afb
393 changed files with 1127 additions and 1127 deletions
@@ -0,0 +1,197 @@
/**
* `refreshLoudnessForTrack` orchestrates the loudness analysis fetch:
* coalesce concurrent calls, distinguish hit vs miss, enqueue backfill
* within bounds, suppress stale-target results. The individual helpers
* (cache, backfill state, window predicate, debug emit) are tested in
* their own modules — this file pins the orchestration only.
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
const hoisted = vi.hoisted(() => {
const auth = {
loudnessTargetLufs: -14,
normalizationEngine: 'loudness' as 'off' | 'replaygain' | 'loudness',
};
const playerState = {
queue: [] as Array<{ id: string }>,
queueIndex: 0,
currentTrack: null as { id: string } | null,
updateReplayGainForCurrentTrack: vi.fn(),
};
return {
auth,
playerState,
invokeMock: vi.fn(async (_cmd: string, _args?: Record<string, unknown>) => null as unknown),
buildStreamUrlMock: vi.fn((id: string) => `https://mock/stream/${id}`),
redactMock: vi.fn((s: string) => s),
playerSetStateMock: vi.fn(),
emitDebugMock: vi.fn(),
forgetLoudnessMock: vi.fn(),
markLoudnessStableMock: vi.fn(),
getBackfillAttemptsMock: vi.fn(() => 0),
isBackfillInFlightMock: vi.fn(() => false),
markBackfillInFlightMock: vi.fn(),
clearBackfillInFlightMock: vi.fn(),
resetBackfillAttemptsMock: vi.fn(),
isTrackInsideWindowMock: vi.fn(() => true),
};
});
vi.mock('@tauri-apps/api/core', () => ({ invoke: hoisted.invokeMock }));
vi.mock('@/lib/api/subsonicStreamUrl', () => ({ buildStreamUrl: hoisted.buildStreamUrlMock }));
vi.mock('@/utils/server/redactSubsonicUrl', () => ({ redactSubsonicUrlForLog: hoisted.redactMock }));
vi.mock('@/store/authStore', () => ({ useAuthStore: { getState: () => hoisted.auth } }));
vi.mock('@/features/playback/store/playerStore', () => ({
usePlayerStore: {
getState: () => hoisted.playerState,
setState: hoisted.playerSetStateMock,
},
}));
vi.mock('@/features/playback/store/normalizationDebug', () => ({ emitNormalizationDebug: hoisted.emitDebugMock }));
vi.mock('@/features/playback/store/loudnessGainCache', () => ({
forgetLoudnessGain: hoisted.forgetLoudnessMock,
markLoudnessStable: hoisted.markLoudnessStableMock,
}));
vi.mock('@/features/playback/store/loudnessBackfillState', () => ({
MAX_BACKFILL_ATTEMPTS_PER_TRACK: 2,
clearBackfillInFlight: hoisted.clearBackfillInFlightMock,
getBackfillAttempts: hoisted.getBackfillAttemptsMock,
isBackfillInFlight: hoisted.isBackfillInFlightMock,
markBackfillInFlight: hoisted.markBackfillInFlightMock,
resetBackfillAttempts: hoisted.resetBackfillAttemptsMock,
}));
vi.mock('@/features/playback/store/loudnessBackfillWindow', () => ({
LOUDNESS_BACKFILL_WINDOW_AHEAD: 5,
isTrackInsideLoudnessBackfillWindow: hoisted.isTrackInsideWindowMock,
loudnessBackfillPriorityForTrack: vi.fn(() => 'middle'),
}));
import {
_resetLoudnessRefreshInflightForTest,
refreshLoudnessForTrack,
} from '@/features/playback/store/loudnessRefresh';
beforeEach(() => {
_resetLoudnessRefreshInflightForTest();
hoisted.auth.loudnessTargetLufs = -14;
hoisted.auth.normalizationEngine = 'loudness';
hoisted.playerState.queue = [];
hoisted.playerState.queueIndex = 0;
hoisted.playerState.currentTrack = null;
hoisted.invokeMock.mockReset();
hoisted.invokeMock.mockResolvedValue(null);
hoisted.playerSetStateMock.mockClear();
hoisted.emitDebugMock.mockClear();
hoisted.forgetLoudnessMock.mockClear();
hoisted.markLoudnessStableMock.mockClear();
hoisted.markBackfillInFlightMock.mockClear();
hoisted.clearBackfillInFlightMock.mockClear();
hoisted.resetBackfillAttemptsMock.mockClear();
hoisted.getBackfillAttemptsMock.mockReset();
hoisted.getBackfillAttemptsMock.mockReturnValue(0);
hoisted.isBackfillInFlightMock.mockReset();
hoisted.isBackfillInFlightMock.mockReturnValue(false);
hoisted.isTrackInsideWindowMock.mockReset();
hoisted.isTrackInsideWindowMock.mockReturnValue(true);
hoisted.playerState.updateReplayGainForCurrentTrack = vi.fn();
});
describe('refreshLoudnessForTrack', () => {
it('is a no-op for empty trackId', async () => {
await refreshLoudnessForTrack('');
expect(hoisted.invokeMock).not.toHaveBeenCalled();
});
it('coalesces concurrent calls for the same key into one inflight promise', async () => {
hoisted.invokeMock.mockResolvedValue(null);
const p1 = refreshLoudnessForTrack('t1');
const p2 = refreshLoudnessForTrack('t1');
await Promise.all([p1, p2]);
// One analysis_get_loudness_for_track call shared between both awaiters.
const getCalls = hoisted.invokeMock.mock.calls.filter(c => c[0] === 'analysis_get_loudness_for_track');
expect(getCalls).toHaveLength(1);
});
it('marks loudness stable on a hit row', async () => {
hoisted.invokeMock.mockResolvedValueOnce({ recommendedGainDb: -7, targetLufs: -14, updatedAt: 123 });
await refreshLoudnessForTrack('t1');
expect(hoisted.markLoudnessStableMock).toHaveBeenCalledWith('t1', -7);
expect(hoisted.resetBackfillAttemptsMock).toHaveBeenCalledWith('t1');
});
it('forgets the cached value on a miss row', async () => {
hoisted.invokeMock.mockResolvedValueOnce(null);
await refreshLoudnessForTrack('t1');
expect(hoisted.forgetLoudnessMock).toHaveBeenCalledWith('t1');
});
it('enqueues a backfill when conditions are met (loudness engine, not inflight, attempts < max, in window)', async () => {
hoisted.invokeMock.mockResolvedValueOnce(null);
await refreshLoudnessForTrack('t1');
expect(hoisted.markBackfillInFlightMock).toHaveBeenCalledWith('t1', 1);
const enqueueCall = hoisted.invokeMock.mock.calls.find(c => c[0] === 'analysis_enqueue_seed_from_url');
expect(enqueueCall).toBeDefined();
});
it('skips backfill when outside the prefetch window', async () => {
hoisted.invokeMock.mockResolvedValueOnce(null);
hoisted.isTrackInsideWindowMock.mockReturnValueOnce(false);
await refreshLoudnessForTrack('t1');
expect(hoisted.markBackfillInFlightMock).not.toHaveBeenCalled();
const enqueueCall = hoisted.invokeMock.mock.calls.find(c => c[0] === 'analysis_enqueue_seed_from_url');
expect(enqueueCall).toBeUndefined();
});
it('skips backfill when attempts already at max', async () => {
hoisted.invokeMock.mockResolvedValueOnce(null);
hoisted.getBackfillAttemptsMock.mockReturnValueOnce(2);
await refreshLoudnessForTrack('t1');
expect(hoisted.markBackfillInFlightMock).not.toHaveBeenCalled();
const throttledCalls = hoisted.emitDebugMock.mock.calls.filter(c => c[0] === 'backfill:throttled');
expect(throttledCalls.length).toBeGreaterThan(0);
});
it('skips backfill when already inflight', async () => {
hoisted.invokeMock.mockResolvedValueOnce(null);
hoisted.isBackfillInFlightMock.mockReturnValueOnce(true);
await refreshLoudnessForTrack('t1');
expect(hoisted.markBackfillInFlightMock).not.toHaveBeenCalled();
});
it('discards results and retries when the LUFS target changes mid-flight', async () => {
hoisted.invokeMock.mockImplementationOnce(async () => {
hoisted.auth.loudnessTargetLufs = -10; // target changes during await
return { recommendedGainDb: -5, targetLufs: -14, updatedAt: 1 };
});
hoisted.invokeMock.mockResolvedValueOnce(null); // retry returns miss
await refreshLoudnessForTrack('t1');
// markLoudnessStable should NOT have been called from the first invocation —
// result is discarded because target changed.
expect(hoisted.markLoudnessStableMock).not.toHaveBeenCalled();
const staleCalls = hoisted.emitDebugMock.mock.calls.filter(c => c[0] === 'refresh:stale-target');
expect(staleCalls).toHaveLength(1);
// Drain pending recursive retries spawned via `void refreshLoudnessForTrack(...)`
// so they don't bleed into the next test's mock queue.
for (let i = 0; i < 10; i++) await Promise.resolve();
});
it('skips engine update when syncPlayingEngine is false', async () => {
hoisted.invokeMock.mockResolvedValueOnce({ recommendedGainDb: -7, targetLufs: -14, updatedAt: 1 });
await refreshLoudnessForTrack('t1', { syncPlayingEngine: false });
expect(hoisted.playerState.updateReplayGainForCurrentTrack).not.toHaveBeenCalled();
});
it('calls updateReplayGainForCurrentTrack by default on hit', async () => {
hoisted.invokeMock.mockResolvedValueOnce({ recommendedGainDb: -7, targetLufs: -14, updatedAt: 1 });
await refreshLoudnessForTrack('t1');
expect(hoisted.playerState.updateReplayGainForCurrentTrack).toHaveBeenCalledTimes(1);
});
it('forgets cache + emits refresh:error on a thrown invoke', async () => {
hoisted.invokeMock.mockRejectedValueOnce(new Error('rust busy'));
await refreshLoudnessForTrack('t1');
expect(hoisted.forgetLoudnessMock).toHaveBeenCalledWith('t1');
const errCalls = hoisted.emitDebugMock.mock.calls.filter(c => c[0] === 'refresh:error');
expect(errCalls).toHaveLength(1);
});
});