feat(api): add auction service
This commit is contained in:
1 parent
b4be06ae83
commit
b12f44424e
5 files changed
+1877
No files matched your search
@@ -0,0 +1,631 @@
|
||||
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,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it('starts an auction without reserving tournament codes before teams are complete', async () => {
|
||||
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 auction = {
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
matchId,
|
||||
toObject: jest.fn().mockReturnValue({ matchId }),
|
||||
};
|
||||
const { service, tournamentCodeService, tournamentResultService } =
|
||||
createService({
|
||||
match: {
|
||||
findById: jest.fn().mockResolvedValue(match),
|
||||
},
|
||||
user: {
|
||||
find: jest.fn().mockResolvedValue(users),
|
||||
},
|
||||
auction: {
|
||||
findOne: jest.fn().mockResolvedValue(null),
|
||||
create: jest.fn().mockResolvedValue(auction),
|
||||
},
|
||||
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: [],
|
||||
});
|
||||
|
||||
expect(match.status).toBe('team-built');
|
||||
expect(match.teamBuiltAt).toBeInstanceOf(Date);
|
||||
expect(match.save).toHaveBeenCalledTimes(1);
|
||||
expect(tournamentCodeService.reserveForMatch).not.toHaveBeenCalled();
|
||||
expect(tournamentResultService.scheduleInitialPoll).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
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('extends a short bid deadline using the configured bid extension seconds', async () => {
|
||||
const now = new Date('2026-05-02T00:00:00.000Z');
|
||||
jest.useFakeTimers().setSystemTime(now);
|
||||
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const captainUserId = new mongoose.Types.ObjectId();
|
||||
const auction = {
|
||||
_id: new mongoose.Types.ObjectId(),
|
||||
matchId,
|
||||
phase: 'bidding',
|
||||
teams: [
|
||||
{
|
||||
teamIndex: 1,
|
||||
captainUserId,
|
||||
points: 1000,
|
||||
members: [],
|
||||
},
|
||||
],
|
||||
currentBid: 10,
|
||||
currentDeadline: new Date(now.getTime() + 2000),
|
||||
maxTimerSeconds: 30,
|
||||
bidExtensionSeconds: 12,
|
||||
bids: [],
|
||||
save: jest.fn().mockResolvedValue(undefined),
|
||||
toObject: jest.fn().mockReturnValue({ _id: 'auction', matchId }),
|
||||
};
|
||||
const { service } = createService({
|
||||
auction: {
|
||||
findOne: jest.fn().mockResolvedValue(auction),
|
||||
},
|
||||
});
|
||||
|
||||
await service.placeBid({
|
||||
matchId,
|
||||
teamIndex: 1,
|
||||
amount: 20,
|
||||
});
|
||||
|
||||
expect(auction.currentDeadline).toEqual(
|
||||
new Date(now.getTime() + 12000),
|
||||
);
|
||||
expect(auction.save).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
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, tournamentCodeService, tournamentResultService } =
|
||||
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,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
expect(tournamentCodeService.reserveForMatch).toHaveBeenCalledWith(match);
|
||||
expect(tournamentResultService.scheduleInitialPoll).toHaveBeenCalledWith(
|
||||
match,
|
||||
);
|
||||
});
|
||||
|
||||
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: [],
|
||||
},
|
||||
],
|
||||
tournamentCodeIds: [new mongoose.Types.ObjectId()],
|
||||
gameSchedule: [{ gameNumber: 1, teamAIndex: 1, teamBIndex: 2 }],
|
||||
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.tournamentCodeIds).toEqual([]);
|
||||
expect(match.gameSchedule).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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user