diff --git a/src/main/runtime/aniskip-runtime.test.ts b/src/main/runtime/aniskip-runtime.test.ts index db798c69..edd4ad90 100644 --- a/src/main/runtime/aniskip-runtime.test.ts +++ b/src/main/runtime/aniskip-runtime.test.ts @@ -557,3 +557,18 @@ test('a successful empty chapter list is not read again on a duration change', a await flushAsync(); assert.equal(state.resolveCalls.length, 1); }); + +test('a cached lookup is reused only for the episode length it was fetched for', async () => { + const { runtime, state } = createHarness(); + // A failing chapter read keeps resolution retrying on each duration change. + state.chapterListFails = true; + runtime.handleMediaPathChange({ path: '/media/show.mkv' }); + await flushAsync(); + runtime.handleDurationChange({ duration: 1440 }); + await flushAsync(); + assert.deepEqual(state.episodeLengths, [1440]); + + runtime.handleDurationChange({ duration: 1500 }); + await flushAsync(); + assert.deepEqual(state.episodeLengths, [1440, 1500]); +}); diff --git a/src/main/runtime/aniskip-runtime.ts b/src/main/runtime/aniskip-runtime.ts index 231c01d8..dcaf512a 100644 --- a/src/main/runtime/aniskip-runtime.ts +++ b/src/main/runtime/aniskip-runtime.ts @@ -89,8 +89,9 @@ export function createAniSkipRuntime(deps: AniSkipRuntimeDeps) { const promptShown: Record = { intro: false, outro: false }; let boundButtonKey: string | null = null; let legacyFallbackBound = false; - // AniSkip results per media path. Only definitive answers are stored. - const lookupCache = new Map(); + // AniSkip results per media path, with the episode length they were fetched + // for (v2 picks times by length). Only definitive answers are stored. + const lookupCache = new Map(); function resolveButtonKey(): string { const key = deps.getAniSkipConfig().aniskipButtonKey.trim(); @@ -238,14 +239,17 @@ export function createAniSkipRuntime(deps: AniSkipRuntimeDeps) { await setSkipChapters(); } - /** Looks up AniSkip, using the cache for definitive answers. Null means a retryable failure. */ + /** + * Looks up AniSkip, using the cache for definitive answers fetched at the same + * episode length. Null means a retryable failure. + */ async function lookUpAniSkip( mediaPath: string, duration: number, generation: number, ): Promise { const cached = lookupCache.get(mediaPath); - if (cached) return cached; + if (cached?.duration === duration) return cached.result; let metadata: AniSkipMetadata; try { @@ -265,7 +269,7 @@ export function createAniSkipRuntime(deps: AniSkipRuntimeDeps) { } // Every other status is definitive (a 404 or an unmatched title), so cache it, // unless a refresh or media change superseded this lookup while it ran. - if (generation === requestGeneration) lookupCache.set(mediaPath, result); + if (generation === requestGeneration) lookupCache.set(mediaPath, { duration, result }); if (metadata.lookupStatus !== 'ready') { deps.logInfo( `AniSkip: no skip times for "${metadata.title}" (status=${metadata.lookupStatus ?? 'unknown'})`,