import { describe, expect, it, vi } from 'vitest'; import type { ServerEvent } from '../../core/protocol.js'; import { deriveAvatarState, type AvatarStateSource } from './avatar-state.js'; /** Minimal stand-in for RealtimeClient/ResilientRealtimeClient — only .on() is used. */ class FakeSource implements AvatarStateSource { private handler: ((event: ServerEvent) => void) | null = null; on(handler: (event: ServerEvent) => void): () => void { this.handler = handler; return () => { this.handler = null; }; } emit(event: ServerEvent): void { this.handler?.(event); } } describe('deriveAvatarState', () => { it('subscribe() emits the current state immediately', () => { const store = deriveAvatarState(new FakeSource()); const run = vi.fn(); store.subscribe(run); expect(run).toHaveBeenCalledTimes(1); expect(run).toHaveBeenCalledWith('idle'); }); it('walks listening -> thinking -> talking -> idle across a full voice turn', () => { const source = new FakeSource(); const store = deriveAvatarState(source); const seen: string[] = []; store.subscribe((s) => seen.push(s)); store.setListening(true); store.setListening(false); source.emit({ type: 'output_audio.chunk', audio: '' }); source.emit({ type: 'output_audio.done' }); expect(seen).toEqual(['idle', 'listening', 'thinking', 'talking', 'idle']); }); it('notifyResponseRequested() drives thinking for the typed-text path', () => { const source = new FakeSource(); const store = deriveAvatarState(source); const seen: string[] = []; store.subscribe((s) => seen.push(s)); store.notifyResponseRequested(); source.emit({ type: 'transcript.delta', role: 'assistant', text: 'hi' }); source.emit({ type: 'transcript.done', role: 'assistant' }); expect(seen).toEqual(['idle', 'thinking', 'talking', 'idle']); }); it('ignores user transcript events (no signal either way)', () => { const source = new FakeSource(); const store = deriveAvatarState(source); const seen: string[] = []; store.subscribe((s) => seen.push(s)); source.emit({ type: 'transcript.delta', role: 'user', text: 'hello' }); expect(seen).toEqual(['idle']); }); it('session.ready and session.closed reset to idle from any state', () => { const source = new FakeSource(); const store = deriveAvatarState(source); const seen: string[] = []; store.subscribe((s) => seen.push(s)); source.emit({ type: 'output_audio.chunk', audio: '' }); // -> talking source.emit({ type: 'session.closed', reason: 'client_requested' }); expect(seen).toEqual(['idle', 'talking', 'idle']); }); it('response.done is a fallback that stops talking even without output_audio.done', () => { const source = new FakeSource(); const store = deriveAvatarState(source); const seen: string[] = []; store.subscribe((s) => seen.push(s)); source.emit({ type: 'output_audio.chunk', audio: '' }); source.emit({ type: 'response.done' }); expect(seen).toEqual(['idle', 'talking', 'idle']); }); it('destroy() stops reacting to further events', () => { const source = new FakeSource(); const store = deriveAvatarState(source); const seen: string[] = []; store.subscribe((s) => seen.push(s)); store.destroy(); source.emit({ type: 'output_audio.chunk', audio: '' }); expect(seen).toEqual(['idle']); }); });