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,217 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { boot, STARTUP_TEXT, type Booted } from '../../apps/even-client/src/boot.ts';
|
||||
import { HOST_WAIT_MS } from '../../apps/even-client/src/device/hostDetect.ts';
|
||||
|
||||
/**
|
||||
* CODY-014, at the rendered-page seam: the real index.html markup, the real boot() that main.ts
|
||||
* calls, the real Even Hub SDK bridge, in a DOM (happy-dom). The Even app host is a stand-in
|
||||
* object providing `flutter_inappwebview.callHandler`; it is not the Even app and not glasses.
|
||||
*/
|
||||
// Resolved from the repository root (vitest's working directory): in this DOM environment
|
||||
// `import.meta.url` is not a file URL.
|
||||
const indexHtml = readFileSync(resolve(process.cwd(), 'apps/even-client/index.html'), 'utf8');
|
||||
const bodyHtml = /<body>([\s\S]*)<\/body>/.exec(indexHtml)![1]!.replace(/<script[\s\S]*?<\/script>/g, '');
|
||||
|
||||
interface HostCall {
|
||||
method: string;
|
||||
handler: string;
|
||||
type: string;
|
||||
}
|
||||
/** Stand-in for the Even app's WebView host: answers the SDK's calls and records them. */
|
||||
function fakeEvenHost() {
|
||||
const calls: HostCall[] = [];
|
||||
const storage = new Map<string, string>();
|
||||
// The SDK calls `callHandler('evenAppMessage', { type: 'call_even_app_method', method, data })`
|
||||
// and awaits the returned value (observed from the published SDK 0.0.16).
|
||||
const callHandler = async (name: string, raw: unknown) => {
|
||||
const message = (typeof raw === 'string' ? JSON.parse(raw) : raw) as { type?: string; method?: string; data?: any };
|
||||
const method = String(message?.method ?? '');
|
||||
const data = message?.data ?? {};
|
||||
calls.push({ method, handler: name, type: String(message?.type ?? '') });
|
||||
switch (method) {
|
||||
case 'createStartUpPageContainer':
|
||||
return 0; // StartUpPageCreateResult.success
|
||||
case 'getLocalStorage':
|
||||
return storage.get(String(data.key)) ?? '';
|
||||
case 'setLocalStorage':
|
||||
storage.set(String(data.key), String(data.value));
|
||||
return true;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
};
|
||||
return { host: { callHandler }, calls };
|
||||
}
|
||||
|
||||
const visibleText = () => (document.body.textContent ?? '').replace(/\s+/g, ' ').trim();
|
||||
const diagnostics = () =>
|
||||
Object.fromEntries([...document.querySelectorAll('details dl dt')].map((dt) => [dt.textContent, dt.nextElementSibling?.textContent ?? '']));
|
||||
const flush = async (ms: number) => {
|
||||
await vi.advanceTimersByTimeAsync(ms);
|
||||
};
|
||||
|
||||
let booted: Booted | null = null;
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
document.body.innerHTML = bodyHtml;
|
||||
delete (window as any).flutter_inappwebview;
|
||||
// window.EvenAppBridge is the SDK's own singleton, created once when the SDK module loads;
|
||||
// it is left alone, as it would be on a real page.
|
||||
localStorage.clear();
|
||||
sessionStorage.clear();
|
||||
});
|
||||
afterEach(async () => {
|
||||
await booted?.dispose();
|
||||
booted = null;
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe('the page as served, before any script has run', () => {
|
||||
it('already shows a meaningful, accessible status instead of a blank page', () => {
|
||||
const startup = document.getElementById('startup')!;
|
||||
expect(startup).not.toBeNull();
|
||||
expect(startup.getAttribute('role')).toBe('status');
|
||||
expect(startup.getAttribute('aria-live')).toBe('polite');
|
||||
expect(startup.hasAttribute('hidden')).toBe(false);
|
||||
expect(startup.textContent).toContain('Hermeven');
|
||||
expect(startup.textContent).toContain('Looking for the Even app');
|
||||
// The status is ahead of the (empty) app container in the document, and styled inline so it
|
||||
// does not depend on the stylesheet or the script having loaded.
|
||||
expect(indexHtml.indexOf('id="startup"')).toBeLessThan(indexHtml.indexOf('id="app"'));
|
||||
expect(indexHtml).toMatch(/<style>[\s\S]*#startup[\s\S]*<\/style>/);
|
||||
expect(indexHtml.indexOf('id="startup"')).toBeLessThan(indexHtml.indexOf('<script'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('start-up in an ordinary browser (no Even host)', () => {
|
||||
it('shows the status promptly, does not fall back early, then replaces it with the labelled browser preview', async () => {
|
||||
const starting = boot(document, window, { bridgeUrl: 'https://bridge.example' });
|
||||
let settled = false;
|
||||
void starting.then(() => (settled = true));
|
||||
|
||||
// Immediately: a visible status, and nothing else decided.
|
||||
await flush(0);
|
||||
const startup = document.getElementById('startup')!;
|
||||
expect(startup.querySelector('[data-startup-status]')!.textContent).toBe(STARTUP_TEXT.waiting);
|
||||
expect(visibleText()).toContain('Looking for the Even app');
|
||||
expect(document.getElementById('preview')!.hidden).toBe(true);
|
||||
expect(document.getElementById('app')!.children.length).toBe(0);
|
||||
|
||||
// Most of the way through the wait: still waiting. No eager browser fallback.
|
||||
await flush(HOST_WAIT_MS - 100);
|
||||
expect(settled).toBe(false);
|
||||
expect(document.getElementById('startup')).not.toBeNull();
|
||||
expect(document.getElementById('preview')!.hidden).toBe(true);
|
||||
expect(document.querySelector('.hud')).toBeNull();
|
||||
expect(document.getElementById('app')!.children.length).toBe(0);
|
||||
|
||||
// After the timeout: the preview, clearly labelled, and the status is gone.
|
||||
await flush(200);
|
||||
booted = await starting;
|
||||
expect(booted.mode).toBe('preview');
|
||||
expect(document.getElementById('startup')).toBeNull();
|
||||
expect(visibleText()).not.toContain('Looking for the Even app');
|
||||
const preview = document.getElementById('preview')!;
|
||||
expect(preview.hidden).toBe(false);
|
||||
expect(preview.textContent).toContain('Browser preview of the HUD text. Not the glasses');
|
||||
expect(preview.querySelector('.hud-body')!.textContent).toContain('Not connected');
|
||||
expect(document.querySelector('#app h1')!.textContent).toBe('Hermeven');
|
||||
expect(visibleText()).toContain('Pair with your bridge');
|
||||
expect((document.querySelector('input[type=url]') as HTMLInputElement).value).toBe('https://bridge.example');
|
||||
expect(diagnostics().Runtime).toBe(`Browser preview: no glasses, no microphone (no Even host after ${HOST_WAIT_MS} ms)`);
|
||||
});
|
||||
|
||||
it('a host that appears after the preview was chosen triggers one reload into the real mode', async () => {
|
||||
let reloads = 0;
|
||||
const starting = boot(document, window, { reload: () => (reloads += 1) });
|
||||
await flush(HOST_WAIT_MS + 50);
|
||||
booted = await starting;
|
||||
expect(booted.mode).toBe('preview');
|
||||
(window as any).flutter_inappwebview = fakeEvenHost().host;
|
||||
await flush(1200);
|
||||
await flush(1200);
|
||||
expect(reloads).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('start-up when a callable Even host appears during the wait', () => {
|
||||
// Real time here: once a host is present the SDK drives its own timers, which a fake clock
|
||||
// does not advance. The waits below are short and bounded.
|
||||
beforeEach(() => vi.useRealTimers());
|
||||
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
||||
|
||||
it('selects the real Even adapter, initialises the SDK bridge, creates the HUD page, and never shows the preview', async () => {
|
||||
const even = fakeEvenHost();
|
||||
const started = Date.now();
|
||||
const starting = boot(document, window, { bridgeUrl: 'https://bridge.example' });
|
||||
await wait(20);
|
||||
expect(visibleText()).toContain('Looking for the Even app');
|
||||
expect(document.getElementById('app')!.children.length).toBe(0);
|
||||
|
||||
// The host turns up 400 ms into the 2.5 s wait.
|
||||
await wait(380);
|
||||
expect(document.getElementById('startup')).not.toBeNull();
|
||||
(window as any).flutter_inappwebview = even.host;
|
||||
booted = await starting;
|
||||
|
||||
// Well before the timeout: detection reacted to the host, it did not wait it out.
|
||||
expect(Date.now() - started).toBeLessThan(HOST_WAIT_MS - 500);
|
||||
expect(booted.mode).toBe('even');
|
||||
expect(booted.runtime).toMatch(/^Even app host \(SDK 0\.0\.16\), found after [34]\d\d ms$/);
|
||||
// The SDK's own bridge object exists and really talked to the host, in the SDK's protocol.
|
||||
expect((window as any).EvenAppBridge).toBeDefined();
|
||||
const methods = even.calls.map((c) => c.method);
|
||||
expect(methods).toContain('getLocalStorage');
|
||||
expect(methods).toContain('createStartUpPageContainer');
|
||||
expect(even.calls.every((c) => c.handler === 'evenAppMessage' && c.type === 'call_even_app_method')).toBe(true);
|
||||
// The real page is mounted, the status is gone, and the preview was never shown.
|
||||
expect(document.getElementById('startup')).toBeNull();
|
||||
const preview = document.getElementById('preview')!;
|
||||
expect(preview.hidden).toBe(true);
|
||||
expect(preview.children.length).toBe(0);
|
||||
expect(document.querySelector('.hud')).toBeNull();
|
||||
expect(visibleText()).toContain('Pair with your bridge');
|
||||
const diag = diagnostics();
|
||||
expect(diag.Runtime).toContain('Even app host');
|
||||
expect(diag['HUD page']).toBe('created');
|
||||
});
|
||||
|
||||
it('a host present from the first instant is used without waiting out the timeout', async () => {
|
||||
const even = fakeEvenHost();
|
||||
(window as any).flutter_inappwebview = even.host;
|
||||
const started = Date.now();
|
||||
booted = await boot(document, window);
|
||||
expect(Date.now() - started).toBeLessThan(1500);
|
||||
expect(booted.mode).toBe('even');
|
||||
expect(booted.runtime).toMatch(/found after \d{1,2} ms$/);
|
||||
expect(document.getElementById('startup')).toBeNull();
|
||||
expect(diagnostics()['HUD page']).toBe('created');
|
||||
});
|
||||
|
||||
it('a host that refuses the HUD page is still the Even app, and the page says the HUD was not created', async () => {
|
||||
const even = fakeEvenHost();
|
||||
const realHandler = even.host.callHandler;
|
||||
// 1 = StartUpPageCreateResult.invalid
|
||||
(even.host as { callHandler: (name: string, raw: unknown) => Promise<unknown> }).callHandler = async (name, raw) => {
|
||||
const message = (typeof raw === 'string' ? JSON.parse(raw) : raw) as { method?: string };
|
||||
return message?.method === 'createStartUpPageContainer' ? 1 : realHandler(name, raw);
|
||||
};
|
||||
(window as any).flutter_inappwebview = even.host;
|
||||
booted = await boot(document, window);
|
||||
expect(booted.mode).toBe('even');
|
||||
expect(diagnostics()['HUD page']).toBe('not created');
|
||||
});
|
||||
});
|
||||
|
||||
describe('an object that only looks like a host', () => {
|
||||
it('without a callable callHandler it is not treated as the Even app', async () => {
|
||||
(window as any).flutter_inappwebview = {};
|
||||
const starting = boot(document, window);
|
||||
await flush(HOST_WAIT_MS + 50);
|
||||
booted = await starting;
|
||||
expect(booted.mode).toBe('preview');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user