486 lines
14 KiB
TypeScript
486 lines
14 KiB
TypeScript
import mongoose from 'mongoose';
|
|
import { AuctionService } from './auction.service';
|
|
|
|
describe('AuctionService', () => {
|
|
function createService(models: Record<string, unknown>) {
|
|
const auctionHistoryModel = {
|
|
findOne: jest.fn().mockReturnValue({
|
|
sort: jest.fn().mockReturnValue({
|
|
select: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue(null),
|
|
}),
|
|
}),
|
|
}),
|
|
}),
|
|
create: jest.fn().mockResolvedValue(undefined),
|
|
deleteMany: jest.fn().mockReturnValue({
|
|
session: jest.fn().mockResolvedValue(undefined),
|
|
}),
|
|
};
|
|
const allModels = {
|
|
auctionHistory: auctionHistoryModel,
|
|
...models,
|
|
};
|
|
const connectionService = {
|
|
getModel: jest.fn((name: string) => allModels[name]),
|
|
createSession: jest.fn(),
|
|
};
|
|
const enrichmentService = {
|
|
enrichRiotAccount: jest.fn(),
|
|
};
|
|
const tournamentCodeService = {
|
|
reserveForMatch: jest.fn().mockResolvedValue(undefined),
|
|
releaseFromMatch: jest.fn().mockResolvedValue(undefined),
|
|
};
|
|
const tournamentResultService = {
|
|
scheduleInitialPoll: jest.fn().mockResolvedValue(null),
|
|
cancelPolling: jest.fn().mockResolvedValue(undefined),
|
|
};
|
|
|
|
return {
|
|
service: new AuctionService(
|
|
connectionService as never,
|
|
enrichmentService as never,
|
|
tournamentCodeService as never,
|
|
tournamentResultService as never,
|
|
),
|
|
connectionService,
|
|
tournamentCodeService,
|
|
tournamentResultService,
|
|
auctionHistoryModel,
|
|
};
|
|
}
|
|
|
|
function createFullTeam(teamIndex: number) {
|
|
return {
|
|
teamIndex,
|
|
captainUserId: new mongoose.Types.ObjectId(),
|
|
points: 100,
|
|
members: Array.from({ length: 4 }, () => ({
|
|
userId: new mongoose.Types.ObjectId(),
|
|
assignedPosition: 'MID',
|
|
cost: 10,
|
|
})),
|
|
};
|
|
}
|
|
|
|
it('rejects bids when the team already has 5 members including captain', async () => {
|
|
const matchId = new mongoose.Types.ObjectId();
|
|
const auction = {
|
|
phase: 'bidding',
|
|
teams: [createFullTeam(1), createFullTeam(2)],
|
|
currentBid: 10,
|
|
save: jest.fn().mockResolvedValue(undefined),
|
|
};
|
|
const { service } = createService({
|
|
auction: {
|
|
findOne: jest.fn().mockResolvedValue(auction),
|
|
},
|
|
});
|
|
|
|
await expect(
|
|
service.placeBid({
|
|
matchId,
|
|
teamIndex: 1,
|
|
amount: 20,
|
|
}),
|
|
).rejects.toThrow('Team already has maximum members.');
|
|
expect(auction.save).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects sold confirmation when the winning team is already full', async () => {
|
|
const matchId = new mongoose.Types.ObjectId();
|
|
const auction = {
|
|
phase: 'bidding',
|
|
currentBidderTeamIndex: 1,
|
|
currentBid: 20,
|
|
currentIndex: 0,
|
|
order: [
|
|
{
|
|
userId: new mongoose.Types.ObjectId(),
|
|
preferredPositions: [{ position: 'ADC', priority: 1 }],
|
|
},
|
|
],
|
|
teams: [createFullTeam(1), createFullTeam(2)],
|
|
save: jest.fn().mockResolvedValue(undefined),
|
|
};
|
|
const { service } = createService({
|
|
auction: {
|
|
findOne: jest.fn().mockResolvedValue(auction),
|
|
},
|
|
});
|
|
|
|
await expect(service.markSold({ matchId })).rejects.toThrow(
|
|
'Team already has maximum members.',
|
|
);
|
|
expect(auction.save).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('marks a zero-bid player as passed and queues them for round two', async () => {
|
|
const matchId = new mongoose.Types.ObjectId();
|
|
const playerId = new mongoose.Types.ObjectId();
|
|
const auction = {
|
|
_id: new mongoose.Types.ObjectId(),
|
|
matchId,
|
|
phase: 'bidding',
|
|
currentBid: 0,
|
|
currentIndex: 0,
|
|
auctionRound: 1,
|
|
order: [
|
|
{
|
|
userId: playerId,
|
|
preferredPositions: [{ position: 'MID', priority: 1 }],
|
|
passCount: 0,
|
|
},
|
|
],
|
|
passedQueue: [],
|
|
save: jest.fn().mockResolvedValue(undefined),
|
|
toObject: jest.fn().mockReturnValue({ _id: 'auction', matchId }),
|
|
};
|
|
const { service } = createService({
|
|
auction: {
|
|
findOne: jest.fn().mockResolvedValue(auction),
|
|
},
|
|
});
|
|
|
|
await service.markPassed(matchId);
|
|
|
|
expect(auction.phase).toBe('passed');
|
|
expect(auction.order[0].passCount).toBe(1);
|
|
expect(auction.passedQueue).toHaveLength(1);
|
|
expect(auction.save).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('force-assigns second-pass players to the open team with most points', async () => {
|
|
const matchId = new mongoose.Types.ObjectId();
|
|
const captainOneId = new mongoose.Types.ObjectId();
|
|
const captainTwoId = new mongoose.Types.ObjectId();
|
|
const passedPlayerId = new mongoose.Types.ObjectId();
|
|
const auction = {
|
|
_id: new mongoose.Types.ObjectId(),
|
|
matchId,
|
|
phase: 'passed',
|
|
currentIndex: 0,
|
|
auctionRound: 2,
|
|
order: [
|
|
{
|
|
userId: passedPlayerId,
|
|
preferredPositions: [{ position: 'SUP', priority: 1 }],
|
|
passCount: 2,
|
|
},
|
|
],
|
|
passedQueue: [],
|
|
teams: [
|
|
{
|
|
teamIndex: 1,
|
|
captainUserId: captainOneId,
|
|
points: 10,
|
|
members: [],
|
|
},
|
|
{
|
|
teamIndex: 2,
|
|
captainUserId: captainTwoId,
|
|
points: 50,
|
|
members: [],
|
|
},
|
|
],
|
|
save: jest.fn().mockResolvedValue(undefined),
|
|
toObject: jest.fn().mockReturnValue({ _id: 'auction', matchId }),
|
|
};
|
|
const match = {
|
|
participants: [
|
|
{
|
|
userId: captainOneId,
|
|
preferredPositions: [{ position: 'TOP', priority: 1 }],
|
|
},
|
|
{
|
|
userId: captainTwoId,
|
|
preferredPositions: [{ position: 'JGL', priority: 1 }],
|
|
},
|
|
{
|
|
userId: passedPlayerId,
|
|
preferredPositions: [{ position: 'SUP', priority: 1 }],
|
|
},
|
|
],
|
|
status: 'team-built',
|
|
teams: [],
|
|
teamBuiltAt: undefined,
|
|
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.teams[1].members).toEqual([
|
|
{
|
|
userId: passedPlayerId,
|
|
assignedPosition: 'SUP',
|
|
cost: 1,
|
|
},
|
|
]);
|
|
expect(auction.teams[1].points).toBe(49);
|
|
});
|
|
|
|
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: [
|
|
{
|
|
teamIndex: 1,
|
|
captainUserId: blueCaptainId,
|
|
points: 970,
|
|
members: [
|
|
{
|
|
userId: blueMemberId,
|
|
assignedPosition: 'ADC',
|
|
cost: 30,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
teamIndex: 2,
|
|
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([
|
|
{
|
|
teamIndex: 1,
|
|
name: '1팀',
|
|
captainUserId: blueCaptainId,
|
|
members: [
|
|
{
|
|
userId: blueCaptainId,
|
|
assignedPosition: 'TOP',
|
|
cost: 0,
|
|
},
|
|
{
|
|
userId: blueMemberId,
|
|
assignedPosition: 'ADC',
|
|
cost: 30,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
teamIndex: 2,
|
|
name: '2팀',
|
|
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: [
|
|
{
|
|
teamIndex: 1,
|
|
name: '1팀',
|
|
members: [],
|
|
},
|
|
],
|
|
participants: [],
|
|
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);
|
|
});
|
|
|
|
it('undoes to a history snapshot and releases resources when leaving complete', async () => {
|
|
const matchId = new mongoose.Types.ObjectId();
|
|
const communityId = new mongoose.Types.ObjectId();
|
|
const auctionId = 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 = {
|
|
_id: auctionId,
|
|
matchId,
|
|
completedAt: new Date(),
|
|
};
|
|
const snapshot = {
|
|
_id: auctionId.toString(),
|
|
matchId: matchId.toString(),
|
|
phase: 'bidding',
|
|
order: [],
|
|
teams: [],
|
|
};
|
|
const restored = { _id: auctionId, phase: 'bidding' };
|
|
const match = {
|
|
communityId,
|
|
status: 'finished',
|
|
result: { games: [] },
|
|
save: jest.fn().mockResolvedValue(undefined),
|
|
};
|
|
const auctionFindOneQuery = {
|
|
session: jest.fn().mockResolvedValue(auction),
|
|
};
|
|
const matchFindByIdQuery = {
|
|
session: jest.fn().mockResolvedValue(match),
|
|
};
|
|
const historyFindOneQuery = {
|
|
session: jest.fn().mockReturnValue({
|
|
lean: jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue({
|
|
auctionId,
|
|
matchId,
|
|
sequence: 3,
|
|
snapshotBefore: snapshot,
|
|
}),
|
|
}),
|
|
}),
|
|
};
|
|
const historyDeleteManyQuery = {
|
|
session: jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue(undefined),
|
|
}),
|
|
};
|
|
const auctionReplaceOne = jest.fn().mockReturnValue({
|
|
exec: jest.fn().mockResolvedValue(undefined),
|
|
});
|
|
const { service, connectionService, tournamentCodeService, tournamentResultService } =
|
|
createService({
|
|
auction: {
|
|
findOne: jest.fn().mockReturnValue(auctionFindOneQuery),
|
|
replaceOne: auctionReplaceOne,
|
|
findById: jest.fn().mockResolvedValue(restored),
|
|
},
|
|
auctionHistory: {
|
|
findOne: jest.fn().mockReturnValue(historyFindOneQuery),
|
|
deleteMany: jest.fn().mockReturnValue(historyDeleteManyQuery),
|
|
},
|
|
match: {
|
|
findById: jest.fn().mockReturnValue(matchFindByIdQuery),
|
|
},
|
|
});
|
|
connectionService.createSession.mockResolvedValue(session);
|
|
|
|
await expect(
|
|
service.undoTo({ matchId, communityId, sequence: 3 }),
|
|
).resolves.toBe(restored);
|
|
|
|
expect(tournamentCodeService.releaseFromMatch).toHaveBeenCalledWith(match);
|
|
expect(tournamentResultService.cancelPolling).toHaveBeenCalledWith(match);
|
|
expect(match.status).toBe('team-built');
|
|
expect(match.result).toBeUndefined();
|
|
expect(auctionReplaceOne).toHaveBeenCalledWith(
|
|
{ _id: auctionId },
|
|
snapshot,
|
|
{ session },
|
|
);
|
|
expect(session.commitTransaction).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|