Files
ForgeBase/Max Avatar/tests/unit/components/App.test.tsx
T
williamandClaude Sonnet 5 6e8a9d2f0d feat(ui): separate chat and pipeline stepper, move stepper beside the avatar
Per user feedback after visually testing the app: the chat and the
pipeline stepper were a single combined panel, which the user wanted
split. Rename SessionPanel to ChatPanel (chat history + objective
validation + input bar only, no stepper), rendered only while a new
chatOpen flag is true - toggled via the context menu's "Ecrire un
message" item, not shown automatically on login.

The PipelineStepper now lives directly in App.tsx and only mounts once
useSessionStore.sessionId is set (i.e. the user has actually started a
training session, not just logged in). It's anchored to the right edge
of the window, vertically centered, instead of stacked above the
avatar: the window itself grows from 420 to 620px wide via the new
set-pipeline-panel-visible IPC call whenever a session becomes active,
and shrinks back when it doesn't. A new .avatar-stage wrapper keeps the
avatar/chat panel's own layout anchored to a fixed 420px column so it
doesn't re-center across the wider window.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 22:39:04 +01:00

239 lines
9.0 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 setPipelinePanelVisible = vi.fn()
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(() => () => {}),
getWindowBounds,
setWindowPosition,
getAuthStatus,
openLoginWindow,
logout,
quitApp,
setPipelinePanelVisible,
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 not show the chat panel when logged out', async () => {
render(<App />)
await waitFor(() => expect(getAuthStatus).toHaveBeenCalled())
expect(screen.queryByLabelText('Message')).not.toBeInTheDocument()
})
it('should not show the chat panel right after login, until "Ecrire un message" is used', async () => {
getAuthStatus.mockResolvedValue({ loggedIn: true, email: 'william@example.com' })
render(<App />)
await waitFor(() => expect(getAuthStatus).toHaveBeenCalled())
expect(screen.queryByLabelText('Message')).not.toBeInTheDocument()
})
it('should open the chat panel via the context menu and shrink the pipeline panel back on toggle off', 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(await screen.findByLabelText('Message')).toBeInTheDocument()
fireEvent.contextMenu(container)
fireEvent.click(screen.getByRole('menuitem', { name: 'Ecrire un message' }))
expect(screen.queryByLabelText('Message')).not.toBeInTheDocument()
})
it('should not show the pipeline stepper before a session exists', async () => {
getAuthStatus.mockResolvedValue({ loggedIn: true, email: 'william@example.com' })
render(<App />)
await waitFor(() => expect(getAuthStatus).toHaveBeenCalled())
expect(screen.queryByText('Objectif')).not.toBeInTheDocument()
expect(setPipelinePanelVisible).toHaveBeenCalledWith(false)
})
it('should show the pipeline stepper and widen the window once a session is created', async () => {
getAuthStatus.mockResolvedValue({ loggedIn: true, email: 'william@example.com' })
sessionsCreate.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
sessionsSendObjectiveMessage.mockResolvedValueOnce({
reply: 'Quel est ton sujet ?',
ready: false
})
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' }))
fireEvent.change(await screen.findByLabelText('Message'), {
target: { value: 'Je veux apprendre' }
})
fireEvent.click(screen.getByRole('button', { name: 'Envoyer le message' }))
await waitFor(() => expect(screen.getByText('Objectif')).toBeInTheDocument())
expect(setPipelinePanelVisible).toHaveBeenCalledWith(true)
})
})