1340 lines
43 KiB
TypeScript
1340 lines
43 KiB
TypeScript
import {
|
|
AuctionDurabilityScheduler,
|
|
buildAuctionHistoryUpsert,
|
|
buildAuctionProjectionGuardedUpdate,
|
|
buildSideEffectApplicationUpsert,
|
|
shouldApplyAuctionProjection,
|
|
shouldProcessAuctionDurabilityPayload,
|
|
} from './auction-durability.scheduler';
|
|
|
|
const payload = {
|
|
actorUserId: 'user-1',
|
|
afterAuctionMongoProjection: { phase: 'sold', revision: 9 },
|
|
auctionActionType: 'sold',
|
|
auctionHistorySequence: 4,
|
|
auctionId: 'auction-1',
|
|
auctionLifecycleId: 'life-1',
|
|
auctionStateTransitionId: 'transition-1',
|
|
committedAtRedisTimestampMS: 1780000000000,
|
|
matchId: 'match-1',
|
|
matchSideEffectMutation: { type: 'none' },
|
|
revision: 9,
|
|
snapshotBefore: { phase: 'bidding' },
|
|
} as const;
|
|
|
|
describe('auction durability projection helpers', () => {
|
|
it('builds idempotent history upsert by auction and transition id', () => {
|
|
expect(buildAuctionHistoryUpsert(payload)).toEqual({
|
|
updateOne: {
|
|
filter: {
|
|
auctionId: 'auction-1',
|
|
auctionStateTransitionId: 'transition-1',
|
|
},
|
|
update: {
|
|
$setOnInsert: {
|
|
action: 'sold',
|
|
actorUserId: 'user-1',
|
|
auctionId: 'auction-1',
|
|
auctionLifecycleId: 'life-1',
|
|
auctionStateTransitionId: 'transition-1',
|
|
matchId: 'match-1',
|
|
matchSideEffectMutation: { type: 'none' },
|
|
revision: 9,
|
|
sequence: 4,
|
|
snapshotBefore: { phase: 'bidding' },
|
|
takenAt: new Date(1780000000000),
|
|
},
|
|
},
|
|
upsert: true,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('guards projection updates by lifecycle and lower revision', () => {
|
|
expect(buildAuctionProjectionGuardedUpdate(payload)).toEqual({
|
|
filter: {
|
|
_id: 'auction-1',
|
|
auctionLifecycleId: 'life-1',
|
|
revision: { $lt: 9 },
|
|
},
|
|
update: {
|
|
$set: { phase: 'sold', revision: 9 },
|
|
},
|
|
});
|
|
});
|
|
|
|
it('builds idempotent side-effect application upsert', () => {
|
|
expect(
|
|
buildSideEffectApplicationUpsert(payload, {
|
|
type: 'syncMatchTeamsAfterComplete',
|
|
}),
|
|
).toEqual({
|
|
filter: {
|
|
auctionId: 'auction-1',
|
|
auctionStateTransitionId: 'transition-1',
|
|
sideEffectType: 'syncMatchTeamsAfterComplete',
|
|
},
|
|
update: {
|
|
$setOnInsert: expect.objectContaining({
|
|
attempts: 0,
|
|
auctionId: 'auction-1',
|
|
auctionLifecycleId: 'life-1',
|
|
auctionStateTransitionId: 'transition-1',
|
|
matchId: 'match-1',
|
|
payload: { type: 'syncMatchTeamsAfterComplete' },
|
|
sideEffectType: 'syncMatchTeamsAfterComplete',
|
|
state: 'pending',
|
|
}),
|
|
},
|
|
});
|
|
});
|
|
|
|
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,
|
|
},
|
|
])('decides whether projection can apply %#', ({ current, expected }) => {
|
|
expect(shouldApplyAuctionProjection(current, payload)).toBe(expected);
|
|
});
|
|
|
|
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: {}, expected: false },
|
|
])(
|
|
'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']);
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{ getModel: jest.fn(), createSession: jest.fn() } as never,
|
|
{
|
|
raw: jest.fn().mockReturnValue({ sendCommand }),
|
|
} as never,
|
|
);
|
|
|
|
await expect(
|
|
scheduler.readDueDurabilityPayloadKeys(1000, 10),
|
|
).resolves.toEqual(['payload-1']);
|
|
expect(sendCommand).toHaveBeenCalledWith([
|
|
'ZRANGEBYSCORE',
|
|
'auction:auctionDurabilityRetryQueue',
|
|
'-inf',
|
|
'1000',
|
|
'LIMIT',
|
|
'0',
|
|
'10',
|
|
]);
|
|
});
|
|
|
|
it('parses a durability payload hash', async () => {
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{ getModel: jest.fn(), createSession: jest.fn() } as never,
|
|
{
|
|
raw: jest.fn().mockReturnValue({
|
|
sendCommand: jest.fn().mockResolvedValue({
|
|
broadcastStateRevision: '9',
|
|
committedAtRedisTimestampMS: '1780000000000',
|
|
payloadJson: JSON.stringify(payload),
|
|
shouldBroadcastState: '1',
|
|
}),
|
|
}),
|
|
} as never,
|
|
);
|
|
|
|
await expect(scheduler.readDurabilityPayload('payload-1')).resolves.toEqual(
|
|
{
|
|
...payload,
|
|
broadcastStateRevision: 9,
|
|
shouldBroadcastState: true,
|
|
},
|
|
);
|
|
});
|
|
|
|
it('applies history and projection updates in a session', async () => {
|
|
const session = {
|
|
abortTransaction: jest.fn().mockResolvedValue(undefined),
|
|
commitTransaction: jest.fn().mockResolvedValue(undefined),
|
|
endSession: jest.fn().mockResolvedValue(undefined),
|
|
startTransaction: jest.fn(),
|
|
};
|
|
const historyUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ upsertedCount: 1 }),
|
|
});
|
|
const auctionUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ modifiedCount: 1 }),
|
|
});
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{
|
|
createSession: jest.fn().mockResolvedValue(session),
|
|
getModel: jest.fn((name: string) =>
|
|
name === 'auctionHistory'
|
|
? { updateOne: historyUpdateOne }
|
|
: { updateOne: auctionUpdateOne },
|
|
),
|
|
} as never,
|
|
{ raw: jest.fn() } as never,
|
|
);
|
|
|
|
await scheduler.applyDurabilityPayload(payload);
|
|
|
|
expect(session.startTransaction).toHaveBeenCalledTimes(1);
|
|
expect(historyUpdateOne).toHaveBeenCalledWith(
|
|
{
|
|
auctionId: 'auction-1',
|
|
auctionStateTransitionId: 'transition-1',
|
|
},
|
|
expect.objectContaining({ $setOnInsert: expect.any(Object) }),
|
|
{ session, upsert: true },
|
|
);
|
|
expect(auctionUpdateOne).toHaveBeenCalledWith(
|
|
{
|
|
_id: 'auction-1',
|
|
auctionLifecycleId: 'life-1',
|
|
revision: { $lt: 9 },
|
|
},
|
|
{ $set: { phase: 'sold', revision: 9 } },
|
|
{ session },
|
|
);
|
|
expect(session.commitTransaction).toHaveBeenCalledTimes(1);
|
|
expect(session.endSession).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('broadcasts latest Redis state when durability payload requests it', async () => {
|
|
const session = {
|
|
abortTransaction: jest.fn().mockResolvedValue(undefined),
|
|
commitTransaction: jest.fn().mockResolvedValue(undefined),
|
|
endSession: jest.fn().mockResolvedValue(undefined),
|
|
startTransaction: jest.fn(),
|
|
};
|
|
const historyUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ upsertedCount: 1 }),
|
|
});
|
|
const auctionUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ modifiedCount: 1 }),
|
|
});
|
|
const readAuctionStateView = jest.fn().mockResolvedValue({
|
|
auctionLifecycleId: 'life-1',
|
|
phase: 'sold',
|
|
revision: 9,
|
|
});
|
|
const broadcastState = jest.fn();
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{
|
|
createSession: jest.fn().mockResolvedValue(session),
|
|
getModel: jest.fn((name: string) =>
|
|
name === 'auctionHistory'
|
|
? { updateOne: historyUpdateOne }
|
|
: { updateOne: auctionUpdateOne },
|
|
),
|
|
} as never,
|
|
{ raw: jest.fn() } as never,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
{ broadcastState } as never,
|
|
{ readAuctionStateView } as never,
|
|
);
|
|
|
|
await scheduler.applyDurabilityPayload({
|
|
...payload,
|
|
broadcastStateRevision: 9,
|
|
shouldBroadcastState: true,
|
|
});
|
|
|
|
expect(readAuctionStateView).toHaveBeenCalledWith('match-1');
|
|
expect(broadcastState).toHaveBeenCalledWith('match-1', {
|
|
auctionLifecycleId: 'life-1',
|
|
phase: 'sold',
|
|
revision: 9,
|
|
});
|
|
});
|
|
|
|
it('does not broadcast stale Redis state below requested revision', async () => {
|
|
const session = {
|
|
abortTransaction: jest.fn().mockResolvedValue(undefined),
|
|
commitTransaction: jest.fn().mockResolvedValue(undefined),
|
|
endSession: jest.fn().mockResolvedValue(undefined),
|
|
startTransaction: jest.fn(),
|
|
};
|
|
const historyUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ upsertedCount: 1 }),
|
|
});
|
|
const auctionUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ modifiedCount: 1 }),
|
|
});
|
|
const readAuctionStateView = jest.fn().mockResolvedValue({
|
|
auctionLifecycleId: 'life-1',
|
|
phase: 'bidding',
|
|
revision: 8,
|
|
});
|
|
const broadcastState = jest.fn();
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{
|
|
createSession: jest.fn().mockResolvedValue(session),
|
|
getModel: jest.fn((name: string) =>
|
|
name === 'auctionHistory'
|
|
? { updateOne: historyUpdateOne }
|
|
: { updateOne: auctionUpdateOne },
|
|
),
|
|
} as never,
|
|
{ raw: jest.fn() } as never,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
{ broadcastState } as never,
|
|
{ readAuctionStateView } as never,
|
|
);
|
|
|
|
await scheduler.applyDurabilityPayload({
|
|
...payload,
|
|
broadcastStateRevision: 9,
|
|
shouldBroadcastState: true,
|
|
});
|
|
|
|
expect(readAuctionStateView).toHaveBeenCalledWith('match-1');
|
|
expect(broadcastState).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('applies undo history mutation only when projection can advance', async () => {
|
|
const session = {
|
|
abortTransaction: jest.fn().mockResolvedValue(undefined),
|
|
commitTransaction: jest.fn().mockResolvedValue(undefined),
|
|
endSession: jest.fn().mockResolvedValue(undefined),
|
|
startTransaction: jest.fn(),
|
|
};
|
|
const historyDeleteMany = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ deletedCount: 2 }),
|
|
});
|
|
const historyUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ upsertedCount: 1 }),
|
|
});
|
|
const auctionUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ modifiedCount: 1 }),
|
|
});
|
|
const projectionExec = jest.fn().mockResolvedValue({
|
|
auctionLifecycleId: 'life-1',
|
|
revision: 8,
|
|
});
|
|
const auctionFindById = jest.fn().mockReturnValue({
|
|
select: jest.fn().mockReturnValue({
|
|
session: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({ exec: projectionExec }),
|
|
}),
|
|
}),
|
|
});
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{
|
|
createSession: jest.fn().mockResolvedValue(session),
|
|
getModel: jest.fn((name: string) => {
|
|
if (name === 'auctionHistory') {
|
|
return {
|
|
deleteMany: historyDeleteMany,
|
|
updateOne: historyUpdateOne,
|
|
};
|
|
}
|
|
if (name === 'auction') {
|
|
return { findById: auctionFindById, updateOne: auctionUpdateOne };
|
|
}
|
|
return { updateOne: jest.fn() };
|
|
}),
|
|
} as never,
|
|
{ raw: jest.fn() } as never,
|
|
);
|
|
|
|
await scheduler.applyDurabilityPayload({
|
|
...payload,
|
|
historyMutation: {
|
|
targetHistorySequence: 2,
|
|
type: 'deleteAfterSequenceAndAppendUndo',
|
|
},
|
|
});
|
|
|
|
expect(historyDeleteMany).toHaveBeenCalledWith(
|
|
{
|
|
auctionId: 'auction-1',
|
|
sequence: { $gt: 2 },
|
|
},
|
|
{ session },
|
|
);
|
|
expect(historyUpdateOne).toHaveBeenCalledWith(
|
|
{
|
|
auctionId: 'auction-1',
|
|
auctionStateTransitionId: 'transition-1',
|
|
},
|
|
expect.objectContaining({
|
|
$setOnInsert: expect.objectContaining({
|
|
historyMutation: {
|
|
targetHistorySequence: 2,
|
|
type: 'deleteAfterSequenceAndAppendUndo',
|
|
},
|
|
}),
|
|
}),
|
|
{ session, upsert: true },
|
|
);
|
|
});
|
|
|
|
it('skips destructive history mutation for stale payloads', async () => {
|
|
const session = {
|
|
abortTransaction: jest.fn().mockResolvedValue(undefined),
|
|
commitTransaction: jest.fn().mockResolvedValue(undefined),
|
|
endSession: jest.fn().mockResolvedValue(undefined),
|
|
startTransaction: jest.fn(),
|
|
};
|
|
const historyDeleteMany = jest.fn();
|
|
const historyUpdateOne = jest.fn();
|
|
const auctionUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ matchedCount: 0, modifiedCount: 0 }),
|
|
});
|
|
const projectionExec = jest.fn().mockResolvedValue({
|
|
auctionLifecycleId: 'life-1',
|
|
revision: 10,
|
|
});
|
|
const auctionFindById = jest.fn().mockReturnValue({
|
|
select: jest.fn().mockReturnValue({
|
|
session: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({ exec: projectionExec }),
|
|
}),
|
|
}),
|
|
});
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{
|
|
createSession: jest.fn().mockResolvedValue(session),
|
|
getModel: jest.fn((name: string) => {
|
|
if (name === 'auctionHistory') {
|
|
return {
|
|
deleteMany: historyDeleteMany,
|
|
updateOne: historyUpdateOne,
|
|
};
|
|
}
|
|
if (name === 'auction') {
|
|
return { findById: auctionFindById, updateOne: auctionUpdateOne };
|
|
}
|
|
return { updateOne: jest.fn() };
|
|
}),
|
|
} as never,
|
|
{ raw: jest.fn() } as never,
|
|
);
|
|
|
|
await scheduler.applyDurabilityPayload({
|
|
...payload,
|
|
historyMutation: {
|
|
targetHistorySequence: 2,
|
|
type: 'deleteAfterSequenceAndAppendUndo',
|
|
},
|
|
});
|
|
|
|
expect(historyDeleteMany).not.toHaveBeenCalled();
|
|
expect(historyUpdateOne).not.toHaveBeenCalled();
|
|
expect(auctionUpdateOne).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('recovers stale append payloads as history-only records', async () => {
|
|
const session = {
|
|
abortTransaction: jest.fn().mockResolvedValue(undefined),
|
|
commitTransaction: jest.fn().mockResolvedValue(undefined),
|
|
endSession: jest.fn().mockResolvedValue(undefined),
|
|
startTransaction: jest.fn(),
|
|
};
|
|
const historyFindOne = jest.fn().mockReturnValue({
|
|
session: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue(null),
|
|
}),
|
|
}),
|
|
});
|
|
const historyUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ upsertedCount: 1 }),
|
|
});
|
|
const auctionUpdateOne = jest.fn();
|
|
const sideEffectUpdateOne = jest.fn();
|
|
const projectionExec = jest.fn().mockResolvedValue({
|
|
auctionLifecycleId: 'life-1',
|
|
revision: 10,
|
|
});
|
|
const auctionFindById = jest.fn().mockReturnValue({
|
|
select: jest.fn().mockReturnValue({
|
|
session: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({ exec: projectionExec }),
|
|
}),
|
|
}),
|
|
});
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{
|
|
createSession: jest.fn().mockResolvedValue(session),
|
|
getModel: jest.fn((name: string) => {
|
|
if (name === 'auctionHistory') {
|
|
return {
|
|
findOne: historyFindOne,
|
|
updateOne: historyUpdateOne,
|
|
};
|
|
}
|
|
if (name === 'auction') {
|
|
return { findById: auctionFindById, updateOne: auctionUpdateOne };
|
|
}
|
|
return { updateOne: sideEffectUpdateOne };
|
|
}),
|
|
} as never,
|
|
{ raw: jest.fn() } as never,
|
|
);
|
|
|
|
await scheduler.applyDurabilityPayload({
|
|
...payload,
|
|
matchSideEffectMutation: { type: 'syncMatchTeamsAfterComplete' },
|
|
});
|
|
|
|
expect(historyFindOne).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
auctionId: 'auction-1',
|
|
sequence: { $gt: 4 },
|
|
}),
|
|
);
|
|
expect(historyUpdateOne).toHaveBeenCalledWith(
|
|
{
|
|
auctionId: 'auction-1',
|
|
auctionStateTransitionId: 'transition-1',
|
|
},
|
|
expect.objectContaining({
|
|
$setOnInsert: expect.objectContaining({
|
|
sequence: 4,
|
|
}),
|
|
}),
|
|
{ session, upsert: true },
|
|
);
|
|
expect(auctionUpdateOne).not.toHaveBeenCalled();
|
|
expect(sideEffectUpdateOne).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('does not resurrect stale append payloads invalidated by later undo', async () => {
|
|
const session = {
|
|
abortTransaction: jest.fn().mockResolvedValue(undefined),
|
|
commitTransaction: jest.fn().mockResolvedValue(undefined),
|
|
endSession: jest.fn().mockResolvedValue(undefined),
|
|
startTransaction: jest.fn(),
|
|
};
|
|
const historyFindOne = jest.fn().mockReturnValue({
|
|
session: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({
|
|
historyMutation: {
|
|
targetHistorySequence: 2,
|
|
type: 'deleteAfterSequenceAndAppendUndo',
|
|
},
|
|
}),
|
|
}),
|
|
}),
|
|
});
|
|
const historyUpdateOne = jest.fn();
|
|
const auctionUpdateOne = jest.fn();
|
|
const projectionExec = jest.fn().mockResolvedValue({
|
|
auctionLifecycleId: 'life-1',
|
|
revision: 10,
|
|
});
|
|
const auctionFindById = jest.fn().mockReturnValue({
|
|
select: jest.fn().mockReturnValue({
|
|
session: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({ exec: projectionExec }),
|
|
}),
|
|
}),
|
|
});
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{
|
|
createSession: jest.fn().mockResolvedValue(session),
|
|
getModel: jest.fn((name: string) => {
|
|
if (name === 'auctionHistory') {
|
|
return {
|
|
findOne: historyFindOne,
|
|
updateOne: historyUpdateOne,
|
|
};
|
|
}
|
|
if (name === 'auction') {
|
|
return { findById: auctionFindById, updateOne: auctionUpdateOne };
|
|
}
|
|
return { updateOne: jest.fn() };
|
|
}),
|
|
} as never,
|
|
{ raw: jest.fn() } as never,
|
|
);
|
|
|
|
await scheduler.applyDurabilityPayload(payload);
|
|
|
|
expect(historyFindOne).toHaveBeenCalled();
|
|
expect(historyUpdateOne).not.toHaveBeenCalled();
|
|
expect(auctionUpdateOne).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('clears stale append payloads after history-only recovery', async () => {
|
|
const sendCommand = jest
|
|
.fn()
|
|
.mockResolvedValueOnce(['payload-1'])
|
|
.mockResolvedValueOnce({
|
|
committedAtRedisTimestampMS: '1780000000000',
|
|
payloadJson: JSON.stringify({
|
|
...payload,
|
|
matchSideEffectMutation: { type: 'syncMatchTeamsAfterComplete' },
|
|
}),
|
|
})
|
|
.mockResolvedValueOnce(1)
|
|
.mockResolvedValueOnce(1);
|
|
const historyFindOne = jest.fn().mockReturnValue({
|
|
session: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue(null),
|
|
}),
|
|
}),
|
|
});
|
|
const historyUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ upsertedCount: 1 }),
|
|
});
|
|
const auctionUpdateOne = jest.fn();
|
|
const sideEffectUpdateOne = jest.fn();
|
|
const auctionFindById = jest.fn().mockReturnValue({
|
|
select: jest.fn().mockReturnValue({
|
|
session: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({
|
|
auctionLifecycleId: 'life-1',
|
|
revision: 10,
|
|
}),
|
|
}),
|
|
}),
|
|
}),
|
|
});
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{
|
|
createSession: jest.fn().mockResolvedValue(null),
|
|
getModel: jest.fn((name: string) => {
|
|
if (name === 'auctionHistory') {
|
|
return {
|
|
findOne: historyFindOne,
|
|
updateOne: historyUpdateOne,
|
|
};
|
|
}
|
|
if (name === 'auction') {
|
|
return { findById: auctionFindById, updateOne: auctionUpdateOne };
|
|
}
|
|
return { updateOne: sideEffectUpdateOne };
|
|
}),
|
|
} as never,
|
|
{
|
|
getCurrentRedisTimestampMS: jest.fn().mockResolvedValue(1000),
|
|
raw: jest.fn().mockReturnValue({ sendCommand }),
|
|
} as never,
|
|
);
|
|
|
|
await expect(scheduler.processDueDurabilityPayloads(10)).resolves.toEqual({
|
|
applied: 1,
|
|
failed: 0,
|
|
skipped: 0,
|
|
});
|
|
|
|
expect(historyUpdateOne).toHaveBeenCalledTimes(1);
|
|
expect(auctionUpdateOne).not.toHaveBeenCalled();
|
|
expect(sideEffectUpdateOne).not.toHaveBeenCalled();
|
|
expect(sendCommand).toHaveBeenNthCalledWith(3, [
|
|
'ZREM',
|
|
'auction:auctionDurabilityRetryQueue',
|
|
'payload-1',
|
|
]);
|
|
expect(sendCommand).toHaveBeenNthCalledWith(4, ['DEL', 'payload-1']);
|
|
});
|
|
|
|
it('processes due payloads and clears successful outbox entries', async () => {
|
|
const sendCommand = jest
|
|
.fn()
|
|
.mockResolvedValueOnce(['payload-1'])
|
|
.mockResolvedValueOnce({
|
|
committedAtRedisTimestampMS: '1780000000000',
|
|
payloadJson: JSON.stringify(payload),
|
|
})
|
|
.mockResolvedValueOnce(1)
|
|
.mockResolvedValueOnce(1);
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{
|
|
createSession: jest.fn().mockResolvedValue(null),
|
|
getModel: jest.fn((name: string) =>
|
|
name === 'auctionHistory'
|
|
? {
|
|
updateOne: jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({}),
|
|
}),
|
|
}
|
|
: {
|
|
updateOne: jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({}),
|
|
}),
|
|
},
|
|
),
|
|
} as never,
|
|
{
|
|
getCurrentRedisTimestampMS: jest.fn().mockResolvedValue(1000),
|
|
raw: jest.fn().mockReturnValue({ sendCommand }),
|
|
} as never,
|
|
);
|
|
|
|
await expect(scheduler.processDueDurabilityPayloads(10)).resolves.toEqual({
|
|
applied: 1,
|
|
failed: 0,
|
|
skipped: 0,
|
|
});
|
|
expect(sendCommand).toHaveBeenLastCalledWith(['DEL', 'payload-1']);
|
|
});
|
|
|
|
it('requeues failed durability payloads with an incremented attempt count', async () => {
|
|
const sendCommand = jest
|
|
.fn()
|
|
.mockResolvedValueOnce(['payload-1'])
|
|
.mockResolvedValueOnce({
|
|
committedAtRedisTimestampMS: '1780000000000',
|
|
payloadJson: JSON.stringify(payload),
|
|
})
|
|
.mockResolvedValueOnce(1)
|
|
.mockResolvedValueOnce(1)
|
|
.mockResolvedValueOnce(1);
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{ createSession: jest.fn(), getModel: jest.fn() } as never,
|
|
{
|
|
getCurrentRedisTimestampMS: jest.fn().mockResolvedValue(1000),
|
|
raw: jest.fn().mockReturnValue({ sendCommand }),
|
|
} as never,
|
|
);
|
|
jest
|
|
.spyOn(scheduler, 'applyDurabilityPayload')
|
|
.mockRejectedValue(new Error('mongo down'));
|
|
|
|
await expect(scheduler.processDueDurabilityPayloads(10)).resolves.toEqual({
|
|
applied: 0,
|
|
failed: 1,
|
|
skipped: 0,
|
|
});
|
|
expect(sendCommand).toHaveBeenNthCalledWith(3, [
|
|
'HINCRBY',
|
|
'payload-1',
|
|
'attempts',
|
|
'1',
|
|
]);
|
|
expect(sendCommand).toHaveBeenNthCalledWith(4, [
|
|
'HSET',
|
|
'payload-1',
|
|
'lastError',
|
|
'mongo down',
|
|
'lastFailedAtRedisTimestampMS',
|
|
'1000',
|
|
]);
|
|
expect(sendCommand).toHaveBeenNthCalledWith(5, [
|
|
'ZADD',
|
|
'auction:auctionDurabilityRetryQueue',
|
|
'6000',
|
|
'payload-1',
|
|
]);
|
|
});
|
|
|
|
it('uses bounded exponential backoff for repeated durability failures', async () => {
|
|
const sendCommand = jest
|
|
.fn()
|
|
.mockResolvedValueOnce(['payload-1'])
|
|
.mockResolvedValueOnce({
|
|
committedAtRedisTimestampMS: '1780000000000',
|
|
payloadJson: JSON.stringify(payload),
|
|
})
|
|
.mockResolvedValueOnce(3)
|
|
.mockResolvedValueOnce(1)
|
|
.mockResolvedValueOnce(1);
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{ createSession: jest.fn(), getModel: jest.fn() } as never,
|
|
{
|
|
getCurrentRedisTimestampMS: jest.fn().mockResolvedValue(1000),
|
|
raw: jest.fn().mockReturnValue({ sendCommand }),
|
|
} as never,
|
|
);
|
|
jest
|
|
.spyOn(scheduler, 'applyDurabilityPayload')
|
|
.mockRejectedValue(new Error('mongo down'));
|
|
|
|
await expect(scheduler.processDueDurabilityPayloads(10)).resolves.toEqual({
|
|
applied: 0,
|
|
failed: 1,
|
|
skipped: 0,
|
|
});
|
|
expect(sendCommand).toHaveBeenNthCalledWith(5, [
|
|
'ZADD',
|
|
'auction:auctionDurabilityRetryQueue',
|
|
'21000',
|
|
'payload-1',
|
|
]);
|
|
});
|
|
|
|
it('moves repeatedly failing durability payloads to the dead-letter queue', async () => {
|
|
const sendCommand = jest
|
|
.fn()
|
|
.mockResolvedValueOnce(['payload-1'])
|
|
.mockResolvedValueOnce({
|
|
committedAtRedisTimestampMS: '1780000000000',
|
|
payloadJson: JSON.stringify(payload),
|
|
})
|
|
.mockResolvedValueOnce(10)
|
|
.mockResolvedValueOnce(1)
|
|
.mockResolvedValueOnce(1)
|
|
.mockResolvedValueOnce(1)
|
|
.mockResolvedValueOnce(1);
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{ createSession: jest.fn(), getModel: jest.fn() } as never,
|
|
{
|
|
getCurrentRedisTimestampMS: jest.fn().mockResolvedValue(1000),
|
|
raw: jest.fn().mockReturnValue({ sendCommand }),
|
|
} as never,
|
|
);
|
|
jest
|
|
.spyOn(scheduler, 'applyDurabilityPayload')
|
|
.mockRejectedValue(new Error('side effect down'));
|
|
|
|
await expect(scheduler.processDueDurabilityPayloads(10)).resolves.toEqual({
|
|
applied: 0,
|
|
failed: 1,
|
|
skipped: 0,
|
|
});
|
|
expect(sendCommand).toHaveBeenNthCalledWith(5, [
|
|
'ZREM',
|
|
'auction:auctionDurabilityRetryQueue',
|
|
'payload-1',
|
|
]);
|
|
expect(sendCommand).toHaveBeenNthCalledWith(6, [
|
|
'HSET',
|
|
'payload-1',
|
|
'deadLetteredAtRedisTimestampMS',
|
|
'1000',
|
|
]);
|
|
expect(sendCommand).toHaveBeenNthCalledWith(7, [
|
|
'ZADD',
|
|
'auction:auctionDurabilityDeadLetterQueue',
|
|
'1000',
|
|
'payload-1',
|
|
]);
|
|
});
|
|
|
|
it('processes durability ticks only while holding the Redis leader lock', async () => {
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{ createSession: jest.fn(), getModel: jest.fn() } as never,
|
|
{
|
|
acquireTokenLock: jest.fn().mockResolvedValue(true),
|
|
releaseTokenLock: jest.fn().mockResolvedValue(true),
|
|
} as never,
|
|
);
|
|
const processDueDurabilityPayloadsSpy = jest
|
|
.spyOn(scheduler, 'processDueDurabilityPayloads')
|
|
.mockResolvedValue({ applied: 0, failed: 0, skipped: 0 });
|
|
|
|
await (
|
|
scheduler as unknown as { safeTick: () => Promise<void> }
|
|
).safeTick();
|
|
|
|
const auctionRedisService = (
|
|
scheduler as unknown as {
|
|
auctionRedisService: {
|
|
acquireTokenLock: jest.Mock;
|
|
releaseTokenLock: jest.Mock;
|
|
};
|
|
}
|
|
).auctionRedisService;
|
|
const leaderLockToken =
|
|
auctionRedisService.acquireTokenLock.mock.calls[0]?.[1];
|
|
expect(auctionRedisService.acquireTokenLock).toHaveBeenCalledWith(
|
|
'auction:auctionDurabilityScheduler:leader',
|
|
expect.any(String),
|
|
2500,
|
|
);
|
|
expect(processDueDurabilityPayloadsSpy).toHaveBeenCalledWith(20);
|
|
expect(auctionRedisService.releaseTokenLock).toHaveBeenCalledWith(
|
|
'auction:auctionDurabilityScheduler:leader',
|
|
leaderLockToken,
|
|
);
|
|
});
|
|
|
|
it('skips durability ticks when another instance holds the leader lock', async () => {
|
|
const scheduler = new AuctionDurabilityScheduler(
|
|
{ createSession: jest.fn(), getModel: jest.fn() } as never,
|
|
{
|
|
acquireTokenLock: jest.fn().mockResolvedValue(false),
|
|
releaseTokenLock: jest.fn().mockResolvedValue(true),
|
|
} as never,
|
|
);
|
|
const processDueDurabilityPayloadsSpy = jest
|
|
.spyOn(scheduler, 'processDueDurabilityPayloads')
|
|
.mockResolvedValue({ applied: 0, failed: 0, skipped: 0 });
|
|
|
|
await (
|
|
scheduler as unknown as { safeTick: () => Promise<void> }
|
|
).safeTick();
|
|
|
|
const auctionRedisService = (
|
|
scheduler as unknown as {
|
|
auctionRedisService: {
|
|
releaseTokenLock: jest.Mock;
|
|
};
|
|
}
|
|
).auctionRedisService;
|
|
expect(processDueDurabilityPayloadsSpy).not.toHaveBeenCalled();
|
|
expect(auctionRedisService.releaseTokenLock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('applies sync match teams side effect after durable projection', async () => {
|
|
const completePayload = {
|
|
...payload,
|
|
afterAuctionMongoProjection: {
|
|
auctionTeams: [
|
|
{
|
|
teamIndex: 1,
|
|
captainUserId: 'captain-1',
|
|
members: [
|
|
{
|
|
assignedPosition: 'ADC',
|
|
memberAcquisitionBidPointAmount: 30,
|
|
userId: 'member-1',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
matchSideEffectRevision: 1,
|
|
phase: 'complete',
|
|
revision: 10,
|
|
},
|
|
auctionActionType: 'complete',
|
|
matchSideEffectMutation: { type: 'syncMatchTeamsAfterComplete' },
|
|
revision: 10,
|
|
} as const;
|
|
const match = {
|
|
participants: [
|
|
{
|
|
userId: { toString: () => 'captain-1' },
|
|
preferredPositions: [{ position: 'TOP' }],
|
|
},
|
|
{
|
|
userId: { toString: () => 'member-1' },
|
|
preferredPositions: [{ position: 'ADC' }],
|
|
},
|
|
],
|
|
save: jest.fn().mockResolvedValue(undefined),
|
|
status: 'recruiting',
|
|
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: 'completed',
|
|
}),
|
|
});
|
|
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 sendCommand = jest.fn().mockResolvedValue(1);
|
|
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 }),
|
|
} as never,
|
|
{ reserveForMatch } as never,
|
|
{ scheduleInitialPoll } as never,
|
|
{ mirrorMatchSideEffectStatus } as never,
|
|
{ broadcastMatchSideEffect } as never,
|
|
);
|
|
|
|
await scheduler.applyDurabilityPayload(completePayload);
|
|
|
|
expect(match.teams).toEqual([
|
|
{
|
|
captainUserId: 'captain-1',
|
|
members: [
|
|
{ assignedPosition: 'TOP', cost: 0, userId: 'captain-1' },
|
|
{ assignedPosition: 'ADC', cost: 30, userId: 'member-1' },
|
|
],
|
|
name: '1팀',
|
|
teamIndex: 1,
|
|
},
|
|
]);
|
|
expect(match.save).toHaveBeenCalledTimes(1);
|
|
expect(reserveForMatch).toHaveBeenCalledWith(match);
|
|
expect(scheduleInitialPoll).toHaveBeenCalledWith(match);
|
|
expect(sideEffectUpdateOne).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
auctionId: 'auction-1',
|
|
auctionStateTransitionId: 'transition-1',
|
|
sideEffectType: 'syncMatchTeamsAfterComplete',
|
|
}),
|
|
expect.objectContaining({ $setOnInsert: expect.any(Object) }),
|
|
{ session: null, upsert: true },
|
|
);
|
|
expect(auctionFindOneAndUpdate).toHaveBeenNthCalledWith(
|
|
1,
|
|
{
|
|
_id: 'auction-1',
|
|
auctionLifecycleId: 'life-1',
|
|
matchSideEffectRevision: 1,
|
|
},
|
|
{
|
|
$inc: { matchSideEffectRevision: 1 },
|
|
$set: { matchSideEffectStatus: 'processing' },
|
|
$unset: { matchSideEffectError: '' },
|
|
},
|
|
expect.objectContaining({ returnDocument: 'after' }),
|
|
);
|
|
expect(auctionFindOneAndUpdate).toHaveBeenNthCalledWith(
|
|
2,
|
|
{
|
|
_id: 'auction-1',
|
|
auctionLifecycleId: 'life-1',
|
|
matchSideEffectRevision: 2,
|
|
},
|
|
{
|
|
$inc: { matchSideEffectRevision: 1 },
|
|
$set: { matchSideEffectStatus: 'completed' },
|
|
$unset: { matchSideEffectError: '' },
|
|
},
|
|
expect.objectContaining({ returnDocument: 'after' }),
|
|
);
|
|
expect(mirrorMatchSideEffectStatus).toHaveBeenCalledWith({
|
|
auctionLifecycleId: 'life-1',
|
|
matchId: 'match-1',
|
|
matchSideEffectRevision: 2,
|
|
matchSideEffectStatus: 'processing',
|
|
});
|
|
expect(mirrorMatchSideEffectStatus).toHaveBeenLastCalledWith({
|
|
auctionLifecycleId: 'life-1',
|
|
matchId: 'match-1',
|
|
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'"),
|
|
'11',
|
|
'auction:match-1:liveAuctionState',
|
|
'auction:bidRoundDeadlineQueue',
|
|
'auction:match-1:auctionStructureState',
|
|
'auction:match-1:appliedAuctionStateTransitionIds',
|
|
'auction:match-1:auctionBidHistory',
|
|
'auction:match-1:bidRequestResults',
|
|
'auction:match-1:bidRequestResultOrder',
|
|
'auction:match-1:auctionActionRequestResults',
|
|
'auction:match-1:auctionActionRequestResultOrder',
|
|
'auction:match-1:rehydrateLock',
|
|
'auction:match-1:auctionStateTransitionLock',
|
|
'match-1',
|
|
'life-1',
|
|
String(7 * 24 * 60 * 60),
|
|
]);
|
|
});
|
|
|
|
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,
|
|
afterAuctionMongoProjection: {
|
|
matchSideEffectRevision: 1,
|
|
matchSideEffectStatus: 'pending',
|
|
phase: 'waiting',
|
|
revision: 10,
|
|
},
|
|
auctionActionType: 'reset',
|
|
historyMutation: { resetMode: 'clear-history', type: 'reset' },
|
|
matchSideEffectMutation: { type: 'resetMatchAfterAuctionReset' },
|
|
revision: 10,
|
|
} as const;
|
|
const match = {
|
|
gameSchedule: [{ gameNumber: 1 }],
|
|
participants: [{ excluded: true }, { excluded: false }],
|
|
result: { games: [] },
|
|
resultPollState: { state: 'polling' },
|
|
save: jest.fn().mockResolvedValue(undefined),
|
|
status: 'team-built',
|
|
teamBuiltAt: new Date(1780000000000),
|
|
teams: [{ teamIndex: 1 }],
|
|
tournamentCodeIds: ['code-1'],
|
|
};
|
|
const historyDeleteMany = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ deletedCount: 3 }),
|
|
});
|
|
const historyUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ upsertedCount: 1 }),
|
|
});
|
|
const auctionUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ modifiedCount: 1 }),
|
|
});
|
|
const projectionExec = jest.fn().mockResolvedValue({
|
|
auctionLifecycleId: 'life-1',
|
|
revision: 9,
|
|
});
|
|
const auctionFindById = jest.fn().mockReturnValue({
|
|
select: jest.fn().mockReturnValue({
|
|
session: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({ exec: projectionExec }),
|
|
}),
|
|
}),
|
|
});
|
|
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: 'completed',
|
|
}),
|
|
});
|
|
const sideEffectUpdateOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({ matchedCount: 1, modifiedCount: 1 }),
|
|
});
|
|
const releaseFromMatch = jest.fn().mockResolvedValue(undefined);
|
|
const cancelPolling = jest.fn().mockResolvedValue(undefined);
|
|
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 {
|
|
deleteMany: historyDeleteMany,
|
|
updateOne: historyUpdateOne,
|
|
};
|
|
}
|
|
if (name === 'auction') {
|
|
return {
|
|
findById: auctionFindById,
|
|
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(),
|
|
} as never,
|
|
{ releaseFromMatch } as never,
|
|
{ cancelPolling } as never,
|
|
{ mirrorMatchSideEffectStatus } as never,
|
|
{ broadcastMatchSideEffect } as never,
|
|
);
|
|
|
|
await scheduler.applyDurabilityPayload(resetPayload);
|
|
|
|
expect(releaseFromMatch).toHaveBeenCalledWith(match);
|
|
expect(cancelPolling).toHaveBeenCalledWith(match);
|
|
expect(match).toMatchObject({
|
|
gameSchedule: [],
|
|
result: undefined,
|
|
resultPollState: undefined,
|
|
status: 'recruiting',
|
|
teamBuiltAt: undefined,
|
|
teams: [],
|
|
tournamentCodeIds: [],
|
|
});
|
|
expect(match.participants).toEqual([
|
|
{ excluded: false },
|
|
{ excluded: false },
|
|
]);
|
|
expect(match.save).toHaveBeenCalledTimes(1);
|
|
expect(sideEffectUpdateOne).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
auctionId: 'auction-1',
|
|
auctionStateTransitionId: 'transition-1',
|
|
sideEffectType: 'resetMatchAfterAuctionReset',
|
|
}),
|
|
expect.objectContaining({ $setOnInsert: expect.any(Object) }),
|
|
{ session: null, upsert: true },
|
|
);
|
|
expect(mirrorMatchSideEffectStatus).toHaveBeenLastCalledWith({
|
|
auctionLifecycleId: 'life-1',
|
|
matchId: 'match-1',
|
|
matchSideEffectRevision: 3,
|
|
matchSideEffectStatus: 'completed',
|
|
});
|
|
expect(broadcastMatchSideEffect).toHaveBeenLastCalledWith('match-1', {
|
|
auctionLifecycleId: 'life-1',
|
|
currentServerTimestampMS: 3000,
|
|
matchSideEffectRevision: 3,
|
|
matchSideEffectStatus: 'completed',
|
|
});
|
|
});
|
|
});
|