Files
ForgeBase/Max Avatar/tests/unit/components/App.test.tsx
T
williamandClaude Sonnet 5 1c2f002eb3 refactor: move app code into Max Avatar/ subfolder (monorepo prep for backend)
Step 1 of adding a backend proxy server (API keys held server-side instead
of shipped in the installer). Moves everything app-specific into
"Max Avatar/" via git mv (preserves history): src/, tests/, scripts/,
assets/, build/, resources/, package.json/-lock.json, electron-builder.yml,
electron.vite.config.ts, eslint.config.mjs, playwright.config.ts,
vitest.config.mts, sonar-project.properties, tsconfig*.json, Dockerfile,
.editorconfig/.env*/.gitignore/.prettier*.

CLAUDE.md, README.md, docs/, .claude/, .gitea/, .husky/, .vscode/, and the
docker-compose files stay at the repo root, since they'll apply to both the
app and the future backend.

No application code needed changes (all its internal paths are relative to
its own tree). Adjusted what referenced the old root from outside:
- .husky/pre-commit: cd into "Max Avatar" before lint-staged
- .gitea/workflows/*.yml: working-directory + cache-dependency-path +
  artifact paths + SonarQube projectBaseDir
- docker-compose.yml: build context -> ./Max Avatar
- Max Avatar/Dockerfile: node:20-alpine -> node:24-alpine (same node:sqlite
  issue already fixed elsewhere, never applied here before)
- .vscode/launch.json, README.md: updated paths/instructions

Verified from Max Avatar/ after the move: npm ci, typecheck, lint,
test:coverage (473/473, same coverage numbers), build, and dev all work
unchanged.

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

171 lines
6.1 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'
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()
}))
vi.mock('../../../src/renderer/src/voice/useCameraCapture', () => ({ useCameraCapture: vi.fn() }))
vi.mock('../../../src/renderer/src/voice/useMeetingRecorder', () => ({
useMeetingRecorder: vi.fn()
}))
let onCourseBuildStepCb: ((state: unknown) => void) | undefined
let onUpdateAvailableCb: (() => void) | undefined
let onUpdateReadyCb: (() => void) | undefined
const sendDroppedFiles = vi.fn()
const getPathForFile = vi.fn(() => 'C:\\fichier.txt')
const showContextMenu = vi.fn()
const getWindowBounds = vi.fn(async () => ({ x: 0, y: 0, width: 100, height: 100 }))
const setWindowPosition = vi.fn()
function setupWindowApi(): void {
;(window as unknown as { api: Record<string, unknown> }).api = {
onCourseBuildStep: vi.fn((cb: (s: unknown) => void) => {
onCourseBuildStepCb = cb
return () => {}
}),
onUpdateAvailable: vi.fn((cb: () => void) => {
onUpdateAvailableCb = cb
return () => {}
}),
onUpdateReady: vi.fn((cb: () => void) => {
onUpdateReadyCb = cb
return () => {}
}),
onToggleListening: vi.fn(() => () => {}),
onToggleMeetingTranscription: vi.fn(() => () => {}),
onProactiveSpeech: vi.fn(() => () => {}),
getPathForFile,
sendDroppedFiles,
showContextMenu,
getWindowBounds,
setWindowPosition
}
}
const { default: App } = await import('../../../src/renderer/src/App')
describe('<App />', () => {
beforeEach(() => {
vi.clearAllMocks()
setupWindowApi()
})
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 course build stepper once a state arrives', () => {
render(<App />)
act(() =>
onCourseBuildStepCb?.({
subject: 'algebre',
steps: [{ key: 'recherche', label: 'Recherche', status: 'done' }]
})
)
expect(screen.getByText(/algebre/)).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 forward dropped files to the main process', () => {
render(<App />)
const container = screen.getByTestId('avatar-viewer').parentElement!
fireEvent.drop(container, { dataTransfer: { files: [{ name: 'a.txt' }] } })
expect(getPathForFile).toHaveBeenCalled()
expect(sendDroppedFiles).toHaveBeenCalledWith(['C:\\fichier.txt'])
})
it('should not call sendDroppedFiles when nothing is dropped', () => {
render(<App />)
const container = screen.getByTestId('avatar-viewer').parentElement!
fireEvent.drop(container, { dataTransfer: { files: [] } })
expect(sendDroppedFiles).not.toHaveBeenCalled()
})
it('should request the context menu with the current listening/transcribing state', () => {
render(<App />)
const container = screen.getByTestId('avatar-viewer').parentElement!
fireEvent.contextMenu(container)
expect(showContextMenu).toHaveBeenCalledWith(true, false)
})
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()
})
})