forked from Furumi/furumi-web
Keep the episode position and skip unplayable items safely in the player
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c14ab8dfea
commit
4d0b352258
3 files changed
+152
-11
No files matched your search
@@ -96,3 +96,103 @@ test('restoring merges library tracks and external items', () => {
|
||||
assert.equal(restored.positions[3], 2);
|
||||
assert.equal(restored.positions[2], undefined);
|
||||
});
|
||||
|
||||
// Production methods by name, for flows that span several of them.
|
||||
function method(name) {
|
||||
const start = template.indexOf(`\n ${name}(`);
|
||||
assert.ok(start >= 0, `method ${name} not found`);
|
||||
const end = template.indexOf('\n },\n', start);
|
||||
return template.slice(start + 1, end + 11);
|
||||
}
|
||||
|
||||
function activePlayer(queueTracks, currentIndex, audioState = {}) {
|
||||
// The browser as the active (local) playback device.
|
||||
const names = [
|
||||
'_executeRemoteCommand', '_applyRemotePlaybackState', '_tickRemoteProgress',
|
||||
'_devicePlaybackStatePayload', '_remotePlaybackPayload', '_trackDuration',
|
||||
'_pauseLocal', '_playLocal', 'resume', 'next', 'playQueueIndex', 'seek',
|
||||
'_skipUnplayableCurrentItem', '_currentItemHasLocalAudio', '_showUnplayableQueueItem',
|
||||
'_queueItemSourceKind', '_isUnplayableQueueItem', '_playableQueueIndex',
|
||||
];
|
||||
const audio = Object.assign({
|
||||
paused: true, currentTime: 0, duration: NaN, src: '',
|
||||
played: [],
|
||||
pause() { this.paused = true; },
|
||||
play() { this.paused = false; this.played.push(this.src); return Promise.resolve(); },
|
||||
removeAttribute(name) { if (name === 'src') this.src = ''; },
|
||||
load() { this.currentTime = 0; },
|
||||
addEventListener() {}, removeEventListener() {},
|
||||
}, audioState);
|
||||
const queue = {
|
||||
tracks: queueTracks, currentIndex,
|
||||
_tracksWithJamDefaults: tracks => tracks,
|
||||
syncCurrentIndexToTrack(track) {
|
||||
const index = this.tracks.indexOf(track);
|
||||
if (index >= 0) this.currentIndex = index;
|
||||
},
|
||||
};
|
||||
const stores = {queue, devices: {heartbeat() {}}};
|
||||
const context = vm.createContext({audio, Alpine: {store: name => stores[name]}});
|
||||
vm.runInContext('this.methods = {' + names.map(method).join('\n') + '};', context);
|
||||
return Object.assign(context.methods, {
|
||||
audio, queue,
|
||||
currentTrack: queueTracks[currentIndex] || null, currentTime: 0, duration: 0, progress: 0,
|
||||
isPlaying: false, shuffle: false, repeatMode: 'off', volume: 0.5,
|
||||
_remoteStateBaseTime: 0, _remoteStateReceivedAt: 0,
|
||||
_isLocalPlaybackDevice: () => true, _shouldSendRemote: () => false,
|
||||
_shouldMirrorRemoteLocally: () => false,
|
||||
_setVolumeLocal(v) { this.volume = v; }, _updateMediaSession() {}, _saveState() {},
|
||||
_recordHistory() {}, _resetPlaybackTracking() {}, _mirrorRemoteTrack() {},
|
||||
});
|
||||
}
|
||||
|
||||
const episode = Object.assign({}, podcast, {duration_seconds: 1800});
|
||||
|
||||
test('handoff of a podcast episode keeps its position for the next handoff', () => {
|
||||
for (const audioTime of [95, 0]) {
|
||||
// <audio> still holds a music track (95 s) or the page is fresh (0 s).
|
||||
const p = activePlayer([song(1)], 0, {currentTime: audioTime, src: '/api/player/stream/1'});
|
||||
p._executeRemoteCommand({command: 'transfer_state', payload: {
|
||||
tracks: [song(1), episode], index: 1, track: episode,
|
||||
position_seconds: 1200, duration_seconds: 1800, paused: false,
|
||||
}});
|
||||
assert.equal(p.currentTrack, episode);
|
||||
assert.equal(p.currentTime, 1200);
|
||||
assert.equal(p.isPlaying, false);
|
||||
assert.equal(p.audio.src, '', 'the previous track is unloaded');
|
||||
const reported = p._devicePlaybackStatePayload();
|
||||
assert.equal(reported.position_seconds, 1200);
|
||||
assert.equal(reported.index, 1);
|
||||
assert.equal(reported.paused, true);
|
||||
}
|
||||
});
|
||||
|
||||
test('library tracks still report the audio position', () => {
|
||||
const p = activePlayer([song(1)], 0, {currentTime: 42, src: '/api/player/stream/1'});
|
||||
p.currentTime = 40;
|
||||
assert.equal(p._devicePlaybackStatePayload().position_seconds, 42);
|
||||
});
|
||||
|
||||
test('resume on an unplayable item skips it, also with repeat one', () => {
|
||||
for (const repeatMode of ['one', 'off', 'all']) {
|
||||
const p = activePlayer([podcast], 0);
|
||||
p.repeatMode = repeatMode;
|
||||
p.currentTime = 30;
|
||||
p.resume();
|
||||
p.next();
|
||||
assert.equal(p.currentTrack, podcast);
|
||||
assert.equal(p.isPlaying, false);
|
||||
assert.equal(p.currentTime, 30, 'the item keeps its position');
|
||||
assert.deepEqual(p.audio.played, []);
|
||||
}
|
||||
const p = activePlayer([song(1), podcast, song(2)], 1);
|
||||
p.repeatMode = 'one';
|
||||
p.resume();
|
||||
assert.equal(p.currentTrack.id, 2);
|
||||
assert.deepEqual(p.audio.played, ['/api/player/stream/2']);
|
||||
// Repeat all wraps around to the first playable item.
|
||||
const wrap = activePlayer([song(1), podcast], 1);
|
||||
wrap.repeatMode = 'all';
|
||||
wrap.resume();
|
||||
assert.equal(wrap.currentTrack.id, 1);
|
||||
});
|
||||
Reference in new issue
Block a user