From 104af3149fad30c835bcd8a3bd53451cc9238f8d Mon Sep 17 00:00:00 2001 From: Antigravity Date: Sun, 14 Jun 2026 19:57:21 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20g=C3=A9n=C3=A9rateur=20d'exercices=20+?= =?UTF-8?q?=20planning=20de=20r=C3=A9vision=20IA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../app/api/ai/generate-exercises/route.ts | 98 +++++++ memento-note/app/api/ai/study-plan/route.ts | 70 +++++ memento-note/components/home-client.tsx | 25 +- .../note-editor/note-editor-toolbar.tsx | 36 ++- .../wizard/study-planner-dialog.tsx | 146 +++++++++++ .../ai/services/exercise-generator.service.ts | 97 +++++++ .../lib/ai/services/study-planner.service.ts | 125 +++++++++ memento-note/locales/en.json | 11 + memento-note/locales/fr.json | 11 + .../tests/unit/chunk-indexing.test.ts | 168 +++++------- memento-note/tests/unit/chunking.test.ts | 247 ++++++++---------- 11 files changed, 791 insertions(+), 243 deletions(-) create mode 100644 memento-note/app/api/ai/generate-exercises/route.ts create mode 100644 memento-note/app/api/ai/study-plan/route.ts create mode 100644 memento-note/components/wizard/study-planner-dialog.tsx create mode 100644 memento-note/lib/ai/services/exercise-generator.service.ts create mode 100644 memento-note/lib/ai/services/study-planner.service.ts diff --git a/memento-note/app/api/ai/generate-exercises/route.ts b/memento-note/app/api/ai/generate-exercises/route.ts new file mode 100644 index 0000000..17c0bb6 --- /dev/null +++ b/memento-note/app/api/ai/generate-exercises/route.ts @@ -0,0 +1,98 @@ +import { NextRequest, NextResponse } from 'next/server' +import { auth } from '@/auth' +import prisma from '@/lib/prisma' +import { exerciseGeneratorService } from '@/lib/ai/services/exercise-generator.service' +import { checkEntitlementOrThrow, QuotaExceededError, incrementUsageAsync } from '@/lib/entitlements' +import { useLanguage } from '@/lib/i18n' + +export async function POST(request: NextRequest) { + try { + const session = await auth() + if (!session?.user?.id) { + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) + } + + const { noteId, count, language } = await request.json() + if (!noteId) { + return NextResponse.json({ error: 'noteId is required' }, { status: 400 }) + } + + try { + await checkEntitlementOrThrow(session.user.id, 'reformulate') + } catch (err) { + if (err instanceof QuotaExceededError) { + const isTierLocked = err.currentQuota === 0 + return NextResponse.json( + { error: isTierLocked ? 'feature_locked' : 'quota_exceeded', errorKey: isTierLocked ? 'ai.featureLocked' : 'ai.quotaExceeded' }, + { status: 402 }, + ) + } + throw err + } + + const note = await prisma.note.findUnique({ + where: { id: noteId }, + select: { id: true, title: true, content: true, notebookId: true }, + }) + + if (!note) { + return NextResponse.json({ error: 'Note not found' }, { status: 404 }) + } + + const exercises = await exerciseGeneratorService.generate( + note.title || 'Sans titre', + note.content, + { count: count || 5, language: language || 'fr' } + ) + + // Create exercise notes in the same notebook + const lang = language || 'fr' + const exerciseLabel = lang === 'fr' ? 'Exercice' : lang === 'fa' ? 'تمرین' : 'Exercise' + const answerLabel = lang === 'fr' ? 'Corrigé' : lang === 'fa' ? 'پاسخ' : 'Answer' + + const createdNotes = [] + for (let i = 0; i < exercises.length; i++) { + const ex = exercises[i] + const difficultyEmoji = ex.difficulty === 'facile' ? '🟢' : ex.difficulty === 'moyen' ? '🟡' : '🔴' + + const content = ` +

${exerciseLabel} ${i + 1} — ${difficultyEmoji} ${ex.difficulty}

+

Énoncé

+

${ex.question}

+

${answerLabel} — cliquer pour révéler

Solution

${ex.answer}

+`.trim() + + const created = await prisma.note.create({ + data: { + title: `${exerciseLabel} ${i + 1} — ${note.title || ''}`, + content, + userId: session.user.id, + notebookId: note.notebookId, + type: 'richtext', + order: 999 + i, + }, + }) + createdNotes.push(created) + + void (async () => { + try { + const { embeddingService } = await import('@/lib/ai/services/embedding.service') + const { upsertNoteEmbedding } = await import('@/lib/embeddings') + const { chunkIndexingService } = await import('@/lib/ai/services/chunk-indexing.service') + const { embedding } = await embeddingService.generateNoteEmbedding(created.title, content) + await upsertNoteEmbedding(created.id, embedding) + await chunkIndexingService.indexNote(created.id, created.title, content) + } catch {} + })() + } + + incrementUsageAsync(session.user.id, 'reformulate') + + return NextResponse.json({ + exercises: createdNotes.map(n => ({ id: n.id, title: n.title })), + }) + } catch (error: any) { + console.error('[Exercise Generator] Error:', error) + return NextResponse.json({ error: error.message || 'Failed to generate exercises' }, { status: 500 }) + } +} diff --git a/memento-note/app/api/ai/study-plan/route.ts b/memento-note/app/api/ai/study-plan/route.ts new file mode 100644 index 0000000..296d2f6 --- /dev/null +++ b/memento-note/app/api/ai/study-plan/route.ts @@ -0,0 +1,70 @@ +import { NextRequest, NextResponse } from 'next/server' +import { auth } from '@/auth' +import prisma from '@/lib/prisma' +import { studyPlannerService } from '@/lib/ai/services/study-planner.service' +import { checkEntitlementOrThrow, QuotaExceededError, incrementUsageAsync } from '@/lib/entitlements' + +export async function POST(request: NextRequest) { + try { + const session = await auth() + if (!session?.user?.id) { + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) + } + + const { notebookId, examDate } = await request.json() + if (!notebookId || !examDate) { + return NextResponse.json({ error: 'notebookId and examDate are required' }, { status: 400 }) + } + + try { + await checkEntitlementOrThrow(session.user.id, 'reformulate') + } catch (err) { + if (err instanceof QuotaExceededError) { + const isTierLocked = err.currentQuota === 0 + return NextResponse.json( + { error: isTierLocked ? 'feature_locked' : 'quota_exceeded', errorKey: isTierLocked ? 'ai.featureLocked' : 'ai.quotaExceeded' }, + { status: 402 }, + ) + } + throw err + } + + const notes = await prisma.note.findMany({ + where: { notebookId, trashedAt: null, userId: session.user.id }, + select: { id: true, title: true }, + orderBy: { order: 'asc' }, + }) + + if (notes.length === 0) { + return NextResponse.json({ error: 'No notes found in notebook' }, { status: 400 }) + } + + const userLang = await prisma.user.findUnique({ + where: { id: session.user.id }, + select: { theme: true }, + }) + + const plan = await studyPlannerService.generate(notes, examDate) + + // Set reminders on notes based on the plan + for (const day of plan.days) { + for (const noteId of day.noteIds) { + try { + const reminderDate = new Date(day.date) + reminderDate.setHours(9, 0, 0, 0) + await prisma.note.update({ + where: { id: noteId }, + data: { reminder: reminderDate }, + }) + } catch {} + } + } + + incrementUsageAsync(session.user.id, 'reformulate') + + return NextResponse.json(plan) + } catch (error: any) { + console.error('[Study Planner] Error:', error) + return NextResponse.json({ error: error.message || 'Failed to generate study plan' }, { status: 500 }) + } +} diff --git a/memento-note/components/home-client.tsx b/memento-note/components/home-client.tsx index 460ebf9..9661c55 100644 --- a/memento-note/components/home-client.tsx +++ b/memento-note/components/home-client.tsx @@ -20,7 +20,7 @@ import { import { NotebookSuggestionToast } from '@/components/notebook-suggestion-toast' import { Button } from '@/components/ui/button' -import { Plus, ArrowUpDown, Search, Sparkles, FileText, FolderOpen, ChevronRight, Tag as TagIcon, X, Menu, LayoutGrid, List, Table, Columns3 } from 'lucide-react' +import { Plus, ArrowUpDown, Search, Sparkles, FileText, FolderOpen, ChevronRight, Tag as TagIcon, X, Menu, LayoutGrid, List, Table, Columns3, CalendarDays } from 'lucide-react' import { emitNoteChange } from '@/lib/note-change-sync' import { useReminderCheck } from '@/hooks/use-reminder-check' import { useAutoLabelSuggestion } from '@/hooks/use-auto-label-suggestion' @@ -30,6 +30,7 @@ import { useLanguage } from '@/lib/i18n' import { useEditorUI } from '@/context/editor-ui-context' import { NoteHistoryModal } from '@/components/note-history-modal' import { CreateNotebookDialog } from '@/components/create-notebook-dialog' +import { StudyPlannerDialog } from '@/components/wizard/study-planner-dialog' import { toast } from 'sonner' import { AnimatePresence, motion } from 'motion/react' @@ -134,6 +135,7 @@ export function HomeClient({ const [layoutMode, setLayoutMode] = useState(initialLayoutMode) const [addPropertyOpen, setAddPropertyOpen] = useState(false) const [isEnablingStructured, setIsEnablingStructured] = useState(false) + const [showStudyPlanner, setShowStudyPlanner] = useState(false) const notebookFilter = searchParams.get('notebook') const schemaHook = useNotebookSchema(notebookFilter) @@ -1036,6 +1038,19 @@ export function HomeClient({ {t('notebook.summary')} )} + {searchParams.get('notebook') && ( + + )} + + +
+ {!plan && !loading && ( +
+

+ {t('wizard.studyPlannerDesc') || `L'IA va créer un planning de révision pour le carnet "${notebookName}" basé sur la répétition espacée.`} +

+
+ + setExamDate(e.target.value)} + className="w-full rounded-lg border border-border bg-background px-4 py-3 text-sm outline-none focus:ring-2 focus:ring-brand-accent/30" + /> +
+ +
+ )} + + {loading && ( +
+ +

{t('wizard.studyPlanLoading') || 'Création du planning...'}

+
+ )} + + {plan && ( +
+
+ + {plan.totalDays} {t('wizard.daysPlanned') || 'jours planifiés'} +
+
+ {plan.days.map((day, i) => ( +
+
+
+ {new Date(day.date).toLocaleDateString(undefined, { weekday: 'short' })} +
+
+ {new Date(day.date).getDate()} +
+
+
+

{day.activity}

+ {day.noteTitles.length > 0 && ( +
+ {day.noteTitles.map((title, j) => ( + + + {title.length > 30 ? title.slice(0, 30) + '...' : title} + + ))} +
+ )} +
+
+ ))} +
+

+ {t('wizard.studyPlanReminders') || 'Des rappels ont été ajoutés automatiquement à vos notes.'} +

+
+ )} +
+ + + ) +} diff --git a/memento-note/lib/ai/services/exercise-generator.service.ts b/memento-note/lib/ai/services/exercise-generator.service.ts new file mode 100644 index 0000000..5a79d21 --- /dev/null +++ b/memento-note/lib/ai/services/exercise-generator.service.ts @@ -0,0 +1,97 @@ +import { getChatProvider } from '../factory' +import { getSystemConfig } from '@/lib/config' + +export interface GeneratedExercise { + question: string + difficulty: 'facile' | 'moyen' | 'difficile' + answer: string +} + +export class ExerciseGeneratorService { + async generate( + noteTitle: string, + noteContent: string, + options: { count?: number; language?: string } + ): Promise { + const count = options.count || 5 + const lang = options.language || 'fr' + const langName = lang === 'fr' ? 'français' : lang === 'fa' ? 'فارسی' : 'English' + + // Strip HTML to plain text for the AI + const plainText = noteContent + .replace(/<[^>]+>/g, ' ') + .replace(/ /g, ' ') + .replace(/\s+/g, ' ') + .trim() + .slice(0, 4000) + + const prompt = `Tu es un professeur expert. Génère ${count} exercices basés sur ce cours. + +COURS : "${noteTitle}" +CONTENU : +${plainText} + +Langue : ${langName} + +Génère ${count} exercices variés avec des niveaux de difficulté différents. +Pour chaque exercice : une question claire et détaillée, et un corrigé complet et expliqué. + +FORMAT JSON UNIQUEMENT : +\`\`\`json +[ + ${Array.from({ length: count }, (_, i) => `{ + "question": "Question détaillée numéro ${i + 1}...", + "difficulty": "${['facile', 'moyen', 'difficile'][i % 3]}", + "answer": "Corrigé complet et expliqué..." + }`).join(',\n ')} +] +\`\`\` + +RÈGLES : +- Les questions doivent couvrir différents aspects du cours +- Mélange les types : application directe, analyse, synthèse +- Les corrigés doivent être détaillés avec les étapes +- Utilise des formules LaTeX avec la notation standard (\\frac, \\sum, etc.)` + + const config = await getSystemConfig() + const provider = getChatProvider(config) + const raw = await provider.generateText(prompt) + + return this.parseResponse(raw) + } + + private parseResponse(raw: string): GeneratedExercise[] { + const jsonMatch = raw.match(/```json\s*([\s\S]+?)\s*```/) + let jsonStr = jsonMatch ? jsonMatch[1] : raw + + const start = jsonStr.indexOf('[') + const end = jsonStr.lastIndexOf(']') + if (start >= 0 && end > start) { + jsonStr = jsonStr.slice(start, end + 1) + } + + try { + const parsed = JSON.parse(jsonStr) + return parsed.map((e: any) => ({ + question: String(e.question || ''), + difficulty: e.difficulty || 'moyen', + answer: String(e.answer || ''), + })) + } catch { + // Fix backslash escaping + try { + const fixed = jsonStr.replace(/\\(?!["\\\/bfnrtu])/g, '\\\\') + const parsed = JSON.parse(fixed) + return parsed.map((e: any) => ({ + question: String(e.question || ''), + difficulty: e.difficulty || 'moyen', + answer: String(e.answer || ''), + })) + } catch { + throw new Error('Failed to parse exercises') + } + } + } +} + +export const exerciseGeneratorService = new ExerciseGeneratorService() diff --git a/memento-note/lib/ai/services/study-planner.service.ts b/memento-note/lib/ai/services/study-planner.service.ts new file mode 100644 index 0000000..a801774 --- /dev/null +++ b/memento-note/lib/ai/services/study-planner.service.ts @@ -0,0 +1,125 @@ +import { getChatProvider } from '../factory' +import { getSystemConfig } from '@/lib/config' + +export interface StudyDay { + date: string + noteIds: string[] + noteTitles: string[] + activity: string +} + +export interface StudyPlan { + days: StudyDay[] + totalDays: number + notesPerDay: number +} + +export class StudyPlannerService { + async generate( + notes: Array<{ id: string; title: string }>, + examDate: string, + language?: string + ): Promise { + const lang = language || 'fr' + const langName = lang === 'fr' ? 'français' : lang === 'fa' ? 'فارسی' : 'English' + + const exam = new Date(examDate) + const today = new Date() + today.setHours(0, 0, 0, 0) + const daysUntilExam = Math.max(1, Math.ceil((exam.getTime() - today.getTime()) / (1000 * 60 * 60 * 24))) + + const notesList = notes.map((n, i) => `${i + 1}. ${n.title} (id: ${n.id})`).join('\n') + + const prompt = `Tu es un expert en pédagogie et apprentissage. Crée un planning de révision optimisé. + +DATE DE L'EXAMEN : ${examDate} +JOURS RESTANTS : ${daysUntilExam} +LANGUE : ${langName} + +NOTES À RÉVISER : +${notesList} + +Crée un planning qui répartit ces notes sur ${daysUntilExam} jours en utilisant la répétition espacée : +- Les premiers jours : couvrir tout le programme une fois +- Les jours suivants : revoir les notes difficiles plus fréquemment +- Les derniers jours : révision globale et fiches synthèses +- Inclus des jours de repos légers (relecture rapide) + +FORMAT JSON UNIQUEMENT : +\`\`\`json +{ + "days": [ + { + "date": "YYYY-MM-DD", + "noteIds": ["id1", "id2"], + "activity": "Description courte de l'activité du jour" + } + ] +} +\`\`\` + +Génère exactement ${Math.min(daysUntilExam, 30)} entrées de jours (max 30). +Les dates vont de aujourd'hui (${today.toISOString().slice(0, 10)}) jusqu'à la veille de l'examen. +Chaque jour doit avoir 1-4 notes à réviser avec une activité descriptive.` + + const config = await getSystemConfig() + const provider = getChatProvider(config) + const raw = await provider.generateText(prompt) + + return this.parseResponse(raw, notes, daysUntilExam) + } + + private parseResponse( + raw: string, + notes: Array<{ id: string; title: string }>, + totalDays: number + ): StudyPlan { + const jsonMatch = raw.match(/```json\s*([\s\S]+?)\s*```/) + let jsonStr = jsonMatch ? jsonMatch[1] : raw + + const start = jsonStr.indexOf('{') + const end = jsonStr.lastIndexOf('}') + if (start >= 0 && end > start) { + jsonStr = jsonStr.slice(start, end + 1) + } + + try { + const parsed = JSON.parse(jsonStr) + const titleMap = new Map(notes.map(n => [n.id, n.title])) + + const days: StudyDay[] = (parsed.days || []).map((d: any) => ({ + date: String(d.date || ''), + noteIds: Array.isArray(d.noteIds) ? d.noteIds.map(String) : [], + noteTitles: (Array.isArray(d.noteIds) ? d.noteIds : []).map((id: string) => titleMap.get(id) || 'Note'), + activity: String(d.activity || 'Révision'), + })) + + return { + days, + totalDays: days.length, + notesPerDay: days.length > 0 ? Math.round(notes.length / days.length) : 0, + } + } catch { + // Fallback: simple distribution + const days: StudyDay[] = [] + const today = new Date() + const notesPerDay = Math.max(1, Math.ceil(notes.length / Math.min(totalDays, 14))) + for (let i = 0; i < Math.min(totalDays, 14); i++) { + const date = new Date(today) + date.setDate(date.getDate() + i) + const dayNotes = notes.slice(i * notesPerDay, (i + 1) * notesPerDay) + if (dayNotes.length === 0 && i > 0) break + days.push({ + date: date.toISOString().slice(0, 10), + noteIds: dayNotes.map(n => n.id), + noteTitles: dayNotes.map(n => n.title), + activity: i === 0 ? 'Première lecture' : `Revoir ${dayNotes.length} notes`, + }) + } + + return { days, totalDays: days.length, notesPerDay } + } + } +} + +export const studyPlannerService = new StudyPlannerService() diff --git a/memento-note/locales/en.json b/memento-note/locales/en.json index 1d27685..abb5f05 100644 --- a/memento-note/locales/en.json +++ b/memento-note/locales/en.json @@ -2558,6 +2558,17 @@ "pdfExportBlocked": "Popup blocked — allow popups to export as PDF", "pdfExportLoading": "Generating PDF...", "pdfExportSuccess": "PDF ready!", + "generateExercises": "Generate exercises", + "exercisesLoading": "Generating exercises...", + "exercisesGenerated": "exercises created!", + "wizardStudyPlanner": "Study Plan", + "wizardStudyPlannerDesc": "AI creates a revision plan based on spaced repetition.", + "wizardExamDate": "Exam date", + "wizardGeneratePlan": "Generate plan", + "wizardStudyPlanLoading": "Creating plan...", + "wizardStudyPlanSuccess": "Plan created! Reminders have been added to your notes.", + "wizardDaysPlanned": "days planned", + "wizardStudyPlanReminders": "Reminders have been automatically added to your notes.", "importMarkdown": "Import Markdown", "markdownExportSuccess": "Note exported as Markdown", "markdownExportError": "Failed to export note", diff --git a/memento-note/locales/fr.json b/memento-note/locales/fr.json index 622f8a7..e4fb740 100644 --- a/memento-note/locales/fr.json +++ b/memento-note/locales/fr.json @@ -2562,6 +2562,17 @@ "pdfExportBlocked": "Popup bloqué — autorisez les popups pour exporter en PDF", "pdfExportLoading": "Génération du PDF...", "pdfExportSuccess": "PDF prêt !", + "generateExercises": "Générer des exercices", + "exercisesLoading": "Génération des exercices...", + "exercisesGenerated": "exercices créés !", + "wizardStudyPlanner": "Planning de révision", + "wizardStudyPlannerDesc": "L'IA crée un planning de révision basé sur la répétition espacée.", + "wizardExamDate": "Date de l'examen", + "wizardGeneratePlan": "Générer le planning", + "wizardStudyPlanLoading": "Création du planning...", + "wizardStudyPlanSuccess": "Planning créé ! Des rappels ont été ajoutés à vos notes.", + "wizardDaysPlanned": "jours planifiés", + "wizardStudyPlanReminders": "Des rappels ont été ajoutés automatiquement à vos notes.", "importMarkdown": "Importer un Markdown", "markdownExportSuccess": "Note exportée en Markdown", "markdownExportError": "Échec de l'export de la note", diff --git a/memento-note/tests/unit/chunk-indexing.test.ts b/memento-note/tests/unit/chunk-indexing.test.ts index f113cdf..bd32ac9 100644 --- a/memento-note/tests/unit/chunk-indexing.test.ts +++ b/memento-note/tests/unit/chunk-indexing.test.ts @@ -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) { - 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: '

Test

', + }, + 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: '

Test

', - }, - 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) +}) diff --git a/memento-note/tests/unit/chunking.test.ts b/memento-note/tests/unit/chunking.test.ts index 181b949..48210bc 100644 --- a/memento-note/tests/unit/chunking.test.ts +++ b/memento-note/tests/unit/chunking.test.ts @@ -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 ===')