Files
ForgeBase/Max Avatar/tests/unit/coachStore.test.ts
T
williamandClaude Sonnet 5 1c2f002eb3 refactor: move app code into Max Avatar/ subfolder (monorepo prep for backend)
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>
2026-10-31 12:15:30 +01:00

195 lines
6.5 KiB
TypeScript

import { describe, it, expect, vi, beforeAll } from 'vitest'
import { mkdtempSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const tempDir = mkdtempSync(join(tmpdir(), 'forge-avatar-coach-test-'))
vi.mock('electron', () => ({
app: { getPath: () => tempDir }
}))
// Importe apres le mock : getDb() lit app.getPath('userData') au premier appel et cree le fichier
// sqlite dans un dossier temporaire dedie a ce fichier de test (jamais la vraie base utilisateur).
const {
setSubjectObjective,
getSubjectObjective,
setSubjectLevel,
getSubjectLevel,
createModules,
listModules,
allModulesHaveContent,
modulesMissingContent,
setModuleTheory,
canWriteModuleContent,
saveAssessment,
hasAssessment,
hasMultipleDifficulties,
moduleDontKnowRatio,
upsertConceptStatus,
getProgress,
getCourseComplianceReport,
getResearchSummary
} = await import('../../src/main/coach/coachStore')
const VALID_CONTENT = [
'Accroche',
'Un vendeur doit calculer rapidement combien de tissu acheter pour un mur triangulaire. '.repeat(
16
),
'Developpement theorique',
'Idee centrale, analogie, definition formelle, exemple resolu et contre-exemple. '.repeat(35),
'Phase guidee',
'ETAPE 1 : identifier les donnees. ETAPE 2 : appliquer la formule. ETAPE 3 : verifier.',
'Exercices progressifs',
'EXERCICE 1 facile. EXERCICE 2 facile. EXERCICE 3 moyen. EXERCICE 4 moyen. EXERCICE 5 difficile. EXERCICE 6 difficile.',
'Exercice de debogage',
"Trouve l'erreur dans ce calcul errone et corrige-la.",
'Synthese',
'Resume final avec question de meta-cognition et test Feynman. '.repeat(15)
].join('\n\n')
describe('coachStore — objective', () => {
it('should return null for a subject objective that was never set', () => {
expect(getSubjectObjective('sujet-sans-objectif')).toBeNull()
})
it('should return null when the subject exists but its objective/research were never set', () => {
createModules('sujet-existant-sans-objectif', [{ name: 'Module unique' }])
expect(getSubjectObjective('sujet-existant-sans-objectif')).toBeNull()
expect(getResearchSummary('sujet-existant-sans-objectif')).toBeNull()
})
it('should persist and retrieve a subject objective', () => {
setSubjectObjective(
'algebre-test',
'Tu seras capable de resoudre une equation du premier degre'
)
expect(getSubjectObjective('algebre-test')).toBe(
'Tu seras capable de resoudre une equation du premier degre'
)
})
})
describe('coachStore — modules et verrouillage sequentiel', () => {
const subject = 'fractions-test'
it('should create modules with only the first one unlocked', () => {
createModules(subject, [{ name: 'Module A' }, { name: 'Module B' }])
const modules = listModules(subject)
expect(modules.map((m) => m.name)).toEqual(['Module A', 'Module B'])
expect(modules[0]?.locked).toBe(false)
expect(modules[1]?.locked).toBe(true)
})
it('should report missing content for every module before any is written', () => {
expect(allModulesHaveContent(subject)).toBe(false)
expect(modulesMissingContent(subject)).toEqual(['Module A', 'Module B'])
})
it('should allow writing module content before a subject level is recorded', () => {
expect(canWriteModuleContent(subject, 'Module A')).toEqual({ ok: true, blockedBy: null })
expect(canWriteModuleContent(subject, 'Module B')).toEqual({ ok: true, blockedBy: null })
})
it('should accept well-structured content and reflect it as no-longer-missing', () => {
expect(setModuleTheory(subject, 'Module A', VALID_CONTENT)).toBe(true)
expect(modulesMissingContent(subject)).toEqual(['Module B'])
})
it('should block writing a later module once a subject level is recorded and an earlier module is unfinished', () => {
setSubjectLevel(subject, 'debutant')
expect(getSubjectLevel(subject)).toBe('debutant')
const result = canWriteModuleContent(subject, 'Module B')
expect(result.ok).toBe(false)
expect(result.blockedBy).toBe('Module A')
})
})
describe("coachStore — evaluations et validation d'un module", () => {
const subject = 'geometrie-test'
const module = 'Module unique'
beforeAll(() => {
createModules(subject, [{ name: module }])
})
it('should report no assessment and a 100% "dont know" ratio before any is saved', () => {
expect(hasAssessment(subject, module, 'module')).toBe(false)
expect(moduleDontKnowRatio(subject, module)).toBe(1)
expect(hasMultipleDifficulties(subject, module)).toBe(false)
})
it('should track multiple distinct difficulties and a correct dont-know ratio after saving assessments', () => {
saveAssessment(
subject,
module,
'Question facile',
'short_answer',
null,
'reponse',
'module',
false,
'facile',
'prediction',
null
)
saveAssessment(
subject,
module,
'Question moyenne',
'short_answer',
null,
'je ne sais pas',
'module',
true,
'moyen',
'debogage',
'erreur de signe'
)
expect(hasAssessment(subject, module, 'module')).toBe(true)
expect(hasMultipleDifficulties(subject, module)).toBe(true)
expect(moduleDontKnowRatio(subject, module)).toBeCloseTo(0.5)
})
it('should default to false/1 (blocking) for a subject that does not exist at all', () => {
expect(hasMultipleDifficulties('sujet-totalement-inconnu', module)).toBe(false)
expect(moduleDontKnowRatio('sujet-totalement-inconnu', module)).toBe(1)
})
it('should persist multiple-choice options as JSON on the assessment', () => {
saveAssessment(
subject,
module,
'QCM',
'multiple_choice',
['A', 'B', 'C'],
'A',
'module',
false,
'difficile',
'qcm_cible',
null
)
expect(hasAssessment(subject, module, 'module')).toBe(true)
})
})
describe('coachStore — progression et conformite', () => {
it('should return an empty list when no subject exists yet', () => {
expect(getProgress('sujet-totalement-inconnu')).toEqual([])
})
it('should return null compliance for an unknown subject', () => {
expect(getCourseComplianceReport('sujet-totalement-inconnu')).toBeNull()
})
it('should include the source-documentation check in the compliance report for a known subject', () => {
upsertConceptStatus('conformite-test', 'Un concept', 'not_started')
const report = getCourseComplianceReport('conformite-test')
expect(report).not.toBeNull()
expect(report?.checks.some((c) => c.key === 'sources')).toBe(true)
})
})