Files
ForgeBase/Max Avatar/tests/unit/skills.askWrittenExercise.initialFinal.test.ts
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

247 lines
8.4 KiB
TypeScript

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-askexercise-test-'))
vi.mock('electron', () => ({
app: { getPath: () => tempDir }
}))
const requestWrittenAnswers = vi.fn(
async (items: { question: string }[]): Promise<(string | null)[]> =>
items.map(() => 'une reponse')
)
vi.mock('../../src/main/coach/exerciseWindow', () => ({ requestWrittenAnswers }))
const {
setResearchSummary,
createModules,
setModuleTheory,
setSubjectLevel,
upsertConceptStatus,
getModuleOrderIndex,
unlockNextModule
} = await import('../../src/main/coach/coachStore')
const { askWrittenExercise } = await import('../../src/main/skills/coach/askWrittenExercise')
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('ask_written_exercise — kind="initial"', () => {
const subject = 'geographie-initial-test'
it('should refuse missing questions array', async () => {
await expect(
askWrittenExercise.execute({ subject, kind: 'initial', questions: [] })
).rejects.toThrow(/questions manquantes/)
})
it('should refuse when no course plan exists yet', async () => {
await expect(
askWrittenExercise.execute({
subject,
kind: 'initial',
questions: [{ question: 'Q ?', type: 'free_text', concept: 'Module A' }]
})
).rejects.toThrow(/Aucun plan de cours ecrit/)
})
it('should refuse when module content is missing', async () => {
setResearchSummary(subject, JSON.stringify({ sources: [] }))
createModules(subject, [{ name: 'Module A' }, { name: 'Module B' }])
const { setSubjectObjective } = await import('../../src/main/coach/coachStore')
setSubjectObjective(subject, 'Tu seras capable de situer un pays sur une carte')
await expect(
askWrittenExercise.execute({
subject,
kind: 'initial',
questions: [
{ question: 'Q A ?', type: 'free_text', concept: 'Module A' },
{ question: 'Q B ?', type: 'free_text', concept: 'Module B' }
]
})
).rejects.toThrow(/contenu de certains modules/)
})
it('should refuse when question coverage does not exactly match the modules', async () => {
setModuleTheory(subject, 'Module A', VALID_CONTENT)
setModuleTheory(subject, 'Module B', VALID_CONTENT)
await expect(
askWrittenExercise.execute({
subject,
kind: 'initial',
questions: [{ question: 'Q A ?', type: 'free_text', concept: 'Module A' }]
})
).rejects.toThrow(/Modules non couverts/)
})
it('should refuse a concept not matching any real module', async () => {
await expect(
askWrittenExercise.execute({
subject,
kind: 'initial',
questions: [
{ question: 'Q ?', type: 'free_text', concept: 'Module A' },
{ question: 'Q2 ?', type: 'free_text', concept: 'Module Fantome' }
]
})
).rejects.toThrow(/Concepts invalides/)
})
it('should accept exactly one question per module and save the assessments', async () => {
const result = await askWrittenExercise.execute({
subject,
kind: 'initial',
questions: [
{ question: 'Q A ?', type: 'free_text', concept: 'Module A' },
{ question: 'Q B ?', type: 'free_text', concept: 'Module B' }
]
})
expect(result).toMatch(/Formulaire soumis/)
})
it('should refuse a second initial evaluation once the level is already recorded', async () => {
setSubjectLevel(subject, 'debutant')
await expect(
askWrittenExercise.execute({
subject,
kind: 'initial',
questions: [{ question: 'Q ?', type: 'free_text', concept: 'Module A' }]
})
).rejects.toThrow(/deja enregistre/)
})
})
describe('ask_written_exercise — kind="final"', () => {
const subject = 'final-test'
it('should refuse when not all modules are acquired yet', async () => {
createModules(subject, [{ name: 'Seul module' }])
await expect(
askWrittenExercise.execute({
subject,
kind: 'final',
questions: [{ question: 'Q finale ?', type: 'free_text', difficulty: 'difficile' }]
})
).rejects.toThrow(/ne sont pas encore valides/)
})
it('should refuse a final evaluation with no demanding question', async () => {
const orderIndex = getModuleOrderIndex(subject, 'Seul module')
upsertConceptStatus(subject, 'Seul module', 'acquired')
if (orderIndex !== null) unlockNextModule(subject, orderIndex)
await expect(
askWrittenExercise.execute({
subject,
kind: 'final',
questions: [{ question: 'Q facile ?', type: 'free_text', difficulty: 'facile' }]
})
).rejects.toThrow(/exigeante/)
})
it('should accept a final evaluation with at least one demanding question', async () => {
const result = await askWrittenExercise.execute({
subject,
kind: 'final',
questions: [{ question: 'Q difficile ?', type: 'free_text', difficulty: 'difficile' }]
})
expect(result).toMatch(/Formulaire soumis/)
})
it('should also accept a demanding exerciseStyle without difficulty="difficile"', async () => {
const result = await askWrittenExercise.execute({
subject,
kind: 'final',
questions: [{ question: 'Projet ?', type: 'free_text', exerciseStyle: 'projet' }]
})
expect(result).toMatch(/Formulaire soumis/)
})
})
describe('ask_written_exercise — validation generale', () => {
it('should refuse an invalid kind', async () => {
await expect(
askWrittenExercise.execute({
subject: 'autre-sujet',
kind: 'invalide',
questions: [{ question: 'Q ?', type: 'free_text' }]
})
).rejects.toThrow(/kind invalide/)
})
it('should refuse a multiple_choice question without enough options', async () => {
await expect(
askWrittenExercise.execute({
subject: 'autre-sujet',
kind: 'module',
questions: [{ question: 'Q ?', type: 'multiple_choice', options: ['A'] }]
})
).rejects.toThrow(/options manquantes/)
})
it('should refuse an invalid difficulty value', async () => {
await expect(
askWrittenExercise.execute({
subject: 'autre-sujet',
kind: 'module',
questions: [{ question: 'Q ?', type: 'free_text', difficulty: 'impossible' }]
})
).rejects.toThrow(/difficulty invalide/)
})
it('should refuse an invalid exerciseStyle value', async () => {
await expect(
askWrittenExercise.execute({
subject: 'autre-sujet',
kind: 'module',
questions: [{ question: 'Q ?', type: 'free_text', exerciseStyle: 'inexistant' }]
})
).rejects.toThrow(/exerciseStyle invalide/)
})
it('should normalize an accented exerciseStyle value', async () => {
const result = await askWrittenExercise.execute({
subject: 'autre-sujet',
kind: 'module',
questions: [{ question: 'Q ?', type: 'free_text', exerciseStyle: 'problème_ouvert' }]
})
expect(result).toMatch(/Formulaire soumis/)
})
it('should record a "dont know" answer distinctly from a real answer', async () => {
requestWrittenAnswers.mockResolvedValueOnce(['Je ne sais pas'])
const result = await askWrittenExercise.execute({
subject: 'autre-sujet',
kind: 'module',
questions: [{ question: 'Q ?', type: 'free_text' }]
})
expect(result).toMatch(/lacune enregistree/)
})
it('should report a missing answer when the window closes without one', async () => {
requestWrittenAnswers.mockResolvedValueOnce([null])
const result = await askWrittenExercise.execute({
subject: 'autre-sujet',
kind: 'module',
questions: [{ question: 'Q ?', type: 'free_text' }]
})
expect(result).toMatch(/pas de reponse/)
})
})