The Console builds the board context itself (control-board createBoard) and answers the board routes in-process. The standalone control-board HTTP writer and page.html are removed; control-board serve exits 2 as retired, and webui --board is refused. /api/seen and /api/reply need --send and a named human, else 403 read-only. Tests are rewritten against the in-process board (inProcessBoard in the webui fixture; createBoard in control-board). Proxy, unreachable-board and HTTP-only tests are dropped; the dropped set is listed in BUILD.md. UNVERIFIED: committed under Sage's slot hold (Gander run 5). No suite or mutant has run on this commit; do not push before the gate is green.
92 lines
5.5 KiB
JavaScript
92 lines
5.5 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { get } from 'node:http';
|
|
import { spawnSync } from 'node:child_process';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { startServer, isLoopback } from '../src/serve.mjs';
|
|
import { fixture, close } from './fixture.mjs';
|
|
|
|
const post = (base, path, body, extra = {}) => fetch(base + path, { method: 'POST', headers: { 'content-type': 'application/json', ...extra }, body: typeof body === 'string' ? body : JSON.stringify(body) });
|
|
|
|
test('loopback host fails closed', async () => {
|
|
for (const h of ['127.0.0.1', '127.2.3.4', '::1', 'localhost']) assert.equal(isLoopback(h), true);
|
|
for (const h of ['0.0.0.0', '192.168.1.1', '::', 'example.com', '']) {
|
|
assert.equal(isLoopback(h), false);
|
|
await assert.rejects(startServer({ host: h, port: 0 }), /non-loopback/);
|
|
}
|
|
});
|
|
|
|
test('real board fixture passes through WebUI; assets and isolated seen/reply work', async () => {
|
|
const f = await fixture();
|
|
try {
|
|
assert.equal(f.web.address().address, '127.0.0.1');
|
|
const data = await (await fetch(f.base + '/api/board')).json();
|
|
assert.equal(data.sessions.length, 4);
|
|
assert.equal(data.sessions.filter(r => r.waitingOnYou).length, 2);
|
|
assert.deepEqual(data.counts, { working: 1, waiting: 1, error: 1, offline: 1, idle: 0, unknown: 0 });
|
|
const r = data.sessions.find(r => r.agent === 'agent1');
|
|
const seen = await post(f.base, '/api/seen', { project: r.project, agent: r.agent, lastActivity: r.lastActivity, seen: true });
|
|
assert.equal(seen.status, 200); assert.equal((await seen.json()).seen.length, 1);
|
|
const reply = await post(f.base, '/api/reply', { agent: 'proj/agent1', text: 'fixture reply' });
|
|
assert.equal((await reply.json()).delivered, true); assert.equal(f.captures.length, 1);
|
|
assert.match(f.captures[0].args.at(-1), /^fixture reply\n\(control-board:/);
|
|
for (const path of ['/', '/app.js', '/brand.js', '/console.css', '/live.css', '/editor.js', '/editor.css', '/shared/app.css', '/assets/fonts/manrope-400.woff2']) {
|
|
const response = await fetch(f.base + path); assert.equal(response.status, 200, path); assert.equal(response.headers.get('cache-control'), 'no-store');
|
|
}
|
|
// #1542: the shell's tokens, sprite, stylesheet and mono fonts, each with its type.
|
|
for (const [path, type] of [['/tokens.css', 'text/css'], ['/shell.css', 'text/css'], ['/icons.svg', 'image/svg+xml'], ...[400, 500, 700].map(w => [`/assets/fonts/jetbrains-mono-${w}.woff2`, 'font/woff2'])]) {
|
|
const response = await fetch(f.base + path); assert.equal(response.status, 200, path); assert.ok(response.headers.get('content-type').startsWith(type), path);
|
|
}
|
|
assert.equal((await fetch(f.base + '/../../AGENTS.md')).status, 404);
|
|
assert.equal((await fetch(f.base + '/api/reply')).status, 405);
|
|
assert.equal((await fetch(f.base + '/api/board', { method: 'OPTIONS' })).status, 405);
|
|
assert.equal((await post(f.base, '/api/reply', { agent: 'proj/agent1', text: 'blocked' }, { origin: 'https://evil.example' })).status, 403);
|
|
assert.equal(f.captures.length, 1);
|
|
const hostileHost = await new Promise((resolve, reject) => {
|
|
get(f.base + '/api/board', { headers: { host: 'evil.example' } }, res => { res.resume(); resolve(res.statusCode); }).on('error', reject);
|
|
});
|
|
assert.equal(hostileHost, 403);
|
|
} finally { await f.close(); }
|
|
});
|
|
|
|
test('in-process board routes: writes refused without --send, reads pass the board result through, a failing read answers an error', async () => {
|
|
const f = await fixture();
|
|
const web = await startServer({ port: 0, board: f.board });
|
|
const base = `http://127.0.0.1:${web.address().port}`;
|
|
try {
|
|
// Without --send both writes refuse before the board runs.
|
|
for (const path of ['/api/seen', '/api/reply']) {
|
|
const res = await post(base, path, { agent: 'proj/agent1', text: 'blocked', seen: true });
|
|
assert.equal(res.status, 403, path); assert.equal((await res.json()).sent, false);
|
|
}
|
|
assert.equal(f.captures.length, 0, 'the board never ran a write');
|
|
assert.equal((await fetch(base + '/api/reply')).status, 405);
|
|
assert.equal((await fetch(base + '/api/board', { method: 'OPTIONS' })).status, 405);
|
|
// Reads: the board's own status and body come back as the board sent them.
|
|
for (const path of ['/api/conversations', '/api/conversation?id=x']) {
|
|
const res = await fetch(base + path);
|
|
const want = await f.board.conversation(new URL(path, 'http://localhost'));
|
|
assert.equal(res.status, want.status, path);
|
|
assert.deepEqual(await res.json(), want.body, path);
|
|
}
|
|
// A failing board read is an error answer, not a crash.
|
|
f.board.setDown(true);
|
|
assert.ok((await fetch(base + '/api/board')).status >= 400);
|
|
} finally { await close(web); await f.close(); }
|
|
});
|
|
|
|
test('no board configured: the board routes answer 503; CLI rejects retired --board and other bad options', async () => {
|
|
const web = await startServer({ port: 0, board: null });
|
|
try {
|
|
const base = `http://127.0.0.1:${web.address().port}`;
|
|
assert.equal((await fetch(base + '/api/board')).status, 503);
|
|
assert.equal((await fetch(base + '/api/conversations')).status, 503);
|
|
} finally { await close(web); }
|
|
for (const args of [[], ['serve', '--port', 'abc'], ['serve', '--host', '0.0.0.0'], ['serve', '--board', 'http://example.com']]) {
|
|
const result = spawnSync(process.execPath, [fileURLToPath(new URL('../src/cli.mjs', import.meta.url)), ...args], { encoding: 'utf8' });
|
|
assert.equal(result.status, 2); assert.match(result.stderr, /^refused:/);
|
|
}
|
|
});
|
|
|
|
|