|
| 1 | +import { spawn } from "child_process"; |
| 2 | +import { createServer } from "net"; |
| 3 | +import { resolve } from "path"; |
| 4 | +import { Network } from "testcontainers"; |
| 5 | +import { PrismaClient } from "@trigger.dev/database"; |
| 6 | +import { createPostgresContainer } from "./utils"; |
| 7 | + |
| 8 | +const WEBAPP_ROOT = resolve(__dirname, "../../../apps/webapp"); |
| 9 | +// pnpm hoists transitive deps to node_modules/.pnpm/node_modules but does NOT symlink them |
| 10 | +// to the root node_modules. We need NODE_PATH so the webapp process can find them at runtime. |
| 11 | +const PNPM_HOISTED_MODULES = resolve(__dirname, "../../../node_modules/.pnpm/node_modules"); |
| 12 | + |
| 13 | +async function findFreePort(): Promise<number> { |
| 14 | + return new Promise((res, rej) => { |
| 15 | + const srv = createServer(); |
| 16 | + srv.listen(0, () => { |
| 17 | + const port = (srv.address() as { port: number }).port; |
| 18 | + srv.close((err) => (err ? rej(err) : res(port))); |
| 19 | + }); |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +async function waitForHealthcheck(url: string, timeoutMs = 60000): Promise<void> { |
| 24 | + const deadline = Date.now() + timeoutMs; |
| 25 | + while (Date.now() < deadline) { |
| 26 | + try { |
| 27 | + const res = await fetch(url); |
| 28 | + if (res.ok) return; |
| 29 | + } catch {} |
| 30 | + await new Promise((r) => setTimeout(r, 500)); |
| 31 | + } |
| 32 | + throw new Error(`Webapp did not become healthy at ${url} within ${timeoutMs}ms`); |
| 33 | +} |
| 34 | + |
| 35 | +export interface WebappInstance { |
| 36 | + baseUrl: string; |
| 37 | + fetch(path: string, init?: RequestInit): Promise<Response>; |
| 38 | +} |
| 39 | + |
| 40 | +export async function startWebapp(databaseUrl: string): Promise<{ |
| 41 | + instance: WebappInstance; |
| 42 | + stop: () => Promise<void>; |
| 43 | +}> { |
| 44 | + const port = await findFreePort(); |
| 45 | + |
| 46 | + // Merge NODE_PATH so transitive pnpm deps (hoisted to .pnpm/node_modules) are resolvable |
| 47 | + const existingNodePath = process.env.NODE_PATH; |
| 48 | + const nodePath = existingNodePath |
| 49 | + ? `${PNPM_HOISTED_MODULES}:${existingNodePath}` |
| 50 | + : PNPM_HOISTED_MODULES; |
| 51 | + |
| 52 | + const proc = spawn(process.execPath, ["build/server.js"], { |
| 53 | + cwd: WEBAPP_ROOT, |
| 54 | + env: { |
| 55 | + ...process.env, |
| 56 | + NODE_ENV: "test", |
| 57 | + DATABASE_URL: databaseUrl, |
| 58 | + DIRECT_URL: databaseUrl, |
| 59 | + PORT: String(port), |
| 60 | + REMIX_APP_PORT: String(port), // override .env file value (vitest loads .env via Vite) |
| 61 | + SESSION_SECRET: "test-session-secret-for-e2e-tests", |
| 62 | + MAGIC_LINK_SECRET: "test-magic-link-secret-32chars!!", |
| 63 | + ENCRYPTION_KEY: "test-encryption-key-for-e2e!!!!!", // exactly 32 bytes |
| 64 | + CLICKHOUSE_URL: "http://localhost:19123", // dummy, auth paths never connect |
| 65 | + DEPLOY_REGISTRY_HOST: "registry.example.com", // dummy, not needed for auth tests |
| 66 | + ELECTRIC_ORIGIN: "http://localhost:3060", |
| 67 | + NODE_PATH: nodePath, |
| 68 | + }, |
| 69 | + stdio: ["ignore", "pipe", "pipe"], |
| 70 | + }); |
| 71 | + |
| 72 | + const stderr: string[] = []; |
| 73 | + proc.stderr?.on("data", (d: Buffer) => { |
| 74 | + const line = d.toString(); |
| 75 | + stderr.push(line); |
| 76 | + if (process.env.WEBAPP_TEST_VERBOSE) { |
| 77 | + process.stderr.write(line); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + const stdout: string[] = []; |
| 82 | + proc.stdout?.on("data", (d: Buffer) => { |
| 83 | + const line = d.toString(); |
| 84 | + stdout.push(line); |
| 85 | + if (process.env.WEBAPP_TEST_VERBOSE) { |
| 86 | + process.stdout.write(line); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + proc.on("error", (err) => { |
| 91 | + throw new Error(`Failed to start webapp: ${err.message}`); |
| 92 | + }); |
| 93 | + |
| 94 | + const baseUrl = `http://localhost:${port}`; |
| 95 | + |
| 96 | + try { |
| 97 | + await waitForHealthcheck(`${baseUrl}/healthcheck`); |
| 98 | + } catch (err) { |
| 99 | + proc.kill("SIGTERM"); |
| 100 | + const output = [...stdout, ...stderr].join("\n"); |
| 101 | + throw new Error(`Webapp failed to start.\nOutput:\n${output}\n\nOriginal error: ${err}`); |
| 102 | + } |
| 103 | + |
| 104 | + return { |
| 105 | + instance: { |
| 106 | + baseUrl, |
| 107 | + fetch: (path: string, init?: RequestInit) => fetch(`${baseUrl}${path}`, init), |
| 108 | + }, |
| 109 | + stop: () => |
| 110 | + new Promise<void>((res) => { |
| 111 | + const timer = setTimeout(() => { |
| 112 | + proc.kill("SIGKILL"); |
| 113 | + res(); |
| 114 | + }, 10_000); |
| 115 | + proc.once("exit", () => { |
| 116 | + clearTimeout(timer); |
| 117 | + res(); |
| 118 | + }); |
| 119 | + proc.kill("SIGTERM"); |
| 120 | + }), |
| 121 | + }; |
| 122 | +} |
| 123 | + |
| 124 | +export interface TestServer { |
| 125 | + webapp: WebappInstance; |
| 126 | + prisma: PrismaClient; |
| 127 | + stop: () => Promise<void>; |
| 128 | +} |
| 129 | + |
| 130 | +/** Convenience helper: starts a postgres container + webapp and returns both for testing. */ |
| 131 | +export async function startTestServer(): Promise<TestServer> { |
| 132 | + const network = await new Network().start(); |
| 133 | + const { url: databaseUrl, container } = await createPostgresContainer(network); |
| 134 | + |
| 135 | + const prisma = new PrismaClient({ datasources: { db: { url: databaseUrl } } }); |
| 136 | + const { instance: webapp, stop: stopWebapp } = await startWebapp(databaseUrl); |
| 137 | + |
| 138 | + const stop = async () => { |
| 139 | + await stopWebapp(); |
| 140 | + await prisma.$disconnect(); |
| 141 | + await container.stop(); |
| 142 | + await network.stop(); |
| 143 | + }; |
| 144 | + |
| 145 | + return { webapp, prisma, stop }; |
| 146 | +} |
0 commit comments