|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act } from 'react' |
| 3 | +import { createRoot, type Root } from 'react-dom/client' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import type { useSpeechToText } from '@/hooks/use-speech-to-text' |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ speech: vi.fn<typeof useSpeechToText>() })) |
| 8 | +vi.mock('@/hooks/use-speech-to-text', () => ({ useSpeechToText: mocks.speech })) |
| 9 | + |
| 10 | +import { useVoiceInput } from '@/hooks/use-voice-input' |
| 11 | + |
| 12 | +let root: Root |
| 13 | +let container: HTMLDivElement |
| 14 | +let value: string |
| 15 | +let voice: ReturnType<typeof useVoiceInput> |
| 16 | + |
| 17 | +function Harness() { |
| 18 | + voice = useVoiceInput({ |
| 19 | + organizationId: 'organization-a', |
| 20 | + getValue: () => value, |
| 21 | + onChange: (next) => { |
| 22 | + value = next |
| 23 | + }, |
| 24 | + }) |
| 25 | + return null |
| 26 | +} |
| 27 | + |
| 28 | +function transcript(text: string) { |
| 29 | + act(() => mocks.speech.mock.calls.at(-1)![0].onTranscript(text)) |
| 30 | +} |
| 31 | + |
| 32 | +beforeEach(() => { |
| 33 | + vi.clearAllMocks() |
| 34 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 35 | + mocks.speech.mockReturnValue({ |
| 36 | + isListening: false, |
| 37 | + isSupported: true, |
| 38 | + audioLevelsRef: { current: new Float32Array(5) }, |
| 39 | + toggleListening: vi.fn(), |
| 40 | + resetTranscript: vi.fn(), |
| 41 | + }) |
| 42 | + value = 'Find' |
| 43 | + container = document.createElement('div') |
| 44 | + document.body.appendChild(container) |
| 45 | + root = createRoot(container) |
| 46 | + act(() => root.render(<Harness />)) |
| 47 | + act(() => voice.toggleListening()) |
| 48 | +}) |
| 49 | + |
| 50 | +afterEach(() => { |
| 51 | + act(() => root.unmount()) |
| 52 | + container.remove() |
| 53 | + vi.unstubAllGlobals() |
| 54 | +}) |
| 55 | + |
| 56 | +describe('voice input manual edits', () => { |
| 57 | + it('preserves edits made before the first transcript arrives', () => { |
| 58 | + value = 'Summarize' |
| 59 | + transcript('the report') |
| 60 | + expect(value).toBe('Summarize the report') |
| 61 | + }) |
| 62 | + |
| 63 | + it('preserves a changed prefix while updating partial speech', () => { |
| 64 | + transcript('the repor') |
| 65 | + value = 'Summarize the repor' |
| 66 | + transcript('the report') |
| 67 | + expect(value).toBe('Summarize the report') |
| 68 | + transcript('the report from June') |
| 69 | + expect(value).toBe('Summarize the report from June') |
| 70 | + }) |
| 71 | + |
| 72 | + it.each(['Replacement draft', ''])( |
| 73 | + 'preserves a replaced or cleared draft (%s)', |
| 74 | + (replacement) => { |
| 75 | + transcript('the report') |
| 76 | + value = replacement |
| 77 | + transcript('the report') |
| 78 | + expect(value).toBe(replacement) |
| 79 | + transcript('the report from June') |
| 80 | + expect(value).toBe(replacement ? `${replacement} from June` : 'from June') |
| 81 | + } |
| 82 | + ) |
| 83 | + |
| 84 | + it('preserves corrections inside dictated text across later partial updates', () => { |
| 85 | + transcript('the red report') |
| 86 | + value = 'Find the blue report' |
| 87 | + transcript('the red report from June') |
| 88 | + expect(value).toBe('Find the blue report from June') |
| 89 | + transcript('the red report from July') |
| 90 | + expect(value).toBe('Find the blue report from July') |
| 91 | + }) |
| 92 | + |
| 93 | + it('keeps manual suffixes after speech revisions and before new speech', () => { |
| 94 | + transcript('the red report') |
| 95 | + value += ' and notes' |
| 96 | + transcript('the blue report') |
| 97 | + expect(value).toBe('Find the blue report and notes') |
| 98 | + transcript('the blue report from June') |
| 99 | + expect(value).toBe('Find the blue report and notes from June') |
| 100 | + }) |
| 101 | + |
| 102 | + it('prefers the manual correction when speech revises the same word', () => { |
| 103 | + value = '' |
| 104 | + act(() => voice.toggleListening()) |
| 105 | + transcript('hel') |
| 106 | + value = 'help' |
| 107 | + transcript('hello') |
| 108 | + expect(value).toBe('help') |
| 109 | + transcript('hello again') |
| 110 | + expect(value).toBe('help again') |
| 111 | + }) |
| 112 | + |
| 113 | + it('keeps appended speech when another revision conflicts with a manual correction', () => { |
| 114 | + transcript('the red report') |
| 115 | + value = 'Find the blue report' |
| 116 | + transcript('the green report from June') |
| 117 | + expect(value).toBe('Find the blue report from June') |
| 118 | + transcript('the green report from June and July') |
| 119 | + expect(value).toBe('Find the blue report from June and July') |
| 120 | + }) |
| 121 | + |
| 122 | + it('preserves separate manual edits around an independently revised word', () => { |
| 123 | + transcript('the red report') |
| 124 | + value = 'Summarize the red report and notes' |
| 125 | + transcript('the green report from June') |
| 126 | + expect(value).toBe('Summarize the green report and notes from June') |
| 127 | + }) |
| 128 | + |
| 129 | + it('starts fresh after the draft and transcript are cleared on submit', () => { |
| 130 | + transcript('the report') |
| 131 | + value = '' |
| 132 | + act(() => voice.resetTranscript()) |
| 133 | + transcript('Next question') |
| 134 | + expect(value).toBe('Next question') |
| 135 | + }) |
| 136 | + |
| 137 | + it('continues dictation after a large draft is manually replaced', () => { |
| 138 | + const longTranscript = Array.from({ length: 300 }, (_, index) => `word${index}`).join(' ') |
| 139 | + transcript(longTranscript) |
| 140 | + value = 'New draft' |
| 141 | + transcript(`${longTranscript} next question`) |
| 142 | + expect(value).toBe('New draft next question') |
| 143 | + }) |
| 144 | +}) |
0 commit comments