Files
ForgeBase/Max Avatar/tests/unit/components/App.test.tsx
T
williamandClaude Sonnet 5 c1440f83c0 feat(windows): route chat/stepper content to their own windows, fix status badge position
- ChatPanel now fills its own window instead of being absolutely
  positioned over the avatar; its interactive regions (messages,
  input bar, objective card) opt out of the window's drag region so
  they stay clickable/selectable while the rest of the panel can be
  used to move the window.
- New StepperView: the #stepper window's root component, relays
  "session-status-changed" from the main process into its own
  Zustand store instance (new setStatus action) since this window has
  no direct access to the chat window's state.
- App.tsx no longer renders ChatPanel/PipelineStepper at all (they
  live in separate windows now) or resizes itself - it only toggles
  the chat window via IPC and keeps a local optimistic flag for the
  context menu's checkmark, resynced if the window is closed
  externally.
- Fixed AvatarStatusIndicator's position: .avatar-status-anchor used
  to be computed from the in-window chat panel's height
  (bottom: calc(2% + 64vh + ...)), which misplaced it once that panel
  stopped living in this window. It now mirrors .thinking's simple
  fixed anchor above the avatar's head.

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

247 lines
9.4 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 open the chat window automatically if a session starts without it (safety net)', 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).toHaveBeenCalledOnce()
})
})