First vertical slice: Even client, bridge, contract v1, tests, docs
Client (apps/even-client): pure interaction reducer with generation-based stale-result rejection, HUD renderer, measured pagination, Even Hub SDK 0.0.16 adapter, bridge HTTP client, phone pairing/status/diagnostics page. Bridge (services/bridge): device pairing and revocable expiring credentials, turn engine with idempotent submit, cancel, deadlines and restart recovery, Hermes Runs API adapter, OpenAI-compatible STT adapter, operator CLI. Contract (packages/contracts): versioned schemas, types and error codes. Tests: 122 unit/integration (fakes), 6 simulator scenarios (Even Hub simulator 0.9.5), 9 scenarios against a real Hermes v0.23.1 gateway with a stub model. No real STT, real model or physical device has been exercised; see docs/test-evidence.md and docs/device-test-plan.md. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { evenHubEventFromJson, OsEventTypeList } from '@evenrealities/even_hub_sdk';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { normalizeEvent, OS_EVENT, type RawEvenHubEvent } from '../src/device/events.ts';
|
||||
|
||||
/**
|
||||
* These run the published SDK's own parser (`evenHubEventFromJson`) over host
|
||||
* payload shapes, then our normaliser. They verify our mapping against the SDK
|
||||
* as shipped, not against a pair of glasses.
|
||||
*/
|
||||
const parse = (payload: unknown) => normalizeEvent(evenHubEventFromJson(payload) as RawEvenHubEvent);
|
||||
|
||||
describe('event constants', () => {
|
||||
it('match the SDK enum', () => {
|
||||
expect(OS_EVENT).toEqual({
|
||||
CLICK: OsEventTypeList.CLICK_EVENT,
|
||||
SCROLL_TOP: OsEventTypeList.SCROLL_TOP_EVENT,
|
||||
SCROLL_BOTTOM: OsEventTypeList.SCROLL_BOTTOM_EVENT,
|
||||
DOUBLE_CLICK: OsEventTypeList.DOUBLE_CLICK_EVENT,
|
||||
FOREGROUND_ENTER: OsEventTypeList.FOREGROUND_ENTER_EVENT,
|
||||
FOREGROUND_EXIT: OsEventTypeList.FOREGROUND_EXIT_EVENT,
|
||||
ABNORMAL_EXIT: OsEventTypeList.ABNORMAL_EXIT_EVENT,
|
||||
SYSTEM_EXIT: OsEventTypeList.SYSTEM_EXIT_EVENT,
|
||||
IMU_DATA_REPORT: OsEventTypeList.IMU_DATA_REPORT,
|
||||
LONG_PRESS: OsEventTypeList.LONG_PRESS_EVENT,
|
||||
LONG_PRESS_RELEASE: OsEventTypeList.LONG_PRESS_RELEASE_EVENT,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('gestures through the SDK parser', () => {
|
||||
it('a tap arrives with no eventType (protobuf omits zero) on either envelope', () => {
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventSource: 1 } })).toEqual([{ type: 'tap' }]);
|
||||
expect(parse({ type: 'textEvent', jsonData: { containerID: 1, containerName: 'body' } })).toEqual([{ type: 'tap' }]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 0 } })).toEqual([{ type: 'tap' }]);
|
||||
});
|
||||
|
||||
it('double tap, swipes and lifecycle map from sysEvent and textEvent', () => {
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 3 } })).toEqual([{ type: 'double_tap' }]);
|
||||
expect(parse({ type: 'textEvent', jsonData: { eventType: 3 } })).toEqual([{ type: 'double_tap' }]);
|
||||
expect(parse({ type: 'textEvent', jsonData: { eventType: 1 } })).toEqual([{ type: 'swipe_up' }]);
|
||||
expect(parse({ type: 'textEvent', jsonData: { eventType: 2 } })).toEqual([{ type: 'swipe_down' }]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 'SCROLL_BOTTOM_EVENT' } })).toEqual([{ type: 'swipe_down' }]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 4 } })).toEqual([{ type: 'foreground_enter' }]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 5 } })).toEqual([{ type: 'foreground_exit' }]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 6 } })).toEqual([{ type: 'abnormal_exit' }]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 7 } })).toEqual([{ type: 'system_exit' }]);
|
||||
});
|
||||
|
||||
it('ring and left-temple sources produce the same semantic gestures', () => {
|
||||
for (const eventSource of [1, 2, 3]) {
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventSource } })).toEqual([{ type: 'tap' }]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventSource, eventType: 3 } })).toEqual([{ type: 'double_tap' }]);
|
||||
}
|
||||
});
|
||||
|
||||
it('audio frames are never mistaken for taps', () => {
|
||||
const out = parse({ type: 'audioEvent', jsonData: { audioPcm: [1, 2, 3, 4] } });
|
||||
expect(out).toHaveLength(1);
|
||||
expect(out[0]!.type).toBe('audio');
|
||||
expect(Array.from((out[0] as { pcm: Uint8Array }).pcm)).toEqual([1, 2, 3, 4]);
|
||||
});
|
||||
|
||||
it('menu selections carry their item id and are not taps', () => {
|
||||
expect(parse({ type: 'menuItemClickEvent', jsonData: { itemID: 2 } })).toEqual([{ type: 'menu', itemId: 2 }]);
|
||||
});
|
||||
|
||||
it('IMU reports and long presses are outside the interaction contract', () => {
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 8, imuData: { x: 1, y: 2, z: 3 } } })).toEqual([]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 9 } })).toEqual([]);
|
||||
expect(parse({ type: 'sysEvent', jsonData: { eventType: 10 } })).toEqual([]);
|
||||
});
|
||||
|
||||
it('an empty or unknown event produces nothing', () => {
|
||||
expect(normalizeEvent({})).toEqual([]);
|
||||
expect(parse({ type: 'somethingNew', jsonData: {} })).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,208 @@
|
||||
import { measureTextWrap } from '@evenrealities/pretext';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { BODY_BOX, HUD, STATUS_BOX, renderHud } from '../src/core/hud.ts';
|
||||
import { paginate, utf8Bytes, type MeasureLines } from '../src/core/paginate.ts';
|
||||
import { toPlainText } from '../src/core/plaintext.ts';
|
||||
import { initialState, type AppState, type Phase } from '../src/core/reducer.ts';
|
||||
|
||||
/**
|
||||
* Layout tests use Even's own measurement library (@evenrealities/pretext),
|
||||
* which mirrors the firmware's LVGL line breaking. That makes these
|
||||
* measured-against-the-vendor-model tests, not device tests.
|
||||
*/
|
||||
const measure: MeasureLines = (text, width) => measureTextWrap(text, width).lineCount;
|
||||
const box = { width: BODY_BOX.innerWidth, maxLines: HUD.bodyLines, maxBytes: HUD.pageMaxBytes };
|
||||
|
||||
const squash = (s: string) => s.replace(/\s+/g, '');
|
||||
|
||||
function expectValidPages(source: string, pages: string[]) {
|
||||
for (const page of pages) {
|
||||
expect(measure(page, box.width)).toBeLessThanOrEqual(box.maxLines);
|
||||
expect(utf8Bytes(page)).toBeLessThanOrEqual(box.maxBytes);
|
||||
expect(page.length).toBeGreaterThan(0);
|
||||
}
|
||||
// Nothing dropped, nothing duplicated, order preserved (whitespace at page breaks aside).
|
||||
expect(squash(pages.join(' '))).toBe(squash(source));
|
||||
}
|
||||
|
||||
describe('geometry', () => {
|
||||
it('the two containers tile the 576x288 canvas exactly', () => {
|
||||
expect(BODY_BOX.height + STATUS_BOX.height).toBe(HUD.height);
|
||||
expect(STATUS_BOX.y).toBe(BODY_BOX.height);
|
||||
expect(BODY_BOX.height - 2 * HUD.padding).toBe(HUD.bodyLines * HUD.lineHeight);
|
||||
expect(STATUS_BOX.height - 2 * HUD.padding).toBeGreaterThanOrEqual(HUD.lineHeight);
|
||||
});
|
||||
});
|
||||
|
||||
describe('paginate', () => {
|
||||
it('keeps a short answer on one page', () => {
|
||||
expect(paginate('Paris.', box, measure)).toEqual(['Paris.']);
|
||||
expect(paginate(' ', box, measure)).toEqual(['']);
|
||||
});
|
||||
|
||||
it('splits a long answer into pages that each fit, losing nothing', () => {
|
||||
const sentence = 'The quick brown fox jumps over the lazy dog near the riverbank at dawn. ';
|
||||
const source = Array.from({ length: 80 }, (_, i) => `${i + 1}. ${sentence}`).join('').trim();
|
||||
const pages = paginate(source, box, measure);
|
||||
expect(pages.length).toBeGreaterThan(5);
|
||||
expectValidPages(source, pages);
|
||||
});
|
||||
|
||||
it('preserves line breaks inside a page', () => {
|
||||
const source = 'First line\nSecond line\n\nNew paragraph';
|
||||
expect(paginate(source, box, measure)).toEqual([source]);
|
||||
});
|
||||
|
||||
it('handles many short lines (lists) by line count, not characters', () => {
|
||||
const source = Array.from({ length: 40 }, (_, i) => `• item ${i + 1}`).join('\n');
|
||||
const pages = paginate(source, box, measure);
|
||||
expect(pages).toHaveLength(Math.ceil(40 / HUD.bodyLines));
|
||||
expectValidPages(source, pages);
|
||||
});
|
||||
|
||||
it('splits an unbroken run longer than a page without cutting a character in half', () => {
|
||||
const url = 'https://example.com/' + 'a'.repeat(1500);
|
||||
const pages = paginate(`See ${url} now`, box, measure);
|
||||
expectValidPages(`See ${url} now`, pages);
|
||||
});
|
||||
|
||||
it('keeps multi-byte text intact and within the byte budget', () => {
|
||||
const cjk = '東京は日本の首都であり、世界でも有数の大都市です。'.repeat(40);
|
||||
const accents = 'Voilà: café, naïve, façade, São Paulo, Zürich, Kraków, Ελληνικά, кириллица. '.repeat(30);
|
||||
for (const source of [cjk, accents]) {
|
||||
const pages = paginate(source, box, measure);
|
||||
expect(pages.length).toBeGreaterThan(1);
|
||||
expectValidPages(source, pages);
|
||||
// Every page is still well-formed UTF-16 (no lone surrogates).
|
||||
for (const page of pages) expect(page.isWellFormed()).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('never splits a surrogate pair or a ZWJ sequence', () => {
|
||||
const family = '👨👩👧👦';
|
||||
const source = family.repeat(300);
|
||||
const pages = paginate(source, box, measure);
|
||||
expectValidPages(source, pages);
|
||||
for (const page of pages) {
|
||||
expect(page.isWellFormed()).toBe(true);
|
||||
expect(page.length % family.length).toBe(0);
|
||||
}
|
||||
});
|
||||
|
||||
it('respects a byte budget tighter than the line budget', () => {
|
||||
const source = 'é'.repeat(4000); // 2 bytes each
|
||||
const pages = paginate(source, { ...box, maxLines: 1000 }, measure);
|
||||
for (const page of pages) expect(utf8Bytes(page)).toBeLessThanOrEqual(box.maxBytes);
|
||||
expect(pages.join('')).toBe(source);
|
||||
});
|
||||
|
||||
it('a 20,000-character answer paginates quickly', () => {
|
||||
const source = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '.repeat(360);
|
||||
const started = performance.now();
|
||||
const pages = paginate(source, box, measure);
|
||||
const elapsed = performance.now() - started;
|
||||
expectValidPages(source, pages);
|
||||
expect(elapsed).toBeLessThan(3000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('plain text', () => {
|
||||
it('removes Markdown markup but keeps every word', () => {
|
||||
const md = [
|
||||
'# Answer',
|
||||
'',
|
||||
'The **capital** of *France* is `Paris`. See [the docs](https://example.com/x) or [the anchor](#a).',
|
||||
'',
|
||||
'- one',
|
||||
'* two',
|
||||
'1. three',
|
||||
'',
|
||||
'> quoted',
|
||||
'',
|
||||
'```js',
|
||||
'const a = b * c * d;',
|
||||
'```',
|
||||
'',
|
||||
'| City | Country |',
|
||||
'|------|---------|',
|
||||
'| Paris | France |',
|
||||
'',
|
||||
'---',
|
||||
'<b>bold</b> & <br/>done',
|
||||
].join('\n');
|
||||
const out = toPlainText(md);
|
||||
expect(out).not.toMatch(/[`#>]|\*\*|\]\(|<b>|\|/);
|
||||
for (const word of ['Answer', 'capital', 'France', 'Paris', 'the docs (https://example.com/x)', 'or the anchor.', '• one', '• two', '1. three', 'quoted', 'const a = b * c * d;', 'City · Country', 'Paris · France', 'bold &\ndone']) {
|
||||
expect(out).toContain(word);
|
||||
}
|
||||
});
|
||||
|
||||
it('leaves ordinary punctuation, maths and identifiers alone', () => {
|
||||
for (const text of ['2 * 3 * 4 = 24', 'snake_case_name and another_one', 'a < b and b > c', 'Price: $5 * 2', 'Use the -v flag']) {
|
||||
expect(toPlainText(text)).toBe(text);
|
||||
}
|
||||
});
|
||||
|
||||
it('preserves Unicode and normalises line endings', () => {
|
||||
expect(toPlainText('Zürich — 東京 – naïve\r\nline two\r\n\r\n\r\n\r\nline three')).toBe('Zürich — 東京 – naïve\nline two\n\nline three');
|
||||
// Decomposed input is composed, so the firmware sees one code point per accented letter.
|
||||
expect(toPlainText('é')).toBe('é');
|
||||
});
|
||||
});
|
||||
|
||||
describe('HUD frames', () => {
|
||||
const phases: Phase[] = ['unpaired', 'ready', 'listening', 'transcribing', 'thinking', 'approval', 'answer', 'error', 'disconnected', 'cancelled'];
|
||||
const variants = (phase: Phase): AppState[] => {
|
||||
const base: AppState = { ...initialState, phase, captureStartedAt: 0, captureMaxSeconds: 120 };
|
||||
if (phase === 'answer') {
|
||||
return [
|
||||
{ ...base, pages: ['x'], pageIndex: 0 },
|
||||
{ ...base, pages: Array(99).fill('x'), pageIndex: 98, partial: true },
|
||||
];
|
||||
}
|
||||
if (phase === 'disconnected') {
|
||||
return [
|
||||
{ ...base, disconnectedFrom: 'glasses' },
|
||||
{ ...base, disconnectedFrom: 'bridge', canResend: true },
|
||||
{ ...base, disconnectedFrom: 'bridge', canResend: false },
|
||||
];
|
||||
}
|
||||
if (phase === 'error') {
|
||||
return [
|
||||
{ ...base, error: { code: 'outcome_unknown', hud: 'The outcome is unknown. Check the agent before asking again.', retryable: false } },
|
||||
{ ...base, error: { code: 'agent_timeout', hud: 'The agent ran out of time and was stopped. Actions it had already taken are not undone.', retryable: true } },
|
||||
];
|
||||
}
|
||||
if (phase === 'ready') return [base, { ...base, notice: 'New conversation' }];
|
||||
return [base];
|
||||
};
|
||||
|
||||
it('every status line fits on one HUD line and every body fits without scrolling', () => {
|
||||
for (const phase of phases) {
|
||||
for (const state of variants(phase)) {
|
||||
const frame = renderHud(state, 119_000);
|
||||
expect(measure(frame.status, STATUS_BOX.width - 2 * HUD.padding), `status of ${phase}: ${frame.status}`).toBe(1);
|
||||
expect(measure(frame.body, BODY_BOX.innerWidth), `body of ${phase}`).toBeLessThanOrEqual(HUD.bodyLines);
|
||||
expect(utf8Bytes(frame.body)).toBeLessThanOrEqual(999);
|
||||
expect(frame.status.length).toBeGreaterThan(0);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('the recording indicator and elapsed time are visible for the whole capture', () => {
|
||||
const state: AppState = { ...initialState, phase: 'listening', captureStartedAt: 10_000, captureMaxSeconds: 30 };
|
||||
expect(renderHud(state, 10_000).status).toContain('● REC 0:00 / 0:30');
|
||||
expect(renderHud(state, 17_400).status).toContain('● REC 0:07 / 0:30');
|
||||
expect(renderHud(state, 17_400).body).toContain('30 seconds');
|
||||
for (const phase of phases.filter((p) => p !== 'listening')) {
|
||||
expect(renderHud({ ...initialState, phase }, 0).status).not.toContain('REC');
|
||||
}
|
||||
});
|
||||
|
||||
it('errors offer a recovery action and send detail to the phone', () => {
|
||||
const frame = renderHud({ ...initialState, phase: 'error', error: { code: 'stt_empty', hud: 'No speech was recognised.', retryable: true } }, 0);
|
||||
expect(frame.body).toContain('No speech was recognised.');
|
||||
expect(frame.body).toContain('phone');
|
||||
expect(frame.status).toContain('Tap');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,332 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { SCHEMA_VERSION, type TurnResult } from '@hermeven/contracts';
|
||||
import { initialState, reduce, type AppEvent, type AppState, type Effect } from '../src/core/reducer.ts';
|
||||
|
||||
const ID1 = '11111111-1111-4111-8111-111111111111';
|
||||
const ID2 = '22222222-2222-4222-8222-222222222222';
|
||||
const CONV = 'cccccccc-cccc-4ccc-8ccc-cccccccccccc';
|
||||
|
||||
function run(state: AppState, ...events: AppEvent[]): { state: AppState; effects: Effect[] } {
|
||||
let effects: Effect[] = [];
|
||||
for (const event of events) {
|
||||
const step = reduce(state, event);
|
||||
state = step.state;
|
||||
effects = step.effects;
|
||||
}
|
||||
return { state, effects };
|
||||
}
|
||||
|
||||
const ready = () =>
|
||||
run(initialState, { type: 'init', paired: true, conversationId: CONV, captureMaxSeconds: 30, resumeRequestId: null }).state;
|
||||
|
||||
function result(requestId: string, over: Partial<TurnResult> = {}): TurnResult {
|
||||
return {
|
||||
schema_version: SCHEMA_VERSION,
|
||||
request_id: requestId,
|
||||
conversation_id: CONV,
|
||||
state: 'done',
|
||||
terminal: true,
|
||||
outcome: 'succeeded',
|
||||
cancel_requested: false,
|
||||
input_text: 'q',
|
||||
answer_text: 'a',
|
||||
error: null,
|
||||
replayed: false,
|
||||
created_at: '2026-01-01T00:00:00.000Z',
|
||||
updated_at: '2026-01-01T00:00:01.000Z',
|
||||
timings: { transcribe_ms: 1, agent_ms: 2 },
|
||||
...over,
|
||||
};
|
||||
}
|
||||
|
||||
describe('happy path', () => {
|
||||
it('READY -> LISTENING -> TRANSCRIBING -> THINKING -> ANSWER', () => {
|
||||
let s = ready();
|
||||
expect(s.phase).toBe('ready');
|
||||
|
||||
let step = reduce(s, { type: 'activate', freshId: ID1 });
|
||||
s = step.state;
|
||||
expect(s.phase).toBe('listening');
|
||||
expect(step.effects).toEqual([{ type: 'start_capture', generation: s.generation }]);
|
||||
|
||||
s = reduce(s, { type: 'capture_started', generation: s.generation, at: 1000 }).state;
|
||||
expect(s.captureStartedAt).toBe(1000);
|
||||
|
||||
step = reduce(s, { type: 'activate', freshId: ID2 });
|
||||
s = step.state;
|
||||
expect(s.phase).toBe('transcribing');
|
||||
// The request id chosen at activation is the one submitted: the stop tap's id is unused.
|
||||
expect(step.effects).toEqual([{ type: 'stop_capture_and_submit', generation: s.generation, requestId: ID1 }]);
|
||||
|
||||
s = reduce(s, {
|
||||
type: 'turn_update',
|
||||
generation: s.generation,
|
||||
result: result(ID1, { state: 'thinking', terminal: false, outcome: null, answer_text: null }),
|
||||
pages: null,
|
||||
}).state;
|
||||
expect(s.phase).toBe('thinking');
|
||||
|
||||
s = reduce(s, { type: 'turn_update', generation: s.generation, result: result(ID1), pages: ['p1', 'p2', 'p3'] }).state;
|
||||
expect(s.phase).toBe('answer');
|
||||
expect(s.pages).toEqual(['p1', 'p2', 'p3']);
|
||||
expect(s.pageIndex).toBe(0);
|
||||
});
|
||||
|
||||
it('a tap on the answer starts a follow-up in the same conversation', () => {
|
||||
let s = run(ready(), { type: 'submit_text', freshId: ID1, text: 'hi' }).state;
|
||||
s = reduce(s, { type: 'turn_update', generation: s.generation, result: result(ID1), pages: ['p'] }).state;
|
||||
const step = reduce(s, { type: 'activate', freshId: ID2 });
|
||||
expect(step.state.phase).toBe('listening');
|
||||
expect(step.state.conversationId).toBe(CONV);
|
||||
expect(step.state.requestId).toBe(ID2);
|
||||
expect(step.state.pages).toEqual([]);
|
||||
});
|
||||
|
||||
it('the capture cap stops and submits exactly once', () => {
|
||||
let s = run(ready(), { type: 'activate', freshId: ID1 }).state;
|
||||
const step = reduce(s, { type: 'capture_limit', generation: s.generation });
|
||||
expect(step.state.phase).toBe('transcribing');
|
||||
expect(step.effects).toHaveLength(1);
|
||||
s = step.state;
|
||||
expect(reduce(s, { type: 'capture_limit', generation: s.generation }).effects).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('paging', () => {
|
||||
it('moves within bounds and only in ANSWER', () => {
|
||||
let s = run(ready(), { type: 'submit_text', freshId: ID1, text: 'hi' }).state;
|
||||
expect(reduce(s, { type: 'next_page' }).state).toBe(s);
|
||||
s = reduce(s, { type: 'turn_update', generation: s.generation, result: result(ID1), pages: ['a', 'b', 'c'] }).state;
|
||||
s = run(s, { type: 'next_page' }, { type: 'next_page' }, { type: 'next_page' }).state;
|
||||
expect(s.pageIndex).toBe(2);
|
||||
s = run(s, { type: 'prev_page' }, { type: 'prev_page' }, { type: 'prev_page' }, { type: 'prev_page' }).state;
|
||||
expect(s.pageIndex).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('cancellation and stale results', () => {
|
||||
it('double-tap while listening discards the capture and never submits', () => {
|
||||
const s = run(ready(), { type: 'activate', freshId: ID1 }).state;
|
||||
const step = reduce(s, { type: 'back' });
|
||||
expect(step.state.phase).toBe('cancelled');
|
||||
expect(step.effects).toEqual([{ type: 'discard_capture' }]);
|
||||
expect(step.state.generation).toBe(s.generation + 1);
|
||||
});
|
||||
|
||||
it('cancel while thinking asks the bridge to cancel, and a late answer cannot resurrect the UI', () => {
|
||||
const s = run(ready(), { type: 'submit_text', freshId: ID1, text: 'hi' }).state;
|
||||
const step = reduce(s, { type: 'back' });
|
||||
expect(step.state.phase).toBe('cancelled');
|
||||
expect(step.effects).toContainEqual({ type: 'cancel_turn', requestId: ID1 });
|
||||
const late = reduce(step.state, { type: 'turn_update', generation: s.generation, result: result(ID1), pages: ['LATE'] });
|
||||
expect(late.state).toBe(step.state);
|
||||
expect(late.state.pages).toEqual([]);
|
||||
});
|
||||
|
||||
it('a late result for an older request cannot replace a newer answer', () => {
|
||||
let s = run(ready(), { type: 'submit_text', freshId: ID1, text: 'first' }).state;
|
||||
const gen1 = s.generation;
|
||||
s = reduce(s, { type: 'back' }).state; // cancelled
|
||||
s = reduce(s, { type: 'submit_text', freshId: ID2, text: 'second' }).state;
|
||||
s = reduce(s, { type: 'turn_update', generation: s.generation, result: result(ID2), pages: ['NEW'] }).state;
|
||||
expect(s.pages).toEqual(['NEW']);
|
||||
// Old generation.
|
||||
expect(reduce(s, { type: 'turn_update', generation: gen1, result: result(ID1), pages: ['OLD'] }).state).toBe(s);
|
||||
// Right generation, wrong request id.
|
||||
expect(reduce(s, { type: 'turn_update', generation: s.generation, result: result(ID1), pages: ['OLD'] }).state).toBe(s);
|
||||
});
|
||||
|
||||
it('a late result for a cancelled request is ignored while a newer request is still in flight', () => {
|
||||
let s = run(ready(), { type: 'submit_text', freshId: ID1, text: 'first' }).state;
|
||||
const gen1 = s.generation;
|
||||
s = reduce(s, { type: 'back' }).state;
|
||||
s = reduce(s, { type: 'submit_text', freshId: ID2, text: 'second' }).state;
|
||||
expect(s.phase).toBe('thinking');
|
||||
// The old turn finishes late, under its old generation.
|
||||
expect(reduce(s, { type: 'turn_update', generation: gen1, result: result(ID1), pages: ['OLD'] }).state).toBe(s);
|
||||
// Even mislabelled with the current generation, the request id does not match.
|
||||
expect(reduce(s, { type: 'turn_update', generation: s.generation, result: result(ID1), pages: ['OLD'] }).state).toBe(s);
|
||||
// And an old-generation event cannot smuggle in the current request id either.
|
||||
expect(reduce(s, { type: 'turn_update', generation: gen1, result: result(ID2), pages: ['OLD'] }).state).toBe(s);
|
||||
});
|
||||
|
||||
it('stale capture and network events are ignored', () => {
|
||||
let s = run(ready(), { type: 'activate', freshId: ID1 }).state;
|
||||
const gen = s.generation;
|
||||
s = reduce(s, { type: 'back' }).state;
|
||||
for (const event of [
|
||||
{ type: 'capture_started', generation: gen, at: 5 },
|
||||
{ type: 'capture_failed', generation: gen, reason: 'mic_refused' },
|
||||
{ type: 'capture_limit', generation: gen },
|
||||
{ type: 'network_lost', generation: gen, stage: 'submit' },
|
||||
{ type: 'turn_rejected', generation: gen, error: { code: 'x', hud: 'x', retryable: false } },
|
||||
] as AppEvent[]) {
|
||||
expect(reduce(s, event).state).toBe(s);
|
||||
}
|
||||
});
|
||||
|
||||
it('a second cancel is a no-op', () => {
|
||||
let s = run(ready(), { type: 'submit_text', freshId: ID1, text: 'hi' }, { type: 'cancel' }).state;
|
||||
const again = reduce(s, { type: 'cancel' });
|
||||
expect(again.state).toBe(s);
|
||||
expect(again.effects).toEqual([]);
|
||||
s = again.state;
|
||||
expect(s.phase).toBe('cancelled');
|
||||
});
|
||||
|
||||
it('back from ANSWER, ERROR and CANCELLED returns to READY; back from READY asks the OS to exit', () => {
|
||||
let s = run(ready(), { type: 'submit_text', freshId: ID1, text: 'hi' }).state;
|
||||
s = reduce(s, { type: 'turn_update', generation: s.generation, result: result(ID1), pages: ['p'] }).state;
|
||||
s = reduce(s, { type: 'back' }).state;
|
||||
expect(s.phase).toBe('ready');
|
||||
expect(reduce(s, { type: 'back' }).effects).toEqual([{ type: 'exit_dialog' }]);
|
||||
expect(reduce(initialState, { type: 'back' }).effects).toEqual([{ type: 'exit_dialog' }]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('outcomes', () => {
|
||||
const thinking = () => run(ready(), { type: 'submit_text', freshId: ID1, text: 'hi' }).state;
|
||||
|
||||
it('incomplete answers are shown and flagged as partial', () => {
|
||||
const s = thinking();
|
||||
const next = reduce(s, {
|
||||
type: 'turn_update',
|
||||
generation: s.generation,
|
||||
result: result(ID1, { outcome: 'incomplete', error: { code: 'agent_incomplete', message: 'partial', retryable: false } }),
|
||||
pages: ['half'],
|
||||
}).state;
|
||||
expect(next.phase).toBe('answer');
|
||||
expect(next.partial).toBe(true);
|
||||
});
|
||||
|
||||
it('failed and unknown become ERROR with the stable code; unknown is not retryable', () => {
|
||||
const s = thinking();
|
||||
const unknown = reduce(s, {
|
||||
type: 'turn_update',
|
||||
generation: s.generation,
|
||||
result: result(ID1, { outcome: 'unknown', answer_text: null, error: { code: 'outcome_unknown', message: 'Unknown.', retryable: false } }),
|
||||
pages: null,
|
||||
}).state;
|
||||
expect(unknown.phase).toBe('error');
|
||||
expect(unknown.error).toEqual({ code: 'outcome_unknown', hud: 'Unknown.', retryable: false });
|
||||
});
|
||||
|
||||
it('approval-required is its own phase and can still be cancelled', () => {
|
||||
let s = thinking();
|
||||
s = reduce(s, {
|
||||
type: 'turn_update',
|
||||
generation: s.generation,
|
||||
result: result(ID1, { state: 'approval_required', terminal: false, outcome: null, answer_text: null }),
|
||||
pages: null,
|
||||
}).state;
|
||||
expect(s.phase).toBe('approval');
|
||||
const step = reduce(s, { type: 'back' });
|
||||
expect(step.state.phase).toBe('cancelled');
|
||||
expect(step.effects).toContainEqual({ type: 'cancel_turn', requestId: ID1 });
|
||||
});
|
||||
|
||||
it('a stopped-by-deadline turn is an error, a user cancel confirmed by the bridge is CANCELLED', () => {
|
||||
const s = thinking();
|
||||
const timeout = reduce(s, {
|
||||
type: 'turn_update',
|
||||
generation: s.generation,
|
||||
result: result(ID1, { outcome: 'cancelled', answer_text: null, error: { code: 'agent_timeout', message: 'Stopped.', retryable: false } }),
|
||||
pages: null,
|
||||
}).state;
|
||||
expect(timeout.phase).toBe('error');
|
||||
const plain = reduce(s, {
|
||||
type: 'turn_update',
|
||||
generation: s.generation,
|
||||
result: result(ID1, { outcome: 'cancelled', answer_text: null }),
|
||||
pages: null,
|
||||
}).state;
|
||||
expect(plain.phase).toBe('cancelled');
|
||||
});
|
||||
});
|
||||
|
||||
describe('connection loss', () => {
|
||||
it('submit never delivered: DISCONNECTED with a safe resend of the same request id', () => {
|
||||
let s = run(ready(), { type: 'submit_text', freshId: ID1, text: 'hi' }).state;
|
||||
s = reduce(s, { type: 'network_lost', generation: s.generation, stage: 'submit' }).state;
|
||||
expect(s.phase).toBe('disconnected');
|
||||
expect(s.canResend).toBe(true);
|
||||
const step = reduce(s, { type: 'activate', freshId: ID2 });
|
||||
expect(step.effects).toEqual([{ type: 'resubmit', generation: s.generation, requestId: ID1 }]);
|
||||
expect(step.state.requestId).toBe(ID1);
|
||||
});
|
||||
|
||||
it('lost while waiting: no resend is offered, and the answer is still accepted when it arrives', () => {
|
||||
let s = run(ready(), { type: 'submit_text', freshId: ID1, text: 'hi' }).state;
|
||||
s = reduce(s, { type: 'network_lost', generation: s.generation, stage: 'poll' }).state;
|
||||
expect(s.canResend).toBe(false);
|
||||
expect(reduce(s, { type: 'activate', freshId: ID2 }).effects).toEqual([]);
|
||||
const restored = reduce(s, { type: 'network_restored', generation: s.generation }).state;
|
||||
expect(restored.phase).toBe('thinking');
|
||||
const answered = reduce(s, { type: 'turn_update', generation: s.generation, result: result(ID1), pages: ['p'] }).state;
|
||||
expect(answered.phase).toBe('answer');
|
||||
});
|
||||
|
||||
it('glasses disconnect while listening stops capture; reconnect never reopens the microphone', () => {
|
||||
let s = run(ready(), { type: 'activate', freshId: ID1 }).state;
|
||||
const step = reduce(s, { type: 'glasses_status', connected: false });
|
||||
expect(step.effects).toEqual([{ type: 'discard_capture' }]);
|
||||
expect(step.state.phase).toBe('disconnected');
|
||||
s = step.state;
|
||||
const back = reduce(s, { type: 'glasses_status', connected: true });
|
||||
expect(back.state.phase).toBe('ready');
|
||||
expect(back.effects).toEqual([]);
|
||||
});
|
||||
|
||||
it('glasses disconnect during a running turn keeps the turn', () => {
|
||||
const s = run(ready(), { type: 'submit_text', freshId: ID1, text: 'hi' }).state;
|
||||
expect(reduce(s, { type: 'glasses_status', connected: false }).state).toBe(s);
|
||||
});
|
||||
|
||||
it('app exit while listening stops capture', () => {
|
||||
const s = run(ready(), { type: 'activate', freshId: ID1 }).state;
|
||||
const step = reduce(s, { type: 'app_exit' });
|
||||
expect(step.effects).toEqual([{ type: 'discard_capture' }]);
|
||||
expect(step.state.phase).toBe('ready');
|
||||
});
|
||||
});
|
||||
|
||||
describe('pairing, restore and conversations', () => {
|
||||
it('unpaired ignores taps', () => {
|
||||
const step = reduce(initialState, { type: 'activate', freshId: ID1 });
|
||||
expect(step.state.phase).toBe('unpaired');
|
||||
expect(step.effects).toEqual([]);
|
||||
});
|
||||
|
||||
it('restoring an in-flight turn polls it and neither resubmits nor records', () => {
|
||||
const step = reduce(initialState, {
|
||||
type: 'init',
|
||||
paired: true,
|
||||
conversationId: CONV,
|
||||
captureMaxSeconds: 20,
|
||||
resumeRequestId: ID1,
|
||||
});
|
||||
expect(step.state.phase).toBe('thinking');
|
||||
expect(step.effects).toEqual([{ type: 'poll', generation: step.state.generation, requestId: ID1 }]);
|
||||
});
|
||||
|
||||
it('new conversation is refused mid-turn and resets cleanly at rest', () => {
|
||||
const busy = run(ready(), { type: 'submit_text', freshId: ID1, text: 'hi' }).state;
|
||||
expect(reduce(busy, { type: 'new_conversation' }).state).toBe(busy);
|
||||
|
||||
const s = ready();
|
||||
const step = reduce(s, { type: 'new_conversation' });
|
||||
expect(step.state.conversationId).toBeNull();
|
||||
expect(step.effects).toEqual([{ type: 'create_conversation', generation: step.state.generation }]);
|
||||
const stale = reduce(step.state, { type: 'conversation_created', generation: s.generation, conversationId: 'old' });
|
||||
expect(stale.state.conversationId).toBeNull();
|
||||
const fresh = reduce(step.state, { type: 'conversation_created', generation: step.state.generation, conversationId: 'new' });
|
||||
expect(fresh.state.conversationId).toBe('new');
|
||||
});
|
||||
|
||||
it('unpairing while listening closes the microphone', () => {
|
||||
const s = run(ready(), { type: 'activate', freshId: ID1 }).state;
|
||||
const step = reduce(s, { type: 'unpaired' });
|
||||
expect(step.state.phase).toBe('unpaired');
|
||||
expect(step.effects).toEqual([{ type: 'discard_capture' }]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user