fix(anilist): resolve later seasons past split-cour halves and specials (#335)

This commit is contained in:
sudacode authored and GitHub committed 2026-11-22 21:03:11 -08:00
1 parent ac3ceab856
commit 9d2057cc30
10 files changed
+1539 -153

No files matched your search

@@ -4,7 +4,13 @@ import * as os from 'os';
import * as path from 'path';
import test from 'node:test';
import { isDeepStrictEqual } from 'node:util';
import { getSnapshotPath, readSnapshot, writeSnapshot } from './cache';
import {
getSnapshotPath,
readCachedMediaResolution,
readSnapshot,
writeCachedMediaResolution,
writeSnapshot,
} from './cache';
import { CHARACTER_DICTIONARY_FORMAT_VERSION } from './constants';
import type { CharacterDictionarySnapshot } from './types';
@@ -121,3 +127,20 @@ test('readSnapshot ignores snapshots written with an older format version', asyn
assert.equal(await readSnapshot(snapshotPath), null);
});
test('media resolutions cached before the season resolver fix are re-resolved', () => {
const outputDir = makeTempDir();
const cachePath = path.join(outputDir, 'anilist-resolution-cache.json');
// Written by the old resolver, which counted "Season 2 Part 2" as season 3.
fs.writeFileSync(
cachePath,
JSON.stringify({
entries: [{ seriesKey: 'mushoku-tensei-s3', mediaId: 166873, mediaTitle: 'Part 2' }],
}),
);
assert.equal(readCachedMediaResolution(outputDir, 'mushoku-tensei-s3'), null);
const entry = { seriesKey: 'mushoku-tensei-s3', mediaId: 178789, mediaTitle: 'Season 3' };
writeCachedMediaResolution(outputDir, entry);
assert.deepEqual(readCachedMediaResolution(outputDir, 'mushoku-tensei-s3'), entry);
});
+14 -1
View File
@@ -28,9 +28,17 @@ type MediaResolutionCacheEntry = {
};
type MediaResolutionCacheFile = {
version?: number;
entries?: MediaResolutionCacheEntry[];
};
/**
* Bump when the AniList season resolver changes what it returns, so matches it used to get
* wrong are re-resolved instead of being served from here forever. Version 2: split-cour
* continuations and specials no longer count as seasons.
*/
const MEDIA_RESOLUTION_CACHE_VERSION = 2;
function getMediaResolutionCachePath(outputDir: string): string {
return path.join(outputDir, 'anilist-resolution-cache.json');
}
@@ -55,6 +63,7 @@ function readMediaResolutionEntries(outputDir: string): MediaResolutionCacheEntr
const parsed = JSON.parse(
fs.readFileSync(getMediaResolutionCachePath(outputDir), 'utf8'),
) as MediaResolutionCacheFile;
if (parsed.version !== MEDIA_RESOLUTION_CACHE_VERSION) return [];
if (!Array.isArray(parsed.entries)) return [];
const byKey = new Map<string, MediaResolutionCacheEntry>();
for (const value of parsed.entries) {
@@ -74,7 +83,11 @@ function writeMediaResolutionEntries(
ensureDir(outputDir);
fs.writeFileSync(
getMediaResolutionCachePath(outputDir),
JSON.stringify({ entries }, null, 2),
JSON.stringify(
{ version: MEDIA_RESOLUTION_CACHE_VERSION, entries } satisfies MediaResolutionCacheFile,
null,
2,
),
'utf8',
);
}