Files
Psychotoxical-psysonic/src/store/seekTargetState.test.ts
T
Frank Stellmacher 4c64844349 refactor(player): E.18 — extract three transport-coordination modules (#581)
Cluster of three small thematically-related cuts in one PR (per the new
'cluster, don't single-shot' convention):

  - `src/store/seekTargetState.ts` — the seek-target guard (`seekTarget`,
    `seekTargetSetAt`, `SEEK_TARGET_GUARD_TIMEOUT_MS`, set/clear/get
    accessors) that suppresses stale Rust progress ticks until the
    engine catches up to the requested position
  - `src/store/togglePlayLock.ts` — the 300 ms double-click cooldown
    behind a `tryAcquireTogglePlayLock()` helper that auto-releases on
    a timer (collapses the three-line inline pattern in `togglePlay`)
  - `src/store/loudnessReseed.ts` — the full `reseedLoudnessForTrackId`
    pipeline (gen-bump → cache + backfill wipe → state reset → server
    row delete → forced seed enqueue), pulled out of playerStore as a
    single async helper

All three were file-private; no caller-side changes outside
playerStore's own imports. The progress handler's seek-guard branch is
now ~3 lines shorter and reads through accessors. `togglePlay` collapses
to one guard check.

24 tests across the three modules pin the API contracts.

playerStore 3189 → 3139 LOC.
2026-05-12 15:23:40 +02:00

60 lines
1.5 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
SEEK_TARGET_GUARD_TIMEOUT_MS,
_resetSeekTargetStateForTest,
clearSeekTarget,
getSeekTarget,
getSeekTargetSetAt,
setSeekTarget,
} from './seekTargetState';
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-05-12T12:00:00Z'));
});
afterEach(() => {
_resetSeekTargetStateForTest();
vi.useRealTimers();
});
describe('SEEK_TARGET_GUARD_TIMEOUT_MS', () => {
it('is the value the progress handler expects', () => {
expect(SEEK_TARGET_GUARD_TIMEOUT_MS).toBe(5000);
});
});
describe('seekTargetState', () => {
it('returns null + 0 before any seek', () => {
expect(getSeekTarget()).toBeNull();
expect(getSeekTargetSetAt()).toBe(0);
});
it('stores target + timestamp on set', () => {
setSeekTarget(42);
expect(getSeekTarget()).toBe(42);
expect(getSeekTargetSetAt()).toBe(Date.now());
});
it('updates timestamp when called again', () => {
setSeekTarget(10);
const first = getSeekTargetSetAt();
vi.advanceTimersByTime(1000);
setSeekTarget(20);
expect(getSeekTarget()).toBe(20);
expect(getSeekTargetSetAt()).toBeGreaterThan(first);
});
it('clearSeekTarget resets both fields', () => {
setSeekTarget(42);
clearSeekTarget();
expect(getSeekTarget()).toBeNull();
expect(getSeekTargetSetAt()).toBe(0);
});
it('clearSeekTarget is a no-op when nothing is set', () => {
expect(() => clearSeekTarget()).not.toThrow();
expect(getSeekTarget()).toBeNull();
});
});