diff --git a/apps/api/src/modules/analysis/similar-player.service.spec.ts b/apps/api/src/modules/analysis/similar-player.service.spec.ts index 6848140..f0f142c 100644 --- a/apps/api/src/modules/analysis/similar-player.service.spec.ts +++ b/apps/api/src/modules/analysis/similar-player.service.spec.ts @@ -276,6 +276,39 @@ describe('SimilarPlayerService', () => { ); }); + it('recomputes when the cached similar-player result is empty', async () => { + const currentProfile = createProfile(); + const candidateProfile = createProfile({ + puuid: 'newly-indexed-similar-player', + score: 0.92, + }); + const findOne = jest.fn(() => createLeanExec(currentProfile)); + const aggregate = jest.fn(() => createAggregateExec([candidateProfile])); + const recommendationFindOne = jest.fn(() => + createSortedLeanExec({ + profileUpdatedAt: new Date('2026-07-02T00:00:00.000Z'), + result: { matches: [] }, + }), + ); + const updateOne = jest.fn(() => createUpdateExec({ acknowledged: true })); + const service = new SimilarPlayerService({ + getModel: jest.fn((modelKey: string) => + Promise.resolve( + modelKey === 'playerProfile' + ? { findOne, aggregate } + : { findOne: recommendationFindOne, updateOne }, + ), + ), + } as unknown as ConnectionService); + + const result = await service.findSimilarPlayers('target-puuid', 420, 10); + + expect(aggregate).toHaveBeenCalledTimes(1); + expect(updateOne).toHaveBeenCalledTimes(1); + expect(result.matches).toHaveLength(1); + expect(result.matches[0]?.puuid).toBe('newly-indexed-similar-player'); + }); + it('returns a fresh cached similar-player recommendation without running vector search', async () => { const currentProfile = createProfile(); const cachedMatch = { diff --git a/apps/api/src/modules/analysis/similar-player.service.ts b/apps/api/src/modules/analysis/similar-player.service.ts index 7054ac2..455fa30 100644 --- a/apps/api/src/modules/analysis/similar-player.service.ts +++ b/apps/api/src/modules/analysis/similar-player.service.ts @@ -427,6 +427,7 @@ export class SimilarPlayerService { ): SimilarPlayer[] | null { const matches = recommendation?.result?.matches; if (!Array.isArray(matches)) return null; + if (matches.length === 0) return null; if (!matches.every((match) => this.isCachedSimilarPlayer(match))) { return null; }