Files
hermeven/tests/fakes/fakeDevice.ts
T
DanandClaude Fable 5.2 1507cea077 Address Cody review round 3: CODY-003 and CODY-006
CODY-003 Request identity is durable before submission. Storage results are
         explicit at the DevicePort boundary (storageSet resolves true only
         on the host's own timely confirmation; storageGet distinguishes
         unknown from empty). A request is not sent until the phone has
         confirmed storing its id; a timeout or refusal holds it back,
         unsent and retryable under the same id. Unreadable storage at
         start is not treated as unpaired; an unsaveable pairing is revoked.
CODY-006 The state directory is set to 0700 on every open, including when
         it already existed, and the bridge refuses to start with an
         operator-facing error if it cannot be. Deployment guide creates
         it with install -d -m 0700.

Checks: typecheck clean; npm test 194 passed; client build ok;
test:real-stt 11 passed; test:real-hermes 9 passed; test:simulator 6
passed; git diff --check clean.

Not reviewed. No physical-device, droplet or production evidence.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 20:29:05 -08:00

93 lines
3.2 KiB
TypeScript

import type { HudFrame } from '../../apps/even-client/src/core/hud.ts';
import type { DeviceEvent } from '../../apps/even-client/src/device/events.ts';
import type { DevicePort, StorageRead } from '../../apps/even-client/src/device/port.ts';
/**
* TEST FAKE. Not glasses, not the simulator.
*
* Records what the app would draw, and lets a test play gestures and audio
* frames. It proves the app's logic against the DevicePort contract only.
*/
export class FakeDevice implements DevicePort {
frames: HudFrame[] = [];
micOpen = false;
micOpens = 0;
micCloses = 0;
micAllowed = true;
exitRequests = 0;
readonly storage = new Map<string, string>();
private readonly listeners = new Set<(event: DeviceEvent) => void>();
private readonly connectionListeners = new Set<(connected: boolean) => void>();
get frame(): HudFrame {
return this.frames.at(-1) ?? { body: '', status: '' };
}
async init(frame: HudFrame): Promise<boolean> {
this.frames.push(frame);
return true;
}
show(frame: HudFrame): void {
this.frames.push(frame);
}
async startMic(): Promise<boolean> {
if (!this.micAllowed) return false;
this.micOpen = true;
this.micOpens += 1;
return true;
}
async stopMic(): Promise<void> {
if (this.micOpen) this.micCloses += 1;
this.micOpen = false;
}
onEvent(listener: (event: DeviceEvent) => void): () => void {
this.listeners.add(listener);
return () => this.listeners.delete(listener);
}
onConnection(listener: (connected: boolean) => void): () => void {
this.connectionListeners.add(listener);
return () => this.connectionListeners.delete(listener);
}
async requestExit(): Promise<void> {
this.exitRequests += 1;
}
/** Set to make storage writes report "not confirmed" without writing. */
storageWritesFail = false;
storageReadsFail = false;
async storageGet(key: string): Promise<StorageRead> {
if (this.storageReadsFail) return { ok: false };
return { ok: true, value: this.storage.get(key) ?? '' };
}
async storageSet(key: string, value: string): Promise<boolean> {
if (this.storageWritesFail) return false;
this.storage.set(key, value);
return true;
}
// ---- test controls
emit(event: DeviceEvent): void {
for (const l of this.listeners) l(event);
}
tap = () => this.emit({ type: 'tap' });
doubleTap = () => this.emit({ type: 'double_tap' });
swipeDown = () => this.emit({ type: 'swipe_down' });
swipeUp = () => this.emit({ type: 'swipe_up' });
menu = (itemId: number) => this.emit({ type: 'menu', itemId });
connection(connected: boolean): void {
for (const l of this.connectionListeners) l(connected);
}
/** Deliver audio the way the SDK documents it: 100 ms frames of 3,200 bytes. Only while the mic is open. */
speak(seconds: number): void {
const frames = Math.round(seconds * 10);
for (let i = 0; i < frames && this.micOpen; i++) {
const pcm = new Uint8Array(3200);
for (let s = 0; s < 1600; s++) {
const v = Math.round(Math.sin((2 * Math.PI * 440 * (i * 1600 + s)) / 16000) * 8000);
pcm[s * 2] = v & 0xff;
pcm[s * 2 + 1] = (v >> 8) & 0xff;
}
this.emit({ type: 'audio', pcm });
}
}
}