mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-19 03:40:50 +00:00
* feat: add realtime dictation providers * fix: cancel realtime dictation startup * fix: refresh local dictation availability * fix: preserve dictation on disconnect * fix: normalize OpenAI language hints
62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
import { act, renderHook, waitFor } from '@testing-library/react'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import type { ApiClient } from '@/api/client'
|
|
import { useRealtimeDictation } from './useRealtimeDictation'
|
|
|
|
const scribe = vi.hoisted(() => ({
|
|
options: null as unknown,
|
|
connect: vi.fn(async () => {}),
|
|
disconnect: vi.fn(),
|
|
commit: vi.fn()
|
|
}))
|
|
|
|
vi.mock('@elevenlabs/react', () => ({
|
|
CommitStrategy: { MANUAL: 'manual' },
|
|
useScribe: (options: unknown) => {
|
|
scribe.options = options
|
|
return {
|
|
connect: scribe.connect,
|
|
disconnect: scribe.disconnect,
|
|
commit: scribe.commit
|
|
}
|
|
}
|
|
}))
|
|
|
|
type ScribeCallbacks = {
|
|
onPartialTranscript: (event: { text: string }) => void
|
|
onDisconnect: () => void
|
|
}
|
|
|
|
describe('useRealtimeDictation', () => {
|
|
afterEach(() => vi.clearAllMocks())
|
|
|
|
it('preserves partial ElevenLabs text on an unexpected disconnect', async () => {
|
|
Object.defineProperty(navigator, 'mediaDevices', {
|
|
configurable: true,
|
|
value: { getUserMedia: vi.fn() }
|
|
})
|
|
const api = {
|
|
fetchRealtimeTranscriptionToken: vi.fn(async () => ({ token: 'single-use-token' }))
|
|
} as unknown as ApiClient
|
|
const onFinalTranscript = vi.fn()
|
|
const { result } = renderHook(() => useRealtimeDictation({
|
|
api,
|
|
provider: 'elevenlabs',
|
|
mode: 'realtime',
|
|
onFinalTranscript
|
|
}))
|
|
|
|
await act(() => result.current.toggle())
|
|
const callbacks = scribe.options as ScribeCallbacks
|
|
act(() => callbacks.onPartialTranscript({ text: 'spoken words' }))
|
|
expect(onFinalTranscript).not.toHaveBeenCalled()
|
|
|
|
act(() => callbacks.onDisconnect())
|
|
|
|
await waitFor(() => expect(result.current.status).toBe('error'))
|
|
expect(result.current.error).toBe('ElevenLabs realtime transcription disconnected')
|
|
expect(result.current.partialTranscript).toBe('')
|
|
expect(onFinalTranscript).toHaveBeenCalledWith('spoken words')
|
|
})
|
|
})
|