feat: connect tournament views to APIs and recover archived stats
Remove development previews and complete hall of fame, match history, official results, and multi-kill API integration. Fix active match pagination and team-ready controls, preserve basic scoreboards, and recover statistics when Riot archives arrive. Use bounded in-process cache invalidation retries and add the Riot match lookup index migration with regression coverage.
This commit is contained in:
1 parent
20e1e6ec04
commit
0d7617e2d2
70 files changed
+3739
-1793
No files matched your search
@@ -0,0 +1,321 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { CommunityStatsService } from './community-stats.service';
|
||||
import { CommunityStatsCacheKey } from './community-stats-cache.service';
|
||||
import type { InternalMatchDetailResponse } from './community-stats.types';
|
||||
|
||||
describe('Community stats raw archive recovery', () => {
|
||||
it.each([true, false])(
|
||||
'recovers a cached missing game even if invalidation needs retry (%s)',
|
||||
async (invalidationSucceeds) => {
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
let archived = false;
|
||||
let enriched = false;
|
||||
let generation = 'before';
|
||||
let cached: InternalMatchDetailResponse | null = null;
|
||||
const exists = jest.fn(() => query(archived ? { _id: 'archive' } : null));
|
||||
const buildAndUpsert = jest.fn(async () => {
|
||||
enriched = true;
|
||||
});
|
||||
const invalidateCommunity = jest.fn(async () => {
|
||||
if (!invalidationSucceeds) return false;
|
||||
cached = null;
|
||||
generation = 'after';
|
||||
return true;
|
||||
});
|
||||
const setDetail = jest.fn(
|
||||
async (
|
||||
key: CommunityStatsCacheKey,
|
||||
value: InternalMatchDetailResponse,
|
||||
) => {
|
||||
expect(key.generation).toBe(generation);
|
||||
cached = value;
|
||||
},
|
||||
);
|
||||
const service = new CommunityStatsService(
|
||||
{ getModel: async () => ({ exists }) } as never,
|
||||
{
|
||||
get: async (key: CommunityStatsCacheKey) => {
|
||||
key.generation = generation;
|
||||
return cached;
|
||||
},
|
||||
setDetail,
|
||||
invalidateCommunityWithRetry: invalidateCommunity,
|
||||
} as never,
|
||||
{} as never,
|
||||
{ buildAndUpsert } as never,
|
||||
);
|
||||
const internals = service as unknown as {
|
||||
getDetailRevision: () => Promise<string>;
|
||||
buildBaseDataset: () => Promise<unknown>;
|
||||
matchDetailInFlight: Map<string, unknown>;
|
||||
};
|
||||
jest
|
||||
.spyOn(internals, 'getDetailRevision')
|
||||
.mockResolvedValue('same-revision');
|
||||
jest
|
||||
.spyOn(internals, 'buildBaseDataset')
|
||||
.mockImplementation(async () => ({
|
||||
matches: [{ matchId: matchId.toString() }],
|
||||
detailsByMatchId: new Map([
|
||||
[
|
||||
matchId.toString(),
|
||||
[
|
||||
{
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_1',
|
||||
rawAvailable: enriched,
|
||||
timelineLoaded: enriched,
|
||||
teams: [],
|
||||
},
|
||||
],
|
||||
],
|
||||
]),
|
||||
competition: {
|
||||
projection: {
|
||||
games: [
|
||||
{
|
||||
id: 'official-game',
|
||||
displayNumber: 1,
|
||||
evidence: { type: 'riot', riotMatchId: 'KR_1' },
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
const missing = await service.getMatchDetail({ communityId, matchId });
|
||||
expect(missing.games[0].analysisAvailability).toBe('missing');
|
||||
expect(await service.getMatchDetail({ communityId, matchId })).toBe(
|
||||
missing,
|
||||
);
|
||||
expect(buildAndUpsert).not.toHaveBeenCalled();
|
||||
|
||||
archived = true;
|
||||
const recovered = await service.getMatchDetail({ communityId, matchId });
|
||||
expect(recovered.games[0]).toMatchObject({
|
||||
gameId: 'official-game',
|
||||
analysisAvailability: 'available',
|
||||
riotMatchId: 'KR_1',
|
||||
});
|
||||
expect(buildAndUpsert).toHaveBeenCalledWith(matchId, {
|
||||
includeTimeline: true,
|
||||
});
|
||||
expect(invalidateCommunity).toHaveBeenCalledWith(communityId.toString());
|
||||
expect(internals.matchDetailInFlight.size).toBe(0);
|
||||
expect(setDetail).toHaveBeenCalledTimes(invalidationSucceeds ? 2 : 1);
|
||||
expect(await service.getMatchDetail({ communityId, matchId })).toEqual(
|
||||
recovered,
|
||||
);
|
||||
expect(buildAndUpsert).toHaveBeenCalledTimes(1);
|
||||
},
|
||||
);
|
||||
|
||||
it('rebuilds only affected missing fragments and invalidates every affected community after the writes', async () => {
|
||||
const first = new mongoose.Types.ObjectId();
|
||||
const second = new mongoose.Types.ObjectId();
|
||||
const fragments = [
|
||||
{
|
||||
matchId: new mongoose.Types.ObjectId(),
|
||||
communityId: first,
|
||||
gameDetails: [{ riotMatchId: 'KR_1', rawAvailable: false }],
|
||||
},
|
||||
{
|
||||
matchId: new mongoose.Types.ObjectId(),
|
||||
communityId: first,
|
||||
gameDetails: [{ riotMatchId: 'KR_1', rawAvailable: true }],
|
||||
},
|
||||
{
|
||||
matchId: new mongoose.Types.ObjectId(),
|
||||
communityId: second,
|
||||
gameDetails: [{ riotMatchId: 'KR_1', rawAvailable: false }],
|
||||
},
|
||||
];
|
||||
const order: string[] = [];
|
||||
const buildAndUpsert = jest.fn(async () => {
|
||||
order.push('build');
|
||||
});
|
||||
const invalidateCommunity = jest.fn(async () => {
|
||||
order.push('invalidate');
|
||||
return true;
|
||||
});
|
||||
const findByRiotMatchIds = jest.fn().mockResolvedValue(fragments);
|
||||
const service = new CommunityStatsService(
|
||||
{
|
||||
getModel: async () => ({ find: () => query([{ matchId: 'KR_1' }]) }),
|
||||
} as never,
|
||||
{ invalidateCommunityWithRetry: invalidateCommunity } as never,
|
||||
{
|
||||
findByRiotMatchIds,
|
||||
findByMatch: async () => ({
|
||||
gameDetails: [{ riotMatchId: 'KR_1', rawAvailable: true }],
|
||||
}),
|
||||
} as never,
|
||||
{ buildAndUpsert } as never,
|
||||
);
|
||||
|
||||
await service.refreshForRiotMatches(['KR_1', 'KR_1', 'KR_ABSENT']);
|
||||
|
||||
expect(findByRiotMatchIds).toHaveBeenCalledWith(['KR_1']);
|
||||
expect(buildAndUpsert.mock.calls).toEqual([
|
||||
[fragments[0].matchId],
|
||||
[fragments[2].matchId],
|
||||
]);
|
||||
expect(invalidateCommunity.mock.calls).toEqual([
|
||||
[first.toString()],
|
||||
[second.toString()],
|
||||
]);
|
||||
expect(order).toEqual(['build', 'build', 'invalidate', 'invalidate']);
|
||||
});
|
||||
|
||||
it('does not invalidate already enriched games or builds that make no progress', async () => {
|
||||
const game = { riotMatchId: 'KR_1', rawAvailable: false };
|
||||
const fragment = {
|
||||
matchId: new mongoose.Types.ObjectId(),
|
||||
communityId: new mongoose.Types.ObjectId(),
|
||||
gameDetails: [game],
|
||||
};
|
||||
const buildAndUpsert = jest.fn().mockResolvedValue(undefined);
|
||||
const invalidateCommunity = jest.fn();
|
||||
const service = new CommunityStatsService(
|
||||
{
|
||||
getModel: async () => ({ find: () => query([{ matchId: 'KR_1' }]) }),
|
||||
} as never,
|
||||
{ invalidateCommunityWithRetry: invalidateCommunity } as never,
|
||||
{
|
||||
findByRiotMatchIds: async () => [fragment],
|
||||
findByMatch: async () => fragment,
|
||||
} as never,
|
||||
{ buildAndUpsert } as never,
|
||||
);
|
||||
await service.refreshForRiotMatches(['KR_1']);
|
||||
game.rawAvailable = true;
|
||||
await service.refreshForRiotMatches(['KR_1']);
|
||||
expect(buildAndUpsert).toHaveBeenCalledTimes(1);
|
||||
expect(invalidateCommunity).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not rebuild when raw is still absent', async () => {
|
||||
const findByRiotMatchIds = jest.fn();
|
||||
const buildAndUpsert = jest.fn();
|
||||
const service = new CommunityStatsService(
|
||||
{ getModel: async () => ({ find: () => query([]) }) } as never,
|
||||
{} as never,
|
||||
{ findByRiotMatchIds } as never,
|
||||
{ buildAndUpsert } as never,
|
||||
);
|
||||
await service.refreshForRiotMatches(['KR_1']);
|
||||
expect(findByRiotMatchIds).not.toHaveBeenCalled();
|
||||
expect(buildAndUpsert).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('invalidates successful communities once even when another game fails mid-batch', async () => {
|
||||
const communities = Array.from(
|
||||
{ length: 4 },
|
||||
() => new mongoose.Types.ObjectId(),
|
||||
);
|
||||
const fragments = [0, 1, 0, 2, 3].map((community, index) => ({
|
||||
matchId: new mongoose.Types.ObjectId(),
|
||||
communityId: communities[community],
|
||||
gameDetails: [{ riotMatchId: 'KR_1', rawAvailable: index === 4 }],
|
||||
}));
|
||||
const buildAndUpsert = jest.fn(async (matchId: mongoose.Types.ObjectId) => {
|
||||
const fragment = fragments.find((item) => item.matchId === matchId)!;
|
||||
if (fragment === fragments[1]) throw new Error('one build fails');
|
||||
fragment.gameDetails[0].rawAvailable = true;
|
||||
});
|
||||
const invalidate = jest.fn().mockResolvedValue(true);
|
||||
const service = new CommunityStatsService(
|
||||
{
|
||||
getModel: async () => ({ find: () => query([{ matchId: 'KR_1' }]) }),
|
||||
} as never,
|
||||
{ invalidateCommunityWithRetry: invalidate } as never,
|
||||
{
|
||||
findByRiotMatchIds: async () => fragments,
|
||||
findByMatch: async (id: mongoose.Types.ObjectId) =>
|
||||
fragments.find((item) => item.matchId === id),
|
||||
} as never,
|
||||
{ buildAndUpsert } as never,
|
||||
);
|
||||
await service.refreshForRiotMatches(['KR_1']);
|
||||
expect(buildAndUpsert).toHaveBeenCalledTimes(4);
|
||||
expect(invalidate.mock.calls).toEqual([
|
||||
[communities[0].toString()],
|
||||
[communities[2].toString()],
|
||||
]);
|
||||
});
|
||||
|
||||
it('backs off raw recovery that makes no progress and does not invalidate the community', async () => {
|
||||
let now = 1000;
|
||||
const clock = jest.spyOn(Date, 'now').mockImplementation(() => now);
|
||||
try {
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
let cached: InternalMatchDetailResponse | null = null;
|
||||
const buildAndUpsert = jest.fn().mockResolvedValue(undefined);
|
||||
const invalidate = jest.fn();
|
||||
const service = new CommunityStatsService(
|
||||
{
|
||||
getModel: async () => ({ exists: () => query({ _id: 'archive' }) }),
|
||||
} as never,
|
||||
{
|
||||
get: async () => cached,
|
||||
setDetail: async (
|
||||
_key: unknown,
|
||||
value: InternalMatchDetailResponse,
|
||||
) => {
|
||||
cached = value;
|
||||
},
|
||||
invalidateCommunityWithRetry: invalidate,
|
||||
} as never,
|
||||
{} as never,
|
||||
{ buildAndUpsert } as never,
|
||||
);
|
||||
const internals = service as unknown as {
|
||||
getDetailRevision: () => Promise<string>;
|
||||
buildBaseDataset: () => Promise<unknown>;
|
||||
};
|
||||
jest
|
||||
.spyOn(internals, 'getDetailRevision')
|
||||
.mockResolvedValue('same-revision');
|
||||
jest.spyOn(internals, 'buildBaseDataset').mockResolvedValue({
|
||||
matches: [{ matchId: matchId.toString() }],
|
||||
detailsByMatchId: new Map([
|
||||
[
|
||||
matchId.toString(),
|
||||
[
|
||||
{
|
||||
gameNumber: 1,
|
||||
riotMatchId: 'KR_1',
|
||||
rawAvailable: false,
|
||||
timelineLoaded: false,
|
||||
teams: [],
|
||||
},
|
||||
],
|
||||
],
|
||||
]),
|
||||
});
|
||||
await service.getMatchDetail({ communityId, matchId });
|
||||
await service.getMatchDetail({ communityId, matchId });
|
||||
expect(buildAndUpsert).toHaveBeenCalledTimes(1);
|
||||
now += 60_000;
|
||||
await service.getMatchDetail({ communityId, matchId });
|
||||
expect(buildAndUpsert).toHaveBeenCalledTimes(2);
|
||||
expect(invalidate).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
clock.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function query<T>(value: T) {
|
||||
return {
|
||||
select() {
|
||||
return this;
|
||||
},
|
||||
lean() {
|
||||
return this;
|
||||
},
|
||||
exec: async () => value,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user