import { describe, it, expect, vi, beforeEach } from 'vitest' import { render, screen, act, fireEvent, waitFor } from '@testing-library/react' import type { VoiceState } from '../../../src/renderer/src/voice/useVoiceAssistant' vi.mock('../../../src/renderer/src/components/AvatarViewer', () => ({ default: ({ onReady }: { onReady: (handle: unknown) => void }) => { onReady({ playAnimation: vi.fn(), playAnimationByName: vi.fn(), animationNames: [] }) return
} })) let capturedOnStateChange: ((state: VoiceState) => void) | undefined let capturedOnError: ((message: string) => void) | undefined const useVoiceAssistant = vi.fn( ( _ref: unknown, _enabled: boolean, onStateChange?: (state: VoiceState) => void, onError?: (message: string) => void ) => { capturedOnStateChange = onStateChange capturedOnError = onError } ) vi.mock('../../../src/renderer/src/voice/useVoiceAssistant', () => ({ useVoiceAssistant })) vi.mock('../../../src/renderer/src/voice/useProactiveSpeech', () => ({ useProactiveSpeech: vi.fn() })) let onUpdateAvailableCb: (() => void) | undefined let onUpdateReadyCb: (() => void) | undefined const showContextMenu = vi.fn() const getWindowBounds = vi.fn(async () => ({ x: 0, y: 0, width: 100, height: 100 })) const setWindowPosition = vi.fn() const getAuthStatus = vi.fn(async (): Promise<{ loggedIn: boolean; email: string | null }> => ({ loggedIn: false, email: null })) function setupWindowApi(): void { ;(window as unknown as { api: Record }).api = { onUpdateAvailable: vi.fn((cb: () => void) => { onUpdateAvailableCb = cb return () => {} }), onUpdateReady: vi.fn((cb: () => void) => { onUpdateReadyCb = cb return () => {} }), onToggleListening: vi.fn(() => () => {}), onProactiveSpeech: vi.fn(() => () => {}), showContextMenu, getWindowBounds, setWindowPosition, getAuthStatus } } const { default: App } = await import('../../../src/renderer/src/App') describe('', () => { beforeEach(() => { vi.clearAllMocks() setupWindowApi() }) it('should render the avatar viewer', () => { render() expect(screen.getByTestId('avatar-viewer')).toBeInTheDocument() }) it('should show the thinking indicator while processing', () => { render() act(() => capturedOnStateChange?.('processing')) expect(screen.getByText('Max reflechit...')).toBeInTheDocument() }) it('should show a voice error banner and clear it when a new state arrives', () => { vi.useFakeTimers() render() act(() => capturedOnError?.('Erreur reseau')) expect(screen.getByText(/Erreur reseau/)).toBeInTheDocument() act(() => { vi.advanceTimersByTime(5000) }) expect(screen.queryByText(/Erreur reseau/)).not.toBeInTheDocument() vi.useRealTimers() }) it('should hide the error banner once recording starts again', () => { render() act(() => capturedOnError?.('Erreur reseau')) expect(screen.getByText(/Erreur reseau/)).toBeInTheDocument() act(() => capturedOnStateChange?.('recording')) expect(screen.queryByText(/Erreur reseau/)).not.toBeInTheDocument() }) it('should display the update banner when an update is available, then ready', () => { render() act(() => onUpdateAvailableCb?.()) expect(screen.getByText(/telechargement en cours/)).toBeInTheDocument() act(() => onUpdateReadyCb?.()) expect(screen.getByText(/prochaine fermeture/)).toBeInTheDocument() }) it('should request the context menu with the current listening state', () => { // Faux par defaut (ecoute non activee tant qu'aucun compte n'est connecte, voir // getAuthStatus ci-dessus qui renvoie loggedIn: false). render() const container = screen.getByTestId('avatar-viewer').parentElement! fireEvent.contextMenu(container) expect(showContextMenu).toHaveBeenCalledWith(false) }) it('should enable listening once getAuthStatus reports an existing session', async () => { getAuthStatus.mockResolvedValueOnce({ loggedIn: true, email: 'william@example.com' }) render() const container = screen.getByTestId('avatar-viewer').parentElement! await waitFor(() => { fireEvent.contextMenu(container) expect(showContextMenu).toHaveBeenCalledWith(true) }) }) it('should start dragging the window on mouse down and stop on mouse up', async () => { render() const container = screen.getByTestId('avatar-viewer').parentElement! fireEvent.mouseDown(container, { button: 0, screenX: 10, screenY: 10 }) await waitFor(() => expect(getWindowBounds).toHaveBeenCalled()) fireEvent.mouseMove(window, { screenX: 20, screenY: 15 }) expect(setWindowPosition).toHaveBeenCalled() fireEvent.mouseUp(window) setWindowPosition.mockClear() fireEvent.mouseMove(window, { screenX: 50, screenY: 50 }) expect(setWindowPosition).not.toHaveBeenCalled() }) it('should ignore a non-primary mouse button for dragging', async () => { render() const container = screen.getByTestId('avatar-viewer').parentElement! fireEvent.mouseDown(container, { button: 2 }) await new Promise((resolve) => setTimeout(resolve, 10)) expect(getWindowBounds).not.toHaveBeenCalled() }) it('should not show the session panel when logged out', async () => { render() await waitFor(() => expect(getAuthStatus).toHaveBeenCalled()) expect(screen.queryByLabelText('Message')).not.toBeInTheDocument() }) it('should show the session panel input once logged in', async () => { getAuthStatus.mockResolvedValue({ loggedIn: true, email: 'william@example.com' }) render() expect(await screen.findByLabelText('Message')).toBeInTheDocument() }) })