fix(api): correct champion history and deduplicate stats builds
Apply participant and champion filters before pagination and exclude timeline payloads. Route result statistics through the durable outbox, reuse current fragments, and serialize projection effects without spending retries on contention. Validated with 716 API tests, 32 isolated Redis integration tests, API typecheck and build, and lint on changed files.
This commit is contained in:
1 parent
ef141a13d0
commit
f2eec479d9
14 files changed
+704
-47
No files matched your search
@@ -22,6 +22,7 @@ import {
|
||||
import { CommunityStatsService } from '../src/modules/community-stats/community-stats.service';
|
||||
import { PROMOTE_SCHEDULED_RIOT_JOB_SCRIPT } from '../src/modules/riot-job-queue/riot-job-queue.scripts';
|
||||
import mongoose from 'mongoose';
|
||||
import { CompetitionOutboxProcessor } from '../src/modules/tournament-result/competition-outbox.processor';
|
||||
|
||||
const client = createClient({
|
||||
url: process.env.REDIS_TEST_URL,
|
||||
@@ -165,6 +166,89 @@ describe('Redis native operations', () => {
|
||||
expect(await client.exists('lock')).toBe(0);
|
||||
});
|
||||
|
||||
it('defers locked Competition effects without spending a retry, then releases its own lock after delivery', async () => {
|
||||
const locks = new DistributedLockService(redis);
|
||||
const competitionId = new mongoose.Types.ObjectId();
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const lockKey = `competition:projection-effects:${competitionId.toString()}`;
|
||||
const owner = await locks.acquireLock({
|
||||
lockKey,
|
||||
lockExpirationDurationMilliseconds: 60_000,
|
||||
});
|
||||
expect(owner).not.toBeNull();
|
||||
const event = {
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
competitionId,
|
||||
revision: 2,
|
||||
type: 'competition.projection-updated',
|
||||
state: 'pending',
|
||||
attemptCount: 10,
|
||||
};
|
||||
const query = <T>(value: T) => ({
|
||||
lean: jest.fn().mockReturnThis(),
|
||||
exec: async () => value,
|
||||
});
|
||||
const publish = jest.fn(() => query({ matchedCount: 1 }));
|
||||
const models = {
|
||||
competitionOutbox: {
|
||||
findOneAndUpdate: () => query(event),
|
||||
updateOne: publish,
|
||||
},
|
||||
competition: {
|
||||
findById: () =>
|
||||
query({
|
||||
_id: competitionId,
|
||||
matchId,
|
||||
communityId: new mongoose.Types.ObjectId(),
|
||||
revision: 2,
|
||||
phase: 'running',
|
||||
teams: [],
|
||||
projection: { phase: 'running', games: [] },
|
||||
}),
|
||||
},
|
||||
match: {
|
||||
findById: () => query({ status: 'in-progress' }),
|
||||
updateOne: () => query({ matchedCount: 1 }),
|
||||
},
|
||||
};
|
||||
const buildAndUpsert = jest.fn().mockResolvedValue(undefined);
|
||||
const invalidateCommunity = jest.fn().mockResolvedValue(undefined);
|
||||
const processor = new CompetitionOutboxProcessor(
|
||||
{ getModel: async (name: keyof typeof models) => models[name] } as never,
|
||||
{
|
||||
provisionReadyAssignments: jest.fn().mockResolvedValue(undefined),
|
||||
} as never,
|
||||
{ buildAndUpsert } as never,
|
||||
{ invalidateCommunity } as never,
|
||||
locks,
|
||||
);
|
||||
try {
|
||||
await processor.drainOnce(1);
|
||||
expect(buildAndUpsert).not.toHaveBeenCalled();
|
||||
expect(publish).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ attemptCount: 10 }),
|
||||
{
|
||||
$set: { state: 'pending', availableAt: expect.any(Date) },
|
||||
$inc: { attemptCount: -1 },
|
||||
},
|
||||
);
|
||||
expect(await client.get(lockKey)).toBe(owner!.lockOwnershipToken);
|
||||
} finally {
|
||||
await locks.releaseLockIfOwned(owner!);
|
||||
}
|
||||
|
||||
await processor.drainOnce(1);
|
||||
expect(buildAndUpsert).toHaveBeenCalledTimes(1);
|
||||
expect(invalidateCommunity).toHaveBeenCalledTimes(1);
|
||||
expect(publish).toHaveBeenLastCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
$set: expect.objectContaining({ state: 'published' }),
|
||||
}),
|
||||
);
|
||||
expect(await client.exists(lockKey)).toBe(0);
|
||||
});
|
||||
|
||||
it('does not clear the dedup token of a newer refresh job', async () => {
|
||||
const job = {
|
||||
jobId: 'old',
|
||||
|
||||
Reference in new issue
Block a user