import { test } from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { mkdtempSync, rmSync } from 'node:fs'; import { get } from 'node:http'; import { join } from 'node:path'; import { reserveOrg, activateOrg } from '../../business/src/org-registry.mjs'; import { humanEnv } from './helpers.mjs'; import { consoleStart, consoleStop } from '../src/org-console.mjs'; const REPO = new URL('../../../', import.meta.url).pathname; test('detached console reports its live loopback URL and stops', { timeout: 60000 }, async t => { const home = mkdtempSync('/mnt/storage/scratch/tmp/s1-'); const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-s1test/config.json') }); const setup = spawnSync(join(REPO, 'scripts/bootstrap.sh'), ['--instance', 's1test'], { env, encoding: 'utf8' }); assert.equal(setup.status, 0, setup.stderr); const ctx = { configDir: join(home, '.config/mosaic-s1test'), dataRoot: join(home, '.mosaic-s1test'), instance: 's1test', env }; await reserveOrg(ctx, 'alpha'); await activateOrg(ctx, 'alpha'); t.after(async () => { try { await consoleStop(ctx); } catch {} rmSync(home, { recursive: true, force: true }); }); const result = await consoleStart(ctx, { devLocalOwner: true, detach: true, port: 'auto' }); const url = new URL(result.url); assert.equal(url.hostname, '127.0.0.1'); assert.ok(Number.isSafeInteger(result.pid)); assert.ok(![7332, 7340, 7341, 33223, 17450, 17443, 44307].includes(Number(url.port))); const status = await new Promise((resolve, reject) => get(url, res => { res.resume(); resolve(res.statusCode); }).on('error', reject)); assert.equal(status, 200); assert.equal((await consoleStop(ctx)).stopped, true); }); test('mosaic console start and stop dispatch through the CLI', { timeout: 60000 }, async t => { const home = mkdtempSync('/mnt/storage/scratch/tmp/s1-'); const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-s1test/config.json') }); assert.equal(spawnSync(join(REPO, 'scripts/bootstrap.sh'), ['--instance', 's1test'], { env }).status, 0); const ctx = { configDir: join(home, '.config/mosaic-s1test'), dataRoot: join(home, '.mosaic-s1test'), instance: 's1test', env }; await reserveOrg(ctx, 'alpha'); await activateOrg(ctx, 'alpha'); t.after(async () => { try { await consoleStop(ctx); } catch {} rmSync(home, { recursive: true, force: true }); }); const start = spawnSync(join(REPO, 'scripts/mosaic'), ['console', 'start', '--dev-local-owner', '--detach', '--port', 'auto'], { env, encoding: 'utf8', timeout: 30000 }); assert.equal(start.status, 0, start.stderr); assert.match(JSON.parse(start.stdout).url, /^http:\/\/127\.0\.0\.1:\d+\/$/); const stop = spawnSync(join(REPO, 'scripts/mosaic'), ['console', 'stop'], { env, encoding: 'utf8', timeout: 30000 }); assert.equal(stop.status, 0, stop.stderr); assert.equal(JSON.parse(stop.stdout).stopped, true); });