Files
minstrel/web/src/lib/player/persisted.test.ts
T
bvandeusen d83b3eab25 fix(web/m7-364): TrackRef field shape + seek-on-restore + drawer inert
CI svelte-check failed on 5 type errors caused by the slice using
duration_ms/album_name fields that don't exist on TrackRef (the real
shape uses duration_sec + album_id/album_title). Fixed across
QueueDrawer, QueueDrawer.test, QueueTrackRow.test, persisted.test,
and the inline `state.current = null` in PlayerBar.test (TrackRef |
undefined, not | null).

Final whole-slice review concerns also rolled in:

- C1 (Critical): persisted position was restored to UI but never
  seeked into the audio element — first timeupdate snapped _position
  back to 0. Added a one-shot _pendingRestorePosition module ref +
  consumePendingRestorePosition() export; layout's loadedmetadata
  handler now seeks once on restore.

- I1 (Important): throttled-write effect now wraps _queue / _index
  reads in untrack so the dependency tracking matches the design
  intent. Position remains the only tracked dep.

- I4 (Important): drawer gets inert={!queueDrawerOpen} so its inner
  buttons drop out of tab order when closed (aria-hidden alone
  doesn't do that). Removed redundant role="complementary" from
  <aside> (implied by the element). New test asserts inert binding.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-03 22:14:59 -04:00

72 lines
2.3 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { readPersistedQueue, writePersistedQueue, clearPersistedQueue } from './persisted';
import type { TrackRef } from '$lib/api/types';
const sampleTrack: TrackRef = {
id: 't1',
title: 'Foo',
album_id: 'al1',
album_title: 'Baz',
artist_id: 'ar1',
artist_name: 'Bar',
duration_sec: 200,
stream_url: '/stream/t1'
};
describe('persisted queue', () => {
beforeEach(() => {
localStorage.clear();
});
afterEach(() => {
localStorage.clear();
vi.restoreAllMocks();
});
it('write-then-read round-trips queue / index / position', () => {
writePersistedQueue('user-1', { queue: [sampleTrack], index: 0, position: 42.5 });
const out = readPersistedQueue('user-1');
expect(out).not.toBeNull();
expect(out!.v).toBe(1);
expect(out!.queue).toEqual([sampleTrack]);
expect(out!.index).toBe(0);
expect(out!.position).toBe(42.5);
expect(typeof out!.savedAt).toBe('number');
});
it('read returns null when no entry is set', () => {
expect(readPersistedQueue('user-1')).toBeNull();
});
it('read returns null on malformed JSON', () => {
localStorage.setItem('minstrel.queue.user-1', '{not-json');
expect(readPersistedQueue('user-1')).toBeNull();
});
it('read returns null when version is not 1', () => {
localStorage.setItem(
'minstrel.queue.user-1',
JSON.stringify({ v: 2, queue: [], index: 0, position: 0, savedAt: 0 })
);
expect(readPersistedQueue('user-1')).toBeNull();
});
it('clearPersistedQueue removes only the named user key', () => {
writePersistedQueue('user-a', { queue: [sampleTrack], index: 0, position: 0 });
writePersistedQueue('user-b', { queue: [sampleTrack], index: 0, position: 0 });
clearPersistedQueue('user-a');
expect(readPersistedQueue('user-a')).toBeNull();
expect(readPersistedQueue('user-b')).not.toBeNull();
});
it('write swallows QuotaExceededError without throwing', () => {
const setItem = vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error('QuotaExceededError');
});
expect(() =>
writePersistedQueue('user-1', { queue: [sampleTrack], index: 0, position: 0 })
).not.toThrow();
setItem.mockRestore();
});
});