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): capture set-up-once page steps [H5,C7]

Corey Olson3ead736e3561
diff --git a/test/chrome-fixture.js b/test/chrome-fixture.js
index f432fddd..33b99e9c 100644
--- a/test/chrome-fixture.js
+++ b/test/chrome-fixture.js
@@ -38,13 +38,15 @@ export async function waitForTargetAbsent(listTargets, targetId, {
}
/** Build the shared bounded page-condition loop around the real CDP evaluator. */
-export function createWaiter(evaluate, { commandTimeoutMs = COMMAND_TIMEOUT_MS, pauseFor = pause, now = Date.now } = {}) {
+export function createWaiter(evaluate, { commandTimeoutMs = COMMAND_TIMEOUT_MS, pauseFor = pause, now = Date.now,
+ includeExpression = true, captureTimeout } = {}) {
return async function waitFor(expression, timeoutMs = commandTimeoutMs, label = 'wait for page condition') {
const waitDeadline = now() + timeoutMs;
let lastEvaluationError = null;
+ const currentLabel = () => typeof label === 'function' ? label() : label;
while (now() < waitDeadline) {
try {
- if (await evaluate(expression, label, waitDeadline - now())) return;
+ if (await evaluate(expression, currentLabel(), waitDeadline - now())) return;
} catch (error) {
if (error instanceof Error && (error.message.startsWith('Browser evaluation failed:')
|| (error.code === -32000 && error.navigated === true))) lastEvaluationError = error;
@@ -52,8 +54,16 @@ export function createWaiter(evaluate, { commandTimeoutMs = COMMAND_TIMEOUT_MS,
}
await pauseFor(Math.max(0, Math.min(50, waitDeadline - now())));
}
- const exception = lastEvaluationError instanceof Error ? `; last evaluation failed: ${lastEvaluationError.message}` : '';
- throw new Error(`Browser condition "${label}" did not arrive within ${timeoutMs}ms; expression: ${expression.slice(0, 200)}${exception}.`);
+ const exception = lastEvaluationError instanceof Error
+ ? includeExpression ? `; last evaluation failed: ${lastEvaluationError.message}` : '; last page evaluation did not complete'
+ : '';
+ const expressionDetail = includeExpression ? `; expression: ${expression.slice(0, 200)}` : '';
+ let capture = '';
+ if (captureTimeout) {
+ try { capture = await captureTimeout(currentLabel()); }
+ catch { capture = '; page-step capture unavailable'; }
+ }
+ throw new Error(`Browser condition "${currentLabel()}" did not arrive within ${timeoutMs}ms${expressionDetail}${exception}${capture}.`);
};
}
@@ -163,6 +173,8 @@ export async function startChrome({
commandTimeoutMs = COMMAND_TIMEOUT_MS,
taskTimeoutMs = 25_000,
cleanupTimeoutMs = CLEANUP_TIMEOUT_MS,
+ capturePageSteps = false,
+ diagnosticTimeoutMs = 750,
env = process.env,
} = {}) {
if (!executable) throw new Error('Chrome was not found; install Chrome or set PULLBOARD_CHROME.');
@@ -187,15 +199,44 @@ export async function startChrome({
loopDelay = monitorEventLoopDelay({ resolution: 10 });
loopDelay.enable();
const relayRequests = new Set();
- const send = createSender(socket, pending, () => ++id, commandTimeoutMs, loopDelay, relayRequests);
- const evaluate = createEvaluator(send);
+ const pageFetches = capturePageSteps ? new Map() : null;
+ const send = createSender(socket, pending, () => ++id, commandTimeoutMs, loopDelay, relayRequests, {}, pageFetches);
+ const rawEvaluate = createEvaluator(send);
+ /** Capture bounded page metadata after a timeout without changing the original operation's deadline. */
+ async function capturePageState(label) {
+ let page = { readyState: 'unavailable', lastDeviceKeyStep: 'unavailable', storageOpenState: 'unavailable', approvalCard: null };
+ const approvalRequestId = /\b([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\b/iu.exec(String(label))?.[1] ?? null;
+ if (approvalRequestId) page.approvalCard = { requestId: approvalRequestId, state: 'unavailable', actionCount: 0 };
+ try {
+ page = await rawEvaluate(`({ readyState: document.readyState,
+ lastDeviceKeyStep: window.__pullboardPageStepCapture?.lastDeviceKeyStep ?? 'not observed',
+ storageOpenState: window.__pullboardPageStepCapture?.storageOpenState ?? 'not observed',
+ approvalCard: ${JSON.stringify(approvalRequestId)} === null ? null : (() => {
+ const card = document.querySelector('.phone-approval[data-request="' + ${JSON.stringify(approvalRequestId)} + '"]');
+ return { requestId: ${JSON.stringify(approvalRequestId)}, state: card ? 'present' : 'absent', actionCount: card?.querySelectorAll('button').length ?? 0 };
+ })() })`,
+ 'capture page state after timeout', Math.max(1, Math.min(750, diagnosticTimeoutMs, commandTimeoutMs)));
+ } catch { /* A page that cannot answer diagnostics still gets a named timeout. */ }
+ const pendingFetches = [...pageFetches.values()].map((request) => ({
+ method: request.method,
+ path: request.path,
+ ageMs: Math.max(0, Date.now() - request.startedAt),
+ }));
+ const stage = String(label).replace(/[\r\n\t]/gu, ' ').slice(0, 120);
+ return `; page-step capture: ${JSON.stringify({ stage, ...page, pendingFetches })}`;
+ }
+ const evaluate = capturePageSteps ? createPageStepEvaluator(rawEvaluate, capturePageState) : rawEvaluate;
let closed = false;
let taskSequence = 0;
/** Navigate the existing isolated browser profile to a local or test-owned URL. */
async function navigate(nextUrl) {
await send('Page.navigate', { url: nextUrl }, 'navigate page');
}
- const waitFor = createWaiter(evaluate, { commandTimeoutMs });
+ const waitFor = createWaiter(evaluate, {
+ commandTimeoutMs,
+ includeExpression: !capturePageSteps,
+ ...(capturePageSteps ? { captureTimeout: capturePageState } : {}),
+ });
/** Wait for non-stream relay traffic to finish before an idle-network assertion. */
async function waitForRelayIdle(timeoutMs = commandTimeoutMs) {
const deadline = Date.now() + timeoutMs;
@@ -243,6 +284,7 @@ export async function startChrome({
await send('Network.enable', {}, 'enable network diagnostics', deadline - Date.now());
await send('Page.enable', {}, 'enable page diagnostics', deadline - Date.now());
await send('Runtime.enable', {}, 'enable runtime diagnostics', deadline - Date.now());
+ if (capturePageSteps) await send('Page.addScriptToEvaluateOnNewDocument', { source: pageStepCaptureScript() }, 'install page-step capture', deadline - Date.now());
await send('Page.navigate', { url }, 'open initial page', deadline - Date.now());
const launchDurationMs = launched.launchDurationMs;
process.stderr.write(`Chrome ready in ${launchDurationMs}ms (${startupTimeoutMs}ms DevTools budget)\n`);
@@ -315,7 +357,7 @@ async function connectDevTools(debuggerUrl, deadline) {
}
/** Create a CDP sender with a deadline checked on the same clock used by timeout diagnostics. */
-export function createSender(socket, pending, nextId, timeoutMs, loopDelay, relayRequests, timers = {}) {
+export function createSender(socket, pending, nextId, timeoutMs, loopDelay, relayRequests, timers = {}, pageFetches = null) {
const now = timers.now ?? Date.now;
const schedule = timers.setTimeout ?? setTimeout;
const cancel = timers.clearTimeout ?? clearTimeout;
@@ -357,12 +399,30 @@ export function createSender(socket, pending, nextId, timeoutMs, loopDelay, rela
if (message.method === 'Network.requestWillBeSent') {
const request = message.params?.request;
const url = request?.url ?? '';
+ if (pageFetches && ['Fetch', 'XHR'].includes(message.params?.type)) {
+ let path = '/';
+ try {
+ path = new URL(url).pathname.split('/').map((segment) => {
+ if (!segment) return '';
+ return new Set(['api', 'auth', 'boards', 'devices', 'approvals', 'enrollments', 'start', 'state', 'events', 'requests', 'authorize', 'github', 'session', 'tokens', 'machines']).has(segment)
+ || /^v[0-9]{1,3}$/u.test(segment)
+ ? segment : ':id';
+ }).join('/');
+ } catch { path = '/[unavailable]'; }
+ pageFetches.set(message.params.requestId, {
+ method: /^[A-Za-z]{1,12}$/u.test(request?.method ?? '') ? request.method.toUpperCase() : 'OTHER',
+ path: path.slice(0, 160),
+ startedAt: now(),
+ });
+ while (pageFetches.size > 32) pageFetches.delete(pageFetches.keys().next().value);
+ }
const eventStream = /\/api\/v1\/boards\/[0-9a-f]{32}\/events(?:\?|$)/.test(url)
&& /text\/event-stream/i.test(request?.headers?.Accept ?? request?.headers?.accept ?? '');
if (/\/api\/v1\/boards\//.test(url) && !eventStream) relayRequests.add(message.params.requestId);
}
if (['Network.loadingFinished', 'Network.loadingFailed'].includes(message.method)) {
relayRequests.delete(message.params?.requestId);
+ pageFetches?.delete(message.params?.requestId);
}
const waiter = pending.get(message.id);
if (!waiter) return;
@@ -450,6 +510,49 @@ export function createEvaluator(send) {
};
}
+/** Add bounded page-state capture to evaluation timeouts while preserving the original failure. */
+export function createPageStepEvaluator(evaluate, captureTimeout) {
+ return async (expression, label = 'evaluate page expression', remainingMs) => {
+ try { return await evaluate(expression, label, remainingMs); }
+ catch (error) {
+ if (error?.code !== 'CDP_TIMEOUT') throw error;
+ try { error.message += await captureTimeout(label); }
+ catch { error.message += '; page-step capture unavailable'; }
+ throw error;
+ }
+ };
+}
+
+/** Instrument only bounded operation names so a timeout can distinguish crypto and storage stages. */
+function pageStepCaptureScript() {
+ return `(() => {
+ const state = window.__pullboardPageStepCapture = { lastDeviceKeyStep: 'no device-key operation observed', storageOpenState: 'not opened' };
+ try {
+ const prototype = Object.getPrototypeOf(indexedDB);
+ const originalOpen = prototype.open;
+ prototype.open = function (...args) {
+ state.storageOpenState = 'opening';
+ const request = originalOpen.apply(this, args);
+ request.addEventListener('success', () => { state.storageOpenState = 'open'; }, { once: true });
+ request.addEventListener('error', () => { state.storageOpenState = 'failed'; }, { once: true });
+ request.addEventListener('blocked', () => { state.storageOpenState = 'blocked'; }, { once: true });
+ return request;
+ };
+ } catch {}
+ try {
+ const prototype = Object.getPrototypeOf(crypto.subtle);
+ for (const name of ['generateKey', 'importKey', 'deriveBits', 'deriveKey', 'encrypt', 'decrypt', 'sign', 'verify', 'exportKey']) {
+ const original = prototype[name];
+ if (typeof original !== 'function') continue;
+ prototype[name] = function (...args) {
+ state.lastDeviceKeyStep = 'crypto.subtle.' + name + ' started';
+ return original.apply(this, args);
+ };
+ }
+ } catch {}
+ })();`;
+}
+
/** Reject outstanding CDP requests during close or startup failure without exposing payloads. */
function rejectPending(pending, message) {
for (const waiter of pending.values()) {
diff --git a/test/relay-setup-once.test.js b/test/relay-setup-once.test.js
index 06905a28..bf6c999c 100644
--- a/test/relay-setup-once.test.js
+++ b/test/relay-setup-once.test.js
@@ -1,9 +1,11 @@
/** One real machine setup reaches current and future projects through one phone key [H5,H15,H17]. */
import assert from 'node:assert/strict';
+import { createServer } from 'node:http';
import { startFixtureChild as spawn, runFixtureChild as spawnSync } from './fixture-child.js';
import { existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { test } from 'node:test';
+import { createPageStepEvaluator } from './chrome-fixture.js';
import { relayClientFixture } from './relay-client-fixture.js';
import { startChrome, findChromeExecutable } from './relay-browser-fixture.js';
@@ -65,6 +67,116 @@ function browserDeviceExpression() {
read.onsuccess = () => { const value=read.result; db.close(); resolve({id:value.deviceId,extractable:value.privateKey.extractable}); }; read.onerror=reject; }; request.onerror=reject; })`;
}
+/** Label a set-up-once page wait without changing the existing timeout or expression. */
+function pageWait(chrome, step, expression, timeoutMs) {
+ return chrome.waitFor(expression, timeoutMs, step);
+}
+
+/** Label a set-up-once page evaluation without changing its expression or deadline. */
+function pageEvaluate(chrome, step, expression) {
+ return chrome.evaluate(expression, step);
+}
+
+test('the set-up-once wait names the step it is on [H5,C7]', {
+ skip: !findChromeExecutable() && 'Chrome is not installed',
+}, async t => {
+ let pendingFetchResponse;
+ const server = createServer((request, response) => {
+ if (request.method === 'POST' && request.url?.startsWith('/api/v1/boards/')) {
+ request.resume();
+ response.writeHead(200, { 'content-type': 'application/json' });
+ response.flushHeaders();
+ pendingFetchResponse = response;
+ return;
+ }
+ response.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
+ response.end('<!doctype html><html><head><title>Capture test</title></head><body>stand-in page</body></html>');
+ });
+ await new Promise((resolveListen, rejectListen) => {
+ server.once('error', rejectListen);
+ server.listen(0, '127.0.0.1', resolveListen);
+ });
+ let chrome;
+ t.after(async () => {
+ pendingFetchResponse?.end();
+ await chrome?.close();
+ await new Promise(resolveClose => server.close(resolveClose));
+ });
+ const address = server.address();
+ chrome = await startChrome({
+ url: `http://127.0.0.1:${address.port}/stand-in`,
+ commandTimeoutMs: 1_000,
+ diagnosticTimeoutMs: 200,
+ capturePageSteps: true,
+ });
+ await pageWait(chrome, 'stand-in document is ready', 'document.readyState === "complete"');
+ await pageEvaluate(chrome, 'stand-in device-key step', `Promise.all([
+ crypto.subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, false, ['deriveBits']),
+ new Promise((resolve, reject) => { const request = indexedDB.open('page-step-capture-probe', 1);
+ request.onsuccess = () => { request.result.close(); resolve(true); }; request.onerror = () => reject(new Error('storage probe failed')); })
+ ]).then(() => true)`);
+ const pathSentinel = 'unknown-path-sentinel';
+ const querySentinel = 'query-sentinel';
+ const fragmentSentinel = 'fragment-sentinel';
+ const bodySentinel = 'body-sentinel';
+ const presentRequestId = '12345678-1234-4234-8234-123456789abc';
+ const absentRequestId = '87654321-4321-4321-8321-cba987654321';
+ const probeUrl = `/api/v1/boards/${pathSentinel}/state?marker=${querySentinel}#${fragmentSentinel}`;
+ await pageEvaluate(chrome, 'start pending privacy probe fetch', `(() => {
+ window.__pullboardCaptureProbe = fetch(${JSON.stringify(probeUrl)}, {
+ method: 'POST', credentials: 'omit', body: ${JSON.stringify(bodySentinel)}
+ }).catch(() => {});
+ return true;
+ })()`);
+ await pageEvaluate(chrome, `install approval card ${presentRequestId}`, `(() => {
+ const card = document.createElement('div'); card.className = 'phone-approval'; card.dataset.request = ${JSON.stringify(presentRequestId)};
+ card.append(document.createElement('button')); document.body.append(card); return true;
+ })()`);
+ /** Assert that forced timeout diagnostics contain only the expected stand-in page state. */
+ const assertPageCapture = (error, step, approvalCard) => {
+ const encoded = /page-step capture: (\{[^\n]+\})/u.exec(error.message)?.[1];
+ assert.ok(encoded, 'the timeout carries one bounded page-state record');
+ const capture = JSON.parse(encoded);
+ assert.equal(capture.stage, step);
+ assert.equal(capture.readyState, 'complete');
+ assert.match(capture.lastDeviceKeyStep, /crypto\.subtle\.generateKey started/u);
+ assert.equal(capture.storageOpenState, 'open');
+ assert.equal(capture.pendingFetches.length, 1);
+ assert.deepEqual({ method: capture.pendingFetches[0].method, path: capture.pendingFetches[0].path }, {
+ method: 'POST', path: '/api/v1/boards/:id/state',
+ });
+ assert.ok(Number.isFinite(capture.pendingFetches[0].ageMs) && capture.pendingFetches[0].ageMs >= 0);
+ assert.deepEqual(capture.approvalCard, approvalCard);
+ for (const sentinel of [pathSentinel, querySentinel, fragmentSentinel, bodySentinel]) {
+ assert.equal(error.message.includes(sentinel), false, `timeout diagnostics omit ${sentinel}`);
+ }
+ return true;
+ };
+ await assert.rejects(pageWait(chrome, `stand-in forced condition step ${presentRequestId}`, 'false', 120), error => assertPageCapture(
+ error, `stand-in forced condition step ${presentRequestId}`,
+ { requestId: presentRequestId, state: 'present', actionCount: 1 },
+ ));
+ await assert.rejects(pageWait(chrome, `stand-in absent approval ${absentRequestId}`, 'false', 80), error => assertPageCapture(
+ error, `stand-in absent approval ${absentRequestId}`,
+ { requestId: absentRequestId, state: 'absent', actionCount: 0 },
+ ));
+ await assert.rejects(pageWait(chrome, 'stand-in forced condition step', 'false', 80), error => assertPageCapture(
+ error, 'stand-in forced condition step', null,
+ ));
+ await assert.rejects(pageEvaluate(chrome, 'stand-in forced evaluate step', 'new Promise(() => {})'), error => {
+ assert.equal(error.code, 'CDP_TIMEOUT');
+ return assertPageCapture(error, 'stand-in forced evaluate step', null);
+ });
+ const originalTimeout = Object.assign(new Error('DevTools Runtime.evaluate timed out after 17ms.'), { code: 'CDP_TIMEOUT' });
+ const unavailableCapture = createPageStepEvaluator(async () => { throw originalTimeout; }, async () => { throw new Error('forced unavailable diagnostic'); });
+ await assert.rejects(unavailableCapture('true', 'forced fallback step'), error => {
+ assert.strictEqual(error, originalTimeout);
+ assert.equal(error.code, 'CDP_TIMEOUT');
+ assert.match(error.message, /^DevTools Runtime\.evaluate timed out after 17ms\.; page-step capture unavailable$/u);
+ return true;
+ });
+});
+
test('set up once links three registered boards, later registration and a persistent phone without another pairing [H5,H15,H17]', {
skip: !findChromeExecutable() && 'Chrome is not installed',
}, async t => {
@@ -89,16 +201,16 @@ test('set up once links three registered boards, later registration and a persis
assert.equal(box.calls.filter(call => call.path === '/auth/device/start').length, 1, 'one GitHub device sign-in covers the entire machine');
const profileDirectory = join(dirname(box.root), 'phone-profile');
- chrome = await startChrome({ profileDirectory });
+ chrome = await startChrome({ profileDirectory, capturePageSteps: true });
assert.equal((await chrome.send('Network.setCookie', { name: 'pb_session', value: (await box.phoneSession()).token, url: box.origin, httpOnly: true, sameSite: 'Lax' })).success, true);
await chrome.navigate(box.origin + '/#device=' + pending.locator + '.' + pending.secret);
const completed = await setup.done;
assert.equal(completed.code, 0);
assert.equal(completed.document.linked.length, 3);
assert.equal(completed.document.paired, true);
- await chrome.waitFor("document.querySelectorAll('#proj-list .proj.repo').length === 3");
- assert.equal(await chrome.evaluate('location.hash'), '', 'the one-use secret is stripped from browser history');
- const device = await chrome.evaluate(browserDeviceExpression());
+ await pageWait(chrome, 'initial pairing renders registered boards', "document.querySelectorAll('#proj-list .proj.repo').length === 3");
+ assert.equal(await pageEvaluate(chrome, 'initial pairing strips the one-use fragment', 'location.hash'), '', 'the one-use secret is stripped from browser history');
+ const device = await pageEvaluate(chrome, 'initial pairing reads the persisted device key', browserDeviceExpression());
assert.equal(device.extractable, false, 'the private phone key is persisted non-extractable in IndexedDB');
assert.equal(JSON.parse(readFileSync(machineFile, 'utf8')).devices[0].deviceId, device.id);
assert.equal(JSON.parse(readFileSync(machineFile, 'utf8')).pending, null);
@@ -130,16 +242,16 @@ test('set up once links three registered boards, later registration and a persis
assert.equal(approvalPosts(), 1, 'status and doctor do not replace or duplicate the request');
await chrome.navigate(box.origin);
const approveLinkButton = '#phone-approve-' + linkProposal.context.id;
- await chrome.waitFor(`document.querySelector(${JSON.stringify(approveLinkButton)}) !== null`);
- const linkCard = await chrome.evaluate(`({text: document.querySelector('.phone-approval[data-request="${linkProposal.context.id}"]').innerText,
+ await pageWait(chrome, `link approval card ${linkProposal.context.id}`, `document.querySelector(${JSON.stringify(approveLinkButton)}) !== null`);
+ const linkCard = await pageEvaluate(chrome, `read link approval card ${linkProposal.context.id}`, `({text: document.querySelector('.phone-approval[data-request="${linkProposal.context.id}"]').innerText,
action: document.querySelector(${JSON.stringify(approveLinkButton)}).innerText})`);
assert.match(linkCard.text, /Link fourth-project\?/u);
assert.match(linkCard.text, /fixture\/repository/u);
assert.match(linkCard.text, new RegExp(new Date(linkProposal.context.expires).toISOString().replace(/[.*+?^${}()|[\]\\]/gu, '\\$&')));
assert.equal(linkCard.action, 'Link project', 'the paired phone presents an explicit one-tap link action');
const approvalsBeforeLinkTap = box.calls.filter(call => call.path.endsWith('/authorize')).length;
- await chrome.evaluate(`document.querySelector(${JSON.stringify(approveLinkButton)}).click()`);
- await chrome.waitFor(`document.querySelector(${JSON.stringify(approveLinkButton)}) === null`);
+ await pageEvaluate(chrome, `tap link approval ${linkProposal.context.id}`, `document.querySelector(${JSON.stringify(approveLinkButton)}).click()`);
+ await pageWait(chrome, `link approval completes ${linkProposal.context.id}`, `document.querySelector(${JSON.stringify(approveLinkButton)}) === null`);
assert.equal(box.calls.filter(call => call.path.endsWith('/authorize')).length, approvalsBeforeLinkTap + 1,
'one explicit phone tap authorizes the link proposal once');
const linkedFourth = await command(fourth, box.env, ['status']).done;
@@ -148,18 +260,18 @@ test('set up once links three registered boards, later registration and a persis
assert.equal(JSON.parse(readFileSync(approvalFile, 'utf8')).approvals.some(entry => entry.root === fourth), false);
assert.equal(approvalPosts(), 1, 'consuming the phone reply does not submit another proposal');
assert.equal(box.calls.filter(call => call.path === '/auth/device/start').length, 1, 'no second machine sign-in');
- await chrome.waitFor("document.querySelectorAll('#proj-list .proj.repo').length === 4");
- assert.deepEqual(await chrome.evaluate(browserDeviceExpression()), device, 'a new page retrieves the same private device key');
+ await pageWait(chrome, 'fourth board appears after approval', "document.querySelectorAll('#proj-list .proj.repo').length === 4");
+ assert.deepEqual(await pageEvaluate(chrome, 'new page retrieves the paired device key', browserDeviceExpression()), device, 'a new page retrieves the same private device key');
await chrome.close();
rmSync(join(profileDirectory, 'DevToolsActivePort'), { force: true });
- chrome = await startChrome({ profileDirectory });
+ chrome = await startChrome({ profileDirectory, capturePageSteps: true });
assert.equal((await chrome.send('Network.setCookie', { name: 'pb_session', value: (await box.phoneSession()).token, url: box.origin, httpOnly: true, sameSite: 'Lax' })).success, true);
await chrome.navigate(box.origin);
- await chrome.waitFor("document.querySelectorAll('#proj-list .proj.repo').length === 4");
- assert.deepEqual(await chrome.evaluate(browserDeviceExpression()), device, 'a new browser process retrieves the original non-extractable key');
+ await pageWait(chrome, 'reopened browser renders linked boards', "document.querySelectorAll('#proj-list .proj.repo').length === 4");
+ assert.deepEqual(await pageEvaluate(chrome, 'reopened browser retrieves the paired device key', browserDeviceExpression()), device, 'a new browser process retrieves the original non-extractable key');
for (const root of allRoots) {
const board = link(root).board;
- assert.equal(await chrome.evaluate(`Object.hasOwn(JSON.parse(localStorage.getItem('pullboard.relay.keys.v1')), ${JSON.stringify(board)})`), true,
+ assert.equal(await pageEvaluate(chrome, 'reopened browser loads a linked board key', `Object.hasOwn(JSON.parse(localStorage.getItem('pullboard.relay.keys.v1')), ${JSON.stringify(board)})`), true,
'the actual browser unwrapped each board key, including the later board');
}
@@ -171,7 +283,7 @@ test('set up once links three registered boards, later registration and a persis
assert.equal(box.calls.filter(call => call.path === '/auth/device/start').length, 2, 'explicit relay on --all performs a fresh foreground sign-in');
assert.equal(box.calls.filter(call => call.method === 'POST' && call.path.startsWith('/api/v1/devices/enrollments/')).length, 1,
'the existing paired phone is retained without another pairing enrollment');
- assert.deepEqual(await chrome.evaluate(browserDeviceExpression()), device);
+ assert.deepEqual(await pageEvaluate(chrome, 'explicit setup retains the paired device key', browserDeviceExpression()), device);
const off = await command(fourth, box.env, ['relay', 'off']).done;
assert.equal(off.code, 0);
@@ -182,8 +294,8 @@ test('set up once links three registered boards, later registration and a persis
const deleteApproval = JSON.parse(deletePost.request.toString('utf8')).context;
assert.equal(deleteApproval.action, 'delete-board', 'remote deletion is a separate phone-approved action');
assert.equal(deleteApproval.target, fourthBoard);
- await chrome.waitFor(`document.querySelector('.phone-approval[data-request="${deleteApproval.id}"]') !== null`);
- const deleteCard = await chrome.evaluate(`({text: document.querySelector('.phone-approval[data-request="${deleteApproval.id}"]').innerText,
+ await pageWait(chrome, `delete approval card ${deleteApproval.id}`, `document.querySelector('.phone-approval[data-request="${deleteApproval.id}"]') !== null`);
+ const deleteCard = await pageEvaluate(chrome, `read delete approval card ${deleteApproval.id}`, `({text: document.querySelector('.phone-approval[data-request="${deleteApproval.id}"]').innerText,
action: document.querySelector('.phone-approval[data-request="${deleteApproval.id}"] button').innerText})`);
assert.match(deleteCard.text, new RegExp(deleteApproval.target));
assert.equal(deleteCard.action, 'Delete relay copy');
@@ -193,8 +305,8 @@ test('set up once links three registered boards, later registration and a persis
assert.equal((await boardsBeforeDeleteTap.json()).boards.some(board => board.id === fourthBoard), true,
'relay off leaves the remote board in place while its phone approval is pending');
const beforeDeleteTap = box.calls.filter(call => call.path.endsWith('/authorize')).length;
- await chrome.evaluate(`document.querySelector('.phone-approval[data-request="${deleteApproval.id}"] button').click()`);
- await chrome.waitFor(`document.querySelector('.phone-approval[data-request="${deleteApproval.id}"]') === null`);
+ await pageEvaluate(chrome, `tap delete approval ${deleteApproval.id}`, `document.querySelector('.phone-approval[data-request="${deleteApproval.id}"] button').click()`);
+ await pageWait(chrome, `delete approval completes ${deleteApproval.id}`, `document.querySelector('.phone-approval[data-request="${deleteApproval.id}"]') === null`);
assert.equal(box.calls.filter(call => call.path.endsWith('/authorize')).length, beforeDeleteTap + 1,
'the remote copy is deleted only after the deliberate phone tap');
const boardsAfterDeleteTap = await fetch(box.origin + '/auth/boards', { headers: { cookie: 'pb_session=' + phone.token } });
@@ -215,8 +327,8 @@ test('set up once links three registered boards, later registration and a persis
'explicitly linking the board again requires a foreground sign-in');
assert.equal(JSON.parse(readFileSync(machineFile, 'utf8')).excluded.includes(fourth), false,
'explicit board linking clears its opt-out');
- await chrome.waitFor("document.querySelectorAll('#proj-list .proj.repo').length === 4");
- assert.deepEqual(await chrome.evaluate(browserDeviceExpression()), device,
+ await pageWait(chrome, 'relinking renders four linked boards', "document.querySelectorAll('#proj-list .proj.repo').length === 4");
+ assert.deepEqual(await pageEvaluate(chrome, 'relinking retains the paired device key', browserDeviceExpression()), device,
'relinking wraps the board for the same paired phone key');
assert.equal(box.calls.filter(call => call.path === '/auth/device/start').length, 4);
assert.equal(box.calls.filter(call => call.method === 'POST' && call.path.startsWith('/api/v1/devices/enrollments/')).length, 1);
@@ -231,15 +343,23 @@ test('set up once links three registered boards, later registration and a persis
assert.equal(durableBytes.includes(Buffer.from(raw, 'base64url')), false, 'no raw board-key bytes in relay records');
}
+ const approvalsBeforeRevoke = box.transit.filter(record => record.method === 'POST' && record.path === '/api/v1/devices/approvals').length;
const revokeCommand = command(box.root, box.env, ['relay', 'revoke', device.id]);
t.after(async () => { if (revokeCommand.child.exitCode === null) revokeCommand.child.kill('SIGTERM'); await revokeCommand.done.catch(() => {}); });
- await chrome.waitFor(`[...document.querySelectorAll('.phone-approval')].some(node => node.textContent.includes(${JSON.stringify(device.id)}))`);
- const revocationCard = await chrome.evaluate(`({text: [...document.querySelectorAll('.phone-approval')].find(node => node.textContent.includes(${JSON.stringify(device.id)})).innerText,
+ /** Read the request id for the newly published revocation without adding another wait. */
+ const revokeRequestId = () => {
+ const approvals = box.transit.filter(record => record.method === 'POST' && record.path === '/api/v1/devices/approvals');
+ const post = approvals.length > approvalsBeforeRevoke ? approvals.at(-1) : null;
+ return post ? JSON.parse(post.request.toString('utf8')).context.id : 'request pending';
+ };
+ await pageWait(chrome, () => `device revocation approval ${revokeRequestId()}`, `[...document.querySelectorAll('.phone-approval')].some(node => node.textContent.includes(${JSON.stringify(device.id)}))`);
+ const revokeId = revokeRequestId();
+ const revocationCard = await pageEvaluate(chrome, `read device revocation approval ${revokeId}`, `({text: [...document.querySelectorAll('.phone-approval')].find(node => node.textContent.includes(${JSON.stringify(device.id)})).innerText,
action: [...document.querySelectorAll('.phone-approval')].find(node => node.textContent.includes(${JSON.stringify(device.id)})).querySelector('button').innerText})`);
assert.match(revocationCard.text, new RegExp(device.id));
assert.equal(revocationCard.action, 'Approve revocation');
const beforeRevokeTap = box.calls.filter(call => call.path.endsWith('/authorize')).length;
- await chrome.evaluate(`[...document.querySelectorAll('.phone-approval')].find(node => node.textContent.includes(${JSON.stringify(device.id)})).querySelector('button').click()`);
+ await pageEvaluate(chrome, `tap device revocation approval ${revokeId}`, `[...document.querySelectorAll('.phone-approval')].find(node => node.textContent.includes(${JSON.stringify(device.id)})).querySelector('button').click()`);
const revoked = await revokeCommand.done;
assert.equal(revoked.code, 0);
assert.match(revoked.document.notice, /already received remain known/u);
@@ -318,7 +438,7 @@ test('a set-up-once run stretched past 30 seconds still gets its approval card [
return JSON.parse(readFileSync(machineFile, 'utf8')).pending;
}, 'set-up-once publishes a real pending phone enrollment');
const profileDirectory = join(dirname(box.root), 'stretched-approval-phone-profile');
- chrome = await startChrome({ profileDirectory });
+ chrome = await startChrome({ profileDirectory, capturePageSteps: true });
const session = await box.phoneSession();
assert.equal((await chrome.send('Network.setCookie', { name: 'pb_session', value: session.token,
url: box.origin, httpOnly: true, sameSite: 'Lax' })).success, true);
@@ -326,7 +446,7 @@ test('a set-up-once run stretched past 30 seconds still gets its approval card [
const completed = await setup.done;
assert.equal(completed.code, 0, 'the actual machine setup pairs the phone');
assert.equal(completed.document.paired, true);
- await chrome.waitFor("document.querySelectorAll('#proj-list .proj.repo').length === 1");
+ await pageWait(chrome, 'initial pairing renders the registered board', "document.querySelectorAll('#proj-list .proj.repo').length === 1");
const stretchStarted = Date.now();
await new Promise(resolve => setTimeout(resolve, 40_100));
@@ -338,16 +458,16 @@ test('a set-up-once run stretched past 30 seconds still gets its approval card [
assert.ok(proposal, 'the later real project registration creates its phone approval');
const button = '#phone-approve-' + proposal.context.id;
await assert.doesNotReject(
- chrome.waitFor(`document.querySelector(${JSON.stringify(button)}) !== null`),
+ pageWait(chrome, `delayed project approval card ${proposal.context.id}`, `document.querySelector(${JSON.stringify(button)}) !== null`),
'the elapsed-clock approval becomes a visible card',
);
assert.equal(proposal.published, true, 'the relay accepts the native approval after the elapsed delay');
- const card = await chrome.evaluate(`({text: document.querySelector('.phone-approval[data-request="${proposal.context.id}"]').innerText,
+ const card = await pageEvaluate(chrome, `read delayed project approval card ${proposal.context.id}`, `({text: document.querySelector('.phone-approval[data-request="${proposal.context.id}"]').innerText,
action: document.querySelector(${JSON.stringify(button)}).innerText})`);
assert.match(card.text, /Link delayed-approval-project\?/u);
assert.equal(card.action, 'Link project');
- await chrome.evaluate(`document.querySelector(${JSON.stringify(button)}).click()`);
- await chrome.waitFor(`document.querySelector(${JSON.stringify(button)}) === null`);
+ await pageEvaluate(chrome, `tap delayed project approval ${proposal.context.id}`, `document.querySelector(${JSON.stringify(button)}).click()`);
+ await pageWait(chrome, `delayed project approval completes ${proposal.context.id}`, `document.querySelector(${JSON.stringify(button)}) === null`);
const consumed = await command(delayed, box.env, ['status']).done;
assert.equal(consumed.code, 0, 'the real native follow-up consumes the phone approval');
assert.equal(existsSync(join(delayed, '.git/pullboard/relay.json')), true);