From 28afd1513495d172b0428eaa247118e85329808a Mon Sep 17 00:00:00 2001 From: Kyle Date: Wed, 25 Mar 2026 16:33:50 -0700 Subject: [PATCH] Tighten YouTube playback launch readiness --- .../runtime/youtube-playback-launch.test.ts | 41 +++++++++++++++++++ src/main/runtime/youtube-playback-launch.ts | 9 ++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main/runtime/youtube-playback-launch.test.ts b/src/main/runtime/youtube-playback-launch.test.ts index b12a469..52e3cad 100644 --- a/src/main/runtime/youtube-playback-launch.test.ts +++ b/src/main/runtime/youtube-playback-launch.test.ts @@ -193,3 +193,44 @@ test('prepare youtube playback accepts a non-youtube resolved path once playable assert.equal(ok, true); assert.deepEqual(commands[4], ['loadfile', 'https://www.youtube.com/watch?v=newvid', 'replace']); }); + +test('prepare youtube playback does not accept a different youtube video after path change', async () => { + const commands: Array> = []; + let nowTick = 0; + const observedPaths = [ + '/videos/episode01.mkv', + 'https://www.youtube.com/watch?v=wrongvid', + 'https://www.youtube.com/watch?v=wrongvid', + ]; + let requestCount = 0; + const prepare = createPrepareYoutubePlaybackInMpvHandler({ + requestPath: async () => { + const value = observedPaths[Math.min(requestCount, observedPaths.length - 1)] ?? null; + requestCount += 1; + return value; + }, + requestProperty: async (name) => { + if (name !== 'track-list') return null; + return [{ type: 'video', id: 1 }]; + }, + sendMpvCommand: (command) => commands.push(command), + wait: createWaitStub(), + now: () => { + nowTick += 100; + return nowTick; + }, + }); + + const ok = await prepare({ + url: 'https://www.youtube.com/watch?v=targetvid', + timeoutMs: 350, + pollIntervalMs: 1, + }); + + assert.equal(ok, false); + assert.deepEqual(commands[4], [ + 'loadfile', + 'https://www.youtube.com/watch?v=targetvid', + 'replace', + ]); +}); diff --git a/src/main/runtime/youtube-playback-launch.ts b/src/main/runtime/youtube-playback-launch.ts index 29514a8..afc24d7 100644 --- a/src/main/runtime/youtube-playback-launch.ts +++ b/src/main/runtime/youtube-playback-launch.ts @@ -140,11 +140,10 @@ export function createPrepareYoutubePlaybackInMpvHandler(deps: YoutubePlaybackLa } } if (previousPath && currentPath !== previousPath) { - if ( - isYoutubeMediaPath(currentPath) && - isYoutubeMediaPath(targetUrl) - ) { - return true; + if (isYoutubeMediaPath(currentPath) && isYoutubeMediaPath(targetUrl)) { + if (!pathMatchesYoutubeTarget(currentPath, targetUrl)) { + continue; + } } if (deps.requestProperty) { try {