// @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 = /([\s\S]*)<\/body>/.exec(indexHtml)![1]!.replace(//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(); // 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(/