feat: set up development database and test cleanup utilities
This commit is contained in:
@@ -13,6 +13,9 @@
|
|||||||
"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:clean": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" node scripts/setup-postgres.js --drop",
|
||||||
|
"db:use-dev": "node scripts/use-dev-db.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",
|
||||||
|
|||||||
@@ -51,6 +51,22 @@ function createDatabase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dropDatabase() {
|
||||||
|
try {
|
||||||
|
const dbUrl = process.env.DATABASE_URL;
|
||||||
|
const dbName = dbUrl.split('/').pop();
|
||||||
|
|
||||||
|
// Drop existing connections and database
|
||||||
|
execSync(`psql "${dbUrl}" -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '${dbName}' AND pid <> pg_backend_pid();" 2>/dev/null || true`, { stdio: 'pipe' });
|
||||||
|
execSync(`psql "${dbUrl}" -c "DROP DATABASE IF EXISTS ${dbName};" 2>/dev/null || true`, { stdio: 'pipe' });
|
||||||
|
console.log(`✅ Database "${dbName}" dropped`);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Failed to drop database:', error.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function runMigrations() {
|
function runMigrations() {
|
||||||
try {
|
try {
|
||||||
console.log('🔄 Running migrations...');
|
console.log('🔄 Running migrations...');
|
||||||
@@ -76,6 +92,9 @@ function generatePrismaClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
|
const args = process.argv.slice(2);
|
||||||
|
const shouldDrop = args.includes('--drop');
|
||||||
|
|
||||||
console.log('=== PostgreSQL Setup for EuchreCamp ===\n');
|
console.log('=== PostgreSQL Setup for EuchreCamp ===\n');
|
||||||
|
|
||||||
// Check if DATABASE_URL is set
|
// Check if DATABASE_URL is set
|
||||||
@@ -90,6 +109,14 @@ function main() {
|
|||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Drop database if requested
|
||||||
|
if (shouldDrop) {
|
||||||
|
console.log('Dropping existing database...');
|
||||||
|
if (!dropDatabase()) {
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create database
|
// Create database
|
||||||
if (!createDatabase()) {
|
if (!createDatabase()) {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|||||||
Executable
+49
@@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Switch to development database
|
||||||
|
* Creates a .env.development.local file with development database settings
|
||||||
|
*/
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const { execSync } = require('child_process');
|
||||||
|
|
||||||
|
const envDevPath = path.join(__dirname, '..', '.env.development');
|
||||||
|
const envDevLocalPath = path.join(__dirname, '..', '.env.development.local');
|
||||||
|
|
||||||
|
console.log('🔧 Setting up development database...\n');
|
||||||
|
|
||||||
|
// Check if .env.development exists
|
||||||
|
if (!fs.existsSync(envDevPath)) {
|
||||||
|
console.error('❌ .env.development file not found');
|
||||||
|
console.error('Please create it first or run: npm run db:setup-dev');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy .env.development to .env.development.local if it doesn't exist
|
||||||
|
if (!fs.existsSync(envDevLocalPath)) {
|
||||||
|
fs.copyFileSync(envDevPath, envDevLocalPath);
|
||||||
|
console.log('✅ Created .env.development.local');
|
||||||
|
} else {
|
||||||
|
console.log('ℹ️ .env.development.local already exists');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set NODE_ENV for the current session
|
||||||
|
process.env.NODE_ENV = 'development';
|
||||||
|
process.env.DATABASE_PROVIDER = 'postgresql';
|
||||||
|
|
||||||
|
// Read the development database URL
|
||||||
|
const envContent = fs.readFileSync(envDevPath, 'utf8');
|
||||||
|
const match = envContent.match(/DATABASE_URL="([^"]+)"/);
|
||||||
|
if (match) {
|
||||||
|
process.env.DATABASE_URL = match[1];
|
||||||
|
console.log(`✅ Development database URL: ${process.env.DATABASE_URL}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('\n✅ Development database configured!');
|
||||||
|
console.log('\nNext steps:');
|
||||||
|
console.log('1. Setup the dev database: npm run db:setup-dev');
|
||||||
|
console.log('2. Start development server: npm run dev');
|
||||||
|
console.log('\nNote: This script sets environment variables for the current session.');
|
||||||
|
console.log('For persistent configuration, use .env.development.local');
|
||||||
@@ -5,10 +5,24 @@
|
|||||||
|
|
||||||
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';
|
||||||
|
|
||||||
const authFile = 'playwright/.auth/user.json';
|
const authFile = 'playwright/.auth/user.json';
|
||||||
const adminAuthFile = 'playwright/.auth/admin.json';
|
const adminAuthFile = 'playwright/.auth/admin.json';
|
||||||
|
|
||||||
|
// Check if we're using the dev database
|
||||||
|
function isDevDatabase(): boolean {
|
||||||
|
const dbUrl = process.env.DATABASE_URL || '';
|
||||||
|
return dbUrl.includes('euchre_camp_dev');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warn if not using dev database
|
||||||
|
if (!isDevDatabase()) {
|
||||||
|
console.warn('⚠️ WARNING: Not using dev database!');
|
||||||
|
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) {
|
||||||
const baseURL = config.projects[0]?.use?.baseURL || 'http://localhost:3000';
|
const baseURL = config.projects[0]?.use?.baseURL || 'http://localhost:3000';
|
||||||
|
|
||||||
@@ -144,18 +158,13 @@ export default async function globalSetup(config: FullConfig) {
|
|||||||
|
|
||||||
// Return teardown function
|
// Return teardown function
|
||||||
return async () => {
|
return async () => {
|
||||||
// Clean up test users
|
console.log('\n=== Global Teardown ===');
|
||||||
|
|
||||||
|
// Clean up all test data
|
||||||
try {
|
try {
|
||||||
await prisma.user.deleteMany({
|
await cleanupAllTestData();
|
||||||
where: {
|
|
||||||
email: {
|
|
||||||
startsWith: 'setup-'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
console.log('Cleaned up test users');
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error cleaning up test users:', error);
|
console.error('Error cleaning up test data:', error);
|
||||||
} finally {
|
} finally {
|
||||||
await prisma.$disconnect();
|
await prisma.$disconnect();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,27 @@
|
|||||||
import { test, expect } from '@playwright/test'
|
import { test, expect } from '@playwright/test'
|
||||||
import { prisma } from '@/lib/prisma'
|
import { prisma } from '@/lib/prisma'
|
||||||
|
import { createTestPlayer, cleanupTestRecords, getCreatedRecordCounts } from '@/__tests__/test-utils'
|
||||||
|
|
||||||
test.describe('Home Page', () => {
|
test.describe('Home Page', () => {
|
||||||
|
test.beforeEach(async () => {
|
||||||
|
// Clean up any existing test records before each test
|
||||||
|
await cleanupTestRecords();
|
||||||
|
});
|
||||||
|
|
||||||
|
test.afterEach(async () => {
|
||||||
|
// Clean up test records after each test
|
||||||
|
await cleanupTestRecords();
|
||||||
|
});
|
||||||
|
|
||||||
test('should display top 10 players', async ({ page }) => {
|
test('should display top 10 players', async ({ page }) => {
|
||||||
// Create some test players with unique names and very high Elo to ensure they're in top 10
|
// Create some test players with unique names and very high Elo to ensure they're in top 10
|
||||||
const timestamp = Date.now()
|
const timestamp = Date.now()
|
||||||
const players = []
|
const players = []
|
||||||
for (let i = 0; i < 3; i++) {
|
for (let i = 0; i < 3; i++) {
|
||||||
const playerName = `Home Test Player ${timestamp} ${i + 1}`
|
const playerName = `Home Test Player ${timestamp} ${i + 1}`
|
||||||
const player = await prisma.player.create({
|
const player = await createTestPlayer({
|
||||||
data: {
|
|
||||||
name: playerName,
|
name: playerName,
|
||||||
normalizedName: playerName.toLowerCase(),
|
currentElo: 2000 - i * 10,
|
||||||
currentElo: 2000 - i * 10, // Very high Elo to ensure they're in top 10
|
|
||||||
gamesPlayed: 10 + i,
|
|
||||||
wins: 5 + Math.floor(i / 2),
|
|
||||||
losses: 5 + Math.ceil(i / 2),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
players.push(player)
|
players.push(player)
|
||||||
}
|
}
|
||||||
@@ -33,10 +38,9 @@ test.describe('Home Page', () => {
|
|||||||
page.locator(`a:has-text("Home Test Player ${timestamp} 1")`)
|
page.locator(`a:has-text("Home Test Player ${timestamp} 1")`)
|
||||||
).toBeVisible()
|
).toBeVisible()
|
||||||
|
|
||||||
// Clean up
|
// Verify cleanup will work
|
||||||
for (const player of players) {
|
const counts = getCreatedRecordCounts();
|
||||||
await prisma.player.delete({ where: { id: player.id } })
|
expect(counts.players).toBe(3);
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('should display club president', async ({ page }) => {
|
test('should display club president', async ({ page }) => {
|
||||||
|
|||||||
@@ -0,0 +1,274 @@
|
|||||||
|
/**
|
||||||
|
* Test Utilities for EuchreCamp
|
||||||
|
*
|
||||||
|
* Provides helper functions for creating and cleaning up test data
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { prisma } from '@/lib/prisma';
|
||||||
|
import type { User, Player, Event, Match } from '@prisma/client';
|
||||||
|
|
||||||
|
// Track created test records for cleanup
|
||||||
|
const createdRecords = {
|
||||||
|
users: [] as string[],
|
||||||
|
players: [] as number[],
|
||||||
|
events: [] as number[],
|
||||||
|
matches: [] as number[],
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a test user with optional player association
|
||||||
|
*/
|
||||||
|
export async function createTestUser(options: {
|
||||||
|
email?: string;
|
||||||
|
name?: string;
|
||||||
|
role?: string;
|
||||||
|
playerId?: number | null;
|
||||||
|
} = {}) {
|
||||||
|
const timestamp = Date.now();
|
||||||
|
const email = options.email || `test-user-${timestamp}@example.com`;
|
||||||
|
const name = options.name || `Test User ${timestamp}`;
|
||||||
|
const role = options.role || 'player';
|
||||||
|
|
||||||
|
const user = await prisma.user.create({
|
||||||
|
data: {
|
||||||
|
email,
|
||||||
|
name,
|
||||||
|
role,
|
||||||
|
playerId: options.playerId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
createdRecords.users.push(user.id);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a test player
|
||||||
|
*/
|
||||||
|
export async function createTestPlayer(options: {
|
||||||
|
name?: string;
|
||||||
|
currentElo?: number;
|
||||||
|
} = {}) {
|
||||||
|
const timestamp = Date.now();
|
||||||
|
const name = options.name || `Test Player ${timestamp}`;
|
||||||
|
|
||||||
|
const player = await prisma.player.create({
|
||||||
|
data: {
|
||||||
|
name,
|
||||||
|
normalizedName: name.toLowerCase(),
|
||||||
|
currentElo: options.currentElo || 1000,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
createdRecords.players.push(player.id);
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a test event (tournament)
|
||||||
|
*/
|
||||||
|
export async function createTestEvent(options: {
|
||||||
|
name?: string;
|
||||||
|
ownerId?: string;
|
||||||
|
} = {}) {
|
||||||
|
const timestamp = Date.now();
|
||||||
|
const name = options.name || `Test Event ${timestamp}`;
|
||||||
|
|
||||||
|
const event = await prisma.event.create({
|
||||||
|
data: {
|
||||||
|
name,
|
||||||
|
eventType: 'tournament',
|
||||||
|
format: 'round_robin',
|
||||||
|
status: 'planned',
|
||||||
|
ownerId: options.ownerId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
createdRecords.events.push(event.id);
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a test match
|
||||||
|
*/
|
||||||
|
export async function createTestMatch(options: {
|
||||||
|
eventId?: number;
|
||||||
|
team1P1Id: number;
|
||||||
|
team1P2Id: number;
|
||||||
|
team2P1Id: number;
|
||||||
|
team2P2Id: number;
|
||||||
|
team1Score?: number;
|
||||||
|
team2Score?: number;
|
||||||
|
}) {
|
||||||
|
const timestamp = Date.now();
|
||||||
|
|
||||||
|
const match = await prisma.match.create({
|
||||||
|
data: {
|
||||||
|
eventId: options.eventId,
|
||||||
|
team1P1Id: options.team1P1Id,
|
||||||
|
team1P2Id: options.team1P2Id,
|
||||||
|
team2P1Id: options.team2P1Id,
|
||||||
|
team2P2Id: options.team2P2Id,
|
||||||
|
team1Score: options.team1Score ?? 10,
|
||||||
|
team2Score: options.team2Score ?? 5,
|
||||||
|
status: 'completed',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
createdRecords.matches.push(match.id);
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up all created test records
|
||||||
|
*/
|
||||||
|
export async function cleanupTestRecords() {
|
||||||
|
console.log('🧹 Cleaning up test records...');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Delete matches first (they reference players)
|
||||||
|
if (createdRecords.matches.length > 0) {
|
||||||
|
await prisma.match.deleteMany({
|
||||||
|
where: { id: { in: createdRecords.matches } },
|
||||||
|
});
|
||||||
|
console.log(` Deleted ${createdRecords.matches.length} matches`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete events
|
||||||
|
if (createdRecords.events.length > 0) {
|
||||||
|
await prisma.event.deleteMany({
|
||||||
|
where: { id: { in: createdRecords.events } },
|
||||||
|
});
|
||||||
|
console.log(` Deleted ${createdRecords.events.length} events`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete users first (to break player associations)
|
||||||
|
if (createdRecords.users.length > 0) {
|
||||||
|
await prisma.user.deleteMany({
|
||||||
|
where: { id: { in: createdRecords.users } },
|
||||||
|
});
|
||||||
|
console.log(` Deleted ${createdRecords.users.length} users`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete players
|
||||||
|
if (createdRecords.players.length > 0) {
|
||||||
|
await prisma.player.deleteMany({
|
||||||
|
where: { id: { in: createdRecords.players } },
|
||||||
|
});
|
||||||
|
console.log(` Deleted ${createdRecords.players.length} players`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset tracking
|
||||||
|
createdRecords.users = [];
|
||||||
|
createdRecords.players = [];
|
||||||
|
createdRecords.events = [];
|
||||||
|
createdRecords.matches = [];
|
||||||
|
|
||||||
|
console.log('✅ Test records cleaned up successfully');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Error cleaning up test records:', error);
|
||||||
|
// Still reset tracking to avoid trying to delete again
|
||||||
|
createdRecords.users = [];
|
||||||
|
createdRecords.players = [];
|
||||||
|
createdRecords.events = [];
|
||||||
|
createdRecords.matches = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get count of created records (for assertions)
|
||||||
|
*/
|
||||||
|
export function getCreatedRecordCounts() {
|
||||||
|
return {
|
||||||
|
users: createdRecords.users.length,
|
||||||
|
players: createdRecords.players.length,
|
||||||
|
events: createdRecords.events.length,
|
||||||
|
matches: createdRecords.matches.length,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up test users by email pattern
|
||||||
|
*/
|
||||||
|
export async function cleanupTestUsersByEmailPattern(pattern: string = 'test-') {
|
||||||
|
try {
|
||||||
|
const result = await prisma.user.deleteMany({
|
||||||
|
where: {
|
||||||
|
email: {
|
||||||
|
contains: pattern,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(` Deleted ${result.count} test users matching pattern "${pattern}"`);
|
||||||
|
return result.count;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Error cleaning up test users:', error);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up test players by name pattern
|
||||||
|
*/
|
||||||
|
export async function cleanupTestPlayersByNamePattern(pattern: string = 'Test ') {
|
||||||
|
try {
|
||||||
|
const result = await prisma.player.deleteMany({
|
||||||
|
where: {
|
||||||
|
name: {
|
||||||
|
contains: pattern,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(` Deleted ${result.count} test players matching pattern "${pattern}"`);
|
||||||
|
return result.count;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Error cleaning up test players:', error);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean up all test data (aggressive cleanup)
|
||||||
|
*/
|
||||||
|
export async function cleanupAllTestData() {
|
||||||
|
console.log('🧹 Aggressive cleanup of all test data...');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Delete test users first
|
||||||
|
const userResult = await prisma.user.deleteMany({
|
||||||
|
where: {
|
||||||
|
email: {
|
||||||
|
contains: 'test-',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(` Deleted ${userResult.count} test users`);
|
||||||
|
|
||||||
|
// Delete test players
|
||||||
|
const playerResult = await prisma.player.deleteMany({
|
||||||
|
where: {
|
||||||
|
name: {
|
||||||
|
contains: 'Test ',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(` Deleted ${playerResult.count} test players`);
|
||||||
|
|
||||||
|
// Delete test events
|
||||||
|
const eventResult = await prisma.event.deleteMany({
|
||||||
|
where: {
|
||||||
|
name: {
|
||||||
|
contains: 'Test ',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(` Deleted ${eventResult.count} test events`);
|
||||||
|
|
||||||
|
console.log('✅ All test data cleaned up');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Error during aggressive cleanup:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export cleanup function for global setup/teardown
|
||||||
|
export const testCleanup = cleanupTestRecords;
|
||||||
Reference in New Issue
Block a user