import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import InputBar from '../../../src/renderer/src/components/InputBar'
describe('', () => {
it('should send the trimmed text and clear the field on Enter', () => {
const onSend = vi.fn()
render()
const input = screen.getByLabelText('Message')
fireEvent.change(input, { target: { value: ' bonjour ' } })
fireEvent.keyDown(input, { key: 'Enter' })
expect(onSend).toHaveBeenCalledWith('bonjour')
expect(input.value).toBe('')
})
it('should not send on Shift+Enter (newline)', () => {
const onSend = vi.fn()
render()
const input = screen.getByLabelText('Message')
fireEvent.change(input, { target: { value: 'ligne 1' } })
fireEvent.keyDown(input, { key: 'Enter', shiftKey: true })
expect(onSend).not.toHaveBeenCalled()
})
it('should send via the send button and disable it when the field is empty', () => {
const onSend = vi.fn()
render()
const sendButton = screen.getByRole('button', { name: 'Envoyer le message' })
expect(sendButton).toBeDisabled()
fireEvent.change(screen.getByLabelText('Message'), { target: { value: 'salut' } })
expect(sendButton).toBeEnabled()
fireEvent.click(sendButton)
expect(onSend).toHaveBeenCalledWith('salut')
})
it('should not send when disabled', () => {
const onSend = vi.fn()
render()
fireEvent.change(screen.getByLabelText('Message'), { target: { value: 'salut' } })
fireEvent.keyDown(screen.getByLabelText('Message'), { key: 'Enter' })
expect(onSend).not.toHaveBeenCalled()
})
it('should call onVoiceStart then onVoiceStop when the microphone button is toggled', () => {
const onVoiceStart = vi.fn()
const onVoiceStop = vi.fn()
render()
const micButton = screen.getByRole('button', { name: 'Activer le microphone' })
fireEvent.click(micButton)
expect(onVoiceStart).toHaveBeenCalledOnce()
fireEvent.click(screen.getByRole('button', { name: "Arreter l'enregistrement vocal" }))
expect(onVoiceStop).toHaveBeenCalledOnce()
})
})