Files
ForgeBase/Max Avatar/tests/unit/components/App.test.tsx
T
williamandClaude Sonnet 5 85da9fcad0 refactor(pipeline): the text bar is not a chat, Max always replies by voice
Two pieces of direct user feedback, addressed together since they touch
the same files:

1. ObjectiveValidationCard now lives in its own dedicated, movable
   window (objectiveWindow.ts / ObjectiveWindowRoot.tsx), independent
   of the text input window. Confirming an objective must never depend
   on opening that window, including when the whole exchange happens
   by voice. Both sendTextTurn and voice-turn open/feed this window
   through the same shared handleRouteMessageResult code path, so text
   and voice behave identically - no more special-casing one channel.

2. The "chat window" was never meant to be a chat: it's an alternate
   text entry point for the exact same voice pipeline. Removed the
   message history/ChatBubble display and the old
   sessions:route-message + voice-turn-synced plumbing that existed to
   sync a conversation transcript across windows. Replaced with a new
   text-turn IPC handler mirroring voice-turn (routeMessage ->
   handleRouteMessageResult -> synthesizeSpeech) minus the STT step;
   the reply is relayed to the avatar window via the existing
   proactive-speech channel and spoken aloud, never displayed. The
   input window shrinks accordingly (560px -> 100px tall) and no
   longer auto-opens when a session starts, since there is nothing to
   show there anymore - it only opens on explicit user request.

useProactiveSpeech now accepts an onStateChange callback so the avatar
status indicator correctly flips back to idle once a text-turn reply
finishes playing (it previously only ever got set to 'speaking', never
reset - a latent gap this reuse surfaced).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-01 10:52:09 +01:00

247 lines
9.5 KiB
TypeScript

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'
import { useSessionStore } from '../../../src/renderer/src/stores/useSessionStore'
vi.mock('../../../src/renderer/src/components/AvatarViewer', () => ({
default: ({ onReady }: { onReady: (handle: unknown) => void }) => {
onReady({ playAnimation: vi.fn(), playAnimationByName: vi.fn(), animationNames: [] })
return <div data-testid="avatar-viewer" />
}
}))
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 getWindowBounds = vi.fn(async () => ({ x: 0, y: 0, width: 100, height: 100 }))
const setWindowPosition = vi.fn()
const openLoginWindow = vi.fn()
const logout = vi.fn()
const quitApp = vi.fn()
const toggleChatWindow = vi.fn()
const notifySessionEnded = vi.fn()
let onChatWindowClosedCb: (() => void) | undefined
const sessionsCreate = vi.fn()
const sessionsSendObjectiveMessage = 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<string, unknown> }).api = {
onUpdateAvailable: vi.fn((cb: () => void) => {
onUpdateAvailableCb = cb
return () => {}
}),
onUpdateReady: vi.fn((cb: () => void) => {
onUpdateReadyCb = cb
return () => {}
}),
onToggleListening: vi.fn(() => () => {}),
onProactiveSpeech: vi.fn(() => () => {}),
onChatWindowClosed: vi.fn((cb: () => void) => {
onChatWindowClosedCb = cb
return () => {}
}),
onSessionStatusChanged: vi.fn(() => () => {}),
getWindowBounds,
setWindowPosition,
getAuthStatus,
openLoginWindow,
logout,
quitApp,
toggleChatWindow,
notifySessionEnded,
sessions: {
create: sessionsCreate,
get: vi.fn(),
sendObjectiveMessage: sessionsSendObjectiveMessage,
confirmObjective: vi.fn()
}
}
}
const { default: App } = await import('../../../src/renderer/src/App')
describe('<App />', () => {
beforeEach(() => {
vi.clearAllMocks()
setupWindowApi()
useSessionStore.getState().restart()
})
it('should render the avatar viewer', () => {
render(<App />)
expect(screen.getByTestId('avatar-viewer')).toBeInTheDocument()
})
it('should show the thinking indicator while processing', () => {
render(<App />)
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(<App />)
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(<App />)
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(<App />)
act(() => onUpdateAvailableCb?.())
expect(screen.getByText(/telechargement en cours/)).toBeInTheDocument()
act(() => onUpdateReadyCb?.())
expect(screen.getByText(/prochaine fermeture/)).toBeInTheDocument()
})
it('should open the custom context menu on right-click', () => {
render(<App />)
const container = screen.getByTestId('avatar-viewer').parentElement!
fireEvent.contextMenu(container, { clientX: 20, clientY: 30 })
expect(screen.getByRole('menu')).toBeInTheDocument()
})
it('should call openLoginWindow when "Me connecter" is clicked from the menu', () => {
render(<App />)
const container = screen.getByTestId('avatar-viewer').parentElement!
fireEvent.contextMenu(container)
fireEvent.click(screen.getByRole('menuitem', { name: 'Me connecter' }))
expect(openLoginWindow).toHaveBeenCalled()
})
it('should call logout and reset the session when "Se deconnecter" is clicked', async () => {
getAuthStatus.mockResolvedValue({ loggedIn: true, email: 'william@example.com' })
render(<App />)
await waitFor(() => expect(getAuthStatus).toHaveBeenCalled())
const container = screen.getByTestId('avatar-viewer').parentElement!
fireEvent.contextMenu(container)
fireEvent.click(screen.getByRole('menuitem', { name: 'Se deconnecter' }))
expect(logout).toHaveBeenCalled()
})
it('should call quitApp when "Quitter" is clicked', () => {
render(<App />)
const container = screen.getByTestId('avatar-viewer').parentElement!
fireEvent.contextMenu(container)
fireEvent.click(screen.getByRole('menuitem', { name: 'Quitter' }))
expect(quitApp).toHaveBeenCalled()
})
it('should start dragging the window on mouse down and stop on mouse up', async () => {
render(<App />)
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(<App />)
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 disable the "Ecrire un message" menu item when logged out', async () => {
getAuthStatus.mockResolvedValue({ loggedIn: false, email: null })
render(<App />)
await waitFor(() => expect(getAuthStatus).toHaveBeenCalled())
const container = screen.getByTestId('avatar-viewer').parentElement!
fireEvent.contextMenu(container)
expect(screen.getByRole('menuitem', { name: 'Ecrire un message' })).toBeDisabled()
})
it('should toggle the separate chat window via the context menu (chat is its own Electron window, not rendered here)', async () => {
getAuthStatus.mockResolvedValue({ loggedIn: true, email: 'william@example.com' })
render(<App />)
await waitFor(() => expect(getAuthStatus).toHaveBeenCalled())
const container = screen.getByTestId('avatar-viewer').parentElement!
fireEvent.contextMenu(container)
fireEvent.click(screen.getByRole('menuitem', { name: 'Ecrire un message' }))
expect(toggleChatWindow).toHaveBeenCalledOnce()
fireEvent.contextMenu(container)
const menuItem = screen.getByRole('menuitem', { name: 'Ecrire un message' })
expect(menuItem.querySelector('.context-menu__check')).not.toBeNull()
fireEvent.click(menuItem)
expect(toggleChatWindow).toHaveBeenCalledTimes(2)
})
it('should resync the menu when the chat window is closed externally (e.g. Alt+F4)', async () => {
getAuthStatus.mockResolvedValue({ loggedIn: true, email: 'william@example.com' })
render(<App />)
await waitFor(() => expect(getAuthStatus).toHaveBeenCalled())
expect(onChatWindowClosedCb).toBeTypeOf('function')
const container = screen.getByTestId('avatar-viewer').parentElement!
fireEvent.contextMenu(container)
fireEvent.click(screen.getByRole('menuitem', { name: 'Ecrire un message' }))
act(() => onChatWindowClosedCb?.())
fireEvent.contextMenu(container)
const menuItem = screen.getByRole('menuitem', { name: 'Ecrire un message' })
expect(menuItem.querySelector('.context-menu__check')).toBeNull()
})
it('should not render the pipeline stepper or chat panel in this window (they live in their own windows)', async () => {
getAuthStatus.mockResolvedValue({ loggedIn: true, email: 'william@example.com' })
render(<App />)
await waitFor(() => expect(getAuthStatus).toHaveBeenCalled())
expect(screen.queryByText('Objectif')).not.toBeInTheDocument()
expect(screen.queryByLabelText('Message')).not.toBeInTheDocument()
})
it('should NOT open the text input window automatically when a session starts (not a chat - only explicit user request opens it)', async () => {
getAuthStatus.mockResolvedValue({ loggedIn: true, email: 'william@example.com' })
render(<App />)
await waitFor(() => expect(getAuthStatus).toHaveBeenCalled())
act(() => useSessionStore.setState({ sessionId: 1, status: 'objective_discovery' }))
expect(toggleChatWindow).not.toHaveBeenCalled()
})
})