import { Test, TestingModule } from '@nestjs/testing'; import { PrismaService } from '../../prisma/prisma.service'; import { OpenRouterService } from '../../llm/openrouter.service'; import { PlanAgent } from './plan.agent'; describe('PlanAgent', () => { let agent: PlanAgent; const chat = jest.fn(); const createCurriculum = jest.fn< Promise, [{ data: { totalModules: number; totalDuration: string } }] >(); const prisma = { sessionCurriculum: { findUnique: jest.fn(), create: createCurriculum }, sessionObjective: { findUniqueOrThrow: jest.fn() }, sessionLevelAssessment: { findUniqueOrThrow: jest.fn() }, sessionResearch: { findUniqueOrThrow: jest.fn() }, }; const validCurriculum = { modules: [ { index: 1, title: 'A', concept: 'concept-a', estimated_duration: '2h', prerequisites: [], status: 'unlocked', }, { index: 2, title: 'B', concept: 'concept-b', estimated_duration: '3h', prerequisites: ['A'], status: 'locked', }, { index: 3, title: 'C', concept: 'concept-c', estimated_duration: '2h', prerequisites: ['B'], status: 'locked', }, ], total_duration: '1 semaine', }; beforeEach(async () => { jest.clearAllMocks(); prisma.sessionObjective.findUniqueOrThrow.mockResolvedValue({ subject: 'Facturation', professionalGoal: 'Facturer sans erreur', duration: '2 semaines', }); prisma.sessionLevelAssessment.findUniqueOrThrow.mockResolvedValue({ masteredConcepts: ['Devis'], gapConcepts: [], unknownConcepts: ['TVA'], overallLevel: 'beginner', }); prisma.sessionResearch.findUniqueOrThrow.mockResolvedValue({ corpus: { subtopics: [{ subtopic: 'TVA', keyPoints: ['taux reduit'] }] }, }); const module: TestingModule = await Test.createTestingModule({ providers: [ PlanAgent, { provide: PrismaService, useValue: prisma }, { provide: OpenRouterService, useValue: { chat } }, ], }).compile(); agent = module.get(PlanAgent); }); describe('runForSession()', () => { it('should do nothing when a curriculum already exists (idempotent)', async () => { prisma.sessionCurriculum.findUnique.mockResolvedValueOnce({ id: 1 }); await agent.runForSession(1, 7); expect(chat).not.toHaveBeenCalled(); expect(prisma.sessionCurriculum.create).not.toHaveBeenCalled(); }); it('should persist a valid curriculum (3-7 modules, only the first unlocked)', async () => { prisma.sessionCurriculum.findUnique.mockResolvedValueOnce(null); chat.mockResolvedValueOnce(JSON.stringify(validCurriculum)); await agent.runForSession(1, 7); const createCall = createCurriculum.mock.calls[0][0]; expect(createCall.data.totalModules).toBe(3); expect(createCall.data.totalDuration).toBe('1 semaine'); }); it('should retry once then throw when the module count is out of the 3-7 range', async () => { prisma.sessionCurriculum.findUnique.mockResolvedValueOnce(null); const tooFew = { modules: [ { index: 1, title: 'A', concept: 'a', estimated_duration: '1h', prerequisites: [], status: 'unlocked', }, ], total_duration: '1h', }; chat.mockResolvedValueOnce(JSON.stringify(tooFew)); chat.mockResolvedValueOnce(JSON.stringify(tooFew)); await expect(agent.runForSession(1, 7)).rejects.toThrow(); expect(chat).toHaveBeenCalledTimes(2); expect(prisma.sessionCurriculum.create).not.toHaveBeenCalled(); }); it('should reject a curriculum where a module other than the first is unlocked', async () => { prisma.sessionCurriculum.findUnique.mockResolvedValueOnce(null); const badLocking = { modules: validCurriculum.modules.map((m) => ({ ...m, status: 'unlocked', })), total_duration: '1 semaine', }; chat.mockResolvedValueOnce(JSON.stringify(badLocking)); chat.mockResolvedValueOnce(JSON.stringify(validCurriculum)); await agent.runForSession(1, 7); expect(chat).toHaveBeenCalledTimes(2); expect(prisma.sessionCurriculum.create).toHaveBeenCalled(); }); }); });