Files
hermeven/tests/fakes/fakeDevice.ts
T
DanandClaude Fable 5.2 e1d183b54d Opening menu and hands-free mode: call an agent by name (0.3.0)
The app now opens on a menu, which is the root page: "Hands free" first,
"Agents" second. Agents is the 0.2.0 app unchanged. Hands free is the same
agents, conversations and question path with one flag set: the microphone
listens while the app is at rest and the app does not leave by itself.
The mode is never remembered; every launch starts at the menu with the
microphone closed, and double tap at Ready returns to the menu and closes
it.

Client: a loudness-only speech segmenter (core/vad.ts) cuts the stream
into utterances; each goes to the bridge for a wake check. "Wendy, <q>"
becomes a text turn to Wendy, with what was heard shown while she works.
A name alone starts a recording that sends itself when the speaker stops.
The status line starts with a dot for exactly as long as the microphone is
open at rest. In wake mode every microphone open and close goes through
one serialised reconciler; without the option the old path is untouched.

Bridge: POST /v1/wake transcribes one utterance in memory and reports
whether it BEGAN with an agent's name (first word of the display name, or
an alias from HERMEVEN_AGENT_<ID>_WAKE). Exact match only; a near miss is
reported as one word and never acted on. Creates no turn, stores nothing,
returns and logs nothing that was not addressed to an agent. One check at
a time per device, HERMEVEN_WAKE_CHECKS_PER_MINUTE (30).

Docs: decision 0004, bridge API, security gaps 3c/3d, device plan D.
The manifest's microphone description changed.

Not reviewed, not deployed, not uploaded, not device-tested.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-28 10:52:06 -08:00

122 lines
4.5 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;
}
/** Times the app asked to leave the glasses by itself, and times that was confirmed. */
exitAttempts = 0;
exits = 0;
/** Scripted answers for the next exit attempts (`false` = refused); then it succeeds. */
exitResults: boolean[] = [];
async exitNow(): Promise<boolean> {
this.exitAttempts += 1;
const ok = this.exitResults.shift() ?? true;
if (ok) this.exits += 1;
return ok;
}
private launched: 'appMenu' | 'glassesMenu' | null = null;
private readonly launchListeners = new Set<(source: 'appMenu' | 'glassesMenu') => void>();
onLaunchSource(listener: (source: 'appMenu' | 'glassesMenu') => void): () => void {
this.launchListeners.add(listener);
if (this.launched) listener(this.launched);
return () => this.launchListeners.delete(listener);
}
launchedFrom(source: 'appMenu' | 'glassesMenu'): void {
this.launched = source;
for (const l of this.launchListeners) l(source);
}
/** 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' });
longPress = () => this.emit({ type: 'long_press' });
longPressRelease = () => this.emit({ type: 'long_press_release' });
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);
}
/** Frames of digital silence, delivered like `speak`. Only while the mic is open. */
silence(seconds: number): void {
const frames = Math.round(seconds * 10);
for (let i = 0; i < frames && this.micOpen; i++) this.emit({ type: 'audio', pcm: new Uint8Array(3200) });
}
/** 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 });
}
}
}