fix: allow public tournament code replacement
This commit is contained in:
1 parent
303886ed53
commit
b9881fcb6f
7 files changed
+117
-27
No files matched your search
@@ -10,6 +10,7 @@ type TournamentCodeServiceInternals = TournamentCodeService & {
|
||||
): { teamAIndex: number; teamBIndex: number };
|
||||
ensureGameSchedule(match: MatchDocument): void;
|
||||
replaceForMatchGame(params: {
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
gameNumber: number;
|
||||
}): Promise<unknown>;
|
||||
@@ -343,6 +344,7 @@ describe('TournamentCodeService code replacement', () => {
|
||||
replacementCode?: Record<string, unknown> | null;
|
||||
match?: MatchDocument;
|
||||
}) {
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const oldCodeId = new mongoose.Types.ObjectId();
|
||||
const newCodeId = new mongoose.Types.ObjectId();
|
||||
@@ -350,6 +352,7 @@ describe('TournamentCodeService code replacement', () => {
|
||||
params?.match ??
|
||||
createMatch({
|
||||
_id: matchId,
|
||||
communityId,
|
||||
status: 'in-progress',
|
||||
gameCount: 1,
|
||||
participants: Array.from({ length: 10 }, () => ({})),
|
||||
@@ -378,7 +381,7 @@ describe('TournamentCodeService code replacement', () => {
|
||||
});
|
||||
|
||||
const MatchModel = {
|
||||
findById: jest.fn().mockReturnValue(queryResult(match)),
|
||||
findOne: jest.fn().mockReturnValue(queryResult(match)),
|
||||
};
|
||||
const TournamentCodeModel = {
|
||||
findOne: jest.fn().mockReturnValue(queryResult(currentCode)),
|
||||
@@ -399,6 +402,7 @@ describe('TournamentCodeService code replacement', () => {
|
||||
|
||||
return {
|
||||
match,
|
||||
communityId,
|
||||
matchId,
|
||||
currentCode,
|
||||
replacementCode,
|
||||
@@ -409,10 +413,19 @@ describe('TournamentCodeService code replacement', () => {
|
||||
}
|
||||
|
||||
it('replaces a reserved code on the same game slot', async () => {
|
||||
const { match, matchId, currentCode, replacementCode, TournamentCodeModel, service } =
|
||||
createReplacementFixture();
|
||||
const {
|
||||
match,
|
||||
communityId,
|
||||
matchId,
|
||||
currentCode,
|
||||
replacementCode,
|
||||
MatchModel,
|
||||
TournamentCodeModel,
|
||||
service,
|
||||
} = createReplacementFixture();
|
||||
|
||||
const slot = await service.replaceForMatchGame({
|
||||
communityId,
|
||||
matchId,
|
||||
gameNumber: 1,
|
||||
});
|
||||
@@ -426,6 +439,10 @@ describe('TournamentCodeService code replacement', () => {
|
||||
state: 'reserved',
|
||||
}),
|
||||
);
|
||||
expect(MatchModel.findOne).toHaveBeenCalledWith({
|
||||
_id: matchId,
|
||||
communityId,
|
||||
});
|
||||
expect(TournamentCodeModel.findOneAndUpdate).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
state: 'available',
|
||||
@@ -454,9 +471,11 @@ describe('TournamentCodeService code replacement', () => {
|
||||
|
||||
it('rejects replacement after the game result is collected', async () => {
|
||||
const matchId = new mongoose.Types.ObjectId();
|
||||
const communityId = new mongoose.Types.ObjectId();
|
||||
const fixture = createReplacementFixture({
|
||||
match: createMatch({
|
||||
_id: matchId,
|
||||
communityId,
|
||||
status: 'in-progress',
|
||||
gameCount: 1,
|
||||
participants: Array.from({ length: 10 }, () => ({})),
|
||||
@@ -471,19 +490,24 @@ describe('TournamentCodeService code replacement', () => {
|
||||
});
|
||||
|
||||
await expect(
|
||||
fixture.service.replaceForMatchGame({ matchId, gameNumber: 1 }),
|
||||
fixture.service.replaceForMatchGame({
|
||||
communityId,
|
||||
matchId,
|
||||
gameNumber: 1,
|
||||
}),
|
||||
).rejects.toThrow('already has a collected result');
|
||||
expect(fixture.TournamentCodeModel.findOne).not.toHaveBeenCalled();
|
||||
expect(fixture.TournamentCodeModel.findOneAndUpdate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('leaves the current code untouched when no replacement code is available', async () => {
|
||||
const { match, matchId, currentCode, service } = createReplacementFixture({
|
||||
replacementCode: null,
|
||||
});
|
||||
const { match, communityId, matchId, currentCode, service } =
|
||||
createReplacementFixture({
|
||||
replacementCode: null,
|
||||
});
|
||||
|
||||
await expect(
|
||||
service.replaceForMatchGame({ matchId, gameNumber: 1 }),
|
||||
service.replaceForMatchGame({ communityId, matchId, gameNumber: 1 }),
|
||||
).rejects.toThrow('No available tournament code');
|
||||
expect(currentCode?.save).not.toHaveBeenCalled();
|
||||
expect(match.save).not.toHaveBeenCalled();
|
||||
|
||||
Reference in new issue
Block a user