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(core): cover offline linked reads [H10]

Corey Olsonedbda9dd4713
diff --git a/test/relay-client-fixture.js b/test/relay-client-fixture.js
index b1ed883f..00f2341b 100644
--- a/test/relay-client-fixture.js
+++ b/test/relay-client-fixture.js
@@ -74,17 +74,34 @@ export async function relayClientFixture(t) {
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
- const origin = 'http://127.0.0.1:' + server.address().port;
+ const relayPort = server.address().port;
+ const origin = 'http://127.0.0.1:' + relayPort;
signIn = createAuthHandler({ auth, publicOrigin: origin });
api = createRelayHandler({ directory: join(scratch, 'relay'), auth, publicOrigin: origin, pollMs: 10, maintenanceMs: 0, now: () => time });
t.after(async () => {
api.close();
server.closeAllConnections();
- await new Promise(ready => server.close(ready));
+ if (server.listening) await new Promise(ready => server.close(ready));
auth.close();
rmSync(scratch, { recursive: true, force: true });
});
+ /** Stop the real relay listener and close active sockets to model a network outage. */
+ async function stopRelay() {
+ if (!server.listening) return;
+ server.closeAllConnections();
+ await new Promise((ready, reject) => server.close(error => error ? reject(error) : ready()));
+ }
+ /** Restart the same relay listener at the same address without changing its journal or credentials. */
+ async function restartRelay() {
+ if (server.listening) return;
+ await new Promise((ready, reject) => {
+ const failed = error => { server.off('listening', ready); reject(error); };
+ server.once('error', failed);
+ server.once('listening', () => { server.off('error', failed); ready(); });
+ server.listen(relayPort, '127.0.0.1');
+ });
+ }
/** Invoke the production CLI in this private repository. */
async function cli(...args) { return childResult(root, env, [CLI, ...args, '--json']); }
assert.equal(spawnSync('git', ['init', '-q', '-b', 'main'], { cwd: root, env }).status, 0);
@@ -122,7 +139,7 @@ export async function relayClientFixture(t) {
return childResult(root, env, ['--input-type=module', '-e', source]);
}
return {
- root, env, lane, before, linkFile, keyFile, calls, cli, link, otherDeviceOff, script,
+ root, env, origin, lane, before, linkFile, keyFile, calls, cli, link, otherDeviceOff, script, stopRelay, restartRelay,
advance(days) { time = Date.now() + days * 86400000; },
overrideDelete(value) { override = value; },
mainURL: new URL('../src/cli.js', import.meta.url).href,
diff --git a/test/relay.test.js b/test/relay.test.js
index 476d0a7a..ba787425 100644
--- a/test/relay.test.js
+++ b/test/relay.test.js
@@ -9,6 +9,7 @@ import { createRelayAuth, ACCESS_WINDOW_MS } from '../relay/auth.js';
import { serveRelay } from '../relay/service.js';
import { githubFixture } from './relay-fixture.js';
import { relayClientFixture } from './relay-client-fixture.js';
+import { serveView } from '../src/serve.js';
/** Use real private SQLite and an actual HTTP provider with a controllable expiry clock. */
async function fixture(t, options = {}) {
@@ -431,6 +432,94 @@ test('unlinked board commands attempt zero outbound requests [H1,P5]', async (t)
assert.ok(result.document.codes.every(code => code === 0), 'all ordinary local board commands succeed without transport');
});
+
+test('offline linked reads use the local board, moves refuse, and the same relay resumes [H10]', async (t) => {
+ const box = await relayClientFixture(t);
+ await box.link();
+ const link = JSON.parse(readFileSync(box.linkFile, 'utf8'));
+ const boardFile = join(box.root, '.git', 'pullboard', 'board.sqlite');
+ const beforeBytes = readFileSync(boardFile);
+ const beforeTables = (await box.cli('export')).document.tables;
+ const beforeEvents = beforeTables.event;
+ const oldEnvironment = new Map(['HOME', 'PULLBOARD_HOME', 'PULLBOARD_MACHINE_HOME']
+ .map(name => [name, process.env[name]]));
+ for (const name of oldEnvironment.keys()) process.env[name] = box.env[name];
+ let view;
+ try {
+ view = await serveView({ port: 0 });
+ const page = await fetch(view.url);
+ assert.equal(page.status, 200, 'the real local view page remains available');
+ assert.match(page.headers.get('content-type') ?? '', /text\/html/);
+
+ await box.stopRelay();
+ const status = await box.cli('status');
+ const listed = await box.cli('list', '--all');
+ const shown = await box.cli('show', '1');
+ assert.equal(status.code, 0, 'status reads its local board while disconnected');
+ assert.equal(listed.code, 0, 'list reads its local board while disconnected');
+ assert.ok(listed.document.items.some(item => item.item_title === 'private cleanup fixture item'));
+ assert.equal(shown.code, 0, 'show reads its local board while disconnected');
+ assert.equal(shown.document.item_title, 'private cleanup fixture item');
+
+ const viewKey = new URL(view.url).searchParams.get('k');
+ const localBoardsUrl = new URL('/api/v1/boards', view.url);
+ localBoardsUrl.searchParams.set('k', viewKey);
+ const localBoardsResponse = await fetch(localBoardsUrl);
+ assert.equal(localBoardsResponse.status, 200, 'the local view API remains available offline');
+ const localBoards = await localBoardsResponse.json();
+ const localBoard = localBoards.boards.find(board => board.root === box.root);
+ assert.ok(localBoard, 'the local view still lists the linked project from its local registry');
+ const localStateUrl = new URL(`/api/v1/boards/${localBoard.id}/state`, view.url);
+ localStateUrl.searchParams.set('k', viewKey);
+ const localStateResponse = await fetch(localStateUrl);
+ assert.equal(localStateResponse.status, 200);
+ const localState = await localStateResponse.json();
+ assert.ok(localState.state.items.some(item => item.title === 'private cleanup fixture item'),
+ 'the local view reads the same private board contents while the relay is stopped');
+
+ for (const args of [
+ ['add', box.lane, 'refused offline add'],
+ ['shout', 'coordinator', 'refused offline shout'],
+ ]) {
+ const refused = await box.cli(...args);
+ assert.equal(refused.code, 1);
+ assert.equal(refused.document.error.code, 'RELAY_UNAVAILABLE');
+ assert.ok(refused.document.error.message.includes(box.origin), 'the refusal names the address attempted');
+ assert.match(refused.document.error.message, /retry this move when the relay is reachable/);
+ }
+ const viewMoveUrl = new URL(`/api/v1/boards/${localBoard.id}/moves`, view.url);
+ viewMoveUrl.searchParams.set('k', viewKey);
+ const viewMove = await fetch(viewMoveUrl, {
+ method: 'POST', headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({ verb: 'add', args: { lane: box.lane, title: 'refused offline view move' } }),
+ });
+ assert.equal(viewMove.status, 409, 'the local view API uses the linked move refusal path');
+ assert.equal((await viewMove.json()).error.code, 'RELAY_UNAVAILABLE');
+
+ assert.deepEqual(readFileSync(boardFile), beforeBytes, 'offline reads and refused moves leave the native board bytes unchanged');
+ assert.deepEqual((await box.cli('export')).document.tables, beforeTables, 'no speculative local move or event was applied');
+
+ await box.restartRelay();
+ const resumed = await box.cli('add', box.lane, 'move after relay returns');
+ assert.equal(resumed.code, 0, 'the relay resumes without relay off/on repair');
+ assert.equal(resumed.document.item.item_title, 'move after relay returns');
+ const afterTables = (await box.cli('export')).document.tables;
+ assert.equal(afterTables.event.length, beforeEvents.length + 1, 'only the acknowledged recovery move is recorded locally');
+ assert.equal(afterTables.event.at(-1).event_kind, 'add');
+ const remoteStateResponse = await fetch(`${box.origin}/api/v1/boards/${link.board}/state`, {
+ headers: { authorization: `Bearer ${link.token}` },
+ });
+ assert.equal(remoteStateResponse.status, 200);
+ assert.equal((await remoteStateResponse.json()).state.sequence, 1, 'the resumed move has exactly one relay position');
+ } finally {
+ if (view) await view.close();
+ for (const [name, value] of oldEnvironment) {
+ if (value === undefined) delete process.env[name];
+ else process.env[name] = value;
+ }
+ }
+});
+
test('plain relay off explains an already-deleted board without printing credentials [H1,H18]', async (t) => {
const box = await relayClientFixture(t);
await box.link();