feat: générateur d'exercices + planning de révision IA
Some checks failed
CI / Lint, Unit Tests & Build (push) Failing after 1m28s
CI / Deploy production (on server) (push) Has been skipped

- Générateur d'exercices : bouton dans menu note → IA crée 5 exercices
  - Niveaux variés (facile/moyen/difficile) avec emojis 🟢🟡🔴
  - Corrigés détaillés dans des toggles (cliquer pour révéler)
  - Callout warning pour le niveau
  - Notes créées dans le même carnet
- Planning de révision : bouton dans barre carnet → IA crée planning
  - Choix date d'examen
  - Répétition espacée (première lecture → revoir → révision globale)
  - Rappels automatiques ajoutés aux notes (9h le jour J)
  - Vue chronologique avec activités et notes par jour
- Services : exercise-generator.service.ts + study-planner.service.ts
- Endpoints : /api/ai/generate-exercises + /api/ai/study-plan
- i18n FR/EN complet
This commit is contained in:
Antigravity
2026-06-14 19:57:21 +00:00
parent 940c3daf62
commit 104af3149f
11 changed files with 791 additions and 243 deletions

View File

@@ -1,99 +1,69 @@
/**
* Test du ChunkIndexingService — dedup, stale deletion, upsert.
* Mocke l'embedding (pas d'appel API), utilise la vraie DB.
*/
import { test, expect, describe, beforeAll, afterAll, beforeEach } from 'vitest'
import { prisma } from '../../lib/prisma'
import { ChunkIndexingService } from '../../lib/ai/services/chunk-indexing.service'
import { embeddingService } from '../../lib/ai/services/embedding.service'
import crypto from 'crypto'
const testNoteId = 'test-chunk-000001'
function test(name: string, fn: () => Promise<void>) {
return fn()
.then(() => console.log(`${name}`))
.catch((err: any) => {
console.error(`${name}: ${err.message}`)
process.exitCode = 1
describe('US-CHUNK-2 : Indexation incrémentale avec dedup', () => {
let originalEmbedText: any
beforeAll(async () => {
originalEmbedText = embeddingService.embedText
embeddingService.embedText = async (text: string) => {
const hash = crypto.createHash('md5').update(text).digest()
return Array.from({ length: 1536 }, (_, i) => hash[i % 16] / 255)
}
await prisma.note.upsert({
where: { id: testNoteId },
create: {
id: testNoteId,
title: 'Test Note for Chunk Indexing',
content: '<p>Test</p>',
},
update: {},
})
}
// Mock l'embedding service : vecteur déterministe basé sur le hash du contenu
const originalEmbedText =
require('../../lib/ai/services/embedding.service').embeddingService.embedText
function mockEmbedding() {
const svc = require('../../lib/ai/services/embedding.service').embeddingService
svc.embedText = async (text: string) => {
const crypto = require('crypto')
const hash = crypto.createHash('md5').update(text).digest()
return Array.from({ length: 1536 }, (_, i) => hash[i % 16] / 255)
}
}
function restoreEmbedding() {
const svc = require('../../lib/ai/services/embedding.service').embeddingService
svc.embedText = originalEmbedText
}
async function ensureTestNote() {
await prisma.note.upsert({
where: { id: testNoteId },
create: {
id: testNoteId,
title: 'Test Note for Chunk Indexing',
content: '<p>Test</p>',
},
update: {},
})
}
async function cleanup() {
await prisma.noteEmbeddingChunk.deleteMany({ where: { noteId: testNoteId } })
}
afterAll(async () => {
embeddingService.embedText = originalEmbedText
await prisma.noteEmbeddingChunk.deleteMany({ where: { noteId: testNoteId } })
await prisma.note.delete({ where: { id: testNoteId } }).catch(() => {})
})
async function removeTestNote() {
await prisma.note.delete({ where: { id: testNoteId } }).catch(() => {})
}
async function main() {
mockEmbedding()
await ensureTestNote()
await cleanup()
console.log('\n=== US-CHUNK-2 : Indexation incrémentale avec dedup ===\n')
const service = new ChunkIndexingService()
beforeEach(async () => {
await prisma.noteEmbeddingChunk.deleteMany({ where: { noteId: testNoteId } })
})
const longContent = Array.from({ length: 8 }, (_, i) =>
`Section ${i} de la note de test. `.repeat(60).trim(),
).join('\n\n')
await test('première indexation → tous les fragments sont nouveaux', async () => {
test('première indexation → tous les fragments sont nouveaux', async () => {
const service = new ChunkIndexingService()
const result = await service.indexNote(testNoteId, 'Note de test', longContent)
if (result.newFragments === 0)
throw new Error(`attendu newFragments > 0, reçu ${result.newFragments}`)
if (result.deleted !== 0)
throw new Error(`attendu deleted=0, reçu ${result.deleted}`)
if (result.skipped !== 0)
throw new Error(`attendu skipped=0, reçu ${result.skipped}`)
console.log(`${result.newFragments} nouveaux, ${result.totalFragments} total`)
expect(result.newFragments).toBeGreaterThan(0)
expect(result.deleted).toBe(0)
expect(result.skipped).toBe(0)
})
await test('deuxième indexation (même contenu) → tout skipped, 0 nouveau', async () => {
test('deuxième indexation (même contenu) → tout skipped, 0 nouveau', async () => {
const service = new ChunkIndexingService()
await service.indexNote(testNoteId, 'Note de test', longContent)
const result = await service.indexNote(testNoteId, 'Note de test', longContent)
if (result.newFragments !== 0)
throw new Error(`attendu 0 nouveau, reçu ${result.newFragments}`)
if (result.skipped === 0)
throw new Error(`attendu skipped > 0, reçu ${result.skipped}`)
if (result.deleted !== 0)
throw new Error(`attendu deleted=0, reçu ${result.deleted}`)
console.log(`${result.skipped} skip, 0 nouveau ✓`)
expect(result.newFragments).toBe(0)
expect(result.skipped).toBeGreaterThan(0)
expect(result.deleted).toBe(0)
})
await test('modification d\'une section → 1 nouveau, reste skip', async () => {
test('modification d\'une section → 1 nouveau, reste skip', async () => {
const service = new ChunkIndexingService()
await service.indexNote(testNoteId, 'Note de test', longContent)
const sections = Array.from({ length: 8 }, (_, i) =>
`Section ${i === 3 ? 'MODIFIÉE' : i} de la note de test. `.repeat(60).trim(),
)
@@ -101,15 +71,12 @@ async function main() {
const result = await service.indexNote(testNoteId, 'Note de test', modified)
if (result.newFragments === 0)
throw new Error(`attendu au moins 1 nouveau fragment, reçu ${result.newFragments}`)
if (result.deleted === 0)
throw new Error(`attendu au moins 1 stale supprimé, reçu ${result.deleted}`)
console.log(`${result.newFragments} nouveau(x), ${result.skipped} skip, ${result.deleted} supprimé(s)`)
expect(result.newFragments).toBeGreaterThanOrEqual(1)
expect(result.deleted).toBeGreaterThanOrEqual(1)
})
await test('suppression d\'une section → fragments stale nettoyés', async () => {
test('suppression d\'une section → fragments stale nettoyés', async () => {
const service = new ChunkIndexingService()
const sections = Array.from({ length: 8 }, (_, i) =>
`Section ${i} de la note de test. `.repeat(60).trim(),
)
@@ -126,15 +93,12 @@ async function main() {
where: { noteId: testNoteId },
})
if (afterCount >= beforeCount)
throw new Error(`attendu count < ${beforeCount}, reçu ${afterCount}`)
if (result.deleted === 0)
throw new Error(`attendu deleted > 0, reçu ${result.deleted}`)
console.log(`${beforeCount}${afterCount} fragments (${result.deleted} supprimés)`)
expect(afterCount).toBeLessThan(beforeCount)
expect(result.deleted).toBeGreaterThan(0)
})
await test('note vide → tous les fragments supprimés', async () => {
test('note vide → tous les fragments supprimés', async () => {
const service = new ChunkIndexingService()
await service.indexNote(testNoteId, 'Note de test', longContent)
const result = await service.indexNote(testNoteId, '', '')
@@ -142,37 +106,27 @@ async function main() {
where: { noteId: testNoteId },
})
if (count !== 0) throw new Error(`attendu 0 fragments, reçu ${count}`)
console.log(` → tous les fragments supprimés ✓`)
expect(count).toBe(0)
})
await test('deleteNoteChunks → supprime tout', async () => {
test('deleteNoteChunks → supprime tout', async () => {
const service = new ChunkIndexingService()
await service.indexNote(testNoteId, 'Note de test', longContent)
await service.deleteNoteChunks(testNoteId)
const count = await prisma.noteEmbeddingChunk.count({
where: { noteId: testNoteId },
})
if (count !== 0) throw new Error(`attendu 0, reçu ${count}`)
console.log(` → 0 fragment restant ✓`)
expect(count).toBe(0)
})
await test('hasChunks → détection correcte', async () => {
test('hasChunks → détection correcte', async () => {
const service = new ChunkIndexingService()
const before = await service.hasChunks(testNoteId)
if (before) throw new Error('attendu false avant indexation')
expect(before).toBe(false)
await service.indexNote(testNoteId, 'Note de test', longContent)
const after = await service.hasChunks(testNoteId)
if (!after) throw new Error('attendu true après indexation')
console.log(` → false avant, true après ✓`)
expect(after).toBe(true)
})
await cleanup()
restoreEmbedding()
await prisma.$disconnect()
console.log('\n=== Tests terminés ===')
}
main().catch(console.error)
})

View File

@@ -1,140 +1,119 @@
import { test, expect, describe } from 'vitest'
import { chunkNoteContent } from '../../lib/text/note-chunking'
function test(name: string, fn: () => void) {
try {
fn()
console.log(`${name}`)
} catch (err: any) {
console.error(`${name}: ${err.message}`)
process.exitCode = 1
}
}
describe('US-CHUNK-1 : Chunking sémantique', () => {
test('note vide → aucun fragment', () => {
const chunks = chunkNoteContent('note1', '')
expect(chunks.length).toBe(0)
})
function assert(condition: any, msg: string) {
if (!condition) throw new Error(msg)
}
test('note très courte (< 10 chars) → aucun fragment', () => {
const chunks = chunkNoteContent('note1', 'Hello')
expect(chunks.length).toBe(0)
})
console.log('\n=== US-CHUNK-1 : Chunking sémantique ===\n')
test('note courte (< 1000 chars) → 1 seul fragment', () => {
const text = 'Ceci est une note courte. Elle parle de productivité et de gestion du temps.'
const chunks = chunkNoteContent('note1', text)
expect(chunks.length).toBe(1)
expect(chunks[0].chunkIndex).toBe(0)
expect(chunks[0].content).toContain('productivité')
expect(chunks[0].charCount).toBe(chunks[0].content.length)
})
test('note vide → aucun fragment', () => {
const chunks = chunkNoteContent('note1', '')
assert(chunks.length === 0, `attendu 0, reçu ${chunks.length}`)
test('note longue avec plusieurs paragraphes → plusieurs fragments', () => {
const paragraphs: string[] = []
for (let i = 0; i < 10; i++) {
paragraphs.push(`Paragraphe ${i}. `.repeat(60).trim())
}
const text = paragraphs.join('\n\n')
const chunks = chunkNoteContent('note2', text)
expect(chunks.length).toBeGreaterThan(1)
expect(chunks.length).toBeLessThanOrEqual(15)
for (let i = 0; i < chunks.length; i++) {
expect(chunks[i].chunkIndex).toBe(i)
}
})
test('fragmentId est stable (déterministe)', () => {
const text = 'Même contenu donne même hash.'
const chunks1 = chunkNoteContent('noteA', text)
const chunks2 = chunkNoteContent('noteA', text)
expect(chunks1[0].fragmentId).toBe(chunks2[0].fragmentId)
})
test('fragmentId diffère entre notes différentes', () => {
const text = 'Même contenu mais note différente.'
const chunks1 = chunkNoteContent('noteA', text)
const chunks2 = chunkNoteContent('noteB', text)
expect(chunks1[0].fragmentId).not.toBe(chunks2[0].fragmentId)
})
test('paragraphe géant (> 1500 chars) → sous-découpé aux phrases', () => {
const giantPara =
'Ceci est une phrase très longue. '.repeat(100) + 'Dernière phrase du paragraphe géant.'
const chunks = chunkNoteContent('note3', giantPara)
expect(chunks.length).toBeGreaterThan(1)
for (const chunk of chunks) {
expect(chunk.content.length).toBeLessThanOrEqual(2000)
}
})
test('persan (RTL) → chunking correct', () => {
const persianText =
'یادداشت درباره بهره‌وری.\n\nاین یک پاراگراف فارسی است. این متن برای تست قالب‌بندی راست‌چین نوشته شده است. یادداشت‌های فارسی باید به درستی پردازش شوند.\n\nپاراگراف سوم. محتوای بیشتری برای اطمینان از صحت پردازش.'
const chunks = chunkNoteContent('note-fa', persianText)
expect(chunks.length).toBeGreaterThanOrEqual(1)
expect(chunks[0].content).toContain('بهره‌وری')
})
test('contenu plain text → pas de transformation', () => {
const plainText = 'Premier paragraphe.\n\nDeuxième paragraphe.'
const chunks = chunkNoteContent('note4', plainText)
expect(chunks.length).toBeGreaterThanOrEqual(1)
expect(chunks[0].content).toContain('Premier')
})
test('paragraphe répété → dedup par fragmentId', () => {
const repeatedPara = 'Paragraphe identique répété volontairement.'
const text = `${repeatedPara}\n\n${repeatedPara}\n\n${repeatedPara}`
const chunks = chunkNoteContent('note5', text)
const uniqueIds = new Set(chunks.map((c) => c.fragmentId))
expect(uniqueIds.size).toBe(chunks.length)
})
test('modification d\'un paragraphe → fragmentId change pour ce fragment uniquement', () => {
const paraA = 'Section A. '.repeat(80).trim()
const paraB = 'Section B. '.repeat(80).trim()
const paraC = 'Section C. '.repeat(80).trim()
const original = `${paraA}\n\n${paraB}\n\n${paraC}`
const modified = `${paraA} MODIFIE.\n\n${paraB}\n\n${paraC}`
const chunksOriginal = chunkNoteContent('note6', original)
const chunksModified = chunkNoteContent('note6', modified)
expect(chunksOriginal.length).toBeGreaterThanOrEqual(2)
const originalIds = new Set(chunksOriginal.map((c) => c.fragmentId))
const newIds = chunksModified.map((c) => c.fragmentId)
const unchanged = newIds.filter((id) => originalIds.has(id))
expect(unchanged.length).toBeGreaterThanOrEqual(1)
expect(unchanged.length).toBeLessThan(newIds.length)
})
test('overlap entre fragments consécutifs', () => {
const paragraphs: string[] = []
for (let i = 0; i < 8; i++) {
paragraphs.push(`Section ${i}. `.repeat(80).trim())
}
const text = paragraphs.join('\n\n')
const chunks = chunkNoteContent('note7', text)
if (chunks.length >= 2) {
const tail = chunks[0].content.slice(-200)
const matchesOverlap = chunks[1].content.startsWith(tail.slice(0, 50)) || chunks[1].content.includes(tail.slice(0, 30))
expect(matchesOverlap).toBe(true)
}
})
})
test('note très courte (< 10 chars) → aucun fragment', () => {
const chunks = chunkNoteContent('note1', 'Hello')
assert(chunks.length === 0, `attendu 0, reçu ${chunks.length}`)
})
test('note courte (< 1000 chars) → 1 seul fragment', () => {
const text = 'Ceci est une note courte. Elle parle de productivité et de gestion du temps.'
const chunks = chunkNoteContent('note1', text)
assert(chunks.length === 1, `attendu 1, reçu ${chunks.length}`)
assert(chunks[0].chunkIndex === 0, 'chunkIndex doit être 0')
assert(chunks[0].content.includes('productivité'), 'le contenu doit être préservé')
assert(chunks[0].charCount === chunks[0].content.length, 'charCount doit correspondre')
})
test('note longue avec plusieurs paragraphes → plusieurs fragments', () => {
const paragraphs: string[] = []
for (let i = 0; i < 10; i++) {
paragraphs.push(`Paragraphe ${i}. `.repeat(60).trim())
}
const text = paragraphs.join('\n\n')
const chunks = chunkNoteContent('note2', text)
assert(chunks.length > 1, `attendu >1, reçu ${chunks.length}`)
assert(chunks.length <= 15, `attendu <=15 fragments, reçu ${chunks.length}`)
for (let i = 0; i < chunks.length; i++) {
assert(chunks[i].chunkIndex === i, `chunkIndex ${i} incorrect`)
}
})
test('fragmentId est stable (déterministe)', () => {
const text = 'Même contenu donne même hash.'
const chunks1 = chunkNoteContent('noteA', text)
const chunks2 = chunkNoteContent('noteA', text)
assert(chunks1[0].fragmentId === chunks2[0].fragmentId, 'les hash doivent être identiques')
})
test('fragmentId diffère entre notes différentes', () => {
const text = 'Même contenu mais note différente.'
const chunks1 = chunkNoteContent('noteA', text)
const chunks2 = chunkNoteContent('noteB', text)
assert(chunks1[0].fragmentId !== chunks2[0].fragmentId, 'les hash doivent différer par noteId')
})
test('paragraphe géant (> 1500 chars) → sous-découpé aux phrases', () => {
const giantPara =
'Ceci est une phrase très longue. '.repeat(100) + 'Dernière phrase du paragraphe géant.'
const chunks = chunkNoteContent('note3', giantPara)
assert(chunks.length > 1, `attendu >1 fragment, reçu ${chunks.length}`)
for (const chunk of chunks) {
assert(
chunk.content.length <= 2000,
`fragment trop long: ${chunk.charCount} chars`,
)
}
})
test('persan (RTL) → chunking correct', () => {
const persianText =
'یادداشت درباره بهره‌وری.\n\nاین یک پاراگراف فارسی است. این متن برای تست قالب‌بندی راست‌چین نوشته شده است. یادداشت‌های فارسی باید به درستی پردازش شوند.\n\nپاراگراف سوم. محتوای بیشتری برای اطمینان از صحت پردازش.'
const chunks = chunkNoteContent('note-fa', persianText)
assert(chunks.length >= 1, `attendu >=1, reçu ${chunks.length}`)
assert(chunks[0].content.includes('بهره‌وری'), 'contenu persan préservé')
})
test('contenu plain text → pas de transformation', () => {
const plainText = 'Premier paragraphe.\n\nDeuxième paragraphe.'
const chunks = chunkNoteContent('note4', plainText)
assert(chunks.length >= 1, 'au moins 1 fragment')
assert(chunks[0].content.includes('Premier'), 'contenu préservé')
// Le strippage HTML est fait en amont par prepareNoteTextForEmbedding, pas par le chunker
})
test('paragraphe répété → dedup par fragmentId', () => {
const repeatedPara = 'Paragraphe identique répété volontairement.'
const text = `${repeatedPara}\n\n${repeatedPara}\n\n${repeatedPara}`
const chunks = chunkNoteContent('note5', text)
const uniqueIds = new Set(chunks.map((c) => c.fragmentId))
assert(uniqueIds.size === chunks.length, 'les doublons doivent être supprimés')
})
test('modification d\'un paragraphe → fragmentId change pour ce fragment uniquement', () => {
const paraA = 'Section A. '.repeat(80).trim()
const paraB = 'Section B. '.repeat(80).trim()
const paraC = 'Section C. '.repeat(80).trim()
const original = `${paraA}\n\n${paraB}\n\n${paraC}`
const modified = `${paraA} MODIFIE.\n\n${paraB}\n\n${paraC}`
const chunksOriginal = chunkNoteContent('note6', original)
const chunksModified = chunkNoteContent('note6', modified)
assert(chunksOriginal.length >= 2, `original devrait avoir >=2 fragments, reçu ${chunksOriginal.length}`)
const originalIds = new Set(chunksOriginal.map((c) => c.fragmentId))
const newIds = chunksModified.map((c) => c.fragmentId)
const unchanged = newIds.filter((id) => originalIds.has(id))
assert(unchanged.length >= 1, `au moins 1 fragment inchangé attendu, reçu ${unchanged.length} sur ${newIds.length}`)
assert(unchanged.length < newIds.length, `au moins 1 fragment modifié attendu`)
})
test('overlap entre fragments consécutifs', () => {
const paragraphs: string[] = []
for (let i = 0; i < 8; i++) {
paragraphs.push(`Section ${i}. `.repeat(80).trim())
}
const text = paragraphs.join('\n\n')
const chunks = chunkNoteContent('note7', text)
if (chunks.length >= 2) {
const tail = chunks[0].content.slice(-200)
assert(
chunks[1].content.startsWith(tail.slice(0, 50)) || chunks[1].content.includes(tail.slice(0, 30)),
'l\'overlap devrait être présent entre fragments consécutifs',
)
}
})
console.log('\n=== Tests terminés ===')