|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Hand-rolled global setup for the ONLY cockpit cap whose backend is not |
| 3 | +// Python: the rt-mastra topic is served by the deployments/ag-ui-mastra |
| 4 | +// Node service. This mirrors createAgUiGlobalSetup (libs/e2e-harness) — |
| 5 | +// aimock + backend + Angular dev server, state registered in |
| 6 | +// __AIMOCK_HARNESS_STATE__ so the shared global-teardown cleans up — but |
| 7 | +// spawns `node server.mjs` instead of `uv run uvicorn`, with the model |
| 8 | +// redirected to aimock via OPENAI_BASE_URL (Mastra's model router honors it, |
| 9 | +// and calls the OpenAI responses API, which aimock speaks). |
| 10 | +import { execSync, spawn, type ChildProcess } from 'node:child_process'; |
| 11 | +import { mkdtempSync, existsSync } from 'node:fs'; |
| 12 | +import { tmpdir } from 'node:os'; |
| 13 | +import { join, resolve } from 'node:path'; |
| 14 | +import { setTimeout as delay } from 'node:timers/promises'; |
| 15 | +import { portsFor } from '../../../../../cockpit/ports.mjs'; |
| 16 | +import { startAimock, type AimockHandle } from '@threadplane-internal/e2e-harness'; |
| 17 | + |
| 18 | +const ports = portsFor('cockpit-runtimes-mastra-angular'); |
| 19 | +const angularProject = 'cockpit-runtimes-mastra-angular'; |
| 20 | +// Parsed by apps/cockpit/cockpit-e2e-wiring.spec.ts (accepts backendCwd for |
| 21 | +// Node-hosted backends alongside langgraphCwd/pythonCwd — keep the |
| 22 | +// `backendCwd: '<path>'` literal shape). |
| 23 | +const wiring = { backendCwd: 'deployments/ag-ui-mastra' }; |
| 24 | +const backendCwd = wiring.backendCwd; |
| 25 | +const backendPort = ports.langgraph; // "backend port" by the ag-ui convention |
| 26 | +const angularPort = ports.angular; |
| 27 | +const INTERNAL_TOKEN = 'dev-local-token'; // proxy.conf.mjs injects the same default |
| 28 | + |
| 29 | +interface SharedState { |
| 30 | + aimock: AimockHandle; |
| 31 | + backend?: ChildProcess; |
| 32 | + backendPort?: number; |
| 33 | + angular: ChildProcess; |
| 34 | + angularPort: number; |
| 35 | +} |
| 36 | + |
| 37 | +declare global { |
| 38 | + // eslint-disable-next-line no-var |
| 39 | + var __AIMOCK_HARNESS_STATE__: Map<string, SharedState> | undefined; |
| 40 | +} |
| 41 | + |
| 42 | +async function waitForPort(url: string, timeoutMs: number, label: string): Promise<void> { |
| 43 | + const start = Date.now(); |
| 44 | + while (Date.now() - start < timeoutMs) { |
| 45 | + try { |
| 46 | + const res = await fetch(url); |
| 47 | + if (res.ok || res.status === 404) return; |
| 48 | + } catch { |
| 49 | + // not up yet |
| 50 | + } |
| 51 | + await delay(500); |
| 52 | + } |
| 53 | + throw new Error(`[${label}] not ready at ${url} within ${timeoutMs}ms`); |
| 54 | +} |
| 55 | + |
| 56 | +export default async function globalSetup(): Promise<void> { |
| 57 | + const root = resolve(__dirname, '../../../../..'); |
| 58 | + const fixturesDir = resolve(__dirname, 'fixtures'); |
| 59 | + const serviceDir = resolve(root, backendCwd); |
| 60 | + |
| 61 | + const aimock = await startAimock({ mode: 'replay', fixturePath: fixturesDir }); |
| 62 | + console.log(`[mastra-harness] aimock listening at ${aimock.baseUrl}`); |
| 63 | + |
| 64 | + // The service is self-contained (own package.json + lockfile, deps NOT in |
| 65 | + // the root workspace). Install once per checkout; CI runners start clean. |
| 66 | + if (!existsSync(join(serviceDir, 'node_modules'))) { |
| 67 | + console.log('[mastra-harness] npm ci in deployments/ag-ui-mastra'); |
| 68 | + execSync('npm ci --no-audit --no-fund', { cwd: serviceDir, stdio: 'inherit' }); |
| 69 | + } |
| 70 | + |
| 71 | + // Fresh LibSQL file per run: suspended-run snapshots and memory from a |
| 72 | + // previous e2e run must not leak into this one. |
| 73 | + const dbDir = mkdtempSync(join(tmpdir(), 'ag-ui-mastra-e2e-')); |
| 74 | + |
| 75 | + const backend = spawn('node', ['server.mjs'], { |
| 76 | + cwd: serviceDir, |
| 77 | + env: { |
| 78 | + ...process.env, |
| 79 | + PORT: String(backendPort), |
| 80 | + AG_UI_INTERNAL_TOKEN: INTERNAL_TOKEN, |
| 81 | + OPENAI_API_KEY: 'test-not-used', |
| 82 | + OPENAI_BASE_URL: aimock.baseUrl, |
| 83 | + AG_UI_MASTRA_DB_PATH: join(dbDir, 'mastra.db'), |
| 84 | + }, |
| 85 | + stdio: 'pipe', |
| 86 | + // Own process group so the shared teardown can kill the whole tree. |
| 87 | + detached: true, |
| 88 | + }); |
| 89 | + backend.stdout?.on('data', (b) => process.stdout.write(`[ag-ui-mastra] ${b}`)); |
| 90 | + backend.stderr?.on('data', (b) => process.stderr.write(`[ag-ui-mastra] ${b}`)); |
| 91 | + |
| 92 | + await waitForPort(`http://localhost:${backendPort}/ok`, 90_000, 'ag-ui-mastra'); |
| 93 | + console.log(`[mastra-harness] backend ready on :${backendPort}`); |
| 94 | + |
| 95 | + const angular = spawn( |
| 96 | + 'npx', |
| 97 | + ['nx', 'serve', angularProject, '--port', String(angularPort)], |
| 98 | + { |
| 99 | + cwd: root, |
| 100 | + env: { ...process.env, AG_UI_INTERNAL_TOKEN: INTERNAL_TOKEN }, |
| 101 | + stdio: 'pipe', |
| 102 | + detached: true, |
| 103 | + }, |
| 104 | + ); |
| 105 | + angular.stdout?.on('data', (b) => process.stdout.write(`[angular] ${b}`)); |
| 106 | + angular.stderr?.on('data', (b) => process.stderr.write(`[angular] ${b}`)); |
| 107 | + |
| 108 | + await waitForPort(`http://localhost:${angularPort}/`, 120_000, 'angular'); |
| 109 | + console.log(`[mastra-harness] angular ready on :${angularPort}`); |
| 110 | + |
| 111 | + if (!globalThis.__AIMOCK_HARNESS_STATE__) { |
| 112 | + globalThis.__AIMOCK_HARNESS_STATE__ = new Map(); |
| 113 | + } |
| 114 | + globalThis.__AIMOCK_HARNESS_STATE__.set(angularProject, { |
| 115 | + aimock, |
| 116 | + backend, |
| 117 | + backendPort, |
| 118 | + angular, |
| 119 | + angularPort, |
| 120 | + }); |
| 121 | +} |
0 commit comments