import assert from 'node:assert/strict'; import { mkdtemp, readFile, rm } from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; import test from 'node:test'; import { platformPorts, selectServices, startServices, webListener } from './local-services.mjs'; const alive = pid => { try { process.kill(pid, 0); return true; } catch { return false; } }; async function gone(pid) { for (let attempt = 0; attempt < 40 && alive(pid); attempt++) await new Promise(resolve => setTimeout(resolve, 50)); return !alive(pid); } test('stopping takes down each service with everything it spawned, even a service ignoring SIGTERM', async t => { const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-services-')); t.after(() => rm(directory, { recursive: true, force: true })); const pids = path.join(directory, 'pids.json'); // A service that ignores SIGTERM and leaves a child of its own running, like a dev server's workers. const service = ` const { spawn } = require('node:child_process'); process.on('SIGTERM', () => {}); const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' }); require('node:fs').writeFileSync(${JSON.stringify(pids)}, JSON.stringify([process.pid, child.pid])); setInterval(() => {}, 1000); `; const services = await startServices( [{ name: 'stubborn', cwd: directory, args: ['-e', service], env: process.env }], '', { graceMs: 300, }, ); const [leader, grandchild] = JSON.parse(await readFile(pids, 'utf8')); assert.ok(alive(leader) && alive(grandchild)); await services.stop(); assert.ok(await gone(leader), 'the service itself stopped'); assert.ok(await gone(grandchild), 'the process it spawned stopped too'); process.exitCode = undefined; }); test('under the supervisor, a service already answering is refused instead of adopted', async t => { const original = globalThis.fetch; t.after(() => { globalThis.fetch = original; process.exitCode = undefined; }); globalThis.fetch = async () => Response.json({ service: 'wishforge-control-plane' }); const api = { name: 'api', url: 'http://127.0.0.1:4000/health', identity: 'wishforge-control-plane', args: [] }; await assert.rejects(startServices([api], '', { reuse: false }), /already answers; stop that process/); // A developer's own start may still reuse it. await startServices([api], ''); }); test('only the production web build listens beyond loopback, and readiness is checked where it listens', () => { assert.deepEqual(webListener({}), { host: '127.0.0.1', port: 3000, probe: '127.0.0.1' }); const lan = { WF_LOCAL_ACCESS: 'true', WF_WEB_SERVE_MODE: 'production' }; assert.deepEqual(webListener({ ...lan, WF_WEB_HOST: '0.0.0.0', WF_WEB_PORT: '3100' }), { host: '0.0.0.0', port: 3100, probe: '127.0.0.1', }); // A listener bound to one address answers only there. assert.equal(webListener({ ...lan, WF_WEB_HOST: '192.168.1.20' }).probe, '192.168.1.20'); assert.throws(() => webListener({ WF_WEB_HOST: '0.0.0.0', WF_WEB_SERVE_MODE: 'production' }), /local sign-in/); // The development server hands visitors its sources and hot-reload channel. assert.throws(() => webListener({ WF_WEB_HOST: '0.0.0.0', WF_LOCAL_ACCESS: 'true' }), /production web build/); assert.equal(webListener({ WF_WEB_HOST: 'localhost', WF_WEB_SERVE_MODE: 'development' }).host, 'localhost'); assert.throws(() => webListener({ WF_WEB_PORT: '70000' }), /TCP port/); }); test("the platform's own ports come from its configured addresses", () => { assert.deepEqual( platformPorts({ WF_API_URL: 'http://127.0.0.1:4000', DATABASE_URL: 'postgresql://user:password@127.0.0.1:55432/wishforge', TEMPORAL_ADDRESS: '127.0.0.1:7233', S3_ENDPOINT: 'http://127.0.0.1:49000', WF_PROVISIONER_URL: 'http://127.0.0.1:4450', WF_HOST_CHECK_BLOCKED_PORTS: '6379', WF_GIT_SERVICE_URL: 'not an address', }), [3000, 4000, 4450, 4504, 6379, 7233, 8233, 49000, 55432], ); }); test('starts every service, or only those a container is given, in their order', () => { const services = ['git', 'api', 'provisioner', 'coordinator', 'deployer', 'web'].map(name => ({ name })); assert.equal(selectServices(services, {}), services); assert.deepEqual( selectServices(services, { WF_LOCAL_SERVICES: 'web, api ,git' }).map(service => service.name), ['git', 'api', 'web'], ); assert.deepEqual( selectServices(services, { WF_LOCAL_SERVICES: 'provisioner' }).map(service => service.name), ['provisioner'], ); assert.throws(() => selectServices(services, { WF_LOCAL_SERVICES: 'api,database' }), /unknown services: database/); });