Address Cody review round 8: CODY-014 start-up status; add acceptance contract

CODY-014 The page is no longer blank while it looks for the Even app. The
         static HTML carries an accessible status (#startup, role=status)
         visible from first paint; boot() keeps it until the real page is
         mounted, then removes it. Start-up moved from main.ts to boot.ts so
         the entry-point wiring can be exercised in a DOM. No eager browser
         fallback: the preview is chosen only after the host wait expires.

tests/integration/entryPoint.test.ts loads the real index.html and runs the
real boot() and the real Even Hub SDK in happy-dom: prompt status, preview
after timeout, real Even mode when a callable host appears during the wait
(SDK bridge initialised, HUD page created).

docs/contracts/: acceptance contract, written by the implementer on Dan's
instruction and tied to his quoted requests.
docs/evidence/device/2026-11-21.md: first run on a physical G2, as reported
by Dan plus the bridge log.

Not reviewed. This commit is not deployed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-21 12:50:45 -08:00
co-authored by Claude Fable 5.2
parent 09e21f6368
commit 7f055d490f
8 changed files with 605 additions and 64 deletions
+116
View File
@@ -0,0 +1,116 @@
import { waitForEvenAppBridge } from '@evenrealities/even_hub_sdk';
import { measureTextWrap } from '@evenrealities/pretext';
import { Controller } from './core/controller.ts';
import { EvenDevice } from './device/evenAdapter.ts';
import { waitForEvenHost, watchForLateHost, type HostWindow } from './device/hostDetect.ts';
import { BrowserPreviewDevice } from './device/previewDevice.ts';
import type { DevicePort } from './device/port.ts';
import { mountPhoneUi } from './ui/phone.ts';
/**
* Page start-up, kept out of main.ts so it can be exercised against a real DOM in tests.
*
* Sequence:
* 1. The static HTML already shows a status line (`#startup`), so the page is never blank.
* 2. Wait briefly for the Even app's connection (see device/hostDetect.ts). There is no eager
* fallback: nothing is mounted, and no device is chosen, until that wait has an answer.
* 3. Even app found: use the real adapter. Not found: show the clearly-labelled browser preview.
* 4. Mount the phone page and remove the status line.
*/
const RELOAD_FLAG = 'hermeven.reloadedForHost';
export const STARTUP_TEXT = {
waiting: 'Starting. Looking for the Even app…',
found: 'Even app found. Connecting to your glasses…',
} as const;
export interface BootOptions {
bridgeUrl?: string;
hostWaitMs?: number;
/** Development build: mirror diagnostics to the console. */
dev?: boolean;
/** Reload hook, replaceable in tests. */
reload?: () => void;
}
export interface Booted {
controller: Controller;
mode: 'even' | 'preview';
runtime: string;
/** Stop background watchers and the controller. */
dispose(): Promise<void>;
}
export async function boot(doc: Document, win: Window, options: BootOptions = {}): Promise<Booted> {
const root = doc.getElementById('app');
const previewRoot = doc.getElementById('preview');
if (!root || !previewRoot) throw new Error('index.html is missing #app or #preview');
const startup = doc.getElementById('startup');
const say = (text: string) => {
const line = startup?.querySelector('[data-startup-status]');
if (line) line.textContent = text;
};
say(STARTUP_TEXT.waiting);
const host = win as unknown as HostWindow;
const detection = await waitForEvenHost(host, options.hostWaitMs === undefined ? {} : { timeoutMs: options.hostWaitMs });
let device: DevicePort;
let mode: Booted['mode'];
let runtime: string;
let stopWatching: () => void = () => undefined;
if (detection.found) {
say(STARTUP_TEXT.found);
const bridge = await waitForEvenAppBridge();
device = new EvenDevice(bridge);
mode = 'even';
runtime = `Even app host (SDK 0.0.16), found after ${detection.waitedMs} ms`;
} else {
previewRoot.hidden = false;
device = new BrowserPreviewDevice(previewRoot);
mode = 'preview';
runtime = `Browser preview: no glasses, no microphone (no Even host after ${detection.waitedMs} ms)`;
// If the host appears later still, reload once into the real mode (once, so a host that
// comes and goes cannot put the page in a reload loop).
stopWatching = watchForLateHost(host, () => {
let already = false;
try {
already = win.sessionStorage.getItem(RELOAD_FLAG) === '1';
win.sessionStorage.setItem(RELOAD_FLAG, '1');
} catch {
already = true;
}
if (!already) (options.reload ?? (() => win.location.reload()))();
});
}
let update: (snapshot: ReturnType<Controller['snapshot']>) => void = () => undefined;
const controller = new Controller({
device,
measure: (text, width) => measureTextWrap(text, width).lineCount,
locale: win.navigator.language || 'en-US',
onChange: (snapshot) => update(snapshot),
// Development builds mirror diagnostics to the console (visible in the simulator and the
// Even dev console). Diagnostics never contain questions, answers or credentials.
onNote: options.dev ? (entry) => console.info(`[hermeven] ${entry.kind}: ${entry.detail}`) : undefined,
});
update = mountPhoneUi(root, controller, { bridgeUrl: options.bridgeUrl ?? '', runtime });
// The real page is on screen: the start-up status has done its job.
startup?.remove();
// Never leave the microphone open behind a page that is going away.
win.addEventListener('beforeunload', () => void controller.stop());
await controller.start();
update(controller.snapshot());
return {
controller,
mode,
runtime,
async dispose() {
stopWatching();
await controller.stop();
},
};
}