import { describe, it, expect, vi, beforeEach } from 'vitest' const sendToRenderer = vi.fn() vi.mock('../../src/main/windowBridge', () => ({ sendToRenderer })) const { recordCourseBuildTool, clearCourseBuild } = await import('../../src/main/coach/courseBuildStepper') interface Payload { subject: string steps: { key: string; status: string; detail?: string }[] } function lastPayload(): Payload { const call = sendToRenderer.mock.calls.at(-1) as [string, Payload] | undefined if (!call) throw new Error('sendToRenderer was never called') return call[1] } describe('courseBuildStepper', () => { beforeEach(() => { sendToRenderer.mockClear() }) it('should ignore a tool call with no subject', () => { recordCourseBuildTool('research_topic', {}) expect(sendToRenderer).not.toHaveBeenCalled() }) it('should ignore an unrecognized tool name', () => { recordCourseBuildTool('unknown_tool', { subject: 'algebre' }) expect(sendToRenderer).not.toHaveBeenCalled() }) it('should mark recherche done and plan active after research_topic, with source count', () => { recordCourseBuildTool('research_topic', { subject: 'algebre', sources: [{}, {}, {}] }) const payload = lastPayload() expect(payload.subject).toBe('algebre') const recherche = payload.steps.find((s) => s.key === 'recherche') const plan = payload.steps.find((s) => s.key === 'plan') expect(recherche?.status).toBe('done') expect(recherche?.detail).toBe('3 source(s)') expect(plan?.status).toBe('active') }) it('should mark plan done and redaction active after generate_course_plan with modules', () => { recordCourseBuildTool('generate_course_plan', { subject: 'algebre', modules: [{ name: 'A' }, { name: 'B' }] }) const payload = lastPayload() const plan = payload.steps.find((s) => s.key === 'plan') const redaction = payload.steps.find((s) => s.key === 'redaction') expect(plan?.status).toBe('done') expect(redaction?.status).toBe('active') expect(redaction?.detail).toBe('0/2') }) it('should mark redaction done and evaluation_initiale active once every module is written', () => { recordCourseBuildTool('write_module_content', { subject: 'algebre' }) let payload = lastPayload() expect(payload.steps.find((s) => s.key === 'redaction')?.detail).toBe('1/2') expect(payload.steps.find((s) => s.key === 'redaction')?.status).toBe('active') recordCourseBuildTool('write_module_content', { subject: 'algebre' }) payload = lastPayload() expect(payload.steps.find((s) => s.key === 'redaction')?.status).toBe('done') expect(payload.steps.find((s) => s.key === 'evaluation_initiale')?.status).toBe('active') }) it('should mark evaluation_initiale done and niveau active on an initial assessment', () => { recordCourseBuildTool('ask_written_exercise', { subject: 'algebre', kind: 'initial' }) const payload = lastPayload() expect(payload.steps.find((s) => s.key === 'evaluation_initiale')?.status).toBe('done') expect(payload.steps.find((s) => s.key === 'niveau')?.status).toBe('active') }) it('should not progress the "niveau" step on a non-initial assessment', () => { recordCourseBuildTool('ask_written_exercise', { subject: 'algebre', kind: 'module' }) const payload = lastPayload() expect(payload.steps.find((s) => s.key === 'niveau')?.status).toBe('active') }) it('should mark niveau done after record_subject_level', () => { recordCourseBuildTool('record_subject_level', { subject: 'algebre' }) const payload = lastPayload() expect(payload.steps.find((s) => s.key === 'niveau')?.status).toBe('done') }) it('should mark revision done on a second generate_course_plan call after the level is recorded', () => { recordCourseBuildTool('generate_course_plan', { subject: 'algebre', modules: [{ name: 'A' }] }) const payload = lastPayload() expect(payload.steps.find((s) => s.key === 'revision')?.status).toBe('done') }) it('should reset the stepper state when a different subject starts', () => { recordCourseBuildTool('research_topic', { subject: 'geometrie', sources: [] }) const payload = lastPayload() expect(payload.subject).toBe('geometrie') expect(payload.steps.find((s) => s.key === 'niveau')?.status).toBe('pending') }) it('should clear the stepper state and notify the renderer with null', () => { sendToRenderer.mockClear() clearCourseBuild() expect(sendToRenderer).toHaveBeenCalledWith('course-build-step', null) }) })