refactor(tests): update test files to use new player field names

This commit is contained in:
2026-04-03 21:04:06 -07:00
parent e0c986f594
commit c222e55a52
5 changed files with 106 additions and 61 deletions
+61 -23
View File
@@ -12,42 +12,71 @@ import {
expectedMatchups,
} from '@/lib/schedule-generator';
// Helper to create team pairings from simple IDs
function createTeams(count: number): { player1Id: number; player2Id: number }[] {
const teams = [];
for (let i = 0; i < count; i++) {
// Create teams with unique player IDs
// Team 1: players 1, 2
// Team 2: players 3, 4
// etc.
teams.push({
player1Id: i * 2 + 1,
player2Id: i * 2 + 2,
});
}
return teams;
}
describe('Round-Robin Schedule Generator', () => {
describe('generateRoundRobin', () => {
test('should return empty array for fewer than 2 teams', () => {
expect(generateRoundRobin([])).toEqual([]);
expect(generateRoundRobin([1])).toEqual([]);
expect(generateRoundRobin([{ player1Id: 1, player2Id: 2 }])).toEqual([]);
});
test('should generate correct schedule for 2 teams', () => {
const rounds = generateRoundRobin([1, 2]);
const rounds = generateRoundRobin([
{ player1Id: 1, player2Id: 2 },
{ player1Id: 3, player2Id: 4 },
]);
expect(rounds).toHaveLength(1);
expect(rounds[0].roundNumber).toBe(1);
expect(rounds[0].matchups).toHaveLength(1);
expect(rounds[0].matchups[0]).toEqual({ team1Id: 1, team2Id: 2 });
expect(rounds[0].matchups[0]).toEqual({
player1P1Id: 1,
player1P2Id: 2,
player2P1Id: 3,
player2P2Id: 4,
});
});
test('should generate N-1 rounds for N even teams', () => {
const teams = [1, 2, 3, 4];
const teams = createTeams(4);
const rounds = generateRoundRobin(teams);
expect(rounds).toHaveLength(3);
});
test('should generate N rounds for N odd teams (with bye)', () => {
const teams = [1, 2, 3];
const teams = createTeams(3);
const rounds = generateRoundRobin(teams);
expect(rounds).toHaveLength(3);
});
test('each team plays every other team exactly once (even)', () => {
const teams = [1, 2, 3, 4];
const teams = createTeams(4);
const rounds = generateRoundRobin(teams);
// Collect all pairings as sorted tuples
const pairings = new Set<string>();
for (const round of rounds) {
for (const matchup of round.matchups) {
const key = [matchup.team1Id, matchup.team2Id].sort().join('-');
const key = [
matchup.player1P1Id,
matchup.player1P2Id,
matchup.player2P1Id,
matchup.player2P2Id,
].sort().join('-');
pairings.add(key);
}
}
@@ -57,13 +86,18 @@ describe('Round-Robin Schedule Generator', () => {
});
test('each team plays every other team exactly once (odd)', () => {
const teams = [1, 2, 3, 4, 5];
const teams = createTeams(5);
const rounds = generateRoundRobin(teams);
const pairings = new Set<string>();
for (const round of rounds) {
for (const matchup of round.matchups) {
const key = [matchup.team1Id, matchup.team2Id].sort().join('-');
const key = [
matchup.player1P1Id,
matchup.player1P2Id,
matchup.player2P1Id,
matchup.player2P2Id,
].sort().join('-');
pairings.add(key);
}
}
@@ -73,14 +107,14 @@ describe('Round-Robin Schedule Generator', () => {
});
test('each team plays exactly once per round (even teams)', () => {
const teams = [1, 2, 3, 4, 5, 6];
const teams = createTeams(6);
const rounds = generateRoundRobin(teams);
for (const round of rounds) {
const teamsInRound = new Set<number>();
const teamsInRound = new Set<string>();
for (const matchup of round.matchups) {
teamsInRound.add(matchup.team1Id);
teamsInRound.add(matchup.team2Id);
teamsInRound.add([matchup.player1P1Id, matchup.player1P2Id].sort().join('-'));
teamsInRound.add([matchup.player2P1Id, matchup.player2P2Id].sort().join('-'));
}
// Each team appears exactly once
expect(teamsInRound.size).toBe(6);
@@ -88,14 +122,14 @@ describe('Round-Robin Schedule Generator', () => {
});
test('each team plays at most once per round (odd teams)', () => {
const teams = [1, 2, 3, 4, 5];
const teams = createTeams(5);
const rounds = generateRoundRobin(teams);
for (const round of rounds) {
const teamsInRound = new Set<number>();
const teamsInRound = new Set<string>();
for (const matchup of round.matchups) {
teamsInRound.add(matchup.team1Id);
teamsInRound.add(matchup.team2Id);
teamsInRound.add([matchup.player1P1Id, matchup.player1P2Id].sort().join('-'));
teamsInRound.add([matchup.player2P1Id, matchup.player2P2Id].sort().join('-'));
}
// 5 teams, one has bye each round, so 4 play
expect(teamsInRound.size).toBe(4);
@@ -103,7 +137,7 @@ describe('Round-Robin Schedule Generator', () => {
});
test('should handle 8 teams (typical euchre tournament)', () => {
const teams = [1, 2, 3, 4, 5, 6, 7, 8];
const teams = createTeams(8);
const rounds = generateRoundRobin(teams);
expect(rounds).toHaveLength(7);
@@ -113,7 +147,7 @@ describe('Round-Robin Schedule Generator', () => {
});
test('round numbers should be sequential starting from 1', () => {
const teams = [1, 2, 3, 4, 5, 6];
const teams = createTeams(6);
const rounds = generateRoundRobin(teams);
rounds.forEach((round, idx) => {
@@ -130,18 +164,22 @@ describe('Round-Robin Schedule Generator', () => {
});
test('should reject single team', () => {
const result = validateScheduleInput([1]);
const result = validateScheduleInput([{ player1Id: 1, player2Id: 2 }]);
expect(result.valid).toBe(false);
});
test('should reject duplicate team IDs', () => {
const result = validateScheduleInput([1, 2, 2]);
test('should reject duplicate team pairings', () => {
const result = validateScheduleInput([
{ player1Id: 1, player2Id: 2 },
{ player1Id: 3, player2Id: 4 },
{ player1Id: 1, player2Id: 2 }, // Duplicate
]);
expect(result.valid).toBe(false);
expect(result.error).toContain('Duplicate');
});
test('should accept valid team list', () => {
const result = validateScheduleInput([1, 2, 3, 4]);
const result = validateScheduleInput(createTeams(4));
expect(result.valid).toBe(true);
expect(result.error).toBeUndefined();
});