This commit is contained in:
dahyeon committed 2026-05-02 11:51:01 -07:00
1 parent fedca82c81
commit 3607e3a99c
13 files changed
+1838 -793

No files matched your search

@@ -3,8 +3,27 @@ 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) => models[name]),
getModel: jest.fn((name: string) => allModels[name]),
createSession: jest.fn(),
};
const enrichmentService = {
@@ -16,6 +35,7 @@ describe('AuctionService', () => {
};
const tournamentResultService = {
scheduleInitialPoll: jest.fn().mockResolvedValue(null),
cancelPolling: jest.fn().mockResolvedValue(undefined),
};
return {
@@ -28,6 +48,7 @@ describe('AuctionService', () => {
connectionService,
tournamentCodeService,
tournamentResultService,
auctionHistoryModel,
};
}
@@ -96,6 +117,119 @@ describe('AuctionService', () => {
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();
@@ -260,4 +394,92 @@ describe('AuctionService', () => {
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);
});
});