This commit is contained in:
dahyeon committed 2026-04-26 04:41:07 -07:00
1 parent 8369fbead7
commit ec2315cc3d
2 files changed
+237

No files matched your search

@@ -0,0 +1,186 @@
import mongoose from 'mongoose';
import { AuctionService } from './auction.service';
describe('AuctionService', () => {
function createService(models: Record<string, unknown>) {
const connectionService = {
getModel: jest.fn((name: string) => models[name]),
createSession: jest.fn(),
};
const enrichmentService = {
enrichRiotAccount: jest.fn(),
};
return {
service: new AuctionService(
connectionService as never,
enrichmentService as never,
),
connectionService,
};
}
it('syncs completed auction teams into the match document', async () => {
const matchId = new mongoose.Types.ObjectId();
const blueCaptainId = new mongoose.Types.ObjectId();
const redCaptainId = new mongoose.Types.ObjectId();
const blueMemberId = new mongoose.Types.ObjectId();
const redMemberId = new mongoose.Types.ObjectId();
const auction = {
matchId,
phase: 'sold',
currentIndex: 1,
order: [{ userId: blueMemberId }, { userId: redMemberId }],
teams: [
{
side: 'blue',
captainUserId: blueCaptainId,
points: 970,
members: [
{
userId: blueMemberId,
assignedPosition: 'ADC',
cost: 30,
},
],
},
{
side: 'red',
captainUserId: redCaptainId,
points: 940,
members: [
{
userId: redMemberId,
assignedPosition: 'JGL',
cost: 60,
},
],
},
],
save: jest.fn().mockResolvedValue(undefined),
};
const match = {
participants: [
{
userId: blueCaptainId,
preferredPositions: [{ position: 'TOP', priority: 1 }],
},
{
userId: redCaptainId,
preferredPositions: [{ position: 'MID', priority: 1 }],
},
{
userId: blueMemberId,
preferredPositions: [{ position: 'ADC', priority: 1 }],
},
{
userId: redMemberId,
preferredPositions: [{ position: 'JGL', priority: 1 }],
},
],
status: 'team-built',
teams: [],
save: jest.fn().mockResolvedValue(undefined),
};
const { service } = createService({
auction: {
findOne: jest.fn().mockResolvedValue(auction),
},
match: {
findById: jest.fn().mockResolvedValue(match),
},
});
await service.next(matchId);
expect(auction.phase).toBe('complete');
expect(auction.completedAt).toBeInstanceOf(Date);
expect(auction.save).toHaveBeenCalledTimes(1);
expect(match.save).toHaveBeenCalledTimes(1);
expect(match.teams).toEqual([
{
side: 'blue',
name: '블루팀',
captainUserId: blueCaptainId,
members: [
{
userId: blueCaptainId,
assignedPosition: 'TOP',
cost: 0,
},
{
userId: blueMemberId,
assignedPosition: 'ADC',
cost: 30,
},
],
},
{
side: 'red',
name: '레드팀',
captainUserId: redCaptainId,
members: [
{
userId: redCaptainId,
assignedPosition: 'MID',
cost: 0,
},
{
userId: redMemberId,
assignedPosition: 'JGL',
cost: 60,
},
],
},
]);
});
it('clears match teams when resetting an auction', async () => {
const matchId = new mongoose.Types.ObjectId();
const communityId = new mongoose.Types.ObjectId();
const session = {
startTransaction: jest.fn(),
commitTransaction: jest.fn().mockResolvedValue(undefined),
abortTransaction: jest.fn().mockResolvedValue(undefined),
endSession: jest.fn().mockResolvedValue(undefined),
};
const auction = {
deleteOne: jest.fn().mockResolvedValue(undefined),
};
const match = {
communityId,
status: 'team-built',
teams: [
{
side: 'blue',
name: '블루팀',
members: [],
},
],
save: jest.fn().mockResolvedValue(undefined),
};
const auctionFindOneQuery = {
session: jest.fn().mockResolvedValue(auction),
};
const matchFindByIdQuery = {
session: jest.fn().mockResolvedValue(match),
};
const { service, connectionService } = createService({
auction: {
findOne: jest.fn().mockReturnValue(auctionFindOneQuery),
},
match: {
findById: jest.fn().mockReturnValue(matchFindByIdQuery),
},
});
connectionService.createSession.mockResolvedValue(session);
await service.reset({ matchId, communityId });
expect(match.status).toBe('recruiting');
expect(match.teams).toEqual([]);
expect(match.save).toHaveBeenCalledWith({ session });
expect(session.commitTransaction).toHaveBeenCalledTimes(1);
});
});
@@ -9,6 +9,7 @@ import { ConnectionService } from '../global/database/connection.service';
import { import {
AuctionDocument, AuctionDocument,
AuctionPlayerSnapshot, AuctionPlayerSnapshot,
MatchTeamMember,
Position, Position,
TeamSide, TeamSide,
} from '../global/database/schema'; } from '../global/database/schema';
@@ -266,6 +267,9 @@ export class AuctionService {
} }
auction.currentDeadline = undefined; auction.currentDeadline = undefined;
await auction.save(); await auction.save();
if (auction.phase === 'complete') {
await this.syncMatchTeamsFromAuction(auction);
}
return auction; return auction;
} }
@@ -296,6 +300,7 @@ export class AuctionService {
await auction.deleteOne({ session }); await auction.deleteOne({ session });
if (match.status === 'team-built') { if (match.status === 'team-built') {
match.status = 'recruiting'; match.status = 'recruiting';
match.teams = [];
await match.save({ session }); await match.save({ session });
} }
await session.commitTransaction(); await session.commitTransaction();
@@ -321,6 +326,52 @@ export class AuctionService {
); );
return Math.max(5, remainingSeconds); return Math.max(5, remainingSeconds);
} }
private async syncMatchTeamsFromAuction(
auction: AuctionDocument,
): Promise<void> {
const MatchModel = await this.connectionService.getModel('match');
const match = await MatchModel.findById(auction.matchId);
if (!match) throw new NotFoundException('Match not found.');
const participantByUserId = new Map(
match.participants.map((participant) => [
participant.userId.toString(),
participant,
]),
);
match.teams = auction.teams.map((team) => {
const captain = participantByUserId.get(team.captainUserId.toString());
const members: MatchTeamMember[] = [
{
userId: team.captainUserId,
assignedPosition: captain?.preferredPositions[0]?.position ?? 'MID',
cost: 0,
},
...team.members
.filter((member) => !member.userId.equals(team.captainUserId))
.map((member) => ({
userId: member.userId,
assignedPosition: member.assignedPosition,
cost: member.cost,
})),
];
return {
side: team.side,
name: this.resolveTeamName(team.side),
captainUserId: team.captainUserId,
members,
};
});
match.status = 'team-built';
await match.save();
}
private resolveTeamName(side: TeamSide): string {
return side === 'blue' ? '블루팀' : '레드팀';
}
} }
async function runWithConcurrency<T>( async function runWithConcurrency<T>(