fix: close competition cutover integrity gaps
This commit is contained in:
1 parent
7ea2002975
commit
683d602d63
57 files changed
+4805
-1582
No files matched your search
@@ -4,6 +4,47 @@ import type { MatchStats } from '../global/database/schema';
|
||||
import { MatchStatsRepository } from './match-stats.repository';
|
||||
|
||||
describe('MatchStatsRepository', () => {
|
||||
it('does not let a legacy upsert overwrite a Competition-backed fragment', async () => {
|
||||
const fragment = {
|
||||
...statsFragment(12),
|
||||
sourceCompetitionId: undefined,
|
||||
sourceRevision: undefined,
|
||||
projectorVersion: undefined,
|
||||
};
|
||||
const updateOne = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce({
|
||||
exec: jest.fn().mockResolvedValue({ matchedCount: 0 }),
|
||||
})
|
||||
.mockReturnValueOnce({
|
||||
exec: jest
|
||||
.fn()
|
||||
.mockResolvedValue({ matchedCount: 1, upsertedCount: 0 }),
|
||||
});
|
||||
const connection = {
|
||||
getModel: jest.fn().mockResolvedValue({ updateOne }),
|
||||
} as unknown as ConnectionService;
|
||||
const repository = new MatchStatsRepository(connection);
|
||||
|
||||
await repository.upsert(fragment);
|
||||
|
||||
expect(updateOne).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
{
|
||||
matchId: fragment.matchId,
|
||||
sourceCompetitionId: { $exists: false },
|
||||
},
|
||||
{ $set: fragment },
|
||||
{ upsert: false },
|
||||
);
|
||||
expect(updateOne).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
{ matchId: fragment.matchId },
|
||||
{ $setOnInsert: fragment },
|
||||
{ upsert: true },
|
||||
);
|
||||
});
|
||||
|
||||
it('only updates a Competition projection when its revision is not stale', async () => {
|
||||
const updateOne = jest.fn().mockReturnValue({
|
||||
exec: jest.fn().mockResolvedValue({ matchedCount: 1, upsertedCount: 0 }),
|
||||
@@ -36,6 +77,122 @@ describe('MatchStatsRepository', () => {
|
||||
{ upsert: false },
|
||||
);
|
||||
});
|
||||
|
||||
it('converges concurrent initial upserts to the highest Competition revision', async () => {
|
||||
const older = statsFragment(12);
|
||||
const newer = {
|
||||
...statsFragment(13),
|
||||
matchId: older.matchId,
|
||||
sourceCompetitionId: older.sourceCompetitionId,
|
||||
};
|
||||
let stored: ReturnType<typeof statsFragment> | undefined;
|
||||
let initialGuardCount = 0;
|
||||
let releaseInitialGuards!: () => void;
|
||||
const initialGuardsReady = new Promise<void>((resolve) => {
|
||||
releaseInitialGuards = resolve;
|
||||
});
|
||||
let releaseOlderInsert!: () => void;
|
||||
const olderInserted = new Promise<void>((resolve) => {
|
||||
releaseOlderInsert = resolve;
|
||||
});
|
||||
const updateOne = jest.fn(
|
||||
(
|
||||
_filter: Record<string, unknown>,
|
||||
update: {
|
||||
$set?: ReturnType<typeof statsFragment>;
|
||||
$setOnInsert?: ReturnType<typeof statsFragment>;
|
||||
},
|
||||
) => ({
|
||||
exec: async () => {
|
||||
const fragment = update.$set ?? update.$setOnInsert!;
|
||||
if (update.$set && !stored && initialGuardCount < 2) {
|
||||
initialGuardCount += 1;
|
||||
if (initialGuardCount === 2) releaseInitialGuards();
|
||||
await initialGuardsReady;
|
||||
return { matchedCount: 0, upsertedCount: 0 };
|
||||
}
|
||||
|
||||
if (update.$setOnInsert) {
|
||||
if (fragment.sourceRevision === 13) await olderInserted;
|
||||
const existed = stored !== undefined;
|
||||
if (!stored) stored = fragment;
|
||||
if (fragment.sourceRevision === 12) releaseOlderInsert();
|
||||
return {
|
||||
matchedCount: existed ? 1 : 0,
|
||||
upsertedCount: existed ? 0 : 1,
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
stored &&
|
||||
stored.sourceCompetitionId?.equals(fragment.sourceCompetitionId!) &&
|
||||
stored.sourceRevision! <= fragment.sourceRevision!
|
||||
) {
|
||||
stored = fragment;
|
||||
return { matchedCount: 1, upsertedCount: 0 };
|
||||
}
|
||||
return { matchedCount: 0, upsertedCount: 0 };
|
||||
},
|
||||
}),
|
||||
);
|
||||
const connection = {
|
||||
getModel: jest.fn().mockResolvedValue({ updateOne }),
|
||||
} as unknown as ConnectionService;
|
||||
const repository = new MatchStatsRepository(connection);
|
||||
|
||||
await Promise.all([repository.upsert(older), repository.upsert(newer)]);
|
||||
|
||||
expect(stored?.sourceRevision).toBe(13);
|
||||
});
|
||||
|
||||
it('guards a zero-game delete so an observed revision cannot remove newer stats', async () => {
|
||||
const matchId = new Types.ObjectId();
|
||||
const sourceCompetitionId = new Types.ObjectId();
|
||||
const deleteOne = jest.fn().mockReturnValue({
|
||||
exec: jest.fn().mockResolvedValue({ deletedCount: 0 }),
|
||||
});
|
||||
const connection = {
|
||||
getModel: jest.fn().mockResolvedValue({ deleteOne }),
|
||||
} as unknown as ConnectionService;
|
||||
const repository = new MatchStatsRepository(connection);
|
||||
|
||||
await repository.deleteByMatch(matchId, {
|
||||
sourceCompetitionId,
|
||||
sourceRevision: 12,
|
||||
});
|
||||
|
||||
expect(deleteOne).toHaveBeenCalledWith({
|
||||
matchId,
|
||||
$or: [
|
||||
{ sourceCompetitionId: { $exists: false } },
|
||||
{
|
||||
sourceCompetitionId,
|
||||
$or: [
|
||||
{ sourceRevision: { $exists: false } },
|
||||
{ sourceRevision: { $lte: 12 } },
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it('deletes only non-Competition stats during legacy source cleanup', async () => {
|
||||
const matchId = new Types.ObjectId();
|
||||
const deleteOne = jest.fn().mockReturnValue({
|
||||
exec: jest.fn().mockResolvedValue({ deletedCount: 0 }),
|
||||
});
|
||||
const connection = {
|
||||
getModel: jest.fn().mockResolvedValue({ deleteOne }),
|
||||
} as unknown as ConnectionService;
|
||||
const repository = new MatchStatsRepository(connection);
|
||||
|
||||
await repository.deleteLegacyByMatch(matchId);
|
||||
|
||||
expect(deleteOne).toHaveBeenCalledWith({
|
||||
matchId,
|
||||
sourceCompetitionId: { $exists: false },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function statsFragment(sourceRevision: number) {
|
||||
|
||||
Reference in new issue
Block a user