Files
euchre_camp/src/__tests__/unit/recalculate-elo.test.ts
T
david bb6be245b7
Release / release (push) Failing after 9s
Build CI Images / build-ci-base (push) Failing after 18s
feat: Implement tournament schedule tab and fix E2E tests (#27)
## Summary

This PR implements the tournament schedule tab functionality and fixes all remaining E2E test failures.

### Changes Included

1. **Tournament Schedule Feature**
   - Added tournament schedule page at `/admin/tournaments/[id]/schedule`
   - Implemented "Generate Schedule" button functionality
   - Added schedule generation logic for round-robin tournaments

2. **E2E Test Fixes**
   - Fixed database connection issues in production builds
   - Improved test reliability with better error handling and debugging
   - Updated test infrastructure to use environment variables instead of hardcoded values

3. **CI/CD Updates**
   - Added E2E test job to PR workflow
   - Configured tests to run against development database
   - Moved database password to Gitea secrets

4. **Code Quality**
   - Removed hardcoded passwords from codebase
   - Improved Prisma client configuration
   - Enhanced authentication and navigation components

### Test Results
All 16 E2E test scenarios are now passing:
- Authentication tests: 
- Registration tests: 
- Tournament schedule tests: 
- Player schedule tests: 
- Admin navigation tests: 

### Database Configuration
- Tests run against `euchre_camp_dev` database
- Production builds use environment variables for database configuration
- Database password stored in Gitea secrets as `DB_PASSWORD`

### CI Pipeline
The PR workflow now includes:
1. Unit tests
2. E2E tests (using production build)
3. Version bump analysis

E2E tests must pass before PR can be merged.

Reviewed-on: #27
Co-authored-by: David Gwilliam <dhgwilliam@gmail.com>
Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
2026-04-27 01:59:16 +00:00

191 lines
5.4 KiB
TypeScript

/**
* Unit Tests: ELO Recalculation
*
* Tests the idempotent full recalculation of ELO ratings and player statistics
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, test, expect, beforeEach, mock } from 'bun:test';
import { recalculateAllElo } from '@/lib/elo-utils';
// Mock Prisma client
const mockPrisma = {
player: {
updateMany: mock(async () => ({ count: 0 })),
update: mock(async () => ({})),
},
eloSnapshot: {
deleteMany: mock(async () => ({ count: 0 })),
create: mock(async () => ({})),
},
partnershipStat: {
deleteMany: mock(async () => ({ count: 0 })),
findFirst: mock(async () => null), // No existing stats initially
update: mock(async () => ({})),
create: mock(async () => ({})),
},
match: {
findMany: mock(async () => []),
},
};
describe('recalculateAllElo', () => {
beforeEach(() => {
mock.clearAllMocks();
});
test('should reset all player stats to zero', async () => {
(mockPrisma.match.findMany as any).mockResolvedValue([]);
const result = await recalculateAllElo(mockPrisma as any);
expect(mockPrisma.player.updateMany).toHaveBeenCalledWith({
data: {
currentElo: 1000,
gamesPlayed: 0,
wins: 0,
losses: 0,
},
});
expect(result.matchesProcessed).toBe(0);
expect(result.playersUpdated).toBe(0);
expect(result.partnershipsUpdated).toBe(0);
});
test('should delete all existing elo snapshots', async () => {
(mockPrisma.match.findMany as any).mockResolvedValue([]);
await recalculateAllElo(mockPrisma as any);
expect(mockPrisma.eloSnapshot.deleteMany).toHaveBeenCalledWith({});
});
test('should delete all existing partnership stats', async () => {
(mockPrisma.match.findMany as any).mockResolvedValue([]);
await recalculateAllElo(mockPrisma as any);
expect(mockPrisma.partnershipStat.deleteMany).toHaveBeenCalledWith({});
});
test('should return correct result for empty match list', async () => {
(mockPrisma.match.findMany as any).mockResolvedValue([]);
const result = await recalculateAllElo(mockPrisma as any);
expect(result).toEqual({
matchesProcessed: 0,
playersUpdated: 0,
partnershipsUpdated: 0,
});
});
test('should process matches in chronological order', async () => {
const mockMatches = [
{
id: 1,
playedAt: new Date('2024-01-01'),
player1P1: { id: 1, name: 'Player 1' },
player1P2: { id: 2, name: 'Player 2' },
player2P1: { id: 3, name: 'Player 3' },
player2P2: { id: 4, name: 'Player 4' },
team1Score: 10,
team2Score: 5,
},
{
id: 2,
playedAt: new Date('2024-01-02'),
player1P1: { id: 1, name: 'Player 1' },
player1P2: { id: 2, name: 'Player 2' },
player2P1: { id: 5, name: 'Player 5' },
player2P2: { id: 6, name: 'Player 6' },
team1Score: 8,
team2Score: 6,
},
];
(mockPrisma.match.findMany as any).mockResolvedValue(mockMatches);
await recalculateAllElo(mockPrisma as any);
// Should process 2 matches
expect(mockPrisma.match.findMany).toHaveBeenCalledWith({
orderBy: { playedAt: 'asc' },
include: {
player1P1: true,
player1P2: true,
player2P1: true,
player2P2: true,
},
});
});
test('should create elo snapshots for each player in each match', async () => {
const mockMatches = [
{
id: 1,
playedAt: new Date('2024-01-01'),
player1P1: { id: 1, name: 'Player 1' },
player1P2: { id: 2, name: 'Player 2' },
player2P1: { id: 3, name: 'Player 3' },
player2P2: { id: 4, name: 'Player 4' },
team1Score: 10,
team2Score: 5,
},
];
(mockPrisma.match.findMany as any).mockResolvedValue(mockMatches);
await recalculateAllElo(mockPrisma as any);
// Should create 4 snapshots (one for each player)
expect(mockPrisma.eloSnapshot.create).toHaveBeenCalledTimes(4);
});
test('should update player stats after processing matches', async () => {
const mockMatches = [
{
id: 1,
playedAt: new Date('2024-01-01'),
player1P1: { id: 1, name: 'Player 1' },
player1P2: { id: 2, name: 'Player 2' },
player2P1: { id: 3, name: 'Player 3' },
player2P2: { id: 4, name: 'Player 4' },
team1Score: 10,
team2Score: 5,
},
];
(mockPrisma.match.findMany as any).mockResolvedValue(mockMatches);
await recalculateAllElo(mockPrisma as any);
// Should update 4 players
expect(mockPrisma.player.update).toHaveBeenCalledTimes(4);
});
test('should update partnership stats after processing matches', async () => {
const mockMatches = [
{
id: 1,
playedAt: new Date('2024-01-01'),
player1P1: { id: 1, name: 'Player 1' },
player1P2: { id: 2, name: 'Player 2' },
player2P1: { id: 3, name: 'Player 3' },
player2P2: { id: 4, name: 'Player 4' },
team1Score: 10,
team2Score: 5,
},
];
(mockPrisma.match.findMany as any).mockResolvedValue(mockMatches);
await recalculateAllElo(mockPrisma as any);
// Should check for existing partnership stats (2 calls) and create new ones (2 calls)
expect(mockPrisma.partnershipStat.findFirst).toHaveBeenCalledTimes(2);
expect(mockPrisma.partnershipStat.create).toHaveBeenCalledTimes(2);
});
});