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
This commit is contained in:
2026-07-17 23:10:45 -07:00
parent 6a92f428a3
commit 85a139cd00
2 changed files with 14 additions and 3 deletions
+13 -3
View File
@@ -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 {
+1
View File
@@ -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 {