import { describe, it, expect, vi, beforeEach } from 'vitest' import { render, screen, waitFor } from '@testing-library/react' import userEvent from '@testing-library/user-event' import type { SubjectProgress, ModuleDetail, CourseComplianceReport } from '../../../src/shared/coach' const drawShape = vi.fn() vi.mock('../../../src/renderer/src/components/drawRoughShapes', () => ({ drawShape })) vi.mock('roughjs', () => ({ default: { canvas: vi.fn(() => ({})) } })) const mermaidRender = vi.fn(async () => ({ svg: '' })) vi.mock('mermaid', () => ({ default: { initialize: vi.fn(), render: mermaidRender } })) let navigateCallback: ((payload: { subject: string; module: string }) => void) | undefined const getCoachProgress = vi.fn() const getModuleDetail = vi.fn() const getCourseCompliance = vi.fn(async (): Promise => null) const requestModuleEvaluation = vi.fn() function setupWindowApi(): void { ;(window as unknown as { api: Record }).api = { getCoachProgress, getModuleDetail, getCourseCompliance, requestModuleEvaluation, onNavigateToModule: vi.fn((cb: (payload: { subject: string; module: string }) => void) => { navigateCallback = cb return () => {} }) } } const { default: CoachProgressView } = await import('../../../src/renderer/src/components/CoachProgressView') const SUBJECT: SubjectProgress = { subject: 'algebre', level: 'debutant', status: 'in_progress', objective: 'Tu seras capable de resoudre une equation', lastSessionAt: new Date().toISOString(), concepts: [ { name: 'Module A', status: 'acquired', lastReviewedAt: new Date().toISOString(), nextReviewAt: null, locked: false, orderIndex: 0 }, { name: 'Module B', status: 'not_started', lastReviewedAt: new Date().toISOString(), nextReviewAt: null, locked: true, orderIndex: 1 } ] } const MODULE_DETAIL: ModuleDetail = { name: 'Module A', status: 'acquired', locked: false, theorySummary: 'Contenu du module.', assessments: [ { question: 'Question 1 ?', answer: 'Reponse', dontKnow: false, difficulty: 'facile', exerciseStyle: 'prediction', targetsCommonError: null, createdAt: new Date().toISOString() } ], diagrams: [] } describe('', () => { beforeEach(() => { vi.clearAllMocks() setupWindowApi() getCoachProgress.mockResolvedValue([SUBJECT]) getModuleDetail.mockResolvedValue(MODULE_DETAIL) getCourseCompliance.mockResolvedValue(null) vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({ clearRect: vi.fn() } as unknown as CanvasRenderingContext2D) }) it('should show a loading state before subjects arrive', () => { getCoachProgress.mockReturnValue(new Promise(() => {})) render() expect(screen.getByText('Chargement...')).toBeInTheDocument() }) it('should list the loaded subjects', async () => { render() expect(await screen.findByText('algebre')).toBeInTheDocument() }) it('should show an empty state when there are no subjects yet', async () => { getCoachProgress.mockResolvedValue([]) render() expect(await screen.findByText(/Aucun sujet d.apprentissage/)).toBeInTheDocument() }) it('should navigate to the modules view and list each module', async () => { const user = userEvent.setup() render() await user.click(await screen.findByText('algebre')) expect(screen.getByText('Module A')).toBeInTheDocument() expect(screen.getByText('Module B')).toBeInTheDocument() expect(screen.getByText(/Niveau : debutant/)).toBeInTheDocument() expect(screen.getByText(/resoudre une equation/)).toBeInTheDocument() }) it('should disable a locked module button', async () => { const user = userEvent.setup() render() await user.click(await screen.findByText('algebre')) expect(screen.getByText('Module B').closest('button')).toBeDisabled() }) it('should show the compliance checklist once loaded', async () => { getCourseCompliance.mockResolvedValue({ subject: 'algebre', checks: [ { key: 'sources', label: 'Recherche sourcee', ok: true, detail: 'ok' }, { key: 'objective', label: 'Objectif mesurable', ok: false, detail: 'manquant' } ] }) const user = userEvent.setup() render() await user.click(await screen.findByText('algebre')) await waitFor(() => expect(screen.getByText(/1\/2/)).toBeInTheDocument()) }) it('should navigate to a module detail view and show its content', async () => { const user = userEvent.setup() render() await user.click(await screen.findByText('algebre')) await user.click(screen.getByText('Module A')) expect(await screen.findByText('Contenu du module.')).toBeInTheDocument() expect(screen.getByText('Question 1 ?')).toBeInTheDocument() }) it('should show the "evaluer ce module" button for an unlocked, unacquired module', async () => { getModuleDetail.mockResolvedValue({ ...MODULE_DETAIL, status: 'in_progress' }) const user = userEvent.setup() render() await user.click(await screen.findByText('algebre')) await user.click(screen.getByText('Module A')) const evalButton = await screen.findByText('Evaluer ce module') await user.click(evalButton) expect(requestModuleEvaluation).toHaveBeenCalledWith('algebre', 'Module A') }) it('should not show the evaluation button once the module is acquired', async () => { const user = userEvent.setup() render() await user.click(await screen.findByText('algebre')) await user.click(screen.getByText('Module A')) await screen.findByText('Contenu du module.') expect(screen.queryByText('Evaluer ce module')).not.toBeInTheDocument() }) it('should navigate directly to a module detail view on an incoming navigate-to-module event', async () => { render() await waitFor(() => expect(navigateCallback).toBeDefined()) navigateCallback?.({ subject: 'algebre', module: 'Module A' }) expect(await screen.findByText('Contenu du module.')).toBeInTheDocument() }) it('should render a shapes diagram for a module', async () => { getModuleDetail.mockResolvedValue({ ...MODULE_DETAIL, diagrams: [ { kind: 'shapes', title: 'Mon schema', data: { title: 'Mon schema', width: 400, height: 300, shapes: [] }, createdAt: new Date().toISOString() } ] }) const user = userEvent.setup() render() await user.click(await screen.findByText('algebre')) await user.click(screen.getByText('Module A')) expect(await screen.findByText('Mon schema')).toBeInTheDocument() }) it('should render a mermaid diagram for a module', async () => { getModuleDetail.mockResolvedValue({ ...MODULE_DETAIL, diagrams: [ { kind: 'mermaid', title: 'Mon diagramme', data: { mermaidCode: 'graph LR; A --> B' }, createdAt: new Date().toISOString() } ] }) const user = userEvent.setup() render() await user.click(await screen.findByText('algebre')) await user.click(screen.getByText('Module A')) expect(await screen.findByText('Mon diagramme')).toBeInTheDocument() await waitFor(() => expect(mermaidRender).toHaveBeenCalled()) }) it('should go back from the module view to the modules list', async () => { const user = userEvent.setup() render() await user.click(await screen.findByText('algebre')) await user.click(screen.getByText('Module A')) await screen.findByText('Contenu du module.') await user.click(screen.getByText(/Retour aux modules/)) expect(screen.getByText('Module A')).toBeInTheDocument() expect(screen.getByText('Module B')).toBeInTheDocument() }) it('should go back from the modules view to the subjects list', async () => { const user = userEvent.setup() render() await user.click(await screen.findByText('algebre')) await user.click(screen.getByText(/Retour aux sujets/)) expect(await screen.findByText('algebre')).toBeInTheDocument() }) })