diff --git a/templates/player/scripts.html b/templates/player/scripts.html
index a2b9485..46db3b1 100644
--- a/templates/player/scripts.html
+++ b/templates/player/scripts.html
@@ -1734,7 +1734,14 @@ document.addEventListener('alpine:init', () => {
_playEpisodeLocal(track, options = {}) {
const ref = this._episodeRef(track);
if (!ref) return;
+ const previous = this._episode;
this._endEpisode('stop');
+ // Preparing the same episode again (for example Play after its
+ // source failed): its final report must reach the register
+ // first, or the server would resume from an older checkpoint
+ // and refuse the report as superseded afterwards.
+ const before = previous && previous.episodeId === ref.episode_id
+ ? previous.writes : null;
const token = ++this._episodeToken;
const raw = options.position_seconds;
const known = raw != null && raw !== '' && Number.isFinite(Number(raw));
@@ -1789,7 +1796,13 @@ document.addEventListener('alpine:init', () => {
};
if (known) body.position_ms = Math.round(position * 1000);
this._updateMediaSession();
- return this._episodeRequest(ref.episode_id, 'playback', body)
+ const prepare = () => this._episode?.token === token
+ ? this._episodeRequest(ref.episode_id, 'playback', body)
+ : null;
+ const request = before
+ ? Promise.race([before, new Promise(resolve => setTimeout(resolve, 5000))]).then(prepare)
+ : prepare();
+ return Promise.resolve(request)
.then(info => {
const episode = this._episode;
// Another item was chosen meanwhile: never start this one.
diff --git a/tests/player_podcasts.test.cjs b/tests/player_podcasts.test.cjs
index 67cdd08..6ed4000 100644
--- a/tests/player_podcasts.test.cjs
+++ b/tests/player_podcasts.test.cjs
@@ -941,3 +941,76 @@ test('a handoff that does not go through still records the listening when the ro
await flush();
assert.deepEqual(moved.calls.slice(1).map(call => call.body?.event ?? call.url), ['handoff', '/api/player/devices/active']);
});
+
+test('Play after a failed source waits for the final report and resumes where the audio stopped', async () => {
+ const env = episodePlayer([episode()], 0);
+ const p = env.player;
+ p.playQueueIndex(0);
+ answer(env.calls[0], {generation: 3});
+ await flush();
+ env.audio.fire('loadedmetadata');
+ p.isPlaying = true;
+ env.audio.currentTime = 640;
+ p._listenedSeconds = 40;
+ p._episode.lastWriteAt -= 20000;
+ p._episodeTick();
+ await flush();
+ env.calls[1].respond({outcome: 'written', generation: 3, status: 'in_progress', position_ms: 640000});
+ await flush();
+ // 15 s later the publisher drops the stream; the user presses Play.
+ // (timeupdate keeps the shown position in step with the audio.)
+ env.audio.currentTime = 655;
+ p.currentTime = 655;
+ p._listenedSeconds = 55;
+ p._onEpisodeAudioError();
+ assert.equal(p.episodeNotice, 'source failed');
+ p.resume();
+ await flush();
+ const stop = env.calls[2];
+ assert.deepEqual([stop.body.event, stop.body.generation, stop.body.position_ms], ['stop', 3, 655000]);
+ assert.equal(env.calls.length, 3, 'the new session waits for the final report');
+ stop.respond({outcome: 'written', generation: 3, status: 'in_progress', position_ms: 655000, session_recorded: true});
+ await flush();
+ const prepare = env.calls[3];
+ assert.equal(prepare.url, '/api/podcasts/episodes/ep1:x/playback');
+ assert.equal(prepare.body.start, true);
+ assert.equal(prepare.body.position_ms, undefined, 'from the register, which now has 655 s');
+ answer(prepare, {position_ms: 655000, generation: 4});
+ await flush();
+ env.audio.fire('loadedmetadata');
+ assert.equal(env.audio.currentTime, 655);
+
+ // A final report that never answers holds the new session back only briefly.
+ const slow = episodePlayer([episode()], 0);
+ slow.player.playQueueIndex(0);
+ answer(slow.calls[0], {generation: 3});
+ await flush();
+ slow.audio.fire('loadedmetadata');
+ slow.player.isPlaying = true;
+ slow.audio.currentTime = 700;
+ slow.player._listenedSeconds = 100;
+ slow.player._onEpisodeAudioError();
+ slow.player.resume();
+ await flush();
+ assert.equal(slow.calls.length, 2);
+ slow.runTimers();
+ await flush();
+ assert.equal(slow.calls.length, 3);
+ assert.equal(slow.calls[2].url, '/api/podcasts/episodes/ep1:x/playback');
+
+ // Choosing another item meanwhile: the old episode is never prepared.
+ const other = episodePlayer([episode(), song(2)], 0);
+ other.player.playQueueIndex(0);
+ answer(other.calls[0], {generation: 3});
+ await flush();
+ other.audio.fire('loadedmetadata');
+ other.player.isPlaying = true;
+ other.audio.currentTime = 700;
+ other.player._onEpisodeAudioError();
+ other.player.resume();
+ other.player.playQueueIndex(1);
+ await flush();
+ other.calls[1].respond({outcome: 'written', generation: 3, status: 'in_progress', position_ms: 700000});
+ await flush();
+ assert.deepEqual(other.calls.map(call => call.url.split('/').pop()), ['playback', 'progress']);
+});