On a real G2 a touch held slightly too long is reported as a long press (sysEvent type 9, then type 10 on release; source 1 right temple, 3 left), not as a tap, and was ignored. A long press now does what a tap does. If it started a recording and is held at least 700 ms, releasing it sends (push-to-talk); released sooner, it behaves like a tap and the recording continues until the next tap or press. Tests: 219 unit/integration; simulator 7 scenarios (long_press / long_press_release through the automation API). Not reviewed, not deployed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
252 lines
12 KiB
TypeScript
252 lines
12 KiB
TypeScript
// @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('Diagnostics shows what the glasses deliver, by type only: a tap is visible, an unknown event is visible as ignored, audio is only counted', async () => {
|
|
const even = fakeEvenHost();
|
|
(window as any).flutter_inappwebview = even.host;
|
|
booted = await boot(document, window);
|
|
expect(diagnostics()['Glasses input']).toContain('0 gesture/lifecycle event(s), 0 ignored, 0 audio frame(s); last: none yet');
|
|
|
|
// The Even app pushes events to the page through this SDK entry point.
|
|
const push = (type: string, jsonData: unknown) => (window as any)._listenEvenAppMessage({ type: 'listen_even_app_data', method: 'evenHubEvent', data: { type, jsonData } });
|
|
push('sysEvent', { eventSource: 1 }); // a tap: no eventType on the wire
|
|
await wait(20);
|
|
const afterTap = diagnostics()['Glasses input']!;
|
|
expect(afterTap).toContain('1 gesture/lifecycle event(s)');
|
|
expect(afterTap).not.toContain('ignored;');
|
|
expect(afterTap).toContain('sysEvent(type=none,source=1) -> tap');
|
|
// The tap really drove the app: unpaired, so the state stays put, but it was delivered.
|
|
push('sysEvent', { eventType: 9, eventSource: 1 }); // long press: recognised
|
|
await wait(20);
|
|
expect(diagnostics()['Glasses input']).toContain('sysEvent(type=9,source=1) -> long_press');
|
|
push('sysEvent', { eventType: 8, imuData: { x: 1, y: 2, z: 3 } }); // IMU: outside the contract
|
|
await wait(20);
|
|
const afterLongPress = diagnostics()['Glasses input']!;
|
|
expect(afterLongPress).toContain('1 ignored');
|
|
expect(afterLongPress).toContain('sysEvent(type=8) -> ignored');
|
|
push('audioEvent', { audioPcm: [1, 2, 3, 4] });
|
|
await wait(20);
|
|
push('sysEvent', { eventType: 3 });
|
|
await wait(20);
|
|
const afterAudio = diagnostics()['Glasses input']!;
|
|
expect(afterAudio).toContain('1 audio frame(s)');
|
|
expect(afterAudio).toContain('-> double_tap');
|
|
// Nothing from the audio payload appears anywhere in Diagnostics.
|
|
expect(JSON.stringify(diagnostics())).not.toContain('1,2,3,4');
|
|
});
|
|
|
|
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');
|
|
});
|
|
});
|