|
| 1 | +import { type ChildProcess, spawn } from 'node:child_process' |
| 2 | +import { expect, test } from '@playwright/test' |
| 3 | +import { omit } from '@sim/utils/object' |
| 4 | + |
| 5 | +/** |
| 6 | + * Starts the zone's real production build as its own process, the way a container does, under the |
| 7 | + * base path it was built with. `instrumentation.ts` runs `assertDeployableMode`, and Next exits the |
| 8 | + * server when that throws. A second zone, because the first must keep serving the suite before this. |
| 9 | + */ |
| 10 | +const PORT = 3035 |
| 11 | +const ORIGIN = `http://127.0.0.1:${PORT}` |
| 12 | + |
| 13 | +let running: ChildProcess | undefined |
| 14 | + |
| 15 | +function startZone(env: Record<string, string>) { |
| 16 | + const output: string[] = [] |
| 17 | + const zone = spawn( |
| 18 | + 'node', |
| 19 | + [ |
| 20 | + '../../node_modules/next/dist/bin/next', |
| 21 | + 'start', |
| 22 | + 'platform', |
| 23 | + '--hostname', |
| 24 | + '127.0.0.1', |
| 25 | + '--port', |
| 26 | + String(PORT), |
| 27 | + ], |
| 28 | + { |
| 29 | + env: { |
| 30 | + ...omit(process.env, ['SIM_PLATFORM_API_ORIGIN', 'SIM_PLATFORM_FIXTURES', 'NODE_ENV']), |
| 31 | + SIM_PLATFORM_BASE_PATH: '/org', |
| 32 | + ...env, |
| 33 | + }, |
| 34 | + } |
| 35 | + ) |
| 36 | + running = zone |
| 37 | + zone.stdout.on('data', (chunk) => output.push(String(chunk))) |
| 38 | + zone.stderr.on('data', (chunk) => output.push(String(chunk))) |
| 39 | + const exitCode = new Promise<number | null>((resolve) => { |
| 40 | + zone.once('exit', (code) => resolve(code)) |
| 41 | + }) |
| 42 | + return { exitCode, output } |
| 43 | +} |
| 44 | + |
| 45 | +test.afterEach(async () => { |
| 46 | + const zone = running |
| 47 | + running = undefined |
| 48 | + if (!zone || zone.exitCode !== null) return |
| 49 | + const exited = new Promise((resolve) => zone.once('exit', resolve)) |
| 50 | + zone.kill('SIGTERM') |
| 51 | + await exited |
| 52 | +}) |
| 53 | + |
| 54 | +test('a production zone with no API origin refuses to start', async () => { |
| 55 | + const { exitCode, output } = startZone({}) |
| 56 | + expect(await exitCode).toBe(1) |
| 57 | + expect(output.join('')).toContain('its data mode is "none"') |
| 58 | +}) |
| 59 | + |
| 60 | +test('a production zone on fixtures refuses to start', async () => { |
| 61 | + const { exitCode, output } = startZone({ |
| 62 | + SIM_PLATFORM_API_ORIGIN: 'http://127.0.0.1:3027', |
| 63 | + SIM_PLATFORM_FIXTURES: '1', |
| 64 | + }) |
| 65 | + expect(await exitCode).toBe(1) |
| 66 | + expect(output.join('')).toContain('its data mode is "fixture"') |
| 67 | +}) |
| 68 | + |
| 69 | +test('a production zone with an API origin starts under the same command', async () => { |
| 70 | + const { output } = startZone({ SIM_PLATFORM_API_ORIGIN: 'http://127.0.0.1:3027' }) |
| 71 | + await expect |
| 72 | + .poll( |
| 73 | + async () => { |
| 74 | + try { |
| 75 | + // No route answers this, so the zone renders its own not-found page. |
| 76 | + return (await fetch(`${ORIGIN}/org/__availability`)).status |
| 77 | + } catch { |
| 78 | + return 'not listening yet' |
| 79 | + } |
| 80 | + }, |
| 81 | + { timeout: 60_000 } |
| 82 | + ) |
| 83 | + .toBe(404) |
| 84 | + expect(output.join('')).not.toContain('its data mode is') |
| 85 | +}) |
0 commit comments