Integration tests for the Gandalf contract, auth and reliability; fix worker offline status
CI / Lint (eslint) (push) Successful in 46s
CI / Security (npm audit) (push) Successful in 26s
CI / Integration (api) (push) Successful in 1m31s
CI / Test (jest) (pull_request) Successful in 50s
CI / UI (playwright) (push) Successful in 2m31s
CI / Integration (api) (pull_request) Successful in 1m58s
CI / Test (jest) (push) Successful in 13s
CI / Deploy (push) Skipped
CI / Notify (push) Skipped
CI / UI (playwright) (pull_request) Successful in 2m16s
CI / Lint (eslint) (pull_request) Successful in 15s
CI / Security (npm audit) (pull_request) Successful in 15s
CI / Deploy (pull_request) Skipped
CI / Notify (pull_request) Skipped

New CI job `Integration (api)` (#45 phase 3) runs the real server and three
workers on Node 20 against MariaDB and tests them as a black box over HTTP
and WebSocket (20 tests, about 12 s):

- Gandalf contract: submit and poll, stdout from command_result, failures
  with exit code and stderr, 401 for wrong or missing key, 400 for missing
  fields or an unknown worker, 404 for an unknown execution
- Auth: SSO identity, 401/403, heartbeat key, /healthz exposes only status;
  WebSocket upgrade refused without credentials, with a wrong key or a wrong
  group; live events reach a signed-in browser; a browser socket cannot
  forge a command result
- Reliability: a worker killed mid-command fails the execution at once; a
  worker at capacity answers with a failure; an aborted execution stays
  failed when the late result arrives; a disconnected worker shows offline

The last test found a bug from #42: the close handler deleted the worker's
runtime-id entry before checking whether the socket was still live under its
database id, and the two ids are normally the same key. So a disconnected
worker was never marked offline and showed 'online' until the stale
heartbeat sweep. Gandalf was unaffected (it checks live sockets).

MariaDB setup for CI moves to scripts/ci/mariadb.sh, shared by both jobs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jaredandClaude Opus 5.5 committed 2026-11-22 14:14:28 -05:00
1 parent 1d5b8cdd53
commit def926d41e
11 files changed
+391 -21

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
'use strict';
const { spawn } = require('child_process');
const path = require('path');
const WebSocket = require('ws');
// Matches scripts/ci/ui-stack.sh.
const BASE = process.env.PULSE_URL || 'http://127.0.0.1:8400';
const WS_BASE = BASE.replace(/^http/, 'ws');
const WORKER_KEY = process.env.PULSE_TEST_WORKER_KEY || 'ui-test-worker-key';
const GANDALF_KEY = process.env.PULSE_TEST_GANDALF_KEY || 'ui-test-gandalf-key';
const SSO = {
'Remote-User': 'int.tester',
'Remote-Name': 'Integration Tester',
'Remote-Email': 'int@example.invalid',
'Remote-Groups': 'admin',
};
async function api(method, url, { headers = {}, body } = {}) {
const res = await fetch(BASE + url, {
method,
headers: { ...(body !== undefined ? { 'Content-Type': 'application/json' } : {}), ...headers },
body: body !== undefined ? JSON.stringify(body) : undefined,
});
const text = await res.text();
let json = null;
try { json = JSON.parse(text); } catch { /* not JSON */ }
return { status: res.status, json, text };
}
const sso = (method, url, body) => api(method, url, { headers: SSO, body });
const gandalf = (method, url, body, key = GANDALF_KEY) =>
api(method, url, { headers: key === null ? {} : { 'X-Gandalf-API-Key': key }, body });
async function waitFor(fn, { timeout = 20_000, interval = 250, what = 'condition' } = {}) {
const end = Date.now() + timeout;
let last;
while (Date.now() < end) {
last = await fn();
if (last) return last;
await new Promise(r => setTimeout(r, interval));
}
throw new Error(`timed out waiting for ${what} (last value: ${JSON.stringify(last)})`);
}
const sleep = ms => new Promise(r => setTimeout(r, ms));
async function workerByName(name) {
const { json } = await sso('GET', '/api/workers');
return (json || []).find(w => w.name === name);
}
// Polls the Gandalf endpoint the way Gandalf does, until the execution leaves 'running'.
async function waitForExecution(id, opts = {}) {
return waitFor(async () => {
const r = await gandalf('GET', `/api/internal/executions/${id}`);
return r.json && r.json.status !== 'running' ? r.json : null;
}, { what: `execution ${id} to finish`, ...opts });
}
const commandResult = ex => (ex.logs || []).find(l => l.action === 'command_result');
// An extra worker process owned by one test, so it can be killed or limited.
async function startWorker(name, env = {}) {
const child = spawn(process.execPath, [path.join(__dirname, '..', '..', 'worker', 'worker.js')], {
cwd: __dirname, // no .env here
env: {
...process.env,
WORKER_NAME: name, PULSE_SERVER: BASE, PULSE_WS: WS_BASE,
WORKER_API_KEY: WORKER_KEY, HEARTBEAT_INTERVAL: '5', ...env,
},
stdio: ['ignore', 'pipe', 'pipe'],
});
let log = '';
child.stdout.on('data', d => { log += d; });
child.stderr.on('data', d => { log += d; });
const worker = await waitFor(async () => {
const w = await workerByName(name);
return w && w.status === 'online' ? w : null;
}, { what: `worker ${name} to come online` }).catch(e => { child.kill('SIGKILL'); throw new Error(`${e.message}\n${log}`); });
return {
id: worker.id,
child,
log: () => log,
async stop() {
if (child.exitCode === null && child.signalCode === null) {
child.kill('SIGKILL');
await new Promise(r => child.once('exit', r));
}
// Remove it so other suites (and the UI tests' worker counts) don't see it.
await waitFor(async () => (await workerByName(name))?.status === 'offline', { timeout: 5_000, what: `${name} to show offline` });
await sso('DELETE', `/api/workers/${worker.id}`);
},
};
}
// Opens a WebSocket and resolves once open, or rejects with the HTTP status if refused.
function openWs(headers = {}) {
return new Promise((resolve, reject) => {
const ws = new WebSocket(WS_BASE, { headers });
const messages = [];
ws.on('message', d => { try { messages.push(JSON.parse(d)); } catch { /* ignore */ } });
ws.once('open', () => resolve(Object.assign(ws, { messages })));
ws.once('unexpected-response', (req, res) => reject(Object.assign(new Error(`HTTP ${res.statusCode}`), { status: res.statusCode })));
ws.once('error', reject);
});
}
module.exports = {
BASE, SSO, GANDALF_KEY, WORKER_KEY, api, sso, gandalf, waitFor, sleep,
workerByName, waitForExecution, commandResult, startWorker, openWs,
};