feat: auto provision tournament codes

This commit is contained in:
dahyeon committed 2026-06-20 02:29:30 -07:00
1 parent 65bc214fb2
commit bac29ac933
13 files changed
+1164 -117

No files matched your search

@@ -1,4 +1,7 @@
import type { MatchDocument } from '../global/database/schema';
import {
TournamentCodeSchema,
type MatchDocument,
} from '../global/database/schema';
import mongoose from 'mongoose';
import { TournamentCodeService } from './tournament-code.service';
@@ -14,6 +17,7 @@ type TournamentCodeServiceInternals = TournamentCodeService & {
matchId: mongoose.Types.ObjectId;
gameNumber: number;
}): Promise<unknown>;
attachPendingMatches(communityId?: mongoose.Types.ObjectId): Promise<number>;
};
function createService() {
@@ -318,6 +322,89 @@ describe('TournamentCodeService round-robin scheduling', () => {
});
});
describe('TournamentCodeService bulk upload', () => {
it('preserves tournament code casing at the schema layer', async () => {
const connection = mongoose.createConnection();
try {
const TournamentCodeModel = connection.model(
'TournamentCodeCasingSpec',
TournamentCodeSchema,
);
const code = new TournamentCodeModel({
code: ' AbC-12345 ',
expiresAt: new Date(Date.now() + 60 * 60 * 1000),
uploadedBy: new mongoose.Types.ObjectId(),
});
expect(code.code).toBe('AbC-12345');
} finally {
await connection.close();
}
});
it('stores tournament codes with the exact casing from input', async () => {
const insertMany = jest.fn().mockResolvedValue(undefined);
const TournamentCodeModel = {
find: jest.fn().mockReturnValue({
select: jest.fn().mockReturnValue({
lean: jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue([]),
}),
}),
}),
insertMany,
};
const connectionService = {
getModel: jest.fn().mockResolvedValue(TournamentCodeModel),
};
const service = new TournamentCodeService(
connectionService as never,
) as unknown as TournamentCodeServiceInternals;
service.attachPendingMatches = jest.fn().mockResolvedValue(0);
await service.bulkUpload({
communityId: new mongoose.Types.ObjectId(),
uploadedBy: new mongoose.Types.ObjectId(),
codes: [
{
code: ' AbC-12345 ',
expiresAt: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
},
],
});
expect(insertMany).toHaveBeenCalledWith(
[
expect.objectContaining({
code: 'AbC-12345',
}),
],
{ ordered: false },
);
});
});
describe('TournamentCodeService pool stats', () => {
it('counts only available codes that are usable beyond the expiry margin', async () => {
const countDocuments = jest.fn().mockReturnValue({
exec: jest.fn().mockResolvedValue(12),
});
const connectionService = {
getModel: jest.fn().mockResolvedValue({ countDocuments }),
};
const service = new TournamentCodeService(connectionService as never);
await expect(service.countUsableAvailable()).resolves.toBe(12);
expect(countDocuments).toHaveBeenCalledWith({
state: 'available',
expiresAt: {
$gt: expect.any(Date),
},
});
});
});
describe('TournamentCodeService code replacement', () => {
function queryResult<T>(value: T) {
return { exec: jest.fn().mockResolvedValue(value) };