import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { HOST_WAIT_MS, PLATFORM_READY_EVENT, waitForEvenHost, watchForLateHost, type HostWindow } from '../../apps/even-client/src/device/hostDetect.ts'; /** * Found on the first real-phone run (2026-11-21): the Even app's connection was not yet on * `window` when the page's script started, and the page wrongly chose browser-preview mode. */ function fakeWindow() { const listeners = new Map void>>(); const win: HostWindow & { fire(type: string): void; listenerCount(): number } = { addEventListener: (type, l) => void (listeners.get(type) ?? listeners.set(type, new Set()).get(type)!).add(l), removeEventListener: (type, l) => void listeners.get(type)?.delete(l), fire: (type) => listeners.get(type)?.forEach((l) => l()), listenerCount: () => [...listeners.values()].reduce((n, s) => n + s.size, 0), }; return win; } const host = { callHandler: () => undefined }; beforeEach(() => vi.useFakeTimers()); afterEach(() => vi.useRealTimers()); describe('detecting the Even app host', () => { it('present from the start (the simulator): found at once', async () => { const win = fakeWindow(); win.flutter_inappwebview = host; expect(await waitForEvenHost(win)).toEqual({ found: true, waitedMs: 0 }); expect(win.listenerCount()).toBe(0); }); it('appears shortly after the page starts (the real phone): found, not mistaken for a browser', async () => { const win = fakeWindow(); const detecting = waitForEvenHost(win); await vi.advanceTimersByTimeAsync(800); win.flutter_inappwebview = host; await vi.advanceTimersByTimeAsync(60); const result = await detecting; expect(result.found).toBe(true); expect(result.waitedMs).toBeGreaterThanOrEqual(800); expect(result.waitedMs).toBeLessThan(1000); }); it("the WebView's platform-ready event triggers an immediate check", async () => { const win = fakeWindow(); const detecting = waitForEvenHost(win, { pollMs: 100_000 }); await vi.advanceTimersByTimeAsync(300); win.flutter_inappwebview = host; win.fire(PLATFORM_READY_EVENT); expect((await detecting).found).toBe(true); }); it('the event alone, with no host object, proves nothing', async () => { const win = fakeWindow(); const detecting = waitForEvenHost(win); win.fire(PLATFORM_READY_EVENT); await vi.advanceTimersByTimeAsync(HOST_WAIT_MS + 10); expect((await detecting).found).toBe(false); }); it('an object without a callable callHandler is not a host', async () => { const win = fakeWindow(); win.flutter_inappwebview = {}; const detecting = waitForEvenHost(win); await vi.advanceTimersByTimeAsync(HOST_WAIT_MS + 10); expect((await detecting).found).toBe(false); }); it('a plain browser: gives up after the wait and cleans up', async () => { const win = fakeWindow(); const detecting = waitForEvenHost(win); await vi.advanceTimersByTimeAsync(HOST_WAIT_MS - 10); let settled = false; void detecting.then(() => (settled = true)); await Promise.resolve(); expect(settled).toBe(false); await vi.advanceTimersByTimeAsync(20); expect(await detecting).toEqual({ found: false, waitedMs: HOST_WAIT_MS }); expect(win.listenerCount()).toBe(0); }); it('a host that turns up after preview mode was chosen triggers exactly one late callback', async () => { const win = fakeWindow(); let late = 0; watchForLateHost(win, () => (late += 1)); await vi.advanceTimersByTimeAsync(3000); expect(late).toBe(0); win.flutter_inappwebview = host; await vi.advanceTimersByTimeAsync(600); win.fire(PLATFORM_READY_EVENT); await vi.advanceTimersByTimeAsync(5000); expect(late).toBe(1); expect(win.listenerCount()).toBe(0); }); });