import { NotFoundException } from '@nestjs/common'; import mongoose from 'mongoose'; import { AuctionService } from './auction.service'; const matchId = new mongoose.Types.ObjectId(); const communityId = new mongoose.Types.ObjectId(); function sessionQuery(value: T) { return { session: async () => value }; } function fixture(options: { auction: Record | null; match: Record; failRedisCleanup?: boolean; }) { const session = { startTransaction: jest.fn(), commitTransaction: jest.fn().mockResolvedValue(undefined), abortTransaction: jest.fn().mockResolvedValue(undefined), endSession: jest.fn().mockResolvedValue(undefined), }; const deleteMany = jest.fn(() => sessionQuery(undefined)); const models: Record = { auction: { findOne: () => sessionQuery(options.auction) }, auctionHistory: { deleteMany }, auctionSideEffectApplication: { deleteMany }, match: { findById: () => sessionQuery(options.match) }, }; const redisCommands: string[][] = []; const sendCommand = jest.fn(async (command: string[]) => { redisCommands.push(command); if (command[0] === 'EVAL' && options.failRedisCleanup) throw new Error('redis unavailable'); return command[0] === 'SCAN' ? ['0', []] : 1; }); const service = new AuctionService( { getModel: async (name: string) => models[name], createSession: async () => session, } as never, {} as never, { releaseFromMatch: jest.fn().mockResolvedValue(undefined) } as never, { cancelPolling: jest.fn().mockResolvedValue(undefined) } as never, { acquireTokenLock: async () => true, releaseTokenLock: async () => true, raw: () => ({ sendCommand }), } as never, {} as never, {} as never, ); return { service, session, redisCommands }; } function liveMatch(): Record { return { communityId, status: 'team-built', teams: [], tournamentCodeIds: [], gameSchedule: [], participants: [], save: jest.fn().mockResolvedValue(undefined), }; } function redisAuction() { return { _id: new mongoose.Types.ObjectId(), auctionEngineVersion: 'redis-v1', auctionLifecycleId: 'life', deleteOne: jest.fn().mockResolvedValue(undefined), }; } it('records the cancelled lifecycle and still succeeds when Redis cleanup fails', async () => { const match = liveMatch(); const f = fixture({ auction: redisAuction(), match, failRedisCleanup: true, }); await expect( f.service.reset({ matchId, communityId, clientObservedAuctionLifecycleId: 'life', }), ).resolves.toBeUndefined(); expect(f.session.commitTransaction).toHaveBeenCalledTimes(1); expect(match.lastCancelledAuctionLifecycleId).toBe('life'); expect(match.status).toBe('recruiting'); }); it('answers a retransmitted cancellation and durably schedules Redis cleanup', async () => { const match = { ...liveMatch(), lastCancelledAuctionLifecycleId: 'life' }; const f = fixture({ auction: null, match }); await expect( f.service.reset({ matchId, communityId, clientObservedAuctionLifecycleId: 'life', }), ).resolves.toBeUndefined(); expect(match).toMatchObject({ auctionCancellationCleanupPending: true, auctionCancellationCleanupRetryAt: expect.any(Date), }); expect(f.redisCommands).toEqual([]); }); it('preserves a committed cancellation even if its lock renewal fails before returning', async () => { jest.useFakeTimers(); try { const match = liveMatch(); const f = fixture({ auction: redisAuction(), match }); f.session.commitTransaction.mockImplementation(async () => { await jest.advanceTimersByTimeAsync(10000); }); await expect( f.service.reset({ matchId, communityId, clientObservedAuctionLifecycleId: 'life', }), ).resolves.toBeUndefined(); expect(match.auctionCancellationCleanupPending).toBe(true); expect(f.session.commitTransaction).toHaveBeenCalledTimes(1); } finally { jest.useRealTimers(); } }); it('keeps 404 for a missing auction that was not cancelled with this lifecycle', async () => { for (const [observed, recorded] of [ ['other', 'life'], [undefined, 'life'], ['life', undefined], ] as const) { const f = fixture({ auction: null, match: { ...liveMatch(), lastCancelledAuctionLifecycleId: recorded }, }); await expect( f.service.reset({ matchId, communityId, clientObservedAuctionLifecycleId: observed, }), ).rejects.toBeInstanceOf(NotFoundException); expect(f.session.abortTransaction).toHaveBeenCalled(); } });