serve --tester-port N starts a second listener on 127.0.0.1:N in the same process, with a bus caller through TESTER_CLI. N is 1024..65535, not --port and never 7332, and the flag needs the bus configured. The tester listener takes only instances.set, routes.set and name.set; settings save, project-file save, assistant stop, bus message and chat writes refuse 403 with no bus call. It refuses the main listener's auth header, and every other listener refuses any request carrying x-mosaic-tester. /api/config answers tester: true and the page shows a tester banner with the routes, instances and seat name editors only. tester-off and tester-required map to 403 in a serve.mjs coreBusStatus wrapper, so bus.mjs stays pinned (row 164). The broker lets a tester read credentials (with #secretCheck), routes, project.files and project.file; project.file.save stays human-only. webui-service.sh takes --tester-port with serve's checks. stack-release activate --tester-port N writes the flag to the pointer and the unit, refuses tester-port-target when the target copy's Console lacks it, and activate or rollback to such a copy drops the flag and says so in --plan. docs/TOOLS.md rewords the stale row 175 two-phase step (#1673 F1). Built by Rocko on473258a51(manifest a4c728fc...52c9), approved by Dewey in round 2 (#1674 comment 29322) after round 1 changes (29309), Sage's gate green on473258a51plus the patch in run 2. Run 1 failed T34's failed-pm_launched subtest with capture-failed: activate and pm-on name the activation by the second, so the two ran in one second and collided on the rollback set. That race is row 169's (the base fails the same way with the id pinned), and a follow-up row fixes it (Vikunja P19 391).
60 lines
4.5 KiB
JavaScript
60 lines
4.5 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { createServer } from 'node:net';
|
|
import { spawn, spawnSync } from 'node:child_process';
|
|
import { once } from 'node:events';
|
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { tmp, fixture } from '../../cli/tests/helpers.mjs';
|
|
import { TESTER_CLI, HUMAN_CLI } from '../../cli/src/transport.mjs';
|
|
import { testerFixture } from './tester-fixture.mjs';
|
|
import { request } from 'node:http';
|
|
const CLI = new URL('../src/cli.mjs', import.meta.url).pathname;
|
|
const get = (port, path) => new Promise((resolve, reject) => {
|
|
const q = request({ host: '127.0.0.1', port, path, headers: { host: `127.0.0.1:${port}` } }, r => {
|
|
let text = ''; r.on('data', c => text += c); r.on('end', () => resolve({ status: r.statusCode, body: text }));
|
|
}); q.on('error', reject); q.end();
|
|
});
|
|
async function freePort() {
|
|
const s = createServer(); s.listen(0, '127.0.0.1'); await once(s, 'listening');
|
|
const port = s.address().port; await new Promise(r => s.close(r)); return port;
|
|
}
|
|
|
|
test('tester-port validates range tunnel main port and requires configured bus before listening', t => {
|
|
const root = tmp(t, 'tester-cli-invalid-'), f = fixture(root);
|
|
for (const flags of [['--tester-port', '7332'], ['--port', '7444', '--tester-port', '7444'], ['--tester-port', '7444', '--port', '7444'], ...['1023', '65536', '1.5', '-1', 'nope'].map(p => ['--tester-port', p])]) {
|
|
const r = spawnSync(process.execPath, [CLI, 'serve', ...flags], { encoding: 'utf8', env: { ...f.env, MOSAIC_CONFIG: join(root, 'missing.json') }, timeout: 5000 });
|
|
assert.equal(r.status, 2); assert.match(r.stderr, /tester.port.*(1024|7332|differ|whole)/i);
|
|
}
|
|
const r = spawnSync(process.execPath, [CLI, 'serve', '--port', '0', '--tester-port', '7333'], { encoding: 'utf8', env: { ...f.env, MOSAIC_CONFIG: join(root, 'missing.json') }, timeout: 5000 });
|
|
assert.equal(r.status, 2); assert.match(r.stderr, /tester-port needs the bus configured/);
|
|
});
|
|
|
|
test('CLI tester listener binds loopback and spawns TESTER_CLI for every Console bus read', { timeout: 30000 }, async t => {
|
|
const f = await testerFixture(t), port = await freePort(), log = join(f.root, 'spawn.jsonl');
|
|
const hook = join(f.root, 'spawn-hook.mjs');
|
|
writeFileSync(log, '');
|
|
writeFileSync(hook, `import cp from 'node:child_process';import {syncBuiltinESMExports} from 'node:module';import {appendFileSync} from 'node:fs';const original=cp.spawn;cp.spawn=function(cmd,args,options){if(Array.isArray(args)&&args[0]?.endsWith('-cli.mjs'))appendFileSync(${JSON.stringify(log)},JSON.stringify({cli:args[0]})+'\\n');return original.call(this,cmd,args,options);};syncBuiltinESMExports();`);
|
|
const child = spawn(process.execPath, [CLI, 'serve', '--board', 'http://127.0.0.1:9', '--port', '0', '--tester-port', String(port), '--business', 'acme', '--public-host', 'console.example', '--auth-header', 'x-user', '--allow-user', 'jason'], { env: { ...f.f.env, NODE_OPTIONS: `--import=${hook}` }, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
let output = '', error = '';
|
|
child.stdout.on('data', c => output += c); child.stderr.on('data', c => error += c);
|
|
t.after(async () => { if (child.exitCode === null) { child.kill('SIGTERM'); await once(child, 'close'); } });
|
|
await new Promise((resolve, reject) => {
|
|
const timer = setTimeout(() => reject(Error('startup did not name tester: ' + output + error)), 5000);
|
|
const check = () => { if (output.includes(`Tester Console: http://127.0.0.1:${port}/`)) { clearTimeout(timer); resolve(); } };
|
|
child.stdout.on('data', check); child.once('exit', code => { clearTimeout(timer); reject(Error('CLI exit ' + code + ': ' + output + error)); }); check();
|
|
});
|
|
assert.match(output, /binds (the )?tester/);
|
|
const paths = ['inbox', 'tasks', 'agents', 'trail?subject=coder', 'decisions?list=roles', 'credentials', 'replies?id=absent', 'messages', 'usage', 'limits', 'names', 'instances', 'routes'].map(p => '/api/bus/' + p);
|
|
paths.push('/api/project-files?project=stack', '/api/project-file?project=stack&path=spec%2Fnorth-star.md');
|
|
for (const path of paths) {
|
|
const before = readFileSync(log, 'utf8').trim().split('\n').filter(Boolean).length;
|
|
const r = await get(port, path);
|
|
const calls = readFileSync(log, 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse);
|
|
assert.equal(calls.length, before + 1, path);
|
|
assert.equal(calls.at(-1).cli, TESTER_CLI, path);
|
|
assert.equal(calls.some(c => c.cli === HUMAN_CLI), false);
|
|
assert.ok(![401, 403, 500, 502, 503].includes(r.status), path + ': ' + r.body);
|
|
}
|
|
});
|