diff --git a/src/main/runtime/aniskip-runtime.test.ts b/src/main/runtime/aniskip-runtime.test.ts index 7e368b24..db798c69 100644 --- a/src/main/runtime/aniskip-runtime.test.ts +++ b/src/main/runtime/aniskip-runtime.test.ts @@ -525,3 +525,35 @@ test('a same-media reload retries a transient lookup failure', async () => { assert.equal(state.resolveCalls.length, 2); assert.deepEqual(runtime.getSkipWindows().intro, { start: 10, end: 95.5, source: 'aniskip' }); }); + +test('a failed chapter-list read is retried on the next duration change or reload', async () => { + const stream = 'https://jellyfin.example/Videos/1/stream.mkv'; + const chapters = [ + { time: 0, title: 'OP' }, + { time: 90, title: 'Episode' }, + ]; + for (const retry of ['duration', 'reload'] as const) { + const { runtime, state } = createHarness({ chapterList: chapters }); + state.chapterListFails = true; + runtime.handleMediaPathChange({ path: stream }); + await flushAsync(); + assert.deepEqual(runtime.getSkipWindows(), NO_SKIP_TIMES); + + state.chapterListFails = false; + if (retry === 'duration') runtime.handleDurationChange({ duration: 1440 }); + else runtime.handleMediaPathChange({ path: stream }); + await flushAsync(); + assert.deepEqual(runtime.getSkipWindows().intro, { start: 0, end: 90, source: 'chapters' }); + } +}); + +test('a successful empty chapter list is not read again on a duration change', async () => { + const { runtime, state } = createHarness({ + metadata: readyMetadata({ ...NO_SKIP_TIMES, lookupStatus: 'missing_payload' }), + }); + runtime.handleMediaPathChange({ path: '/media/show.mkv' }); + await flushAsync(); + runtime.handleDurationChange({ duration: 1441 }); + await flushAsync(); + assert.equal(state.resolveCalls.length, 1); +}); diff --git a/src/main/runtime/aniskip-runtime.ts b/src/main/runtime/aniskip-runtime.ts index 4c3257b2..231c01d8 100644 --- a/src/main/runtime/aniskip-runtime.ts +++ b/src/main/runtime/aniskip-runtime.ts @@ -81,8 +81,10 @@ export function createAniSkipRuntime(deps: AniSkipRuntimeDeps) { let currentDuration: number | null = null; // True once resolution for the current media has started (or finished). let resolutionStarted = false; - // True when the last AniSkip lookup for the current media failed in a retryable way. - let lastLookupFailed = false; + // True when resolution for the current media hit a retryable failure (the + // chapter-list read or the AniSkip lookup), so a reload or duration change + // runs it again. + let retryResolution = false; let windows: AniSkipWindows = { intro: null, outro: null }; const promptShown: Record = { intro: false, outro: false }; let boundButtonKey: string | null = null; @@ -190,7 +192,7 @@ export function createAniSkipRuntime(deps: AniSkipRuntimeDeps) { function clearState(): void { requestGeneration += 1; resolutionStarted = false; - lastLookupFailed = false; + retryResolution = false; windows = { intro: null, outro: null }; promptShown.intro = false; promptShown.outro = false; @@ -277,8 +279,11 @@ export function createAniSkipRuntime(deps: AniSkipRuntimeDeps) { const generation = requestGeneration; const isStale = () => generation !== requestGeneration || mediaPath !== currentMediaPath; - const fromChapters = classifyChapterSkipIntervals(await readChapterList(), duration); + const chapterList = await readChapterList(); if (isStale()) return; + // A failed read is retried later; a successful empty list is a final answer. + if (chapterList === null) retryResolution = true; + const fromChapters = classifyChapterSkipIntervals(chapterList ?? [], duration); const next: AniSkipWindows = { intro: fromChapters.intro ? { ...fromChapters.intro, source: 'chapters' } : null, outro: fromChapters.outro ? { ...fromChapters.outro, source: 'chapters' } : null, @@ -293,7 +298,7 @@ export function createAniSkipRuntime(deps: AniSkipRuntimeDeps) { const lookup = await lookUpAniSkip(mediaPath, duration, generation); if (isStale()) return; - lastLookupFailed = lookup === null; + if (lookup === null) retryResolution = true; if (lookup) await mergeAniSkipWindows(lookup); } @@ -374,14 +379,15 @@ export function createAniSkipRuntime(deps: AniSkipRuntimeDeps) { function handleDurationChange({ duration }: { duration: number }): void { if (!isUsableDuration(duration) || !currentMediaPath) return; currentDuration = duration; + if (retryResolution) clearState(); maybeStartResolution(); } function handleMediaPathChange({ path }: { path: string }): void { const nextPath = typeof path === 'string' ? path : ''; if (nextPath === currentMediaPath && resolutionStarted) { - if (lastLookupFailed) { - // The last lookup failed transiently; a reload is the retry. + if (retryResolution) { + // The last resolution failed transiently; a reload is the retry. clearState(); maybeStartResolution(); return;