import { describe, it, expect, vi, beforeEach } from 'vitest' import { renderHook } from '@testing-library/react' let onToggleCallback: ((transcribing: boolean) => void) | undefined const sendMeetingAudioChunk = vi.fn() const sendMeetingRecordingStopped = vi.fn() const onToggleMeetingTranscription = vi.fn((cb: (transcribing: boolean) => void) => { onToggleCallback = cb return () => {} }) function fakeStream(): MediaStream { const stop = vi.fn() return { getTracks: () => [{ stop }] } as unknown as MediaStream } const getUserMedia = vi.fn(async () => fakeStream()) const enumerateDevices = vi.fn(async () => []) class FakeMediaRecorder { static instances: FakeMediaRecorder[] = [] state: 'recording' | 'inactive' = 'recording' ondataavailable: ((event: { data: Blob }) => void) | null = null onstop: (() => void) | null = null constructor( public stream: MediaStream, public options: { mimeType: string } ) { FakeMediaRecorder.instances.push(this) } start(): void { this.state = 'recording' } stop(): void { this.state = 'inactive' this.ondataavailable?.({ data: new Blob(['chunk'], { type: 'audio/webm' }) }) this.onstop?.() } } const { useMeetingRecorder } = await import('../../../src/renderer/src/voice/useMeetingRecorder') describe('useMeetingRecorder()', () => { beforeEach(() => { vi.clearAllMocks() FakeMediaRecorder.instances = [] ;(window as unknown as { api: Record }).api = { onToggleMeetingTranscription, sendMeetingAudioChunk, sendMeetingRecordingStopped } Object.defineProperty(navigator, 'mediaDevices', { value: { getUserMedia, enumerateDevices }, configurable: true }) vi.stubGlobal('MediaRecorder', FakeMediaRecorder) }) it('should start recording the microphone when transcription is toggled on', async () => { renderHook(() => useMeetingRecorder()) onToggleCallback?.(true) await vi.waitFor(() => expect(FakeMediaRecorder.instances.length).toBe(1)) expect(getUserMedia).toHaveBeenCalledWith({ audio: true }) }) it('should give up quietly when the microphone is unavailable, without ever starting a recorder', async () => { getUserMedia.mockRejectedValueOnce(new Error('no mic')) renderHook(() => useMeetingRecorder()) onToggleCallback?.(true) await vi.waitFor(() => expect(getUserMedia).toHaveBeenCalled()) // Rien n'a jamais demarre : stopper ensuite ne doit ni planter ni envoyer de notification pour // une session qui n'a jamais existe. expect(() => onToggleCallback?.(false)).not.toThrow() expect(FakeMediaRecorder.instances.length).toBe(0) }) it('should send the recorded chunk and notify the main process when stopped', async () => { renderHook(() => useMeetingRecorder()) onToggleCallback?.(true) await vi.waitFor(() => expect(FakeMediaRecorder.instances.length).toBe(1)) onToggleCallback?.(false) await vi.waitFor(() => expect(sendMeetingAudioChunk).toHaveBeenCalled()) expect(sendMeetingRecordingStopped).toHaveBeenCalled() }) it('should start a new segment automatically instead of stopping when not asked to stop', async () => { renderHook(() => useMeetingRecorder()) onToggleCallback?.(true) await vi.waitFor(() => expect(FakeMediaRecorder.instances.length).toBe(1)) FakeMediaRecorder.instances[0]?.stop() await vi.waitFor(() => expect(FakeMediaRecorder.instances.length).toBe(2)) expect(sendMeetingRecordingStopped).not.toHaveBeenCalled() }) })