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:
@@ -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();
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,74 +1,17 @@
|
||||
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';
|
||||
import { boot } from './boot.ts';
|
||||
import './ui/phone.css';
|
||||
|
||||
/**
|
||||
* Entry point. Inside the Even app (or the Even Hub simulator) the SDK bridge
|
||||
* is present and the real adapter is used. In a plain browser there is no
|
||||
* bridge, so a clearly-labelled preview device is used instead: it has no
|
||||
* microphone and proves nothing about hardware.
|
||||
* Entry point. All start-up logic is in boot.ts. Inside the Even app (or the Even Hub
|
||||
* simulator) the SDK bridge is present and the real adapter is used. In a plain browser there
|
||||
* is no bridge, so a clearly-labelled preview device is used instead: it has no microphone and
|
||||
* proves nothing about hardware.
|
||||
*/
|
||||
const RELOAD_FLAG = 'hermeven.reloadedForHost';
|
||||
|
||||
async function resolveDevice(): Promise<{ device: DevicePort; runtime: string }> {
|
||||
const host = window as unknown as HostWindow;
|
||||
// Wait briefly for the Even app's connection rather than deciding on the first instant
|
||||
// (a precaution; see device/hostDetect.ts).
|
||||
const detection = await waitForEvenHost(host);
|
||||
if (detection.found) {
|
||||
const bridge = await waitForEvenAppBridge();
|
||||
return { device: new EvenDevice(bridge), runtime: `Even app host (SDK 0.0.16), found after ${detection.waitedMs} ms` };
|
||||
}
|
||||
const preview = document.getElementById('preview')!;
|
||||
preview.hidden = false;
|
||||
// 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).
|
||||
watchForLateHost(host, () => {
|
||||
let already = false;
|
||||
try {
|
||||
already = sessionStorage.getItem(RELOAD_FLAG) === '1';
|
||||
sessionStorage.setItem(RELOAD_FLAG, '1');
|
||||
} catch {
|
||||
already = true;
|
||||
}
|
||||
if (!already) location.reload();
|
||||
});
|
||||
return {
|
||||
device: new BrowserPreviewDevice(preview),
|
||||
runtime: `Browser preview: no glasses, no microphone (no Even host after ${detection.waitedMs} ms)`,
|
||||
};
|
||||
}
|
||||
|
||||
const { device, runtime } = await resolveDevice();
|
||||
const root = document.getElementById('app')!;
|
||||
|
||||
let update: (snapshot: ReturnType<Controller['snapshot']>) => void = () => undefined;
|
||||
const controller = new Controller({
|
||||
device,
|
||||
measure: (text, width) => measureTextWrap(text, width).lineCount,
|
||||
locale: 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: import.meta.env.DEV ? (entry) => console.info(`[hermeven] ${entry.kind}: ${entry.detail}`) : undefined,
|
||||
});
|
||||
update = mountPhoneUi(root, controller, {
|
||||
const { controller } = await boot(document, window, {
|
||||
bridgeUrl: import.meta.env.VITE_HERMEVEN_BRIDGE_URL ?? '',
|
||||
runtime,
|
||||
dev: import.meta.env.DEV,
|
||||
});
|
||||
|
||||
// Never leave the microphone open behind a page that is going away.
|
||||
window.addEventListener('beforeunload', () => void controller.stop());
|
||||
|
||||
await controller.start();
|
||||
update(controller.snapshot());
|
||||
|
||||
// Development-only convenience for automated simulator runs, where nothing can type into the
|
||||
// phone page: `?bridge=<url>&pair=<code>` pairs on load. Compiled out of production builds.
|
||||
if (import.meta.env.DEV) {
|
||||
|
||||
Reference in New Issue
Block a user