fix: prevent empty-team auction completion state

This commit is contained in:
dahyeon committed 2026-06-25 05:26:09 -07:00
1 parent 2984bd1604
commit ab721873f3
6 files changed
+748 -452

No files matched your search

@@ -92,7 +92,10 @@ describe('auction durability projection helpers', () => {
it.each([
{ current: { auctionLifecycleId: 'life-1', revision: 8 }, expected: true },
{ current: { auctionLifecycleId: 'life-1', revision: 9 }, expected: false },
{ current: { auctionLifecycleId: 'new-life', revision: 1 }, expected: false },
{
current: { auctionLifecycleId: 'new-life', revision: 1 },
expected: false,
},
])('decides whether projection can apply %#', ({ current, expected }) => {
expect(shouldApplyAuctionProjection(current, payload)).toBe(expected);
});
@@ -100,14 +103,23 @@ describe('auction durability projection helpers', () => {
it.each([
{ current: { auctionLifecycleId: 'life-1', revision: 8 }, expected: true },
{ current: { auctionLifecycleId: 'life-1', revision: 9 }, expected: true },
{ current: { auctionLifecycleId: 'life-1', revision: 10 }, expected: false },
{ current: { auctionLifecycleId: 'new-life', revision: 1 }, expected: false },
{
current: { auctionLifecycleId: 'life-1', revision: 10 },
expected: false,
},
{
current: { auctionLifecycleId: 'new-life', revision: 1 },
expected: false,
},
{ current: {}, expected: false },
])('decides whether a durability payload can be processed %#', ({ current, expected }) => {
expect(shouldProcessAuctionDurabilityPayload(current, payload)).toBe(
expected,
);
});
])(
'decides whether a durability payload can be processed %#',
({ current, expected }) => {
expect(shouldProcessAuctionDurabilityPayload(current, payload)).toBe(
expected,
);
},
);
it('reads due durability payload keys from Redis', async () => {
const sendCommand = jest.fn().mockResolvedValue(['payload-1']);
@@ -333,7 +345,10 @@ describe('auction durability projection helpers', () => {
createSession: jest.fn().mockResolvedValue(session),
getModel: jest.fn((name: string) => {
if (name === 'auctionHistory') {
return { deleteMany: historyDeleteMany, updateOne: historyUpdateOne };
return {
deleteMany: historyDeleteMany,
updateOne: historyUpdateOne,
};
}
if (name === 'auction') {
return { findById: auctionFindById, updateOne: auctionUpdateOne };
@@ -404,7 +419,10 @@ describe('auction durability projection helpers', () => {
createSession: jest.fn().mockResolvedValue(session),
getModel: jest.fn((name: string) => {
if (name === 'auctionHistory') {
return { deleteMany: historyDeleteMany, updateOne: historyUpdateOne };
return {
deleteMany: historyDeleteMany,
updateOne: historyUpdateOne,
};
}
if (name === 'auction') {
return { findById: auctionFindById, updateOne: auctionUpdateOne };
@@ -613,7 +631,9 @@ describe('auction durability projection helpers', () => {
.spyOn(scheduler, 'processDueDurabilityPayloads')
.mockResolvedValue({ applied: 0, failed: 0, skipped: 0 });
await (scheduler as unknown as { safeTick: () => Promise<void> }).safeTick();
await (
scheduler as unknown as { safeTick: () => Promise<void> }
).safeTick();
const auctionRedisService = (
scheduler as unknown as {
@@ -649,7 +669,9 @@ describe('auction durability projection helpers', () => {
.spyOn(scheduler, 'processDueDurabilityPayloads')
.mockResolvedValue({ applied: 0, failed: 0, skipped: 0 });
await (scheduler as unknown as { safeTick: () => Promise<void> }).safeTick();
await (
scheduler as unknown as { safeTick: () => Promise<void> }
).safeTick();
const auctionRedisService = (
scheduler as unknown as {
@@ -747,7 +769,8 @@ describe('auction durability projection helpers', () => {
if (name === 'auctionSideEffectApplication') {
return { updateOne: sideEffectUpdateOne };
}
if (name === 'match') return { findById: jest.fn().mockReturnValue(match) };
if (name === 'match')
return { findById: jest.fn().mockReturnValue(match) };
return {};
}),
} as never,
@@ -829,26 +852,18 @@ describe('auction durability projection helpers', () => {
matchSideEffectRevision: 3,
matchSideEffectStatus: 'completed',
});
expect(broadcastMatchSideEffect).toHaveBeenNthCalledWith(
1,
'match-1',
{
auctionLifecycleId: 'life-1',
currentServerTimestampMS: 2000,
matchSideEffectRevision: 2,
matchSideEffectStatus: 'processing',
},
);
expect(broadcastMatchSideEffect).toHaveBeenNthCalledWith(
2,
'match-1',
{
auctionLifecycleId: 'life-1',
currentServerTimestampMS: 3000,
matchSideEffectRevision: 3,
matchSideEffectStatus: 'completed',
},
);
expect(broadcastMatchSideEffect).toHaveBeenNthCalledWith(1, 'match-1', {
auctionLifecycleId: 'life-1',
currentServerTimestampMS: 2000,
matchSideEffectRevision: 2,
matchSideEffectStatus: 'processing',
});
expect(broadcastMatchSideEffect).toHaveBeenNthCalledWith(2, 'match-1', {
auctionLifecycleId: 'life-1',
currentServerTimestampMS: 3000,
matchSideEffectRevision: 3,
matchSideEffectStatus: 'completed',
});
expect(sendCommand).toHaveBeenCalledWith([
'EVAL',
expect.stringContaining("redis.call('EXPIRE'"),
@@ -870,6 +885,101 @@ describe('auction durability projection helpers', () => {
]);
});
it('fails sync match teams side effect when the completed projection has no teams', async () => {
const completePayload = {
...payload,
afterAuctionMongoProjection: {
auctionTeams: [],
matchSideEffectRevision: 1,
phase: 'complete',
revision: 10,
},
auctionActionType: 'complete',
matchSideEffectMutation: { type: 'syncMatchTeamsAfterComplete' },
revision: 10,
} as const;
const match = {
participants: [],
save: jest.fn().mockResolvedValue(undefined),
status: 'team-built',
teamBuiltAt: undefined,
teams: [],
};
const historyUpdateOne = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue({ upsertedCount: 1 }),
});
const auctionUpdateOne = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue({ modifiedCount: 1 }),
});
const auctionFindOneAndUpdate = jest
.fn()
.mockReturnValueOnce({
exec: jest.fn().mockResolvedValue({
auctionLifecycleId: 'life-1',
matchSideEffectRevision: 2,
matchSideEffectStatus: 'processing',
}),
})
.mockReturnValueOnce({
exec: jest.fn().mockResolvedValue({
auctionLifecycleId: 'life-1',
matchSideEffectRevision: 3,
matchSideEffectStatus: 'failed',
}),
});
const sideEffectUpdateOne = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue({ matchedCount: 1, modifiedCount: 1 }),
});
const reserveForMatch = jest.fn().mockResolvedValue(match);
const scheduleInitialPoll = jest.fn().mockResolvedValue(null);
const mirrorMatchSideEffectStatus = jest.fn().mockResolvedValue(true);
const broadcastMatchSideEffect = jest.fn();
const scheduler = new AuctionDurabilityScheduler(
{
createSession: jest.fn().mockResolvedValue(null),
getModel: jest.fn((name: string) => {
if (name === 'auctionHistory') return { updateOne: historyUpdateOne };
if (name === 'auction') {
return {
findOneAndUpdate: auctionFindOneAndUpdate,
updateOne: auctionUpdateOne,
};
}
if (name === 'auctionSideEffectApplication') {
return { updateOne: sideEffectUpdateOne };
}
if (name === 'match')
return { findById: jest.fn().mockReturnValue(match) };
return {};
}),
} as never,
{
getCurrentRedisTimestampMS: jest
.fn()
.mockResolvedValueOnce(2000)
.mockResolvedValueOnce(3000),
raw: jest.fn().mockReturnValue({ sendCommand: jest.fn() }),
} as never,
{ reserveForMatch } as never,
{ scheduleInitialPoll } as never,
{ mirrorMatchSideEffectStatus } as never,
{ broadcastMatchSideEffect } as never,
);
await expect(
scheduler.applyDurabilityPayload(completePayload),
).rejects.toThrow('produced no match teams');
expect(match.save).not.toHaveBeenCalled();
expect(reserveForMatch).not.toHaveBeenCalled();
expect(scheduleInitialPoll).not.toHaveBeenCalled();
expect(mirrorMatchSideEffectStatus).toHaveBeenLastCalledWith(
expect.objectContaining({
matchSideEffectStatus: 'failed',
}),
);
});
it('applies reset match side effect after durable projection', async () => {
const resetPayload = {
...payload,
@@ -943,7 +1053,10 @@ describe('auction durability projection helpers', () => {
createSession: jest.fn().mockResolvedValue(null),
getModel: jest.fn((name: string) => {
if (name === 'auctionHistory') {
return { deleteMany: historyDeleteMany, updateOne: historyUpdateOne };
return {
deleteMany: historyDeleteMany,
updateOne: historyUpdateOne,
};
}
if (name === 'auction') {
return {
@@ -955,7 +1068,8 @@ describe('auction durability projection helpers', () => {
if (name === 'auctionSideEffectApplication') {
return { updateOne: sideEffectUpdateOne };
}
if (name === 'match') return { findById: jest.fn().mockReturnValue(match) };
if (name === 'match')
return { findById: jest.fn().mockReturnValue(match) };
return {};
}),
} as never,
@@ -985,7 +1099,10 @@ describe('auction durability projection helpers', () => {
teams: [],
tournamentCodeIds: [],
});
expect(match.participants).toEqual([{ excluded: false }, { excluded: false }]);
expect(match.participants).toEqual([
{ excluded: false },
{ excluded: false },
]);
expect(match.save).toHaveBeenCalledTimes(1);
expect(sideEffectUpdateOne).toHaveBeenCalledWith(
expect.objectContaining({