Skip to content
File

Blob: worker/src/client/radio/playback.test.ts

typescript37 lines
1import assert from 'node:assert/strict';
2import { test } from 'node:test';
3import { acceptMetadata, matchesPlayback, playbackView } from './playback.ts';
4import { initialState } from './session-state.ts';
5 
6const first = { title: 'First', artist: '', durationMs: 2000 };
7const current = { revision: 0xffff_ffff, trackIndex: 0, trackCount: 2, track: first };
8 
9test('metadata wraps revisions and ignores delayed or conflicting same-revision snapshots', () => {
10 const next = { ...current, revision: 0, trackIndex: 1, track: null };
11 assert.equal(acceptMetadata(current, next), next);
12 assert.equal(acceptMetadata(next, current), next);
13 assert.equal(acceptMetadata(next, { ...next, track: first }), next);
14 assert.equal(acceptMetadata(undefined, current), current);
15 assert.equal(matchesPlayback(current.revision, next), false);
16 assert.equal(matchesPlayback(0, next), true);
17 assert.equal(matchesPlayback(undefined, next), true);
18});
19 
20test('unknown current metadata never falls back to an older HTTP song', () => {
21 const state = initialState();
22 state.room.track = first;
23 assert.equal(playbackView(state).track, first);
24 state.nowPlaying = { ...current, track: null };
25 assert.equal(playbackView(state).track, undefined);
26 assert.equal(playbackView(state).duration, 0);
27 state.nowPlaying = current;
28 state.paused = true;
29 assert.deepEqual(playbackView(state), {
30 track: first,
31 trackCount: 2,
32 position: 0,
33 duration: 2000,
34 paused: true,
35 });
36});