Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

test(relay): study faults through a proxy [H5,H17]

Corey Olsoneef7bf3de832
diff --git a/test/relay-client-fixture.js b/test/relay-client-fixture.js
index d09c52fd..8c108656 100644
--- a/test/relay-client-fixture.js
+++ b/test/relay-client-fixture.js
@@ -243,7 +243,6 @@ globalThis.fetch = function tracedFetch(input, init) {
let stateReadStarted = 0;
let refuseSnapshotWrites = false;
let stallApi = false;
- let studyApiFault = null;
let holdPublishes = false;
const heldPublishes = [];
let holdProbes = false;
@@ -394,16 +393,6 @@ globalThis.fetch = function tracedFetch(input, init) {
recordResponse({ method: req.method, path: req.url, request: Buffer.concat(requestChunks), response: Buffer.concat(responseChunks) });
});
calls.push(call);
- if (studyApiFault && (req.url ?? '').startsWith('/api/')) {
- const fault = studyApiFault;
- if (fault.delayMs) await new Promise(resolve => setTimeout(resolve, fault.delayMs));
- if (fault.status) {
- request.resume();
- res.writeHead(fault.status, { 'content-type': 'application/json' });
- res.end(JSON.stringify({ version: 1, error: { code: fault.code, message: 'private study fault' } }));
- return;
- }
- }
if (stallApi && (req.url ?? '').startsWith('/api/')) { request.resume(); await new Promise(() => {}); }
// A held API2 checkpoint publish waits until released, so a test can act while an upload is in flight (#1014).
if (holdPublishes && req.method === 'POST' && /^\/api\/v2\/boards\/[0-9a-f]{32}\/uploads\/[^/]+\/publish$/u.test(req.url ?? '')) {
@@ -824,13 +813,6 @@ globalThis.fetch = function tracedFetch(input, init) {
refuseSnapshotWrites(value) { refuseSnapshotWrites = value; },
/** Leave every board API request unanswered until its client times out, like an overloaded relay. */
stallApi(value = true) { stallApi = value; },
- /** Apply a private study's real HTTP fault to both snapshot protocol versions. */
- studyApiFault(value = null) {
- assert.ok(value === null || Number.isSafeInteger(value.delayMs) && value.delayMs >= 0 && value.delayMs <= 5000 // budget: declared study fault ceiling, the 5 s slow relay the #1281 bar names
- && (value.status === null || [409, 503].includes(value.status))
- && (value.status === null || ['ENGINE_VERSION', 'RELAY_UNAVAILABLE'].includes(value.code)));
- studyApiFault = value;
- },
/** Hold every API2 checkpoint publish until called with false, which releases the held ones. */
holdPublishes(value = true) {
holdPublishes = value;
diff --git a/test/relay-study-failure.study.js b/test/relay-study-failure.study.js
index 89ef5b92..6ff5e680 100644
--- a/test/relay-study-failure.study.js
+++ b/test/relay-study-failure.study.js
@@ -5,13 +5,15 @@ import { performance } from 'node:perf_hooks';
import { test } from 'node:test';
import * as store from '../src/board.js';
import { relayClientFixture } from './relay-client-fixture.js';
+import { faultProxy } from './relay-study-fixture.js';
test('relay study measures native reads and local moves across private transport failures [H5,H17]', { timeout: 300_000 }, async t => {
for (const fault of ['down', 'slow-5s', '5xx', 'stale-engine']) {
const box = await relayClientFixture(t);
await box.link();
+ const proxy = fault === 'down' ? null : await faultProxy(t, box);
if (fault === 'down') await box.stopRelay();
- else box.studyApiFault({ delayMs: fault === 'slow-5s' ? 5000 : 0,
+ else proxy.set({ delayMs: fault === 'slow-5s' ? 5000 : 0,
status: fault === '5xx' ? 503 : fault === 'stale-engine' ? 409 : null,
code: fault === 'stale-engine' ? 'ENGINE_VERSION' : 'RELAY_UNAVAILABLE' });
const before = box.calls.length;
@@ -41,7 +43,7 @@ test('relay study measures native reads and local moves across private transport
requests: box.calls.slice(before).map(call => ({ method: call.method, path: call.path })),
limitation: fault === 'stale-engine' ? 'Injected real HTTP409 ENGINE_VERSION response; current CLI binary itself remains current.'
: 'Private real HTTP failure; this stage measures native CLI only, not browser behavior.' }));
- box.studyApiFault();
+ proxy?.set();
if (fault === 'down') await box.restartRelay();
}
});
diff --git a/test/relay-study-fixture.js b/test/relay-study-fixture.js
index 58f59c45..00c26b86 100644
--- a/test/relay-study-fixture.js
+++ b/test/relay-study-fixture.js
@@ -1,6 +1,7 @@
/** Shared authenticated readers for the measured private relay study [H5,H17]. */
import assert from 'node:assert/strict';
-import { readFileSync } from 'node:fs';
+import { readFileSync, writeFileSync } from 'node:fs';
+import { createServer, request as httpRequest } from 'node:http';
import { performance } from 'node:perf_hooks';
import { ENGINE_VERSION } from '../src/machine.js';
import { decodeBoardKey } from '../src/seal.js';
@@ -64,3 +65,44 @@ export async function readRelay(box) {
return { document, requests, receivedBytes, readMs: performance.now() - started };
}
+
+/**
+ * Put a real HTTP fault proxy between the linked CLI and the fixture relay by pointing this scratch board's link at
+ * it. API requests can be delayed, or answered with a refusal, without changing the shared relay fixture.
+ */
+export async function faultProxy(t, box) {
+ const target = new URL(box.origin);
+ let fault = null;
+ const server = createServer((req, res) => {
+ /** Forward one request unchanged, after any configured delay or instead of a configured refusal. */
+ async function handle() {
+ const api = (req.url ?? '').startsWith('/api/');
+ if (api && fault?.delayMs) await new Promise(resolve => setTimeout(resolve, fault.delayMs));
+ if (api && fault?.status) {
+ req.resume();
+ res.writeHead(fault.status, { 'content-type': 'application/json' });
+ res.end(JSON.stringify({ version: 1, error: { code: fault.code, message: 'private study fault' } }));
+ return;
+ }
+ const upstream = httpRequest({ host: target.hostname, port: target.port, method: req.method, path: req.url,
+ headers: { ...req.headers, host: target.host } }, reply => { res.writeHead(reply.statusCode, reply.headers); reply.pipe(res); });
+ upstream.on('error', () => { if (!res.headersSent) res.writeHead(502); res.end(); });
+ req.pipe(upstream);
+ }
+ handle().catch(() => { if (!res.headersSent) res.writeHead(500); res.end(); });
+ });
+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
+ t.after(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); }));
+ const original = readFileSync(box.linkFile, 'utf8');
+ const state = JSON.parse(original);
+ state.url = 'http://127.0.0.1:' + server.address().port;
+ writeFileSync(box.linkFile, JSON.stringify(state), { mode: 0o600 });
+ return {
+ /** Set the fault for later API requests: { delayMs, status, code }, or null to pass them through. */
+ set(value = null) {
+ assert.ok(value === null || Number.isSafeInteger(value.delayMs) && value.delayMs >= 0
+ && (value.status === null || [409, 503].includes(value.status)));
+ fault = value;
+ },
+ };
+}