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
@@ -294,16 +294,40 @@ describe('Legacy import Competition result cutover', () => {
|
||||
|
||||
it('cascades admin deletion through Competition facts and read models', async () => {
|
||||
const fixture = createImportFixture();
|
||||
const session = {
|
||||
startTransaction: jest.fn(),
|
||||
commitTransaction: jest.fn().mockResolvedValue(undefined),
|
||||
abortTransaction: jest.fn().mockResolvedValue(undefined),
|
||||
endSession: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const competitionFactModel = deleteManyModel();
|
||||
const competitionOutboxModel = deleteManyModel();
|
||||
const assignmentModel = deleteManyModel();
|
||||
const reservedCodeId = new mongoose.Types.ObjectId();
|
||||
const usedCodeId = new mongoose.Types.ObjectId();
|
||||
const assignmentModel = {
|
||||
...deleteManyModel(),
|
||||
find: jest.fn().mockReturnValue({
|
||||
select: jest.fn().mockReturnValue({
|
||||
session: jest.fn().mockReturnValue({
|
||||
lean: jest.fn().mockReturnValue(
|
||||
execResult([
|
||||
{ tournamentCodeId: reservedCodeId, state: 'reserved' },
|
||||
{ tournamentCodeId: usedCodeId, state: 'used' },
|
||||
]),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
const competitionModel = {
|
||||
...deleteManyModel(),
|
||||
find: jest.fn().mockReturnValue({
|
||||
select: jest.fn().mockReturnValue({
|
||||
lean: jest
|
||||
.fn()
|
||||
.mockReturnValue(execResult([{ _id: fixture.competition._id }])),
|
||||
session: jest.fn().mockReturnValue({
|
||||
lean: jest
|
||||
.fn()
|
||||
.mockReturnValue(execResult([{ _id: fixture.competition._id }])),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
@@ -311,11 +335,44 @@ describe('Legacy import Competition result cutover', () => {
|
||||
find: jest.fn().mockReturnValue({
|
||||
select: jest
|
||||
.fn()
|
||||
.mockReturnValue(execResult([{ _id: fixture.match._id }])),
|
||||
.mockReturnValue(sessionResult([{ _id: fixture.match._id }])),
|
||||
}),
|
||||
deleteMany: jest.fn().mockReturnValue(execResult({ deletedCount: 1 })),
|
||||
deleteMany: jest.fn().mockReturnValue(sessionResult({ deletedCount: 1 })),
|
||||
};
|
||||
const tournamentCodeModel = {
|
||||
find: jest.fn().mockReturnValue({
|
||||
select: jest.fn().mockReturnValue({
|
||||
session: jest.fn().mockReturnValue({
|
||||
lean: jest.fn().mockReturnValue(
|
||||
execResult([
|
||||
{
|
||||
_id: reservedCodeId,
|
||||
state: 'reserved',
|
||||
matchId: fixture.match._id,
|
||||
},
|
||||
{
|
||||
_id: usedCodeId,
|
||||
state: 'used',
|
||||
matchId: fixture.match._id,
|
||||
},
|
||||
]),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
updateMany: jest
|
||||
.fn()
|
||||
.mockReturnValue(sessionResult({ matchedCount: 1, modifiedCount: 1 })),
|
||||
};
|
||||
const matchStatsModel = deleteManyModel();
|
||||
const quarantineCollection = {
|
||||
deleteMany: jest.fn().mockResolvedValue({ deletedCount: 1 }),
|
||||
};
|
||||
const connectionService = {
|
||||
createSession: jest.fn().mockResolvedValue(session),
|
||||
getConnection: jest.fn().mockResolvedValue({
|
||||
collection: jest.fn().mockReturnValue(quarantineCollection),
|
||||
}),
|
||||
getModel: jest.fn((name: string) => {
|
||||
const models: Record<string, unknown> = {
|
||||
match: matchModel,
|
||||
@@ -323,6 +380,8 @@ describe('Legacy import Competition result cutover', () => {
|
||||
competitionFact: competitionFactModel,
|
||||
competitionOutbox: competitionOutboxModel,
|
||||
tournamentCodeAssignment: assignmentModel,
|
||||
tournamentCode: tournamentCodeModel,
|
||||
matchStats: matchStatsModel,
|
||||
};
|
||||
const model = models[name];
|
||||
return model
|
||||
@@ -330,15 +389,11 @@ describe('Legacy import Competition result cutover', () => {
|
||||
: Promise.reject(new Error(`Unexpected model ${name}`));
|
||||
}),
|
||||
} as unknown as ConnectionService;
|
||||
const matchStatsService = {
|
||||
removeByMatches: jest.fn().mockResolvedValue(undefined),
|
||||
} as unknown as jest.Mocked<MatchStatsService>;
|
||||
const communityStatsService = {
|
||||
invalidateCommunity: jest.fn().mockResolvedValue(undefined),
|
||||
} as unknown as jest.Mocked<CommunityStatsService>;
|
||||
const service = createService({
|
||||
connectionService,
|
||||
matchStatsService,
|
||||
communityStatsService,
|
||||
});
|
||||
|
||||
@@ -360,9 +415,98 @@ describe('Legacy import Competition result cutover', () => {
|
||||
expect(competitionModel.deleteMany).toHaveBeenCalledWith({
|
||||
_id: { $in: [fixture.competition._id] },
|
||||
});
|
||||
expect(matchStatsService.removeByMatches).toHaveBeenCalledWith([
|
||||
fixture.match._id,
|
||||
]);
|
||||
expect(tournamentCodeModel.updateMany).toHaveBeenCalledTimes(2);
|
||||
expect(matchStatsModel.deleteMany).toHaveBeenCalledWith({
|
||||
matchId: { $in: [fixture.match._id] },
|
||||
});
|
||||
expect(session.commitTransaction).toHaveBeenCalledTimes(1);
|
||||
expect(session.abortTransaction).not.toHaveBeenCalled();
|
||||
expect(communityStatsService.invalidateCommunity).toHaveBeenCalledWith(
|
||||
fixture.match.communityId,
|
||||
);
|
||||
});
|
||||
|
||||
it('aborts legacy deletion when an active assignment points to another Match code', async () => {
|
||||
const harness = createBulkDeleteFailureHarness({
|
||||
codeMatchId: new mongoose.Types.ObjectId(),
|
||||
});
|
||||
|
||||
await expect(
|
||||
harness.service.bulkDeleteLegacyMatches({
|
||||
communityId: harness.fixture.match.communityId,
|
||||
body: { matchIds: [harness.fixture.match._id.toString()] },
|
||||
}),
|
||||
).rejects.toThrow(/Tournament Code assignment/);
|
||||
|
||||
expect(harness.session.abortTransaction).toHaveBeenCalledTimes(1);
|
||||
expect(harness.session.commitTransaction).not.toHaveBeenCalled();
|
||||
expect(harness.matchModel.deleteMany).not.toHaveBeenCalled();
|
||||
expect(harness.competitionFactModel.deleteMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('aborts legacy deletion when a validated Tournament Code transition does not update exactly once', async () => {
|
||||
const harness = createBulkDeleteFailureHarness({
|
||||
updateResult: { matchedCount: 0, modifiedCount: 0 },
|
||||
});
|
||||
|
||||
await expect(
|
||||
harness.service.bulkDeleteLegacyMatches({
|
||||
communityId: harness.fixture.match.communityId,
|
||||
body: { matchIds: [harness.fixture.match._id.toString()] },
|
||||
}),
|
||||
).rejects.toThrow(/Tournament Code transition/);
|
||||
|
||||
expect(harness.session.abortTransaction).toHaveBeenCalledTimes(1);
|
||||
expect(harness.session.commitTransaction).not.toHaveBeenCalled();
|
||||
expect(harness.matchModel.deleteMany).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('invalidates community caches again when a committed deletion is retried', async () => {
|
||||
const harness = createBulkDeleteFailureHarness({});
|
||||
harness.matchModel.find
|
||||
.mockReset()
|
||||
.mockReturnValueOnce({
|
||||
select: jest
|
||||
.fn()
|
||||
.mockReturnValue(sessionResult([{ _id: harness.fixture.match._id }])),
|
||||
})
|
||||
.mockReturnValueOnce({
|
||||
select: jest.fn().mockReturnValue(sessionResult([])),
|
||||
});
|
||||
harness.communityStatsService.invalidateCommunity
|
||||
.mockRejectedValueOnce(new Error('Redis unavailable'))
|
||||
.mockResolvedValueOnce(0);
|
||||
|
||||
await expect(
|
||||
harness.service.bulkDeleteLegacyMatches({
|
||||
communityId: harness.fixture.match.communityId,
|
||||
body: { matchIds: [harness.fixture.match._id.toString()] },
|
||||
}),
|
||||
).rejects.toThrow('Redis unavailable');
|
||||
|
||||
await expect(
|
||||
harness.service.bulkDeleteLegacyMatches({
|
||||
communityId: harness.fixture.match.communityId,
|
||||
body: { matchIds: [harness.fixture.match._id.toString()] },
|
||||
}),
|
||||
).resolves.toEqual({ deleted: 0 });
|
||||
expect(
|
||||
harness.communityStatsService.invalidateCommunity,
|
||||
).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('removes migration quarantine records in the same legacy deletion transaction', async () => {
|
||||
const harness = createBulkDeleteFailureHarness({});
|
||||
|
||||
await harness.service.bulkDeleteLegacyMatches({
|
||||
communityId: harness.fixture.match.communityId,
|
||||
body: { matchIds: [harness.fixture.match._id.toString()] },
|
||||
});
|
||||
|
||||
expect(harness.quarantineCollection.deleteMany).toHaveBeenCalledWith(
|
||||
{ _id: { $in: [harness.fixture.match._id] } },
|
||||
{ session: harness.session },
|
||||
);
|
||||
});
|
||||
|
||||
it('rebuilds a historical legacy Match into Competition facts without rewriting Match.result', async () => {
|
||||
@@ -707,10 +851,135 @@ function execResult<T>(value: T) {
|
||||
return { exec: jest.fn().mockResolvedValue(value) };
|
||||
}
|
||||
|
||||
function sessionResult<T>(value: T) {
|
||||
const query = {
|
||||
exec: jest.fn().mockResolvedValue(value),
|
||||
session: jest.fn(),
|
||||
};
|
||||
query.session.mockReturnValue(query);
|
||||
return query;
|
||||
}
|
||||
|
||||
function deleteManyModel() {
|
||||
return {
|
||||
deleteMany: jest
|
||||
.fn()
|
||||
.mockReturnValue(execResult({ acknowledged: true, deletedCount: 1 })),
|
||||
.mockReturnValue(sessionResult({ acknowledged: true, deletedCount: 1 })),
|
||||
};
|
||||
}
|
||||
|
||||
function createBulkDeleteFailureHarness(options: {
|
||||
codeMatchId?: mongoose.Types.ObjectId;
|
||||
codeState?: 'reserved' | 'used';
|
||||
assignmentState?: 'reserved' | 'used';
|
||||
updateResult?: { matchedCount: number; modifiedCount: number };
|
||||
}) {
|
||||
const fixture = createImportFixture();
|
||||
const session = {
|
||||
startTransaction: jest.fn(),
|
||||
commitTransaction: jest.fn().mockResolvedValue(undefined),
|
||||
abortTransaction: jest.fn().mockResolvedValue(undefined),
|
||||
endSession: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
const tournamentCodeId = new mongoose.Types.ObjectId();
|
||||
const assignmentState = options.assignmentState ?? 'reserved';
|
||||
const codeState = options.codeState ?? assignmentState;
|
||||
const assignmentModel = {
|
||||
...deleteManyModel(),
|
||||
find: jest.fn().mockReturnValue({
|
||||
select: jest.fn().mockReturnValue({
|
||||
session: jest.fn().mockReturnValue({
|
||||
lean: jest
|
||||
.fn()
|
||||
.mockReturnValue(
|
||||
execResult([{ tournamentCodeId, state: assignmentState }]),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
const competitionModel = {
|
||||
...deleteManyModel(),
|
||||
find: jest.fn().mockReturnValue({
|
||||
select: jest.fn().mockReturnValue({
|
||||
session: jest.fn().mockReturnValue({
|
||||
lean: jest
|
||||
.fn()
|
||||
.mockReturnValue(execResult([{ _id: fixture.competition._id }])),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
};
|
||||
const matchModel = {
|
||||
find: jest.fn().mockReturnValue({
|
||||
select: jest
|
||||
.fn()
|
||||
.mockReturnValue(sessionResult([{ _id: fixture.match._id }])),
|
||||
}),
|
||||
deleteMany: jest.fn().mockReturnValue(sessionResult({ deletedCount: 1 })),
|
||||
};
|
||||
const tournamentCodeModel = {
|
||||
find: jest.fn().mockReturnValue({
|
||||
select: jest.fn().mockReturnValue({
|
||||
session: jest.fn().mockReturnValue({
|
||||
lean: jest.fn().mockReturnValue(
|
||||
execResult([
|
||||
{
|
||||
_id: tournamentCodeId,
|
||||
state: codeState,
|
||||
matchId: options.codeMatchId ?? fixture.match._id,
|
||||
},
|
||||
]),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
updateMany: jest
|
||||
.fn()
|
||||
.mockReturnValue(
|
||||
sessionResult(
|
||||
options.updateResult ?? { matchedCount: 1, modifiedCount: 1 },
|
||||
),
|
||||
),
|
||||
};
|
||||
const competitionFactModel = deleteManyModel();
|
||||
const competitionOutboxModel = deleteManyModel();
|
||||
const matchStatsModel = deleteManyModel();
|
||||
const quarantineCollection = {
|
||||
deleteMany: jest.fn().mockResolvedValue({ deletedCount: 1 }),
|
||||
};
|
||||
const connectionService = {
|
||||
createSession: jest.fn().mockResolvedValue(session),
|
||||
getConnection: jest.fn().mockResolvedValue({
|
||||
collection: jest.fn().mockReturnValue(quarantineCollection),
|
||||
}),
|
||||
getModel: jest.fn((name: string) => {
|
||||
const models: Record<string, unknown> = {
|
||||
match: matchModel,
|
||||
competition: competitionModel,
|
||||
competitionFact: competitionFactModel,
|
||||
competitionOutbox: competitionOutboxModel,
|
||||
tournamentCodeAssignment: assignmentModel,
|
||||
tournamentCode: tournamentCodeModel,
|
||||
matchStats: matchStatsModel,
|
||||
};
|
||||
const model = models[name];
|
||||
return model
|
||||
? Promise.resolve(model)
|
||||
: Promise.reject(new Error(`Unexpected model ${name}`));
|
||||
}),
|
||||
} as unknown as ConnectionService;
|
||||
const communityStatsService = {
|
||||
invalidateCommunity: jest.fn().mockResolvedValue(0),
|
||||
} as unknown as jest.Mocked<CommunityStatsService>;
|
||||
const service = createService({ connectionService, communityStatsService });
|
||||
return {
|
||||
fixture,
|
||||
session,
|
||||
service,
|
||||
matchModel,
|
||||
competitionFactModel,
|
||||
communityStatsService,
|
||||
quarantineCollection,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user