From 85a139cd00e56a1d971ce79c18eb962ac4b8f6d9 Mon Sep 17 00:00:00 2001 From: sudacode Date: Fri, 17 Jul 2026 23:10:45 -0700 Subject: [PATCH] fix(launcher): find previous season when current dir is missing - findLastEpisodeInPreviousSeason now falls back to matching by season number when the current season dir isn't in the listed seasons (e.g. deleted file's season folder) - test: delete season-2 dir contents so the fallback path is exercised --- launcher/history-navigation.ts | 16 +++++++++++++--- launcher/history.test.ts | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/launcher/history-navigation.ts b/launcher/history-navigation.ts index f6df2986..b59c1142 100644 --- a/launcher/history-navigation.ts +++ b/launcher/history-navigation.ts @@ -136,9 +136,19 @@ function findLastEpisodeInPreviousSeason(resolvedCurrent: string, dir: string): if (seriesRoot === dir) return null; const seasons = listSeasonDirs(seriesRoot); const currentIdx = seasons.findIndex((season) => path.resolve(season.path) === dir); - if (currentIdx <= 0) return null; - const previousSeason = sortVideosByEpisode(collectVideos(seasons[currentIdx - 1]!.path, false)); - return previousSeason[previousSeason.length - 1] ?? null; + const currentSeason = seasonNumberFromDirName(path.basename(dir)); + const previousSeasonEntry = + currentIdx >= 0 + ? seasons[currentIdx - 1] + : seasons + .filter( + (season) => + currentSeason !== null && season.season !== null && season.season < currentSeason, + ) + .at(-1); + if (!previousSeasonEntry) return null; + const previousSeason = sortVideosByEpisode(collectVideos(previousSeasonEntry.path, false)); + return previousSeason.at(-1) ?? null; } export function findPreviousEpisode(currentPath: string): string | null { diff --git a/launcher/history.test.ts b/launcher/history.test.ts index 0e968794..eade09fe 100644 --- a/launcher/history.test.ts +++ b/launcher/history.test.ts @@ -240,6 +240,7 @@ test('findPreviousEpisode falls back to prior season when a deleted file was the try { const season1 = path.join(seriesRoot, 'Season-1'); const season2 = path.join(seriesRoot, 'Season-2'); + fs.rmSync(path.join(season2, 'Show - S02E01.mkv')); const missing = path.join(season2, 'Show - S02E01 - Deleted Cut.mkv'); assert.equal(findPreviousEpisode(missing), path.join(season1, 'Show - S01E03.mkv')); } finally {