import { describe, it, expect, vi } from 'vitest' import { mkdtempSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' const tempDir = mkdtempSync(join(tmpdir(), 'forge-avatar-research-test-')) vi.mock('electron', () => ({ app: { getPath: () => tempDir } })) const { getResearchSummary } = await import('../../src/main/coach/coachStore') const { researchTopic } = await import('../../src/main/skills/coach/researchTopic') function buildSources(count: number, level = 1): { title: string; url: string; level: number }[] { return Array.from({ length: count }, (_, i) => ({ title: `Source ${i + 1}`, url: `https://example.edu/source-${i + 1}`, level })) } const BASE_ARGS = { subject: 'trigonometrie-recherche-test', subjectDomain: 'math_science', definition: 'La trigonometrie etudie les relations entre angles et longueurs.', usageToday: 'Utilisee en architecture, navigation, physique.', whyUsedToday: 'Permet de calculer des distances inaccessibles directement.', practicalCases: 'Calcul de la hauteur d-un batiment via son ombre.', application: 'Mesurer un angle puis appliquer sinus/cosinus/tangente.', prerequisites: 'Connaitre le theoreme de Pythagore et les triangles rectangles.', keyConcepts: ['sinus', 'cosinus', 'tangente', 'hypotenuse', 'angle'], commonErrors: [ 'confondre sinus et cosinus', 'oublier de convertir en radians', 'mauvais triangle de reference' ], feynmanExplanation: 'Imagine une echelle contre un mur : la trigonometrie dit comment l-angle change la hauteur atteinte.' } describe('research_topic', () => { it('should reject an invalid subjectDomain', async () => { await expect( researchTopic.execute({ ...BASE_ARGS, subjectDomain: 'domaine-inconnu', sources: buildSources(5) }) ).rejects.toThrow(/subjectDomain invalide/) }) it('should reject a source with an invalid URL', async () => { await expect( researchTopic.execute({ ...BASE_ARGS, sources: [{ title: 'Source', url: 'pas-une-url', level: 1 }] }) ).rejects.toThrow(/URL valide/) }) it('should reject when fewer sources than the domain minimum are provided', async () => { await expect(researchTopic.execute({ ...BASE_ARGS, sources: buildSources(3) })).rejects.toThrow( /Au moins 5 sources/ ) }) it('should reject when every source is level 4 only', async () => { await expect( researchTopic.execute({ ...BASE_ARGS, sources: buildSources(5, 4) }) ).rejects.toThrow(/niveau 1, 2 ou 3/) }) it('should reject fewer than 5 key concepts', async () => { await expect( researchTopic.execute({ ...BASE_ARGS, sources: buildSources(5), keyConcepts: ['a', 'b'] }) ).rejects.toThrow(/keyConcepts/) }) it('should reject fewer than 3 common errors', async () => { await expect( researchTopic.execute({ ...BASE_ARGS, sources: buildSources(5), commonErrors: ['a'] }) ).rejects.toThrow(/commonErrors/) }) it('should require a higher minimum source count for medicine_law_finance', async () => { await expect( researchTopic.execute({ ...BASE_ARGS, subjectDomain: 'medicine_law_finance', sources: buildSources(5) }) ).rejects.toThrow(/Au moins 10 sources/) }) it('should accept a fully valid research payload and persist it', async () => { const result = await researchTopic.execute({ ...BASE_ARGS, sources: buildSources(5) }) expect(result).toMatch(/enregistree/) const stored = getResearchSummary(BASE_ARGS.subject) expect(stored).not.toBeNull() const parsed = JSON.parse(stored ?? '{}') as { sources: unknown[]; subjectDomain: string } expect(parsed.sources).toHaveLength(5) expect(parsed.subjectDomain).toBe('math_science') }) })