Merge branch 'feat/database-test-safety' of ssh://git.notsosm.art/david/euchre_camp

This commit is contained in:
2026-03-31 18:44:08 -07:00
12 changed files with 189 additions and 35 deletions
+3 -3
View File
@@ -7,15 +7,15 @@
services: services:
app: app:
image: euchre-camp/euchre-camp:0.1.0 image: euchre-camp/euchre-camp:0.1.0.dev
container_name: euchre-camp container_name: euchre-camp
ports: ports:
- "51193:3000" - "51193:3000"
environment: environment:
# Database Configuration (REQUIRED: Set via CasaOS environment variables) # Database Configuration (REQUIRED: Set via CasaOS environment variables)
- DATABASE_PROVIDER=postgresql - DATABASE_PROVIDER=postgresql
- DATABASE_URL="postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp" - DATABASE_URL=${DATABASE_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp}
- DATABASE_SHADOW_URL="postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_shadow" - DATABASE_SHADOW_URL=${DATABASE_SHADOW_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp_shadow}
# Better Auth Configuration # Better Auth Configuration
- BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET:-change-this-secret-in-production} - BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET:-change-this-secret-in-production}
+2 -2
View File
@@ -14,8 +14,8 @@ services:
environment: environment:
# Database Configuration # Database Configuration
- DATABASE_PROVIDER=postgresql - DATABASE_PROVIDER=postgresql
- DATABASE_URL=postgresql://euchre_camp:password@db:5432/euchre_camp - DATABASE_URL=${DATABASE_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp}
- DATABASE_SHADOW_URL=postgresql://euchre_camp:password@db:5432/euchre_camp_shadow - DATABASE_SHADOW_URL=${DATABASE_SHADOW_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp_shadow}
# Better Auth Configuration # Better Auth Configuration
- BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET:-dev-secret-change-in-production} - BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET:-dev-secret-change-in-production}
+3 -3
View File
@@ -3,15 +3,15 @@
services: services:
app: app:
image: euchre-camp/euchre-camp:0.1.0 image: euchre-camp/euchre-camp:0.1.0.dev
container_name: euchre-camp-app container_name: euchre-camp-app
ports: ports:
- "3000:3000" - "3000:3000"
environment: environment:
# Database Configuration # Database Configuration
- DATABASE_PROVIDER=postgresql - DATABASE_PROVIDER=postgresql
- DATABASE_URL=postgresql://euchre_camp:password@db:5432/euchre_camp - DATABASE_URL=${DATABASE_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp}
- DATABASE_SHADOW_URL=postgresql://euchre_camp:password@db:5432/euchre_camp_shadow - DATABASE_SHADOW_URL=${DATABASE_SHADOW_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp_shadow}
# Better Auth Configuration # Better Auth Configuration
- BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET:-change-this-secret-in-production} - BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET:-change-this-secret-in-production}
+3 -2
View File
@@ -13,11 +13,12 @@
"test:acceptance:headed": "playwright test src/__tests__/e2e/ --headed", "test:acceptance:headed": "playwright test src/__tests__/e2e/ --headed",
"db:switch": "node scripts/switch-database.js", "db:switch": "node scripts/switch-database.js",
"db:setup-postgres": "node scripts/setup-postgres.js", "db:setup-postgres": "node scripts/setup-postgres.js",
"db:setup-dev": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" node scripts/setup-postgres.js", "db:setup-dev": "node scripts/setup-postgres.js",
"db:setup-dev:clean": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" node scripts/setup-postgres.js --drop", "db:setup-dev:clean": "node scripts/setup-postgres.js --drop",
"db:reset-dev": "node scripts/reset-dev-db.js", "db:reset-dev": "node scripts/reset-dev-db.js",
"db:use-dev": "node scripts/use-dev-db.js", "db:use-dev": "node scripts/use-dev-db.js",
"db:cleanup-prod": "node scripts/cleanup-prod-db.js", "db:cleanup-prod": "node scripts/cleanup-prod-db.js",
"db:check-prod": "node scripts/check-test-records.js",
"db:seed": "node scripts/seed.js", "db:seed": "node scripts/seed.js",
"docker:up": "docker-compose up -d", "docker:up": "docker-compose up -d",
"docker:down": "docker-compose down", "docker:down": "docker-compose down",
+18 -2
View File
@@ -7,8 +7,24 @@
const { execSync } = require('child_process'); const { execSync } = require('child_process');
// Database connection string // Database connection string - must be explicitly set via environment variable
const DB_URL = 'postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp'; 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);
}
// Helper to run SQL and log results // Helper to run SQL and log results
function runSQL(sql, description) { function runSQL(sql, description) {
+18 -2
View File
@@ -10,8 +10,24 @@
const { execSync } = require('child_process'); const { execSync } = require('child_process');
// Database connection string // Database connection string - must be explicitly set via environment variable
const DB_URL = 'postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp'; 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);
}
// Helper to run SQL and log results // Helper to run SQL and log results
function runSQL(sql, description) { function runSQL(sql, description) {
+6 -6
View File
@@ -64,8 +64,8 @@ services:
environment: environment:
# Database Configuration (REQUIRED: Set via CasaOS environment variables) # Database Configuration (REQUIRED: Set via CasaOS environment variables)
- DATABASE_PROVIDER=postgresql - DATABASE_PROVIDER=postgresql
- DATABASE_URL="postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp" - DATABASE_URL=\${DATABASE_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp}
- DATABASE_SHADOW_URL="postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_shadow" - DATABASE_SHADOW_URL=\${DATABASE_SHADOW_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp_shadow}
# Better Auth Configuration # Better Auth Configuration
- BETTER_AUTH_SECRET=\${BETTER_AUTH_SECRET:-change-this-secret-in-production} - BETTER_AUTH_SECRET=\${BETTER_AUTH_SECRET:-change-this-secret-in-production}
@@ -113,8 +113,8 @@ services:
environment: environment:
# Database Configuration # Database Configuration
- DATABASE_PROVIDER=postgresql - DATABASE_PROVIDER=postgresql
- DATABASE_URL=postgresql://euchre_camp:password@db:5432/euchre_camp - DATABASE_URL=\${DATABASE_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp}
- DATABASE_SHADOW_URL=postgresql://euchre_camp:password@db:5432/euchre_camp_shadow - DATABASE_SHADOW_URL=\${DATABASE_SHADOW_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp_shadow}
# Better Auth Configuration # Better Auth Configuration
- BETTER_AUTH_SECRET=\${BETTER_AUTH_SECRET:-change-this-secret-in-production} - BETTER_AUTH_SECRET=\${BETTER_AUTH_SECRET:-change-this-secret-in-production}
@@ -181,8 +181,8 @@ services:
environment: environment:
# Database Configuration # Database Configuration
- DATABASE_PROVIDER=postgresql - DATABASE_PROVIDER=postgresql
- DATABASE_URL=postgresql://euchre_camp:password@db:5432/euchre_camp - DATABASE_URL=\${DATABASE_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp}
- DATABASE_SHADOW_URL=postgresql://euchre_camp:password@db:5432/euchre_camp_shadow - DATABASE_SHADOW_URL=\${DATABASE_SHADOW_URL:-postgresql://euchre_camp:password@db:5432/euchre_camp_shadow}
# Better Auth Configuration # Better Auth Configuration
- BETTER_AUTH_SECRET=\${BETTER_AUTH_SECRET:-dev-secret-change-in-production} - BETTER_AUTH_SECRET=\${BETTER_AUTH_SECRET:-dev-secret-change-in-production}
+20 -6
View File
@@ -11,21 +11,35 @@ const { execSync } = require('child_process');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
// Load environment variables // Load environment variables - prioritize explicit DEV_DATABASE_URL
// Falls back to DATABASE_URL, then .env.development
const envPath = path.resolve(__dirname, '..', '.env.development'); const envPath = path.resolve(__dirname, '..', '.env.development');
if (fs.existsSync(envPath)) { if (fs.existsSync(envPath)) {
require('dotenv').config({ path: envPath }); require('dotenv').config({ path: envPath });
} }
// Check if DATABASE_URL is set // Use DEV_DATABASE_URL if set, otherwise use DATABASE_URL
if (!process.env.DATABASE_URL) { const databaseUrl = process.env.DEV_DATABASE_URL || process.env.DATABASE_URL;
console.error('❌ DATABASE_URL is not set');
console.error('Please set it in .env.development'); // Check if database URL is set
if (!databaseUrl) {
console.error('❌ No database URL set');
console.error('Please set DEV_DATABASE_URL or DATABASE_URL environment variable');
console.error('Or ensure .env.development file exists with DATABASE_URL');
process.exit(1);
}
// Validate that we're using the dev database
if (!databaseUrl.includes('_dev') && !databaseUrl.includes('_dev_')) {
console.error('❌ ERROR: This script should only be used with the development database!');
console.error(' Current URL:', databaseUrl);
console.error(' Expected pattern: euchre_camp_dev');
console.error(' Please set DEV_DATABASE_URL for development database operations.');
process.exit(1); process.exit(1);
} }
// Extract database name from URL // Extract database name from URL
const dbUrl = process.env.DATABASE_URL; const dbUrl = databaseUrl;
const dbName = dbUrl.split('/').pop(); const dbName = dbUrl.split('/').pop();
console.log('🔄 Resetting development database...\n'); console.log('🔄 Resetting development database...\n');
+5 -1
View File
@@ -9,8 +9,12 @@ const { execSync } = require('child_process');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
// Load .env file if it exists // Load .env.development file first (if it exists), then .env file
const envDevPath = path.resolve(__dirname, '..', '.env.development');
const envPath = path.resolve(__dirname, '..', '.env'); const envPath = path.resolve(__dirname, '..', '.env');
if (fs.existsSync(envDevPath)) {
require('dotenv').config({ path: envDevPath });
}
if (fs.existsSync(envPath)) { if (fs.existsSync(envPath)) {
require('dotenv').config({ path: envPath }); require('dotenv').config({ path: envPath });
} }
+43 -3
View File
@@ -6,6 +6,22 @@
import { chromium, type FullConfig } from '@playwright/test'; import { chromium, type FullConfig } from '@playwright/test';
import { prisma } from '@/lib/prisma'; import { prisma } from '@/lib/prisma';
import { cleanupAllTestData } from '@/__tests__/test-utils'; import { cleanupAllTestData } from '@/__tests__/test-utils';
import path from 'path';
import fs from 'fs';
// Load .env file first, then .env.development (which will override .env)
const envPath = path.resolve(process.cwd(), '.env');
const envDevPath = path.resolve(process.cwd(), '.env.development');
// Load base .env file
if (fs.existsSync(envPath)) {
require('dotenv').config({ path: envPath });
}
// Load .env.development file (will override .env settings)
if (fs.existsSync(envDevPath)) {
require('dotenv').config({ path: envDevPath, override: true });
}
const authFile = 'playwright/.auth/user.json'; const authFile = 'playwright/.auth/user.json';
const adminAuthFile = 'playwright/.auth/admin.json'; const adminAuthFile = 'playwright/.auth/admin.json';
@@ -16,11 +32,35 @@ function isDevDatabase(): boolean {
return dbUrl.includes('euchre_camp_dev'); return dbUrl.includes('euchre_camp_dev');
} }
// Warn if not using dev database function isProductionDatabase(): boolean {
const dbUrl = process.env.DATABASE_URL || '';
return dbUrl.includes('euchre_camp') && !dbUrl.includes('_dev');
}
// Strict check - fail if using production database
if (isProductionDatabase()) {
console.error('');
console.error('='.repeat(80));
console.error('CRITICAL ERROR: Tests are attempting to run against PRODUCTION database!');
console.error('='.repeat(80));
console.error('');
console.error('Current DATABASE_URL:', process.env.DATABASE_URL);
console.error('');
console.error('Tests MUST run against the development database (euchre_camp_dev)');
console.error('');
console.error('To fix this:');
console.error(' 1. Run: npm run test:acceptance');
console.error(' 2. Or set: DATABASE_URL environment variable to dev database URL');
console.error(' 3. Or load .env.development: source .env.development && npm run test:acceptance');
console.error('');
console.error('Aborting test execution to prevent data corruption.');
console.error('');
process.exit(1);
}
if (!isDevDatabase()) { if (!isDevDatabase()) {
console.warn('⚠️ WARNING: Not using dev database!'); console.warn('⚠️ WARNING: DATABASE_URL does not contain euchre_camp_dev');
console.warn(' Current DATABASE_URL:', process.env.DATABASE_URL); console.warn(' Current DATABASE_URL:', process.env.DATABASE_URL);
console.warn(' Expected to contain: euchre_camp_dev');
} }
export default async function globalSetup(config: FullConfig) { export default async function globalSetup(config: FullConfig) {
+7 -5
View File
@@ -86,12 +86,14 @@ export default async function PlayerProfilePage({ params }: PageProps) {
const totalWins = player.wins const totalWins = player.wins
const winRate = totalGames > 0 ? ((totalWins / totalGames) * 100).toFixed(1) : "0.0" const winRate = totalGames > 0 ? ((totalWins / totalGames) * 100).toFixed(1) : "0.0"
// Get best partnership // Get best partnership based on ELO contribution
// Best partner is the one who contributed most to your rating (highest totalEloChange)
// Must have played at least 2 games together to be considered
const bestPartnership = partnershipStats.reduce((best, stat) => { const bestPartnership = partnershipStats.reduce((best, stat) => {
if (stat.gamesPlayed < 3) return best // Need at least 3 games for partnership to be meaningful if (stat.gamesPlayed < 2) return best // Need at least 2 games for partnership to be meaningful
const currentRate = stat.wins / stat.gamesPlayed const currentEloChange = stat.totalEloChange
const bestRate = best ? best.wins / best.gamesPlayed : 0 const bestEloChange = best ? best.totalEloChange : -Infinity
return currentRate > bestRate ? stat : best return currentEloChange > bestEloChange ? stat : best
}, null as (typeof partnershipStats[0] | null)) }, null as (typeof partnershipStats[0] | null))
return ( return (
+61
View File
@@ -1 +1,62 @@
import '@testing-library/jest-dom' import '@testing-library/jest-dom'
import path from 'path'
import fs from 'fs'
/**
* Vitest Setup File
*
* This file runs before each test file in the Vitest environment.
* It validates that tests are running against the development database
* to prevent accidental data corruption.
*/
// Load .env file first, then .env.development (which will override .env)
const envPath = path.resolve(process.cwd(), '.env');
const envDevPath = path.resolve(process.cwd(), '.env.development');
// Load base .env file
if (fs.existsSync(envPath)) {
require('dotenv').config({ path: envPath });
}
// Load .env.development file (will override .env settings)
if (fs.existsSync(envDevPath)) {
require('dotenv').config({ path: envDevPath, override: true });
}
// Check if DATABASE_URL is set and points to dev database
const databaseUrl = process.env.DATABASE_URL;
if (databaseUrl) {
const isDevDatabase = databaseUrl.includes('euchre_camp_dev');
const isProductionDatabase =
databaseUrl.includes('euchre_camp') &&
!databaseUrl.includes('_dev') &&
!databaseUrl.includes('_dev_');
if (isProductionDatabase) {
console.error('');
console.error('='.repeat(80));
console.error('CRITICAL ERROR: Tests are attempting to run against PRODUCTION database!');
console.error('='.repeat(80));
console.error('');
console.error('Current DATABASE_URL:', databaseUrl);
console.error('');
console.error('Tests MUST run against the development database (euchre_camp_dev)');
console.error('');
console.error('To fix this:');
console.error(' 1. Run: npm run test:run');
console.error(' 2. Or set: DATABASE_URL environment variable to dev database URL');
console.error(' 3. Or load .env.development: source .env.development && npm run test:run');
console.error('');
console.error('Aborting test execution to prevent data corruption.');
console.error('');
process.exit(1);
}
if (isDevDatabase) {
console.log('✓ Tests running against development database (euchre_camp_dev)');
}
} else {
console.warn('⚠ No DATABASE_URL set - tests may fail or use unexpected database');
}