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>
132 lines
4.3 KiB
TypeScript
132 lines
4.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { render, screen, waitFor, act } from '@testing-library/react'
|
|
|
|
const drawShape = vi.fn()
|
|
vi.mock('../../../src/renderer/src/components/drawRoughShapes', () => ({ drawShape }))
|
|
|
|
const roughCanvas = vi.fn(() => ({}))
|
|
vi.mock('roughjs', () => ({ default: { canvas: roughCanvas } }))
|
|
|
|
const mermaidRender = vi.fn(async () => ({ svg: '<svg></svg>' }))
|
|
const mermaidInitialize = vi.fn()
|
|
vi.mock('mermaid', () => ({
|
|
default: { initialize: mermaidInitialize, render: mermaidRender }
|
|
}))
|
|
|
|
interface InitDrawingPayload {
|
|
title: string
|
|
width: number
|
|
height: number
|
|
}
|
|
interface RevealShapePayload {
|
|
requestId: string
|
|
shape: { type: string }
|
|
audio: ArrayBuffer | null
|
|
}
|
|
interface RenderMermaidPayload {
|
|
requestId: string
|
|
title: string
|
|
mermaidCode: string
|
|
}
|
|
interface PlayCaptionPayload {
|
|
requestId: string
|
|
audio: ArrayBuffer
|
|
}
|
|
|
|
const listeners: {
|
|
onInitDrawing?: (payload: InitDrawingPayload) => void
|
|
onRevealShape?: (payload: RevealShapePayload) => void
|
|
onRenderMermaid?: (payload: RenderMermaidPayload) => void
|
|
onPlayCaption?: (payload: PlayCaptionPayload) => void
|
|
} = {}
|
|
|
|
function setupWindowApi(): void {
|
|
;(window as unknown as { api: Record<string, unknown> }).api = {
|
|
onInitDrawing: vi.fn((cb: (payload: InitDrawingPayload) => void) => {
|
|
listeners.onInitDrawing = cb
|
|
return () => {}
|
|
}),
|
|
onRevealShape: vi.fn((cb: (payload: RevealShapePayload) => void) => {
|
|
listeners.onRevealShape = cb
|
|
return () => {}
|
|
}),
|
|
onRenderMermaid: vi.fn((cb: (payload: RenderMermaidPayload) => void) => {
|
|
listeners.onRenderMermaid = cb
|
|
return () => {}
|
|
}),
|
|
onPlayCaption: vi.fn((cb: (payload: PlayCaptionPayload) => void) => {
|
|
listeners.onPlayCaption = cb
|
|
return () => {}
|
|
}),
|
|
acknowledgeShapeReveal: vi.fn(),
|
|
acknowledgeMermaidRendered: vi.fn(),
|
|
acknowledgeCaptionDone: vi.fn()
|
|
}
|
|
}
|
|
|
|
const { default: CoachDrawingView } =
|
|
await import('../../../src/renderer/src/components/CoachDrawingView')
|
|
|
|
describe('<CoachDrawingView />', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
setupWindowApi()
|
|
vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue(
|
|
{} as CanvasRenderingContext2D
|
|
)
|
|
})
|
|
|
|
it('should show a waiting message before any drawing is initialized', () => {
|
|
render(<CoachDrawingView />)
|
|
expect(screen.getByText(/En attente/)).toBeInTheDocument()
|
|
})
|
|
|
|
it('should display the title once a drawing is initialized', () => {
|
|
render(<CoachDrawingView />)
|
|
act(() => listeners.onInitDrawing?.({ title: 'Mon dessin', width: 700, height: 500 }))
|
|
expect(screen.getByText('Mon dessin')).toBeInTheDocument()
|
|
})
|
|
|
|
it('should draw a revealed shape and acknowledge it when there is no audio', () => {
|
|
render(<CoachDrawingView />)
|
|
act(() => listeners.onInitDrawing?.({ title: 'Mon dessin', width: 700, height: 500 }))
|
|
const api = (window as unknown as { api: { acknowledgeShapeReveal: ReturnType<typeof vi.fn> } })
|
|
.api
|
|
act(() =>
|
|
listeners.onRevealShape?.({ requestId: 'r1', shape: { type: 'circle' }, audio: null })
|
|
)
|
|
expect(drawShape).toHaveBeenCalled()
|
|
expect(api.acknowledgeShapeReveal).toHaveBeenCalledWith('r1')
|
|
})
|
|
|
|
it('should switch to mermaid mode and render the diagram on success, waiting for the container to mount', async () => {
|
|
render(<CoachDrawingView />)
|
|
act(() =>
|
|
listeners.onRenderMermaid?.({
|
|
requestId: 'r2',
|
|
title: 'Cycle de l-eau',
|
|
mermaidCode: 'graph LR; A --> B'
|
|
})
|
|
)
|
|
expect(screen.getByText('Cycle de l-eau')).toBeInTheDocument()
|
|
const api = (
|
|
window as unknown as { api: { acknowledgeMermaidRendered: ReturnType<typeof vi.fn> } }
|
|
).api
|
|
await waitFor(() => expect(api.acknowledgeMermaidRendered).toHaveBeenCalledWith('r2', true))
|
|
})
|
|
|
|
it('should report a mermaid render failure', async () => {
|
|
mermaidRender.mockRejectedValueOnce(new Error('syntaxe invalide'))
|
|
render(<CoachDrawingView />)
|
|
act(() =>
|
|
listeners.onRenderMermaid?.({ requestId: 'r3', title: 'Echec', mermaidCode: 'invalide' })
|
|
)
|
|
const api = (
|
|
window as unknown as { api: { acknowledgeMermaidRendered: ReturnType<typeof vi.fn> } }
|
|
).api
|
|
await waitFor(() =>
|
|
expect(api.acknowledgeMermaidRendered).toHaveBeenCalledWith('r3', false, 'syntaxe invalide')
|
|
)
|
|
})
|
|
})
|