|
| 1 | +import { type ChildProcess, execSync, spawn } from "node:child_process"; |
| 2 | +import { testEnv } from "./env.ts"; |
| 3 | + |
| 4 | +const processes: ChildProcess[] = []; |
| 5 | + |
| 6 | +/** |
| 7 | + * Get all test environment variables for subprocess |
| 8 | + */ |
| 9 | +function getTestEnvVars(): NodeJS.ProcessEnv { |
| 10 | + return { |
| 11 | + ...process.env, |
| 12 | + DATABASE_URL: testEnv.DATABASE_URL, |
| 13 | + PORT: testEnv.PORT, |
| 14 | + CORS_ORIGIN: testEnv.CORS_ORIGIN, |
| 15 | + DISABLE_AUTH: testEnv.DISABLE_AUTH, |
| 16 | + PUBLIC_DISABLE_AUTH: testEnv.PUBLIC_DISABLE_AUTH, |
| 17 | + PUBLIC_API_BASE_URL: testEnv.PUBLIC_API_BASE_URL, |
| 18 | + BETTER_AUTH_SECRET: testEnv.BETTER_AUTH_SECRET, |
| 19 | + GOOGLE_CLIENT_ID: testEnv.GOOGLE_CLIENT_ID, |
| 20 | + GOOGLE_CLIENT_SECRET: testEnv.GOOGLE_CLIENT_SECRET, |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Wait for a URL to respond with 200 OK |
| 26 | + */ |
| 27 | +async function waitForUrl(url: string, timeout = 30000): Promise<void> { |
| 28 | + const start = Date.now(); |
| 29 | + while (Date.now() - start < timeout) { |
| 30 | + try { |
| 31 | + const res = await fetch(url); |
| 32 | + if (res.ok) return; |
| 33 | + } catch { |
| 34 | + // Not ready yet |
| 35 | + } |
| 36 | + await new Promise((r) => setTimeout(r, 500)); |
| 37 | + } |
| 38 | + throw new Error(`Timeout waiting for ${url}`); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Wait for PostgreSQL to be ready |
| 43 | + */ |
| 44 | +async function waitForDb(timeout = 30000): Promise<void> { |
| 45 | + const start = Date.now(); |
| 46 | + while (Date.now() - start < timeout) { |
| 47 | + try { |
| 48 | + execSync( |
| 49 | + `docker exec ${testEnv.DB_CONTAINER_NAME} pg_isready -U postgres`, |
| 50 | + { stdio: "ignore" }, |
| 51 | + ); |
| 52 | + return; |
| 53 | + } catch { |
| 54 | + // Not ready yet |
| 55 | + } |
| 56 | + await new Promise((r) => setTimeout(r, 500)); |
| 57 | + } |
| 58 | + throw new Error("Timeout waiting for database"); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Start PostgreSQL Docker container |
| 63 | + */ |
| 64 | +function startDatabase(): void { |
| 65 | + console.log("🐘 Starting test database..."); |
| 66 | + |
| 67 | + // Remove existing container if any |
| 68 | + try { |
| 69 | + execSync(`docker rm -f ${testEnv.DB_CONTAINER_NAME}`, { stdio: "ignore" }); |
| 70 | + } catch { |
| 71 | + // Container doesn't exist, that's fine |
| 72 | + } |
| 73 | + |
| 74 | + // Start new container |
| 75 | + execSync( |
| 76 | + `docker run -d --name ${testEnv.DB_CONTAINER_NAME} -p ${testEnv.DB_PORT}:5432 -e POSTGRES_PASSWORD=postgres postgres:16`, |
| 77 | + { stdio: "inherit" }, |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Run database migrations |
| 83 | + */ |
| 84 | +function runMigrations(): void { |
| 85 | + console.log("📦 Running migrations..."); |
| 86 | + execSync("bun db:migrate", { |
| 87 | + cwd: "apps/server", |
| 88 | + stdio: "inherit", |
| 89 | + env: getTestEnvVars(), |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Run database seed |
| 95 | + */ |
| 96 | +function runSeed(): void { |
| 97 | + console.log("🌱 Seeding database..."); |
| 98 | + execSync("bun db:seed", { |
| 99 | + stdio: "inherit", |
| 100 | + env: getTestEnvVars(), |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Start the API server |
| 106 | + */ |
| 107 | +function startServer(): ChildProcess { |
| 108 | + console.log("🦊 Starting server..."); |
| 109 | + const server = spawn("bun", ["run", "src/index.ts"], { |
| 110 | + cwd: "apps/server", |
| 111 | + stdio: "inherit", |
| 112 | + env: getTestEnvVars(), |
| 113 | + }); |
| 114 | + processes.push(server); |
| 115 | + return server; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Start the frontend dev server |
| 120 | + */ |
| 121 | +function startFrontend(): ChildProcess { |
| 122 | + console.log("🌐 Starting frontend..."); |
| 123 | + const frontend = spawn( |
| 124 | + "bun", |
| 125 | + ["vite", "dev", "--port", testEnv.FRONTEND_PORT], |
| 126 | + { |
| 127 | + cwd: "apps/desktop", |
| 128 | + stdio: "inherit", |
| 129 | + env: { |
| 130 | + ...process.env, |
| 131 | + PUBLIC_API_BASE_URL: testEnv.PUBLIC_API_BASE_URL, |
| 132 | + PUBLIC_DISABLE_AUTH: testEnv.PUBLIC_DISABLE_AUTH, |
| 133 | + }, |
| 134 | + }, |
| 135 | + ); |
| 136 | + processes.push(frontend); |
| 137 | + return frontend; |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Global setup for Playwright tests |
| 142 | + */ |
| 143 | +export default async function globalSetup(): Promise<void> { |
| 144 | + console.log("\n🚀 Starting E2E test environment...\n"); |
| 145 | + |
| 146 | + // 1. Start database |
| 147 | + startDatabase(); |
| 148 | + await waitForDb(); |
| 149 | + console.log("✅ Database ready"); |
| 150 | + |
| 151 | + // 2. Run migrations and seed |
| 152 | + runMigrations(); |
| 153 | + runSeed(); |
| 154 | + console.log("✅ Database seeded"); |
| 155 | + |
| 156 | + // 3. Start server |
| 157 | + startServer(); |
| 158 | + await waitForUrl(`${testEnv.SERVER_URL}/health`); |
| 159 | + console.log("✅ Server ready"); |
| 160 | + |
| 161 | + // 4. Start frontend |
| 162 | + startFrontend(); |
| 163 | + await waitForUrl(testEnv.FRONTEND_URL); |
| 164 | + console.log("✅ Frontend ready"); |
| 165 | + |
| 166 | + console.log("\n✨ E2E environment ready!\n"); |
| 167 | + |
| 168 | + // Store process info for teardown |
| 169 | + (globalThis as Record<string, unknown>).__E2E_PROCESSES__ = processes; |
| 170 | +} |
0 commit comments