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.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
refactor(test): share unchanged API fixture helpers [A2]
Corey Olsone9e7ed0a45a5
diff --git a/test/api-fixture.js b/test/api-fixture.jsnew file mode 100644index 00000000..eec525f0--- /dev/null+++ b/test/api-fixture.js@@ -0,0 +1,62 @@+/** Shared assertions and request helpers for local API fixtures. */+import assert from 'node:assert/strict';+import { performance } from 'node:perf_hooks';+import { JSON_SHAPES } from '../src/json.js';+import { fetchFresh } from './http-fixture.js';++/** Match one catalog field to its declared JSON value type. */+function hasType(value, type) {+ if (type.includes('|')) return type.split('|').some((part) => hasType(value, part));+ if (type === 'array') return Array.isArray(value);+ if (type === 'object') return value !== null && typeof value === 'object' && !Array.isArray(value);+ if (type === 'null') return value === null;+ return typeof value === type;+}++/** Check one required-field map from JSON_SHAPES. */+function assertRequiredShape(value, required, label) {+ for (const [field, type] of Object.entries(required ?? {})) {+ assert.ok(Object.hasOwn(value, field), `${label} must include ${field}`);+ assert.ok(hasType(value[field], type), `${label}.${field} must be ${type}`);+ }+}++/** Send one bounded HTTP request to the fixture API on its own connection, authenticating by header unless overridden. */+function apiFetch(api, path, options = {}) {+ const { noSecret = false, ...requestOptions } = options;+ const headers = { ...(noSecret ? {} : { 'x-pullboard-key': api.secret }), ...requestOptions.headers };+ return fetchFresh(new URL(path, api.origin), { ...requestOptions, headers, signal: requestOptions.signal ?? AbortSignal.timeout(10_000) });+}++/** The milliseconds one awaited request takes, end to end. */+async function timedMs(work) { const started = performance.now(); await work(); return performance.now() - started; }++/** Parse and validate a versioned API refusal. */+async function apiError(response, status) {+ assert.equal(response.status, status);+ const document = await response.json();+ assertRequiredShape(document, JSON_SHAPES.error.required, 'HTTP error envelope');+ assertRequiredShape(document.error, JSON_SHAPES.errorFields, 'HTTP error');+ return document.error;+}++/** Read exactly the next non-comment server-sent event, preserving partial frames across chunks. */+async function nextSseEvent(reader, pending = { text: '' }) {+ const decoder = new TextDecoder();+ while (true) {+ const boundary = pending.text.indexOf('\n\n');+ if (boundary >= 0) {+ const frame = pending.text.slice(0, boundary);+ pending.text = pending.text.slice(boundary + 2);+ const id = /^id: (.+)$/m.exec(frame)?.[1];+ const data = [...frame.matchAll(/^data: (.+)$/gm)].map((match) => match[1]).join('\n');+ if (id !== undefined && data) return { id, document: JSON.parse(data) };+ continue;+ }+ const { done, value } = await reader.read();+ assert.equal(done, false, 'the live event stream remains open');+ pending.text += decoder.decode(value, { stream: true });+ }+}++export { apiFetch, apiError, assertRequiredShape, hasType, nextSseEvent, timedMs };diff --git a/test/api.test.js b/test/api.test.jsindex 7138d772..8ad34bbc 100644--- a/test/api.test.js+++ b/test/api.test.js@@ -31,7 +31,13 @@ import { JSON_SHAPES } from '../src/json.js';import { projectState, serveView } from '../src/serve.js';import { registerProject } from '../src/projects.js';import { SSH_SHELL_MARKERS } from '../src/person.js';-import { fetchFresh } from './http-fixture.js';+import {+ apiError,+ apiFetch,+ assertRequiredShape,+ nextSseEvent,+ timedMs,+} from './api-fixture.js';const BIN = resolve(import.meta.dirname, '../bin/pullboard.js');const TEMP_DIRS = [];@@ -270,23 +276,6 @@ function shapeFor(command, subcommand) {return JSON_SHAPES.commands[key];}-/** Match one catalog field to its declared JSON value type. */-function hasType(value, type) {- if (type.includes('|')) return type.split('|').some((part) => hasType(value, part));- if (type === 'array') return Array.isArray(value);- if (type === 'object') return value !== null && typeof value === 'object' && !Array.isArray(value);- if (type === 'null') return value === null;- return typeof value === type;-}--/** Check one required-field map from JSON_SHAPES. */-function assertRequiredShape(value, required, label) {- for (const [field, type] of Object.entries(required ?? {})) {- assert.ok(Object.hasOwn(value, field), `${label} must include ${field}`);- assert.ok(hasType(value[field], type), `${label}.${field} must be ${type}`);- }-}-/** Parse a command's sole JSON document and check its public command shape. */function json(box, cwd, command, args = [], subcommand) {const result = box.run(cwd, command, ...args, '--json');@@ -836,44 +825,6 @@ async function startApi(t, box, { traceStartup = false, traceFile: requestedTracreturn { child, document, origin: parsed.origin, secret, stderr: () => stderr };}-/** Send one bounded HTTP request to the fixture API on its own connection, authenticating by header unless overridden. */-function apiFetch(api, path, options = {}) {- const { noSecret = false, ...requestOptions } = options;- const headers = { ...(noSecret ? {} : { 'x-pullboard-key': api.secret }), ...requestOptions.headers };- return fetchFresh(new URL(path, api.origin), { ...requestOptions, headers, signal: requestOptions.signal ?? AbortSignal.timeout(10_000) });-}--/** The milliseconds one awaited request takes, end to end. */-async function timedMs(work) { const started = performance.now(); await work(); return performance.now() - started; }--/** Parse and validate a versioned API refusal. */-async function apiError(response, status) {- assert.equal(response.status, status);- const document = await response.json();- assertRequiredShape(document, JSON_SHAPES.error.required, 'HTTP error envelope');- assertRequiredShape(document.error, JSON_SHAPES.errorFields, 'HTTP error');- return document.error;-}--/** Read exactly the next non-comment server-sent event, preserving partial frames across chunks. */-async function nextSseEvent(reader, pending = { text: '' }) {- const decoder = new TextDecoder();- while (true) {- const boundary = pending.text.indexOf('\n\n');- if (boundary >= 0) {- const frame = pending.text.slice(0, boundary);- pending.text = pending.text.slice(boundary + 2);- const id = /^id: (.+)$/m.exec(frame)?.[1];- const data = [...frame.matchAll(/^data: (.+)$/gm)].map((match) => match[1]).join('\n');- if (id !== undefined && data) return { id, document: JSON.parse(data) };- continue;- }- const { done, value } = await reader.read();- assert.equal(done, false, 'the live event stream remains open');- pending.text += decoder.decode(value, { stream: true });- }-}-test('[N38] local API and sealed presentation resolve shouts beyond recent state, with complete export history', async (t) => {const box = project();const api = await startApi(t, box);