Files
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

211 lines
6.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* Clean up production database - remove test records only
*
* WARNING: This script modifies the production database.
* It only removes records that match test patterns.
* It preserves actual player data, matches, and tournaments.
*/
const { execSync } = require('child_process');
// Database connection string - must be explicitly set via environment variable
const DB_URL = process.env.PROD_DATABASE_URL || process.env.DATABASE_URL;
// Check if database URL is set
if (!DB_URL) {
console.error('❌ No database URL set');
console.error('Please set PROD_DATABASE_URL or DATABASE_URL environment variable');
console.error('For production: PROD_DATABASE_URL="postgresql://user:pass@host:port/dbname"');
process.exit(1);
}
// Validate that we're not accidentally using dev database
if (DB_URL.includes('_dev') || DB_URL.includes('_dev_')) {
console.error('❌ ERROR: This script should not be used with the development database!');
console.error(' Current DATABASE_URL:', DB_URL);
console.error(' Please set PROD_DATABASE_URL for production database operations.');
process.exit(1);
}
// Single source of truth for test object patterns
const TEST_PATTERNS = {
players: [
'%Test%',
'%Setup%',
'%Home Test%',
'%Home Match Player%',
'%Admin User%',
'%NinePart%',
'%Nine Part%',
'%Test Player%',
'%TestUser%',
'%Cucumber%',
'%Config Admin%',
],
events: [
'%Test%',
'%Setup%',
'%Recent%',
'%Test Tournament%',
'%Cucumber%',
],
users: [
'%test%',
'%setup%',
'%cucumber%',
'%TestUser%',
]
};
// Helper to build SQL LIKE clause from patterns
function buildLikeClause(patterns) {
return patterns.map(p => `name LIKE '${p}'`).join(' OR ');
}
// Helper to build email LIKE clause
function buildEmailLikeClause(patterns) {
return patterns.map(p => `email LIKE '${p}'`).join(' OR ');
}
// Helper to run SQL and log results
function runSQL(sql, description) {
console.log(`\n🔍 ${description}...`);
try {
const result = execSync(`psql "${DB_URL}" -c "${sql}"`, { encoding: 'utf8' });
console.log(result);
return true;
} catch (error) {
console.error(`❌ Error: ${error.message}`);
return false;
}
}
// Helper to run SQL via file (avoids quote escaping issues)
function runSQLViaFile(sql, description) {
console.log(`\n🔍 ${description}...`);
const fs = require('fs');
const os = require('os');
const path = require('path');
try {
// Create temp file with SQL
const tmpFile = path.join(os.tmpdir(), `cleanup_${Date.now()}.sql`);
fs.writeFileSync(tmpFile, sql);
const result = execSync(`psql "${DB_URL}" -f "${tmpFile}"`, { encoding: 'utf8' });
console.log(result);
// Clean up temp file
fs.unlinkSync(tmpFile);
return true;
} catch (error) {
console.error(`❌ Error: ${error.message}`);
return false;
}
}
// Helper to count records matching any of the patterns
function countRecordsByPatterns(table, patterns) {
const whereClause = table === 'users'
? buildEmailLikeClause(patterns)
: buildLikeClause(patterns);
const sql = `SELECT COUNT(*) FROM ${table} WHERE ${whereClause};`;
try {
const result = execSync(`psql "${DB_URL}" -c "${sql}" -t`, { encoding: 'utf8' }).trim();
return parseInt(result);
} catch (error) {
return 0;
}
}
async function main() {
console.log('🧹 Cleaning up test records from production database');
console.log('=============================================\n');
// Confirm with user
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('⚠️ This will DELETE test records from PRODUCTION database. Continue? (y/N) ', async (answer) => {
if (answer.toLowerCase() !== 'y' && answer.toLowerCase() !== 'yes') {
console.log('❌ Cleanup cancelled');
rl.close();
process.exit(0);
}
rl.close();
console.log('\n📋 Checking test records...\n');
// Count test records using centralized patterns
const testPlayerCount = countRecordsByPatterns('players', TEST_PATTERNS.players);
console.log(`Test players found: ${testPlayerCount}`);
const testEventCount = countRecordsByPatterns('events', TEST_PATTERNS.events);
console.log(`Test tournaments found: ${testEventCount}`);
const testUserCount = countRecordsByPatterns('users', TEST_PATTERNS.users);
console.log(`Test users found: ${testUserCount}`);
console.log('\n🔄 Starting cleanup...\n');
// Step 1: Delete test tournaments (with matches due to cascade delete)
console.log('1. Deleting test tournaments (matches will be deleted via cascade)...');
const eventWhere = buildLikeClause(TEST_PATTERNS.events);
runSQLViaFile(
`DELETE FROM events WHERE (${eventWhere});`,
'Deleted test tournaments'
);
// Step 2: Delete test players (all of them, since we deleted their matches)
console.log('\n2. Deleting test players...');
const playerWhere = buildLikeClause(TEST_PATTERNS.players);
runSQLViaFile(
`DELETE FROM players WHERE (${playerWhere});`,
'Deleted test players'
);
// Step 3: Delete test users (they shouldn't have player associations)
console.log('\n3. Deleting test users...');
const userWhere = buildEmailLikeClause(TEST_PATTERNS.users);
runSQLViaFile(
`DELETE FROM users WHERE (${userWhere}) AND "playerId" IS NULL;`,
'Deleted test users'
);
// Summary
console.log('\n✅ Cleanup complete!');
console.log('\n📊 Remaining test records:');
const remainingPlayerWhere = buildLikeClause(TEST_PATTERNS.players);
runSQLViaFile(
`SELECT COUNT(*) FROM players WHERE ${remainingPlayerWhere};`,
'Remaining test players'
);
const remainingEventWhere = buildLikeClause(TEST_PATTERNS.events);
runSQLViaFile(
`SELECT COUNT(*) FROM events WHERE ${remainingEventWhere};`,
'Remaining test tournaments'
);
const remainingUserWhere = buildEmailLikeClause(TEST_PATTERNS.users);
runSQLViaFile(
`SELECT COUNT(*) FROM users WHERE ${remainingUserWhere};`,
'Remaining test users'
);
console.log('\n💡 Note: All test records deleted. Matches are automatically deleted');
console.log(' via cascade delete when their parent tournament is deleted.');
console.log('\n📝 To add new test patterns, edit TEST_PATTERNS in this script.');
});
}
// Run main function
main();