diff --git a/apps/api/src/modules/auction/auction.service.spec.ts b/apps/api/src/modules/auction/auction.service.spec.ts index b423e7cf..ff5cfc6f 100644 --- a/apps/api/src/modules/auction/auction.service.spec.ts +++ b/apps/api/src/modules/auction/auction.service.spec.ts @@ -66,6 +66,7 @@ describe('AuctionService', () => { } afterEach(() => { + jest.restoreAllMocks(); jest.useRealTimers(); }); @@ -158,6 +159,99 @@ describe('AuctionService', () => { expect(tournamentResultService.scheduleInitialPoll).not.toHaveBeenCalled(); }); + it('randomizes the non-captain player order when starting an auction', async () => { + const randomSpy = jest.spyOn(Math, 'random').mockReturnValue(0); + const matchId = new mongoose.Types.ObjectId(); + const communityId = new mongoose.Types.ObjectId(); + const userIds = Array.from( + { length: 10 }, + () => new mongoose.Types.ObjectId(), + ); + const participants = userIds.map((userId, index) => ({ + userId, + preferredPositions: [{ position: 'MID', priority: 1 }], + joinedAt: new Date(index), + excluded: false, + })); + const match = { + _id: matchId, + communityId, + participants, + status: 'recruiting', + teamBuiltAt: undefined, + save: jest.fn().mockResolvedValue(undefined), + }; + const users = userIds.slice(2).map((userId, index) => ({ + _id: userId, + riotAccounts: [ + { + puuid: `puuid-${index}`, + gameName: `Player${index}`, + tagLine: 'KR1', + profileIconId: index, + tiers: [], + championMasteries: [], + recentStatsSummary: undefined, + lastRefreshRequestedAt: new Date(), + isPrimary: true, + }, + ], + })); + const auctionCreate = jest.fn().mockImplementation(async (payload) => ({ + _id: new mongoose.Types.ObjectId(), + matchId, + toObject: jest.fn().mockReturnValue({ matchId, ...payload }), + })); + const { service } = createService({ + match: { + findById: jest.fn().mockResolvedValue(match), + }, + user: { + find: jest.fn().mockResolvedValue(users), + }, + auction: { + findOne: jest.fn().mockResolvedValue(null), + create: auctionCreate, + }, + community: { + findById: jest.fn().mockReturnValue({ + select: jest.fn().mockReturnValue({ + lean: jest.fn().mockReturnValue({ + exec: jest.fn().mockResolvedValue({ + riotPlatform: 'kr', + riotRegion: 'asia', + }), + }), + }), + }), + }, + }); + + await service.start({ + matchId, + communityId, + captains: [ + { teamIndex: 1, userId: userIds[0] }, + { teamIndex: 2, userId: userIds[1] }, + ], + initialPoints: 1000, + maxTimerSeconds: 30, + bidExtensionSeconds: 5, + topTierThreshold: 'NONE', + manualTopTierUserIds: [], + }); + + const signupOrder = userIds.slice(2).map((userId) => userId.toString()); + const auctionOrder = auctionCreate.mock.calls[0][0].order.map( + (player: { userId: mongoose.Types.ObjectId }) => + player.userId.toString(), + ); + + expect(auctionOrder).not.toEqual(signupOrder); + expect([...auctionOrder].sort()).toEqual([...signupOrder].sort()); + expect(randomSpy).toHaveBeenCalled(); + }); + it('rejects bids when the team already has 5 members including captain', async () => { const matchId = new mongoose.Types.ObjectId(); const auction = { diff --git a/apps/api/src/modules/auction/auction.service.ts b/apps/api/src/modules/auction/auction.service.ts index b4e587f1..b93d66b8 100644 --- a/apps/api/src/modules/auction/auction.service.ts +++ b/apps/api/src/modules/auction/auction.service.ts @@ -167,8 +167,9 @@ export class AuctionService { passCount: 0, }); } + const shuffledOrder = this.shuffleAuctionPlayerOrder(order); const prioritizedOrder = this.applyTopTierPriority( - order, + shuffledOrder, params.topTierThreshold, params.manualTopTierUserIds, ); @@ -588,6 +589,20 @@ export class AuctionService { return [...top, ...rest]; } + private shuffleAuctionPlayerOrder( + order: AuctionPlayerSnapshot[], + ): AuctionPlayerSnapshot[] { + const shuffled = [...order]; + for (let index = shuffled.length - 1; index > 0; index -= 1) { + const swapIndex = Math.floor(Math.random() * (index + 1)); + [shuffled[index], shuffled[swapIndex]] = [ + shuffled[swapIndex], + shuffled[index], + ]; + } + return shuffled; + } + private forceAssignRemainingPasses(auction: AuctionDocument): void { const forcedPlayers = auction.order.filter( (player) =>