From ceb520d49d0c9ff3056fce32075b201499805fcf Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Mon, 1 Sep 2025 20:10:30 +0000 Subject: [PATCH] fix(source): consider season and episode for cache key --- src/source/Source.ts | 7 ++++--- src/utils/id/ImdbId.test.ts | 30 ++++++++++++++++++------------ src/utils/id/ImdbId.ts | 4 ++++ src/utils/id/TmdbId.test.ts | 30 ++++++++++++++++++------------ src/utils/id/TmdbId.ts | 4 ++++ 5 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/source/Source.ts b/src/source/Source.ts index 8276f1b..f45bf19 100644 --- a/src/source/Source.ts +++ b/src/source/Source.ts @@ -32,8 +32,9 @@ export abstract class Source { protected abstract handleInternal(ctx: Context, type: ContentType, id: Id): Promise<(SourceResult[])>; public async handle(ctx: Context, type: ContentType, id: Id): Promise<(SourceResult[])> { - if (this.sourceResultCache.has(id.id)) { - return this.sourceResultCache.get(id.id) as SourceResult[]; + const cacheKey = id.toString(); + if (this.sourceResultCache.has(cacheKey)) { + return this.sourceResultCache.get(cacheKey) as SourceResult[]; } let sourceResults: SourceResult[]; @@ -47,7 +48,7 @@ export abstract class Source { } } - this.sourceResultCache.set(id.id, sourceResults); + this.sourceResultCache.set(cacheKey, sourceResults); return sourceResults; } diff --git a/src/utils/id/ImdbId.test.ts b/src/utils/id/ImdbId.test.ts index fa14d9f..8825bd9 100644 --- a/src/utils/id/ImdbId.test.ts +++ b/src/utils/id/ImdbId.test.ts @@ -2,27 +2,33 @@ import { ImdbId } from './ImdbId'; describe('can be created from string', () => { test('splits id properly', () => { - const { id, season, episode } = ImdbId.fromString('tt2085059:2:4'); + const imdbId = ImdbId.fromString('tt2085059:2:4'); - expect(id).toBe('tt2085059'); - expect(season).toBe(2); - expect(episode).toBe(4); + expect(imdbId.id).toBe('tt2085059'); + expect(imdbId.season).toBe(2); + expect(imdbId.episode).toBe(4); + + expect(imdbId.toString()).toBe('tt2085059:2:4'); }); test('handles weird 0 prefixes in series and episode', () => { - const { id, season, episode } = ImdbId.fromString('tt2085059:02:04'); + const imdbId = ImdbId.fromString('tt2085059:02:04'); - expect(id).toBe('tt2085059'); - expect(season).toBe(2); - expect(episode).toBe(4); + expect(imdbId.id).toBe('tt2085059'); + expect(imdbId.season).toBe(2); + expect(imdbId.episode).toBe(4); + + expect(imdbId.toString()).toBe('tt2085059:2:4'); }); test('supports movie with missing series and episode', () => { - const { id, season, episode } = ImdbId.fromString('tt2085059'); + const imdbId = ImdbId.fromString('tt2085059'); - expect(id).toBe('tt2085059'); - expect(season).toBeUndefined(); - expect(episode).toBeUndefined(); + expect(imdbId.id).toBe('tt2085059'); + expect(imdbId.season).toBeUndefined(); + expect(imdbId.episode).toBeUndefined(); + + expect(imdbId.toString()).toBe('tt2085059'); }); test('throws for empty ids', () => { diff --git a/src/utils/id/ImdbId.ts b/src/utils/id/ImdbId.ts index 83262c3..2258128 100644 --- a/src/utils/id/ImdbId.ts +++ b/src/utils/id/ImdbId.ts @@ -22,4 +22,8 @@ export class ImdbId { idParts[2] ? parseInt(idParts[2]) : undefined, ); } + + public toString(): string { + return this.season ? `${this.id}:${this.season}:${this.episode}` : this.id; + } } diff --git a/src/utils/id/TmdbId.test.ts b/src/utils/id/TmdbId.test.ts index 16afa39..70d481e 100644 --- a/src/utils/id/TmdbId.test.ts +++ b/src/utils/id/TmdbId.test.ts @@ -2,27 +2,33 @@ import { TmdbId } from './TmdbId'; describe('can be created from string', () => { test('splits id properly', () => { - const { id, season, episode } = TmdbId.fromString('2085059:2:4'); + const tmdbId = TmdbId.fromString('2085059:2:4'); - expect(id).toBe(2085059); - expect(season).toBe(2); - expect(episode).toBe(4); + expect(tmdbId.id).toBe(2085059); + expect(tmdbId.season).toBe(2); + expect(tmdbId.episode).toBe(4); + + expect(tmdbId.toString()).toBe('2085059:2:4'); }); test('handles weird 0 prefixes in series and episode', () => { - const { id, season, episode } = TmdbId.fromString('2085059:02:04'); + const tmdbId = TmdbId.fromString('2085059:02:04'); - expect(id).toBe(2085059); - expect(season).toBe(2); - expect(episode).toBe(4); + expect(tmdbId.id).toBe(2085059); + expect(tmdbId.season).toBe(2); + expect(tmdbId.episode).toBe(4); + + expect(tmdbId.toString()).toBe('2085059:2:4'); }); test('supports movie with missing series and episode', () => { - const { id, season, episode } = TmdbId.fromString('2085059'); + const tmdbId = TmdbId.fromString('2085059'); - expect(id).toBe(2085059); - expect(season).toBeUndefined(); - expect(episode).toBeUndefined(); + expect(tmdbId.id).toBe(2085059); + expect(tmdbId.season).toBeUndefined(); + expect(tmdbId.episode).toBeUndefined(); + + expect(tmdbId.toString()).toBe('2085059'); }); test('throws for empty ids', () => { diff --git a/src/utils/id/TmdbId.ts b/src/utils/id/TmdbId.ts index 6562c17..e7ba5e8 100644 --- a/src/utils/id/TmdbId.ts +++ b/src/utils/id/TmdbId.ts @@ -22,4 +22,8 @@ export class TmdbId { idParts[2] ? parseInt(idParts[2]) : undefined, ); } + + public toString(): string { + return this.season ? `${this.id}:${this.season}:${this.episode}` : `${this.id}`; + } }