import { describe, it, expect, vi, beforeEach } from 'vitest' import { renderHook } from '@testing-library/react' const bindSpeechAvatar = vi.fn() const enqueueSpeech = vi.fn(async () => {}) vi.mock('../../../src/renderer/src/voice/speechQueue', () => ({ bindSpeechAvatar, enqueueSpeech })) let onProactiveSpeechCallback: ((audio: ArrayBuffer) => void) | undefined const onProactiveSpeech = vi.fn((cb: (audio: ArrayBuffer) => void) => { onProactiveSpeechCallback = cb return () => {} }) const { useProactiveSpeech } = await import('../../../src/renderer/src/voice/useProactiveSpeech') describe('useProactiveSpeech()', () => { beforeEach(() => { vi.clearAllMocks() ;(window as unknown as { api: Record }).api = { onProactiveSpeech } }) it('should bind the avatar ref to the shared speech queue', () => { const avatarRef = { current: null } renderHook(() => useProactiveSpeech(avatarRef)) expect(bindSpeechAvatar).toHaveBeenCalledWith(avatarRef) }) it('should enqueue any audio received via onProactiveSpeech', () => { renderHook(() => useProactiveSpeech({ current: null })) const audio = new ArrayBuffer(4) onProactiveSpeechCallback?.(audio) expect(enqueueSpeech).toHaveBeenCalledWith(audio) }) it('should report speaking then idle around playback (used by the avatar status indicator for text-turn replies)', async () => { const onStateChange = vi.fn() renderHook(() => useProactiveSpeech({ current: null }, onStateChange)) onProactiveSpeechCallback?.(new ArrayBuffer(4)) expect(onStateChange).toHaveBeenCalledWith('speaking') await Promise.resolve() await Promise.resolve() expect(onStateChange).toHaveBeenCalledWith('idle') }) })