fix: unify theme system - fix theme switching persistence

- Unified localStorage key to 'theme-preference' across all components
- Fixed header.tsx using wrong localStorage key ('theme' instead of 'theme-preference')
- Added localStorage hybrid persistence for instant theme changes
- Removed router.refresh() which was causing stale data revert
- Replaced Blue theme with Sepia
- Consolidated auth() calls to prevent race conditions
- Updated UserSettingsData types to include all themes
This commit is contained in:
2026-01-18 22:33:41 +01:00
parent ef60dafd73
commit ddb67ba9e5
306 changed files with 59580 additions and 6063 deletions

View File

@@ -0,0 +1,227 @@
import { test, expect } from '@playwright/test';
/**
* E2E Test: Auto-Labeling Bug Fix (Story 7.1)
*
* This test verifies that auto-labeling works correctly when creating a new note.
*
* Acceptance Criteria:
* 1. Given a user creates a new note with content
* 2. When the note is saved
* 3. Then the system should:
* - Automatically analyze the note content for relevant labels
* - Assign suggested labels to the note
* - Display the note in the UI with labels visible
* - NOT require a page refresh to see labels
*/
test.describe('Auto-Labeling Bug Fix', () => {
test.beforeEach(async ({ page }) => {
// Login
await page.goto('/');
await page.waitForURL('**/sign-in', { timeout: 5000 });
// Fill login form
await page.fill('input[type="email"]', 'test@example.com');
await page.fill('input[type="password"]', 'password123');
await page.click('button[type="submit"]');
// Wait for navigation to home page
await page.waitForURL('/', { timeout: 5000 });
});
test('should auto-label a note about programming', async ({ page }) => {
// Create a new note about programming
const noteContent = 'Need to learn React and TypeScript for web development';
// Click "New Note" button
await page.click('[data-testid="new-note-button"], button:has-text("New Note"), .create-note-btn');
// Wait for note to be created
await page.waitForSelector('.note-card, [data-testid^="note-"]', { timeout: 3000 });
// Find the newly created note (first one in the list)
const firstNote = page.locator('.note-card, [data-testid^="note-"]').first();
await expect(firstNote).toBeVisible();
// Click on the note to edit it
await firstNote.click();
// Wait for note editor to appear
await page.waitForSelector('[data-testid="note-editor"], textarea.note-content, .note-editor', { timeout: 3000 });
// Type content about programming
const textarea = page.locator('[data-testid="note-editor"] textarea, textarea.note-content, .note-editor textarea');
await textarea.fill(noteContent);
// Wait a moment for auto-labeling to process
await page.waitForTimeout(1000);
// Find labels in the note
const labels = page.locator('.label-badge, .tag, [data-testid*="label"]');
// Verify that labels appear (auto-labeling should have assigned them)
await expect(labels.first()).toBeVisible({ timeout: 5000 });
// Verify the label text contains relevant keywords (like "code", "development", "programming", etc.)
const labelText = await labels.first().textContent();
expect(labelText?.toLowerCase()).toMatch(/code|development|programming|react|typescript|web/);
console.log('✓ Auto-labeling applied relevant labels:', labelText);
});
test('should auto-label a note about meetings', async ({ page }) => {
// Create a note about a meeting
const noteContent = 'Team meeting scheduled for tomorrow at 2pm to discuss project roadmap';
// Click "New Note" button
await page.click('[data-testid="new-note-button"], button:has-text("New Note"), .create-note-btn');
// Wait for note to be created
await page.waitForSelector('.note-card, [data-testid^="note-"]', { timeout: 3000 });
// Find the newly created note
const firstNote = page.locator('.note-card, [data-testid^="note-"]').first();
await expect(firstNote).toBeVisible();
// Click on the note to edit it
await firstNote.click();
// Wait for note editor
await page.waitForSelector('[data-testid="note-editor"], textarea.note-content, .note-editor', { timeout: 3000 });
// Type content about meeting
const textarea = page.locator('[data-testid="note-editor"] textarea, textarea.note-content, .note-editor textarea');
await textarea.fill(noteContent);
// Wait for auto-labeling
await page.waitForTimeout(1000);
// Check for labels
const labels = page.locator('.label-badge, .tag, [data-testid*="label"]');
await expect(labels.first()).toBeVisible({ timeout: 5000 });
// Verify meeting-related label
const labelText = await labels.first().textContent();
expect(labelText?.toLowerCase()).toMatch(/meeting|team|project|roadmap|discussion/);
console.log('✓ Auto-labeling applied meeting-related labels:', labelText);
});
test('should display labels immediately without page refresh', async ({ page }) => {
// This test verifies the critical requirement: labels should be visible WITHOUT refreshing
const noteContent = 'Need to buy groceries: milk, bread, eggs, and vegetables';
// Click "New Note"
await page.click('[data-testid="new-note-button"], button:has-text("New Note"), .create-note-btn');
// Wait for note creation
await page.waitForSelector('.note-card, [data-testid^="note-"]', { timeout: 3000 });
// Click on the note
const firstNote = page.locator('.note-card, [data-testid^="note-"]').first();
await firstNote.click();
// Wait for editor
await page.waitForSelector('[data-testid="note-editor"], textarea.note-content, .note-editor', { timeout: 3000 });
// Type content
const textarea = page.locator('[data-testid="note-editor"] textarea, textarea.note-content, .note-editor textarea');
await textarea.fill(noteContent);
// CRITICAL: Wait for labels to appear WITHOUT refreshing
const labels = page.locator('.label-badge, .tag, [data-testid*="label"]');
// Labels should appear within 5 seconds (optimistic update + server processing)
await expect(labels.first()).toBeVisible({ timeout: 5000 });
console.log('✓ Labels appeared immediately without page refresh');
});
test('should handle auto-labeling failure gracefully', async ({ page }) => {
// This test verifies error handling: if auto-labeling fails, the note should still be created
const noteContent = 'Test note with very short content';
// Click "New Note"
await page.click('[data-testid="new-note-button"], button:has-text("New Note"), .create-note-btn');
// Wait for note creation
await page.waitForSelector('.note-card, [data-testid^="note-"]', { timeout: 3000 });
// Click on the note
const firstNote = page.locator('.note-card, [data-testid^="note-"]').first();
await firstNote.click();
// Wait for editor
await page.waitForSelector('[data-testid="note-editor"], textarea.note-content, .note-editor', { timeout: 3000 });
// Type very short content (may not generate labels)
const textarea = page.locator('[data-testid="note-editor"] textarea, textarea.note-content, .note-editor textarea');
await textarea.fill(noteContent);
// Wait for processing
await page.waitForTimeout(2000);
// Note should still be visible even if no labels are assigned
await expect(firstNote).toBeVisible();
console.log('✓ Note created successfully even if auto-labeling fails or returns no suggestions');
});
test('should auto-label in notebook context', async ({ page }) => {
// Test that auto-labeling uses notebook context for suggestions
const noteContent = 'Planning a trip to Japan next month';
// Create a notebook first (if not exists)
const notebookExists = await page.locator('.notebook-item:has-text("Travel")').count();
if (notebookExists === 0) {
await page.click('[data-testid="create-notebook-button"], button:has-text("Create Notebook")');
await page.fill('input[placeholder*="notebook name"], input[placeholder*="name"]', 'Travel');
await page.click('button:has-text("Create"), button[type="submit"]');
}
// Navigate to Travel notebook
await page.click('.notebook-item:has-text("Travel")');
// Wait for navigation
await page.waitForTimeout(500);
// Click "New Note"
await page.click('[data-testid="new-note-button"], button:has-text("New Note"), .create-note-btn');
// Wait for note creation
await page.waitForSelector('.note-card, [data-testid^="note-"]', { timeout: 3000 });
// Click on the note
const firstNote = page.locator('.note-card, [data-testid^="note-"]').first();
await firstNote.click();
// Wait for editor
await page.waitForSelector('[data-testid="note-editor"], textarea.note-content, .note-editor', { timeout: 3000 });
// Type travel-related content
const textarea = page.locator('[data-testid="note-editor"] textarea, textarea.note-content, .note-editor textarea');
await textarea.fill(noteContent);
// Wait for auto-labeling
await page.waitForTimeout(1000);
// Check for labels (should be travel-related)
const labels = page.locator('.label-badge, .tag, [data-testid*="label"]');
const labelCount = await labels.count();
// At least one label should appear (or note should still be visible if no labels)
if (labelCount > 0) {
const labelText = await labels.first().textContent();
console.log('✓ Auto-labeling in notebook context applied labels:', labelText);
} else {
console.log('✓ Note created in notebook context (no labels generated for this content)');
}
// Note should be visible regardless
await expect(firstNote).toBeVisible();
});
});

View File

@@ -0,0 +1,147 @@
import { test, expect } from '@playwright/test'
test.describe('Admin Dashboard', () => {
test.beforeEach(async ({ page }) => {
// Navigate to admin dashboard
await page.goto('/admin')
})
test('should redirect to home if not authenticated', async ({ page, context }) => {
// Clear authentication
await context.clearCookies()
await page.goto('/admin')
await expect(page).toHaveURL('/')
})
test('should show sidebar navigation with all sections', async ({ page }) => {
// Check sidebar exists
const sidebar = page.locator('aside')
await expect(sidebar).toBeVisible()
// Check all navigation items exist
await expect(page.getByRole('link', { name: /dashboard/i })).toBeVisible()
await expect(page.getByRole('link', { name: /users/i })).toBeVisible()
await expect(page.getByRole('link', { name: /ai management/i })).toBeVisible()
await expect(page.getByRole('link', { name: /settings/i })).toBeVisible()
})
test('should show main content area with metrics', async ({ page }) => {
// Check main content area exists
const main = page.locator('main')
await expect(main).toBeVisible()
// Check metrics are displayed
await expect(page.getByText(/total users/i)).toBeVisible()
await expect(page.getByText(/active sessions/i)).toBeVisible()
await expect(page.getByText(/total notes/i)).toBeVisible()
await expect(page.getByText(/ai requests/i)).toBeVisible()
})
test('should highlight active section in sidebar', async ({ page }) => {
// Dashboard should be active on /admin
const dashboardLink = page.getByRole('link', { name: /dashboard/i })
await expect(dashboardLink).toHaveClass(/bg-gray-100|bg-zinc-800/)
})
test('should navigate between sections', async ({ page }) => {
// Navigate to Users
await page.click('a[href="/admin/users"]')
await expect(page).toHaveURL(/\/admin\/users/)
await expect(page.getByRole('heading', { name: /users/i })).toBeVisible()
// Navigate to AI Management
await page.click('a[href="/admin/ai"]')
await expect(page).toHaveURL(/\/admin\/ai/)
await expect(page.getByRole('heading', { name: /ai management/i })).toBeVisible()
// Navigate to Settings
await page.click('a[href="/admin/settings"]')
await expect(page).toHaveURL(/\/admin\/settings/)
await expect(page.getByRole('heading', { name: /settings/i }).first()).toBeVisible()
// Navigate back to Dashboard
await page.click('a[href="/admin"]')
await expect(page).toHaveURL(/\/admin\/?$/)
await expect(page.getByRole('heading', { name: /dashboard/i })).toBeVisible()
})
test('should be responsive on desktop (1024px+)', async ({ page }) => {
await page.setViewportSize({ width: 1024, height: 768 })
// Check sidebar is visible on desktop
const sidebar = page.locator('aside')
await expect(sidebar).toBeVisible()
await expect(sidebar).toHaveCSS('width', '256px')
// Check content area takes remaining space
const main = page.locator('main')
await expect(main).toBeVisible()
})
test('should be responsive on tablet (640px-1023px)', async ({ page }) => {
await page.setViewportSize({ width: 768, height: 1024 })
// Check layout still works on tablet
const sidebar = page.locator('aside')
await expect(sidebar).toBeVisible()
const main = page.locator('main')
await expect(main).toBeVisible()
})
test('should show metrics with trend indicators', async ({ page }) => {
// Check for trend indicators in metrics
const trends = page.locator('.text-green-600, .text-red-600, .dark\\:text-green-400, .dark\\:text-red-400')
await expect(trends).toHaveCount(3) // 3 metrics have trend indicators
})
test('should show users page correctly', async ({ page }) => {
await page.goto('/admin/users')
await expect(page.getByRole('heading', { name: /users/i })).toBeVisible()
await expect(page.getByText(/manage application users and permissions/i)).toBeVisible()
})
test('should show AI management page correctly', async ({ page }) => {
await page.goto('/admin/ai')
await expect(page.getByRole('heading', { name: /ai management/i })).toBeVisible()
await expect(page.getByText(/monitor and configure ai features/i)).toBeVisible()
await expect(page.getByText(/total requests/i)).toBeVisible()
await expect(page.getByText(/active ai features/i)).toBeVisible()
})
test('should show settings page correctly', async ({ page }) => {
await page.goto('/admin/settings')
await expect(page.getByRole('heading', { name: /settings/i }).first()).toBeVisible()
await expect(page.getByText(/configure application-wide settings/i)).toBeVisible()
})
})
test.describe('Admin Dashboard Accessibility', () => {
test('should be keyboard navigable', async ({ page }) => {
await page.goto('/admin')
// Check tab order
await page.keyboard.press('Tab')
await expect(page.getByRole('link', { name: /dashboard/i })).toBeFocused()
await page.keyboard.press('Tab')
await expect(page.getByRole('link', { name: /users/i })).toBeFocused()
await page.keyboard.press('Tab')
await expect(page.getByRole('link', { name: /ai management/i })).toBeFocused()
await page.keyboard.press('Tab')
await expect(page.getByRole('link', { name: /settings/i })).toBeFocused()
})
test('should have proper heading hierarchy', async ({ page }) => {
await page.goto('/admin')
// Check main heading is h1
const h1 = page.getByRole('heading', { level: 1, name: /dashboard/i })
await expect(h1).toBeVisible()
})
})

View File

@@ -0,0 +1,448 @@
/**
* Test suite for AI field migrations
* Validates that Note and AiFeedback models work correctly with new AI fields
*/
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
describe('AI Fields Migration Tests', () => {
beforeAll(async () => {
// Ensure clean test environment
await prisma.aiFeedback.deleteMany({})
await prisma.note.deleteMany({
where: { title: { contains: 'TEST_AI' } }
})
})
afterAll(async () => {
// Cleanup
await prisma.aiFeedback.deleteMany({})
await prisma.note.deleteMany({
where: { title: { contains: 'TEST_AI' } }
})
await prisma.$disconnect()
})
describe('Note Model - AI Fields', () => {
test('should create note without AI fields (backward compatibility)', async () => {
const note = await prisma.note.create({
data: {
title: 'TEST_AI: Note without AI',
content: 'This is a test note without AI fields',
userId: 'test-user-id'
}
})
expect(note).toBeDefined()
expect(note.id).toBeDefined()
expect(note.title).toBe('TEST_AI: Note without AI')
expect(note.autoGenerated).toBeNull()
expect(note.aiProvider).toBeNull()
expect(note.aiConfidence).toBeNull()
expect(note.language).toBeNull()
expect(note.languageConfidence).toBeNull()
expect(note.lastAiAnalysis).toBeNull()
})
test('should create note with all AI fields populated', async () => {
const testDate = new Date()
const note = await prisma.note.create({
data: {
title: 'TEST_AI: Note with AI fields',
content: 'This is a test note with AI fields',
userId: 'test-user-id',
autoGenerated: true,
aiProvider: 'openai',
aiConfidence: 95,
language: 'fr',
languageConfidence: 0.98,
lastAiAnalysis: testDate
}
})
expect(note).toBeDefined()
expect(note.autoGenerated).toBe(true)
expect(note.aiProvider).toBe('openai')
expect(note.aiConfidence).toBe(95)
expect(note.language).toBe('fr')
expect(note.languageConfidence).toBe(0.98)
expect(note.lastAiAnalysis).toEqual(testDate)
})
test('should update note with AI fields', async () => {
const note = await prisma.note.create({
data: {
title: 'TEST_AI: Note for update test',
content: 'Initial content',
userId: 'test-user-id'
}
})
const updatedNote = await prisma.note.update({
where: { id: note.id },
data: {
autoGenerated: true,
aiProvider: 'ollama',
aiConfidence: 87
}
})
expect(updatedNote.autoGenerated).toBe(true)
expect(updatedNote.aiProvider).toBe('ollama')
expect(updatedNote.aiConfidence).toBe(87)
})
test('should query notes filtered by AI fields', async () => {
await prisma.note.create({
data: {
title: 'TEST_AI: Auto-generated note 1',
content: 'Test content',
userId: 'test-user-id',
autoGenerated: true,
aiProvider: 'openai'
}
})
await prisma.note.create({
data: {
title: 'TEST_AI: Auto-generated note 2',
content: 'Test content',
userId: 'test-user-id',
autoGenerated: true,
aiProvider: 'ollama'
}
})
const autoGeneratedNotes = await prisma.note.findMany({
where: {
title: { contains: 'TEST_AI' },
autoGenerated: true
}
})
expect(autoGeneratedNotes.length).toBeGreaterThanOrEqual(2)
expect(autoGeneratedNotes.every(n => n.autoGenerated === true)).toBe(true)
})
})
describe('AiFeedback Model', () => {
test('should create feedback entry', async () => {
const note = await prisma.note.create({
data: {
title: 'TEST_AI: Note for feedback',
content: 'Test note',
userId: 'test-user-id'
}
})
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'AI suggested title',
metadata: JSON.stringify({
aiProvider: 'openai',
confidence: 95,
model: 'gpt-4',
timestamp: new Date().toISOString()
})
}
})
expect(feedback).toBeDefined()
expect(feedback.id).toBeDefined()
expect(feedback.feedbackType).toBe('thumbs_up')
expect(feedback.feature).toBe('title_suggestion')
expect(feedback.originalContent).toBe('AI suggested title')
expect(feedback.noteId).toBe(note.id)
})
test('should handle thumbs_down feedback', async () => {
const note = await prisma.note.create({
data: {
title: 'TEST_AI: Note for thumbs down',
content: 'Test note',
userId: 'test-user-id'
}
})
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_down',
feature: 'title_suggestion',
originalContent: 'Bad suggestion'
}
})
expect(feedback.feedbackType).toBe('thumbs_down')
})
test('should handle correction feedback', async () => {
const note = await prisma.note.create({
data: {
title: 'TEST_AI: Note for correction',
content: 'Test note',
userId: 'test-user-id'
}
})
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'correction',
feature: 'title_suggestion',
originalContent: 'Wrong suggestion',
correctedContent: 'Corrected version'
}
})
expect(feedback.feedbackType).toBe('correction')
expect(feedback.correctedContent).toBe('Corrected version')
})
test('should query feedback by note', async () => {
const note = await prisma.note.create({
data: {
title: 'TEST_AI: Note for feedback query',
content: 'Test note',
userId: 'test-user-id'
}
})
await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Feedback 1'
}
})
await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_down',
feature: 'title_suggestion',
originalContent: 'Feedback 2'
}
})
const feedbacks = await prisma.aiFeedback.findMany({
where: { noteId: note.id },
orderBy: { createdAt: 'asc' }
})
expect(feedbacks.length).toBe(2)
})
test('should query feedback by feature', async () => {
const note = await prisma.note.create({
data: {
title: 'TEST_AI: Note for feature query',
content: 'Test note',
userId: 'test-user-id'
}
})
await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Feedback 1'
}
})
await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'semantic_search',
originalContent: 'Feedback 2'
}
})
const titleFeedbacks = await prisma.aiFeedback.findMany({
where: { feature: 'title_suggestion' }
})
expect(titleFeedbacks.length).toBeGreaterThanOrEqual(1)
expect(titleFeedbacks.every(f => f.feature === 'title_suggestion')).toBe(true)
})
})
describe('Cascade Deletion', () => {
test('should cascade delete feedback when note is deleted', async () => {
const note = await prisma.note.create({
data: {
title: 'TEST_AI: Note for cascade test',
content: 'Test note',
userId: 'test-user-id'
}
})
await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Feedback to be deleted'
}
})
// Verify feedback exists
const feedbacksBefore = await prisma.aiFeedback.findMany({
where: { noteId: note.id }
})
expect(feedbacksBefore.length).toBe(1)
// Delete the note
await prisma.note.delete({
where: { id: note.id }
})
// Verify feedback was cascade deleted
const feedbacksAfter = await prisma.aiFeedback.findMany({
where: { noteId: note.id }
})
expect(feedbacksAfter.length).toBe(0)
})
test('should cascade delete feedback when user is deleted', async () => {
// This test would require a User model with proper setup
// For now, we'll skip as user deletion is a more complex operation
// that may involve authentication and authorization
})
})
describe('Index Performance', () => {
test('should have indexes on critical fields', async () => {
// Verify indexes exist by checking query plan or performance
// For SQLite, indexes are created in the migration
// This is more of a documentation test than a runtime test
const note = await prisma.note.create({
data: {
title: 'TEST_AI: Note for index test',
content: 'Test note',
userId: 'test-user-id'
}
})
await prisma.aiFeedback.createMany({
data: [
{
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Feedback 1'
},
{
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_down',
feature: 'semantic_search',
originalContent: 'Feedback 2'
}
]
})
// Query by noteId (should use index)
const byNoteId = await prisma.aiFeedback.findMany({
where: { noteId: note.id }
})
expect(byNoteId.length).toBe(2)
// Query by userId (should use index)
const byUserId = await prisma.aiFeedback.findMany({
where: { userId: 'test-user-id' }
})
expect(byUserId.length).toBeGreaterThanOrEqual(2)
// Query by feature (should use index)
const byFeature = await prisma.aiFeedback.findMany({
where: { feature: 'title_suggestion' }
})
expect(byFeature.length).toBeGreaterThanOrEqual(1)
})
})
describe('Data Types Validation', () => {
test('should accept valid aiProvider values', async () => {
const providers = ['openai', 'ollama', null]
for (const provider of providers) {
const note = await prisma.note.create({
data: {
title: `TEST_AI: Note with provider ${provider}`,
content: 'Test note',
userId: 'test-user-id',
aiProvider: provider
}
})
expect(note.aiProvider).toBe(provider)
}
})
test('should accept valid aiConfidence range (0-100)', async () => {
const confidences = [0, 50, 100]
for (const conf of confidences) {
const note = await prisma.note.create({
data: {
title: `TEST_AI: Note with confidence ${conf}`,
content: 'Test note',
userId: 'test-user-id',
aiConfidence: conf
}
})
expect(note.aiConfidence).toBe(conf)
}
})
test('should accept valid languageConfidence range (0.0-1.0)', async () => {
const confidences = [0.0, 0.5, 0.99, 1.0]
for (const conf of confidences) {
const note = await prisma.note.create({
data: {
title: `TEST_AI: Note with lang confidence ${conf}`,
content: 'Test note',
userId: 'test-user-id',
languageConfidence: conf
}
})
expect(note.languageConfidence).toBe(conf)
}
})
test('should accept valid ISO 639-1 language codes', async () => {
const languages = ['en', 'fr', 'es', 'de', 'fa', null]
for (const lang of languages) {
const note = await prisma.note.create({
data: {
title: `TEST_AI: Note in language ${lang}`,
content: 'Test note',
userId: 'test-user-id',
language: lang
}
})
expect(note.language).toBe(lang)
}
})
})
})

View File

@@ -0,0 +1,156 @@
# Migration Tests
This directory contains comprehensive test suites for validating Prisma schema and data migrations for the Keep notes application.
## Test Files
- **setup.ts** - Test utilities for database setup, teardown, and test data generation
- **schema-migration.test.ts** - Validates schema migrations (tables, columns, indexes, relationships)
- **data-migration.test.ts** - Validates data migration (transformation, integrity, edge cases)
- **rollback.test.ts** - Validates rollback capability and data recovery
- **performance.test.ts** - Validates migration performance with various dataset sizes
- **integrity.test.ts** - Validates data integrity (no loss/corruption, foreign keys, indexes)
## Running Tests
### Run all migration tests
```bash
npm run test:migration
```
### Run migration tests in watch mode
```bash
npm run test:migration:watch
```
### Run specific test file
```bash
npm run test:unit tests/migration/schema-migration.test.ts
```
### Run tests with coverage
```bash
npm run test:unit:coverage
```
## Test Coverage Goals
- **Minimum threshold:** 80% coverage for migration-related code
- **Coverage areas:** Migration scripts, test utilities, schema transformations
- **Exclude from coverage:** Test files themselves (`*.test.ts`)
## Test Structure
### Schema Migration Tests
- Core table existence (User, Note, Notebook, Label, etc.)
- AI feature tables (AiFeedback, MemoryEchoInsight, UserAISettings)
- Note table AI fields (autoGenerated, aiProvider, aiConfidence, etc.)
- Index creation on critical fields
- Foreign key relationships
- Unique constraints
- Default values
### Data Migration Tests
- Empty database migration
- Basic note migration
- AI fields data migration
- AiFeedback data migration
- MemoryEchoInsight data migration
- UserAISettings data migration
- Data integrity verification
- Edge cases (empty strings, long content, special characters)
- Performance benchmarks
### Rollback Tests
- Schema state verification
- Column/table rollback simulation
- Data recovery after rollback
- Orphaned record handling
- Rollback safety checks
- Rollback error handling
### Performance Tests
- Empty migration (< 1 second)
- Small dataset (10 notes) (< 1 second)
- Medium dataset (100 notes) (< 5 seconds)
- Target dataset (1,000 notes) (< 30 seconds)
- Stress test (10,000 notes) (< 30 seconds)
- AI features performance
- Database size tracking
- Concurrent operations
### Integrity Tests
- No data loss
- No data corruption
- Foreign key relationship maintenance
- Index integrity
- AI fields preservation
- Batch operations integrity
- Data type integrity
## Test Utilities
The `setup.ts` file provides reusable utilities:
- `setupTestEnvironment()` - Initialize test environment
- `createTestPrismaClient()` - Create isolated Prisma client
- `initializeTestDatabase()` - Apply all migrations
- `cleanupTestDatabase()` - Clean up test database
- `createSampleNotes()` - Generate sample test notes
- `createSampleAINotes()` - Generate AI-enabled test notes
- `measureExecutionTime()` - Performance measurement helper
- `verifyDataIntegrity()` - Data integrity checks
- `verifyTableExists()` - Table existence verification
- `verifyColumnExists()` - Column existence verification
- `verifyIndexExists()` - Index existence verification
- `getTableSchema()` - Get table schema information
- `getDatabaseSize()` - Get database file size
## Acceptance Criteria Coverage
**AC 1:** Unit tests exist for all migration scripts
**AC 2:** Integration tests verify database state before/after migrations
**AC 3:** Test suite validates rollback capability
**AC 4:** Performance tests ensure migrations complete within acceptable limits
**AC 5:** Tests verify data integrity (no loss/corruption)
**AC 6:** Test coverage meets minimum threshold (80%)
## CI/CD Integration
These tests are configured to run in CI/CD pipelines:
```yaml
# Example CI configuration
- name: Run migration tests
run: npm run test:migration
- name: Check coverage
run: npm run test:unit:coverage
```
## Isolation
Each test suite runs in an isolated test database:
- **Location:** `prisma/test-databases/migration-test.db`
- **Lifecycle:** Created before test suite, deleted after
- **Conflict:** No conflict with development database
## Troubleshooting
### Tests fail with database locked error
Ensure no other process is using the test database. The test utilities automatically clean up the test database.
### Tests timeout
Increase timeout values in `vitest.config.ts` if necessary.
### Coverage below threshold
Review coverage report in `coverage/index.html` to identify untested code.
## Contributing
When adding new migrations:
1. Add corresponding test cases in appropriate test files
2. Update this README with new test coverage
3. Ensure coverage threshold (80%) is maintained
4. Run all migration tests before committing

View File

@@ -0,0 +1,631 @@
/**
* Data Migration Tests
* Validates that data migration scripts work correctly
* Tests data transformation, integrity, and edge cases
*/
import { PrismaClient } from '@prisma/client'
import {
setupTestEnvironment,
createTestPrismaClient,
initializeTestDatabase,
cleanupTestDatabase,
createSampleNotes,
createSampleAINotes,
verifyDataIntegrity,
measureExecutionTime
} from './setup'
describe('Data Migration Tests', () => {
let prisma: PrismaClient
beforeAll(async () => {
await setupTestEnvironment()
prisma = createTestPrismaClient()
await initializeTestDatabase(prisma)
})
afterAll(async () => {
await cleanupTestDatabase(prisma)
})
describe('Empty Database Migration', () => {
test('should migrate empty database successfully', async () => {
// Verify database is empty
const noteCount = await prisma.note.count()
expect(noteCount).toBe(0)
// Data migration should handle empty database gracefully
// No data should be created or lost
expect(noteCount).toBe(0)
})
})
describe('Basic Data Migration', () => {
beforeEach(async () => {
// Clean up before each test
await prisma.note.deleteMany({})
await prisma.aiFeedback.deleteMany({})
})
test('should migrate basic notes without AI fields', async () => {
// Create sample notes (simulating pre-migration data)
await createSampleNotes(prisma, 10)
// Verify notes are created
const noteCount = await prisma.note.count()
expect(noteCount).toBe(10)
// All notes should have null AI fields (backward compatibility)
const notes = await prisma.note.findMany()
notes.forEach(note => {
expect(note.autoGenerated).toBeNull()
expect(note.aiProvider).toBeNull()
expect(note.aiConfidence).toBeNull()
expect(note.language).toBeNull()
expect(note.languageConfidence).toBeNull()
expect(note.lastAiAnalysis).toBeNull()
})
})
test('should preserve existing note data during migration', async () => {
// Create a note with all fields
const originalNote = await prisma.note.create({
data: {
title: 'Original Note',
content: 'Original content',
color: 'blue',
isPinned: true,
isArchived: false,
type: 'text',
size: 'medium',
userId: 'test-user-id',
order: 0
}
})
// Simulate migration by querying the note
const noteAfterMigration = await prisma.note.findUnique({
where: { id: originalNote.id }
})
// Verify all original fields are preserved
expect(noteAfterMigration?.title).toBe('Original Note')
expect(noteAfterMigration?.content).toBe('Original content')
expect(noteAfterMigration?.color).toBe('blue')
expect(noteAfterMigration?.isPinned).toBe(true)
expect(noteAfterMigration?.isArchived).toBe(false)
expect(noteAfterMigration?.type).toBe('text')
expect(noteAfterMigration?.size).toBe('medium')
expect(noteAfterMigration?.order).toBe(0)
})
})
describe('AI Fields Data Migration', () => {
test('should handle notes with all AI fields populated', async () => {
const testNote = await prisma.note.create({
data: {
title: 'AI Test Note',
content: 'Test content',
userId: 'test-user-id',
autoGenerated: true,
aiProvider: 'openai',
aiConfidence: 95,
language: 'en',
languageConfidence: 0.98,
lastAiAnalysis: new Date()
}
})
// Verify AI fields are correctly stored
expect(testNote.autoGenerated).toBe(true)
expect(testNote.aiProvider).toBe('openai')
expect(testNote.aiConfidence).toBe(95)
expect(testNote.language).toBe('en')
expect(testNote.languageConfidence).toBe(0.98)
expect(testNote.lastAiAnalysis).toBeDefined()
})
test('should handle notes with partial AI fields', async () => {
// Create note with only some AI fields
const note1 = await prisma.note.create({
data: {
title: 'Partial AI Note 1',
content: 'Test content',
userId: 'test-user-id',
autoGenerated: true,
aiProvider: 'ollama'
// Other AI fields are null
}
})
expect(note1.autoGenerated).toBe(true)
expect(note1.aiProvider).toBe('ollama')
expect(note1.aiConfidence).toBeNull()
expect(note1.language).toBeNull()
// Create note with different partial fields
const note2 = await prisma.note.create({
data: {
title: 'Partial AI Note 2',
content: 'Test content',
userId: 'test-user-id',
aiConfidence: 87,
language: 'fr',
languageConfidence: 0.92
// Other AI fields are null
}
})
expect(note2.autoGenerated).toBeNull()
expect(note2.aiProvider).toBeNull()
expect(note2.aiConfidence).toBe(87)
expect(note2.language).toBe('fr')
expect(note2.languageConfidence).toBe(0.92)
})
test('should handle null values in AI fields correctly', async () => {
const note = await prisma.note.create({
data: {
title: 'Null AI Fields Note',
content: 'Test content',
userId: 'test-user-id'
// All AI fields are null by default
}
})
// Verify all AI fields are null
expect(note.autoGenerated).toBeNull()
expect(note.aiProvider).toBeNull()
expect(note.aiConfidence).toBeNull()
expect(note.language).toBeNull()
expect(note.languageConfidence).toBeNull()
expect(note.lastAiAnalysis).toBeNull()
})
})
describe('AiFeedback Data Migration', () => {
test('should create and retrieve AiFeedback entries', async () => {
const note = await prisma.note.create({
data: {
title: 'Feedback Test Note',
content: 'Test content',
userId: 'test-user-id'
}
})
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'AI suggested title',
metadata: JSON.stringify({
aiProvider: 'openai',
confidence: 95,
timestamp: new Date().toISOString()
})
}
})
// Verify feedback is correctly stored
expect(feedback.noteId).toBe(note.id)
expect(feedback.feedbackType).toBe('thumbs_up')
expect(feedback.feature).toBe('title_suggestion')
expect(feedback.originalContent).toBe('AI suggested title')
expect(feedback.metadata).toBeDefined()
})
test('should handle different feedback types', async () => {
const note = await prisma.note.create({
data: {
title: 'Feedback Types Note',
content: 'Test content',
userId: 'test-user-id'
}
})
const feedbackTypes = [
{ type: 'thumbs_up', feature: 'title_suggestion', content: 'Good suggestion' },
{ type: 'thumbs_down', feature: 'semantic_search', content: 'Bad result' },
{ type: 'correction', feature: 'title_suggestion', content: 'Wrong', corrected: 'Correct' }
]
for (const fb of feedbackTypes) {
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: fb.type,
feature: fb.feature,
originalContent: fb.content,
correctedContent: fb.corrected
}
})
expect(feedback.feedbackType).toBe(fb.type)
}
})
test('should store and retrieve metadata JSON correctly', async () => {
const note = await prisma.note.create({
data: {
title: 'Metadata Test Note',
content: 'Test content',
userId: 'test-user-id'
}
})
const metadata = {
aiProvider: 'ollama',
model: 'llama2-7b',
confidence: 87,
timestamp: new Date().toISOString(),
additional: {
latency: 234,
tokens: 456
}
}
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Test suggestion',
metadata: JSON.stringify(metadata)
}
})
// Parse and verify metadata
const parsedMetadata = JSON.parse(feedback.metadata || '{}')
expect(parsedMetadata.aiProvider).toBe('ollama')
expect(parsedMetadata.confidence).toBe(87)
expect(parsedMetadata.additional).toBeDefined()
})
})
describe('MemoryEchoInsight Data Migration', () => {
test('should create and retrieve MemoryEchoInsight entries', async () => {
const note1 = await prisma.note.create({
data: {
title: 'Echo Note 1',
content: 'Content about programming',
userId: 'test-user-id'
}
})
const note2 = await prisma.note.create({
data: {
title: 'Echo Note 2',
content: 'Content about coding',
userId: 'test-user-id'
}
})
const insight = await prisma.memoryEchoInsight.create({
data: {
userId: 'test-user-id',
note1Id: note1.id,
note2Id: note2.id,
similarityScore: 0.85,
insight: 'These notes are similar because they both discuss programming concepts',
insightDate: new Date()
}
})
expect(insight.note1Id).toBe(note1.id)
expect(insight.note2Id).toBe(note2.id)
expect(insight.similarityScore).toBe(0.85)
expect(insight.insight).toBeDefined()
})
test('should handle insight feedback and dismissal', async () => {
const note1 = await prisma.note.create({
data: {
title: 'Echo Feedback Note 1',
content: 'Content A',
userId: 'test-user-id'
}
})
const note2 = await prisma.note.create({
data: {
title: 'Echo Feedback Note 2',
content: 'Content B',
userId: 'test-user-id'
}
})
const insight = await prisma.memoryEchoInsight.create({
data: {
userId: 'test-user-id',
note1Id: note1.id,
note2Id: note2.id,
similarityScore: 0.75,
insight: 'Test insight',
feedback: 'useful',
dismissed: false
}
})
expect(insight.feedback).toBe('useful')
expect(insight.dismissed).toBe(false)
// Update insight to mark as dismissed
const updatedInsight = await prisma.memoryEchoInsight.update({
where: { id: insight.id },
data: { dismissed: true }
})
expect(updatedInsight.dismissed).toBe(true)
})
})
describe('UserAISettings Data Migration', () => {
test('should create and retrieve UserAISettings', async () => {
const user = await prisma.user.create({
data: {
email: 'ai-settings@test.com',
name: 'AI Settings User'
}
})
const settings = await prisma.userAISettings.create({
data: {
userId: user.id,
titleSuggestions: true,
semanticSearch: false,
memoryEcho: true,
memoryEchoFrequency: 'weekly',
aiProvider: 'ollama',
preferredLanguage: 'fr'
}
})
expect(settings.userId).toBe(user.id)
expect(settings.titleSuggestions).toBe(true)
expect(settings.semanticSearch).toBe(false)
expect(settings.memoryEchoFrequency).toBe('weekly')
expect(settings.aiProvider).toBe('ollama')
expect(settings.preferredLanguage).toBe('fr')
})
test('should handle default values correctly', async () => {
const user = await prisma.user.create({
data: {
email: 'default-ai-settings@test.com',
name: 'Default AI User'
}
})
const settings = await prisma.userAISettings.create({
data: {
userId: user.id
// All other fields should use defaults
}
})
expect(settings.titleSuggestions).toBe(true)
expect(settings.semanticSearch).toBe(true)
expect(settings.memoryEcho).toBe(true)
expect(settings.memoryEchoFrequency).toBe('daily')
expect(settings.aiProvider).toBe('auto')
expect(settings.preferredLanguage).toBe('auto')
})
})
describe('Data Integrity', () => {
test('should verify no data loss after migration', async () => {
// Create initial data
const initialNotes = await createSampleNotes(prisma, 50)
const initialCount = initialNotes.length
// Simulate migration by querying data
const notesAfterMigration = await prisma.note.findMany()
const finalCount = notesAfterMigration.length
// Verify no data loss
expect(finalCount).toBe(initialCount)
// Verify each note's data is intact
for (const note of notesAfterMigration) {
expect(note.title).toBeDefined()
expect(note.content).toBeDefined()
}
})
test('should verify no data corruption after migration', async () => {
// Create notes with complex data
await prisma.note.create({
data: {
title: 'Complex Data Note',
content: 'This is a note with **markdown** formatting',
checkItems: JSON.stringify([{ text: 'Task 1', done: false }, { text: 'Task 2', done: true }]),
images: JSON.stringify([{ url: 'image1.jpg', caption: 'Caption 1' }]),
userId: 'test-user-id'
}
})
const note = await prisma.note.findFirst({
where: { title: 'Complex Data Note' }
})
// Verify complex data is preserved
expect(note?.content).toContain('**markdown**')
if (note?.checkItems) {
const checkItems = JSON.parse(note.checkItems)
expect(checkItems.length).toBe(2)
}
if (note?.images) {
const images = JSON.parse(note.images)
expect(images.length).toBe(1)
}
})
test('should maintain foreign key relationships', async () => {
// Create a user
const user = await prisma.user.create({
data: {
email: 'fk-test@test.com',
name: 'FK Test User'
}
})
// Create a notebook for the user
const notebook = await prisma.notebook.create({
data: {
name: 'FK Test Notebook',
order: 0,
userId: user.id
}
})
// Create notes in the notebook
await prisma.note.createMany({
data: [
{
title: 'FK Note 1',
content: 'Content 1',
userId: user.id,
notebookId: notebook.id
},
{
title: 'FK Note 2',
content: 'Content 2',
userId: user.id,
notebookId: notebook.id
}
]
})
// Verify relationships are maintained
const retrievedNotebook = await prisma.notebook.findUnique({
where: { id: notebook.id },
include: { notes: true }
})
expect(retrievedNotebook?.notes.length).toBe(2)
expect(retrievedNotebook?.userId).toBe(user.id)
})
})
describe('Edge Cases', () => {
test('should handle empty strings in text fields', async () => {
const note = await prisma.note.create({
data: {
title: '',
content: 'Content with empty title',
userId: 'test-user-id'
}
})
expect(note.title).toBe('')
expect(note.content).toBe('Content with empty title')
})
test('should handle very long text content', async () => {
const longContent = 'A'.repeat(10000)
const note = await prisma.note.create({
data: {
title: 'Long Content Note',
content: longContent,
userId: 'test-user-id'
}
})
expect(note.content).toHaveLength(10000)
})
test('should handle special characters in text fields', async () => {
const specialChars = 'Note with émojis 🎉 and spëcial çharacters & spåcial ñumbers 123'
const note = await prisma.note.create({
data: {
title: specialChars,
content: specialChars,
userId: 'test-user-id'
}
})
expect(note.title).toBe(specialChars)
expect(note.content).toBe(specialChars)
})
test('should handle null userId in some tables (optional relationships)', async () => {
const note = await prisma.note.create({
data: {
title: 'No User Note',
content: 'Note without userId',
userId: null
}
})
expect(note.userId).toBeNull()
})
})
describe('Migration Performance', () => {
test('should complete migration within acceptable time for 100 notes', async () => {
// Clean up
await prisma.note.deleteMany({})
// Create 100 notes and measure time
const { result, duration } = await measureExecutionTime(async () => {
await createSampleNotes(prisma, 100)
})
// Migration should complete quickly (< 5 seconds for 100 notes)
expect(duration).toBeLessThan(5000)
expect(result.length).toBe(100)
})
})
describe('Batch Operations', () => {
test('should handle batch insert of notes', async () => {
const notesData = Array.from({ length: 20 }, (_, i) => ({
title: `Batch Note ${i + 1}`,
content: `Batch content ${i + 1}`,
userId: 'test-user-id',
order: i
}))
await prisma.note.createMany({
data: notesData
})
const count = await prisma.note.count()
expect(count).toBe(20)
})
test('should handle batch insert of feedback', async () => {
const note = await prisma.note.create({
data: {
title: 'Batch Feedback Note',
content: 'Test content',
userId: 'test-user-id'
}
})
const feedbackData = Array.from({ length: 10 }, (_, i) => ({
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: `Feedback ${i + 1}`
}))
await prisma.aiFeedback.createMany({
data: feedbackData
})
const count = await prisma.aiFeedback.count()
expect(count).toBe(10)
})
})
})

View File

@@ -0,0 +1,721 @@
/**
* Data Integrity Tests
* Validates that data is preserved and not corrupted during migration
* Tests data loss prevention, foreign key relationships, and indexes
*/
import { PrismaClient } from '@prisma/client'
import {
setupTestEnvironment,
createTestPrismaClient,
initializeTestDatabase,
cleanupTestDatabase,
createSampleNotes,
verifyDataIntegrity
} from './setup'
describe('Data Integrity Tests', () => {
let prisma: PrismaClient
beforeAll(async () => {
await setupTestEnvironment()
prisma = createTestPrismaClient()
await initializeTestDatabase(prisma)
})
afterAll(async () => {
await cleanupTestDatabase(prisma)
})
describe('No Data Loss', () => {
test('should preserve all notes after migration', async () => {
// Create test notes
const initialNotes = await createSampleNotes(prisma, 50)
const initialCount = initialNotes.length
// Query after migration
const notesAfterMigration = await prisma.note.findMany()
const finalCount = notesAfterMigration.length
// Verify no data loss
expect(finalCount).toBe(initialCount)
})
test('should preserve note titles', async () => {
const testTitles = [
'Important Meeting Notes',
'Shopping List',
'Project Ideas',
'Recipe Collection',
'Book Reviews'
]
// Create notes with specific titles
for (const title of testTitles) {
await prisma.note.create({
data: {
title,
content: `Content for ${title}`,
userId: 'test-user-id'
}
})
}
// Verify all titles are preserved
const notes = await prisma.note.findMany({
where: { title: { in: testTitles } }
})
const preservedTitles = notes.map(n => n.title)
for (const title of testTitles) {
expect(preservedTitles).toContain(title)
}
})
test('should preserve note content', async () => {
const testContent = [
'This is a simple text note',
'Note with **markdown** formatting',
'Note with [links](https://example.com)',
'Note with numbers: 1, 2, 3, 4, 5',
'Note with special characters: émojis 🎉 & çharacters'
]
// Create notes with specific content
for (const content of testContent) {
await prisma.note.create({
data: {
title: `Content Test`,
content,
userId: 'test-user-id'
}
})
}
// Verify all content is preserved
const notes = await prisma.note.findMany({
where: { title: 'Content Test' }
})
const preservedContent = notes.map(n => n.content)
for (const content of testContent) {
expect(preservedContent).toContain(content)
}
})
test('should preserve note metadata', async () => {
// Create note with all metadata
const note = await prisma.note.create({
data: {
title: 'Metadata Test Note',
content: 'Test content',
color: 'red',
isPinned: true,
isArchived: false,
type: 'text',
size: 'large',
userId: 'test-user-id',
order: 5
}
})
// Verify metadata is preserved
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.color).toBe('red')
expect(retrieved?.isPinned).toBe(true)
expect(retrieved?.isArchived).toBe(false)
expect(retrieved?.type).toBe('text')
expect(retrieved?.size).toBe('large')
expect(retrieved?.order).toBe(5)
})
})
describe('No Data Corruption', () => {
test('should preserve checkItems JSON structure', async () => {
const checkItems = JSON.stringify([
{ text: 'Buy groceries', done: false },
{ text: 'Call dentist', done: true },
{ text: 'Finish report', done: false },
{ text: 'Schedule meeting', done: false }
])
const note = await prisma.note.create({
data: {
title: 'Checklist Test Note',
content: 'My checklist',
checkItems,
userId: 'test-user-id'
}
})
// Verify checkItems are preserved and can be parsed
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.checkItems).toBeDefined()
const parsedCheckItems = JSON.parse(retrieved?.checkItems || '[]')
expect(parsedCheckItems.length).toBe(4)
expect(parsedCheckItems[0].text).toBe('Buy groceries')
expect(parsedCheckItems[0].done).toBe(false)
expect(parsedCheckItems[1].done).toBe(true)
})
test('should preserve images JSON structure', async () => {
const images = JSON.stringify([
{ url: 'image1.jpg', caption: 'First image' },
{ url: 'image2.jpg', caption: 'Second image' },
{ url: 'image3.jpg', caption: 'Third image' }
])
const note = await prisma.note.create({
data: {
title: 'Images Test Note',
content: 'Note with images',
images,
userId: 'test-user-id'
}
})
// Verify images are preserved and can be parsed
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.images).toBeDefined()
const parsedImages = JSON.parse(retrieved?.images || '[]')
expect(parsedImages.length).toBe(3)
expect(parsedImages[0].url).toBe('image1.jpg')
expect(parsedImages[0].caption).toBe('First image')
})
test('should preserve labels JSON structure', async () => {
const labels = JSON.stringify(['work', 'important', 'project'])
const note = await prisma.note.create({
data: {
title: 'Labels Test Note',
content: 'Note with labels',
labels,
userId: 'test-user-id'
}
})
// Verify labels are preserved and can be parsed
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.labels).toBeDefined()
const parsedLabels = JSON.parse(retrieved?.labels || '[]')
expect(parsedLabels.length).toBe(3)
expect(parsedLabels).toContain('work')
expect(parsedLabels).toContain('important')
expect(parsedLabels).toContain('project')
})
test('should preserve embedding JSON structure', async () => {
const embedding = JSON.stringify({
vector: [0.1, 0.2, 0.3, 0.4, 0.5],
model: 'text-embedding-ada-002',
timestamp: new Date().toISOString()
})
const note = await prisma.note.create({
data: {
title: 'Embedding Test Note',
content: 'Note with embedding',
embedding,
userId: 'test-user-id'
}
})
// Verify embedding is preserved and can be parsed
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.embedding).toBeDefined()
const parsedEmbedding = JSON.parse(retrieved?.embedding || '{}')
expect(parsedEmbedding.vector).toEqual([0.1, 0.2, 0.3, 0.4, 0.5])
expect(parsedEmbedding.model).toBe('text-embedding-ada-002')
})
test('should preserve links JSON structure', async () => {
const links = JSON.stringify([
{ url: 'https://example.com', title: 'Example' },
{ url: 'https://docs.example.com', title: 'Documentation' }
])
const note = await prisma.note.create({
data: {
title: 'Links Test Note',
content: 'Note with links',
links,
userId: 'test-user-id'
}
})
// Verify links are preserved and can be parsed
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.links).toBeDefined()
const parsedLinks = JSON.parse(retrieved?.links || '[]')
expect(parsedLinks.length).toBe(2)
expect(parsedLinks[0].url).toBe('https://example.com')
expect(parsedLinks[0].title).toBe('Example')
})
})
describe('Foreign Key Relationships', () => {
test('should maintain Note to User relationship', async () => {
const user = await prisma.user.create({
data: {
email: 'fk-integrity@test.com',
name: 'FK Integrity User'
}
})
const note = await prisma.note.create({
data: {
title: 'User Integrity Note',
content: 'Test content',
userId: user.id
}
})
// Verify relationship is maintained
expect(note.userId).toBe(user.id)
// Query user's notes
const userNotes = await prisma.note.findMany({
where: { userId: user.id }
})
expect(userNotes.length).toBeGreaterThan(0)
expect(userNotes.some(n => n.id === note.id)).toBe(true)
})
test('should maintain Note to Notebook relationship', async () => {
const notebook = await prisma.notebook.create({
data: {
name: 'Integrity Notebook',
order: 0,
userId: 'test-user-id'
}
})
const note = await prisma.note.create({
data: {
title: 'Notebook Integrity Note',
content: 'Test content',
userId: 'test-user-id',
notebookId: notebook.id
}
})
// Verify relationship is maintained
expect(note.notebookId).toBe(notebook.id)
// Query notebook's notes
const notebookNotes = await prisma.note.findMany({
where: { notebookId: notebook.id }
})
expect(notebookNotes.length).toBeGreaterThan(0)
expect(notebookNotes.some(n => n.id === note.id)).toBe(true)
})
test('should maintain AiFeedback to Note relationship', async () => {
const note = await prisma.note.create({
data: {
title: 'Feedback Integrity Note',
content: 'Test content',
userId: 'test-user-id'
}
})
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Test feedback'
}
})
// Verify relationship is maintained
expect(feedback.noteId).toBe(note.id)
// Query note's feedback
const noteFeedback = await prisma.aiFeedback.findMany({
where: { noteId: note.id }
})
expect(noteFeedback.length).toBeGreaterThan(0)
expect(noteFeedback.some(f => f.id === feedback.id)).toBe(true)
})
test('should maintain AiFeedback to User relationship', async () => {
const user = await prisma.user.create({
data: {
email: 'feedback-user@test.com',
name: 'Feedback User'
}
})
const note = await prisma.note.create({
data: {
title: 'User Feedback Note',
content: 'Test content',
userId: user.id
}
})
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: user.id,
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Test feedback'
}
})
// Verify relationship is maintained
expect(feedback.userId).toBe(user.id)
// Query user's feedback
const userFeedback = await prisma.aiFeedback.findMany({
where: { userId: user.id }
})
expect(userFeedback.length).toBeGreaterThan(0)
expect(userFeedback.some(f => f.id === feedback.id)).toBe(true)
})
test('should maintain cascade delete correctly', async () => {
// Create a note with feedback
const note = await prisma.note.create({
data: {
title: 'Cascade Delete Note',
content: 'Test content',
userId: 'test-user-id'
}
})
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Test feedback'
}
})
// Verify feedback exists
const feedbacksBefore = await prisma.aiFeedback.findMany({
where: { noteId: note.id }
})
expect(feedbacksBefore.length).toBe(1)
// Delete note
await prisma.note.delete({
where: { id: note.id }
})
// Verify feedback is cascade deleted
const feedbacksAfter = await prisma.aiFeedback.findMany({
where: { noteId: note.id }
})
expect(feedbacksAfter.length).toBe(0)
})
})
describe('Index Integrity', () => {
test('should maintain index on Note.isPinned', async () => {
// Create notes with various pinned states
await prisma.note.createMany({
data: [
{ title: 'Pinned 1', content: 'Content 1', userId: 'test-user-id', isPinned: true },
{ title: 'Not Pinned 1', content: 'Content 2', userId: 'test-user-id', isPinned: false },
{ title: 'Pinned 2', content: 'Content 3', userId: 'test-user-id', isPinned: true },
{ title: 'Not Pinned 2', content: 'Content 4', userId: 'test-user-id', isPinned: false }
]
})
// Query by isPinned (should use index)
const pinnedNotes = await prisma.note.findMany({
where: { isPinned: true }
})
expect(pinnedNotes.length).toBe(2)
expect(pinnedNotes.every(n => n.isPinned === true)).toBe(true)
})
test('should maintain index on Note.order', async () => {
// Create notes with specific order
await prisma.note.createMany({
data: [
{ title: 'Order 0', content: 'Content 0', userId: 'test-user-id', order: 0 },
{ title: 'Order 1', content: 'Content 1', userId: 'test-user-id', order: 1 },
{ title: 'Order 2', content: 'Content 2', userId: 'test-user-id', order: 2 },
{ title: 'Order 3', content: 'Content 3', userId: 'test-user-id', order: 3 }
]
})
// Query ordered by order (should use index)
const orderedNotes = await prisma.note.findMany({
orderBy: { order: 'asc' }
})
expect(orderedNotes[0].order).toBe(0)
expect(orderedNotes[1].order).toBe(1)
expect(orderedNotes[2].order).toBe(2)
expect(orderedNotes[3].order).toBe(3)
})
test('should maintain index on AiFeedback.noteId', async () => {
const note = await prisma.note.create({
data: {
title: 'Index Feedback Note',
content: 'Test content',
userId: 'test-user-id'
}
})
// Create multiple feedback entries
await prisma.aiFeedback.createMany({
data: [
{ noteId: note.id, userId: 'test-user-id', feedbackType: 'thumbs_up', feature: 'title_suggestion', originalContent: 'Feedback 1' },
{ noteId: note.id, userId: 'test-user-id', feedbackType: 'thumbs_down', feature: 'semantic_search', originalContent: 'Feedback 2' },
{ noteId: note.id, userId: 'test-user-id', feedbackType: 'thumbs_up', feature: 'paragraph_refactor', originalContent: 'Feedback 3' }
]
})
// Query by noteId (should use index)
const feedbacks = await prisma.aiFeedback.findMany({
where: { noteId: note.id }
})
expect(feedbacks.length).toBe(3)
})
test('should maintain index on AiFeedback.userId', async () => {
const note = await prisma.note.create({
data: {
title: 'User Index Feedback Note',
content: 'Test content',
userId: 'test-user-id'
}
})
// Create multiple feedback entries for same user
await prisma.aiFeedback.createMany({
data: [
{ noteId: note.id, userId: 'test-user-id', feedbackType: 'thumbs_up', feature: 'title_suggestion', originalContent: 'Feedback 1' },
{ noteId: note.id, userId: 'test-user-id', feedbackType: 'thumbs_down', feature: 'semantic_search', originalContent: 'Feedback 2' }
]
})
// Query by userId (should use index)
const userFeedbacks = await prisma.aiFeedback.findMany({
where: { userId: 'test-user-id' }
})
expect(userFeedbacks.length).toBeGreaterThanOrEqual(2)
})
test('should maintain index on AiFeedback.feature', async () => {
const note1 = await prisma.note.create({
data: {
title: 'Feature Index Note 1',
content: 'Test content 1',
userId: 'test-user-id'
}
})
const note2 = await prisma.note.create({
data: {
title: 'Feature Index Note 2',
content: 'Test content 2',
userId: 'test-user-id'
}
})
// Create feedback with same feature
await prisma.aiFeedback.createMany({
data: [
{ noteId: note1.id, userId: 'test-user-id', feedbackType: 'thumbs_up', feature: 'title_suggestion', originalContent: 'Feedback 1' },
{ noteId: note2.id, userId: 'test-user-id', feedbackType: 'thumbs_up', feature: 'title_suggestion', originalContent: 'Feedback 2' }
]
})
// Query by feature (should use index)
const titleFeedbacks = await prisma.aiFeedback.findMany({
where: { feature: 'title_suggestion' }
})
expect(titleFeedbacks.length).toBeGreaterThanOrEqual(2)
})
})
describe('AI Fields Integrity', () => {
test('should preserve AI fields correctly', async () => {
const note = await prisma.note.create({
data: {
title: 'AI Fields Integrity Note',
content: 'Test content',
userId: 'test-user-id',
autoGenerated: true,
aiProvider: 'openai',
aiConfidence: 95,
language: 'en',
languageConfidence: 0.98,
lastAiAnalysis: new Date()
}
})
// Verify all AI fields are preserved
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.autoGenerated).toBe(true)
expect(retrieved?.aiProvider).toBe('openai')
expect(retrieved?.aiConfidence).toBe(95)
expect(retrieved?.language).toBe('en')
expect(retrieved?.languageConfidence).toBeCloseTo(0.98)
expect(retrieved?.lastAiAnalysis).toBeDefined()
})
test('should preserve null AI fields correctly', async () => {
const note = await prisma.note.create({
data: {
title: 'Null AI Fields Note',
content: 'Test content',
userId: 'test-user-id'
// All AI fields are null by default
}
})
// Verify all AI fields are null
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.autoGenerated).toBeNull()
expect(retrieved?.aiProvider).toBeNull()
expect(retrieved?.aiConfidence).toBeNull()
expect(retrieved?.language).toBeNull()
expect(retrieved?.languageConfidence).toBeNull()
expect(retrieved?.lastAiAnalysis).toBeNull()
})
})
describe('Batch Operations Integrity', () => {
test('should preserve data integrity during batch insert', async () => {
const notesData = Array.from({ length: 50 }, (_, i) => ({
title: `Batch Integrity Note ${i}`,
content: `Content ${i}`,
userId: 'test-user-id',
order: i,
isPinned: i % 2 === 0
}))
const result = await prisma.note.createMany({
data: notesData
})
expect(result.count).toBe(50)
// Verify all notes are created correctly
const notes = await prisma.note.findMany({
where: { title: { contains: 'Batch Integrity Note' } }
})
expect(notes.length).toBe(50)
// Verify data integrity
for (const note of notes) {
expect(note.content).toBeDefined()
expect(note.userId).toBe('test-user-id')
}
})
})
describe('Data Type Integrity', () => {
test('should preserve boolean values correctly', async () => {
const note = await prisma.note.create({
data: {
title: 'Boolean Test Note',
content: 'Test content',
userId: 'test-user-id',
isPinned: true,
isArchived: false
}
})
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.isPinned).toBe(true)
expect(retrieved?.isArchived).toBe(false)
})
test('should preserve numeric values correctly', async () => {
const note = await prisma.note.create({
data: {
title: 'Numeric Test Note',
content: 'Test content',
userId: 'test-user-id',
order: 42,
aiConfidence: 87,
languageConfidence: 0.95
}
})
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.order).toBe(42)
expect(retrieved?.aiConfidence).toBe(87)
expect(retrieved?.languageConfidence).toBeCloseTo(0.95)
})
test('should preserve date values correctly', async () => {
const testDate = new Date('2024-01-15T10:30:00Z')
const note = await prisma.note.create({
data: {
title: 'Date Test Note',
content: 'Test content',
userId: 'test-user-id',
reminder: testDate,
lastAiAnalysis: testDate
}
})
const retrieved = await prisma.note.findUnique({
where: { id: note.id }
})
expect(retrieved?.reminder).toBeDefined()
expect(retrieved?.lastAiAnalysis).toBeDefined()
})
})
})

View File

@@ -0,0 +1,573 @@
/**
* Performance Tests
* Validates that migrations complete within acceptable time limits
* Tests scalability with various data sizes
*/
import { PrismaClient } from '@prisma/client'
import {
setupTestEnvironment,
createTestPrismaClient,
initializeTestDatabase,
cleanupTestDatabase,
createSampleNotes,
measureExecutionTime,
getDatabaseSize
} from './setup'
describe('Performance Tests', () => {
let prisma: PrismaClient
beforeAll(async () => {
await setupTestEnvironment()
prisma = createTestPrismaClient()
await initializeTestDatabase(prisma)
})
afterAll(async () => {
await cleanupTestDatabase(prisma)
})
describe('Empty Migration Performance', () => {
test('should complete empty database migration quickly', async () => {
// Clean up any existing data
await prisma.note.deleteMany({})
// Measure time to "migrate" empty database
const { result, duration } = await measureExecutionTime(async () => {
const noteCount = await prisma.note.count()
return { count: noteCount }
})
// Empty migration should complete instantly (< 1 second)
expect(duration).toBeLessThan(1000)
expect(result.count).toBe(0)
})
})
describe('Small Dataset Performance (10 notes)', () => {
beforeEach(async () => {
await prisma.note.deleteMany({})
})
test('should complete migration for 10 notes within 1 second', async () => {
// Create 10 notes
const { result: notes, duration: createDuration } = await measureExecutionTime(async () => {
return await createSampleNotes(prisma, 10)
})
expect(notes.length).toBe(10)
expect(createDuration).toBeLessThan(1000)
// Measure query performance
const { result, duration: queryDuration } = await measureExecutionTime(async () => {
return await prisma.note.findMany()
})
expect(result.length).toBe(10)
expect(queryDuration).toBeLessThan(500)
})
test('should complete create operation for 10 notes within 1 second', async () => {
const { result, duration } = await measureExecutionTime(async () => {
const notes = []
for (let i = 0; i < 10; i++) {
const note = await prisma.note.create({
data: {
title: `Perf Test Note ${i}`,
content: `Test content ${i}`,
userId: 'test-user-id',
order: i
}
})
notes.push(note)
}
return notes
})
expect(result.length).toBe(10)
expect(duration).toBeLessThan(1000)
})
test('should complete update operation for 10 notes within 1 second', async () => {
// Create notes first
await createSampleNotes(prisma, 10)
// Measure update performance
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.updateMany({
data: { isPinned: true },
where: { title: { contains: 'Test Note' } }
})
})
expect(duration).toBeLessThan(1000)
})
})
describe('Medium Dataset Performance (100 notes)', () => {
beforeEach(async () => {
await prisma.note.deleteMany({})
})
test('should complete migration for 100 notes within 5 seconds', async () => {
// Create 100 notes
const { result: notes, duration: createDuration } = await measureExecutionTime(async () => {
return await createSampleNotes(prisma, 100)
})
expect(notes.length).toBe(100)
expect(createDuration).toBeLessThan(5000)
// Measure query performance
const { result, duration: queryDuration } = await measureExecutionTime(async () => {
return await prisma.note.findMany()
})
expect(result.length).toBe(100)
expect(queryDuration).toBeLessThan(1000)
})
test('should complete create operation for 100 notes within 5 seconds', async () => {
const { result, duration } = await measureExecutionTime(async () => {
const notes = []
for (let i = 0; i < 100; i++) {
const note = await prisma.note.create({
data: {
title: `Perf Test Note ${i}`,
content: `Test content ${i}`,
userId: 'test-user-id',
order: i
}
})
notes.push(note)
}
return notes
})
expect(result.length).toBe(100)
expect(duration).toBeLessThan(5000)
})
test('should complete batch insert for 100 notes within 2 seconds', async () => {
const notesData = Array.from({ length: 100 }, (_, i) => ({
title: `Batch Note ${i}`,
content: `Batch content ${i}`,
userId: 'test-user-id',
order: i
}))
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.createMany({
data: notesData
})
})
expect(result.count).toBe(100)
expect(duration).toBeLessThan(2000)
})
test('should complete filtered query for 100 notes within 500ms', async () => {
await createSampleNotes(prisma, 100)
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.findMany({
where: {
isPinned: true
}
})
})
expect(duration).toBeLessThan(500)
})
})
describe('Target Dataset Performance (1,000 notes)', () => {
beforeEach(async () => {
await prisma.note.deleteMany({})
})
test('should complete migration for 1,000 notes within 30 seconds', async () => {
// Create 1,000 notes in batches for better performance
const { result: notes, duration: createDuration } = await measureExecutionTime(async () => {
const allNotes = []
const batchSize = 100
const totalNotes = 1000
for (let batch = 0; batch < totalNotes / batchSize; batch++) {
const batchData = Array.from({ length: batchSize }, (_, i) => ({
title: `Perf Note ${batch * batchSize + i}`,
content: `Test content ${batch * batchSize + i}`,
userId: 'test-user-id',
order: batch * batchSize + i
}))
await prisma.note.createMany({ data: batchData })
}
return await prisma.note.findMany()
})
expect(notes.length).toBe(1000)
expect(createDuration).toBeLessThan(30000)
})
test('should complete batch insert for 1,000 notes within 10 seconds', async () => {
const notesData = Array.from({ length: 1000 }, (_, i) => ({
title: `Batch Note ${i}`,
content: `Batch content ${i}`,
userId: 'test-user-id',
order: i
}))
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.createMany({
data: notesData
})
})
expect(result.count).toBe(1000)
expect(duration).toBeLessThan(10000)
})
test('should complete query for 1,000 notes within 1 second', async () => {
const notesData = Array.from({ length: 1000 }, (_, i) => ({
title: `Query Test Note ${i}`,
content: `Query test content ${i}`,
userId: 'test-user-id',
order: i
}))
await prisma.note.createMany({ data: notesData })
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.findMany()
})
expect(result.length).toBe(1000)
expect(duration).toBeLessThan(1000)
})
test('should complete filtered query for 1,000 notes within 1 second', async () => {
// Create notes with various pinned states
const notesData = Array.from({ length: 1000 }, (_, i) => ({
title: `Filter Test Note ${i}`,
content: `Filter test content ${i}`,
userId: 'test-user-id',
order: i,
isPinned: i % 3 === 0 // Every 3rd note is pinned
}))
await prisma.note.createMany({ data: notesData })
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.findMany({
where: {
isPinned: true
}
})
})
expect(result.length).toBeGreaterThan(0)
expect(duration).toBeLessThan(1000)
})
test('should complete indexed query for 1,000 notes within 500ms', async () => {
// Create notes
const notesData = Array.from({ length: 1000 }, (_, i) => ({
title: `Index Test Note ${i}`,
content: `Index test content ${i}`,
userId: 'test-user-id',
order: i,
isPinned: i % 2 === 0
}))
await prisma.note.createMany({ data: notesData })
// Query using indexed field (isPinned)
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.findMany({
where: {
isPinned: true
},
orderBy: {
order: 'asc'
}
})
})
expect(result.length).toBeGreaterThan(0)
expect(duration).toBeLessThan(500)
})
})
describe('Stress Test Performance (10,000 notes)', () => {
beforeEach(async () => {
await prisma.note.deleteMany({})
})
test('should complete batch insert for 10,000 notes within 30 seconds', async () => {
const notesData = Array.from({ length: 10000 }, (_, i) => ({
title: `Stress Note ${i}`,
content: `Stress test content ${i}`,
userId: 'test-user-id',
order: i
}))
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.createMany({
data: notesData
})
})
expect(result.count).toBe(10000)
expect(duration).toBeLessThan(30000)
})
test('should complete query for 10,000 notes within 2 seconds', async () => {
const notesData = Array.from({ length: 10000 }, (_, i) => ({
title: `Stress Query Note ${i}`,
content: `Stress query content ${i}`,
userId: 'test-user-id',
order: i
}))
await prisma.note.createMany({ data: notesData })
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.findMany({
take: 100 // Limit to 100 for performance
})
})
expect(result.length).toBe(100)
expect(duration).toBeLessThan(2000)
})
test('should handle pagination for 10,000 notes efficiently', async () => {
const notesData = Array.from({ length: 10000 }, (_, i) => ({
title: `Pagination Note ${i}`,
content: `Pagination content ${i}`,
userId: 'test-user-id',
order: i
}))
await prisma.note.createMany({ data: notesData })
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.findMany({
skip: 100,
take: 50
})
})
expect(result.length).toBe(50)
expect(duration).toBeLessThan(1000)
})
})
describe('AI Features Performance', () => {
beforeEach(async () => {
await prisma.note.deleteMany({})
await prisma.aiFeedback.deleteMany({})
})
test('should create AI-enabled notes efficiently (100 notes)', async () => {
const { result, duration } = await measureExecutionTime(async () => {
const notes = []
for (let i = 0; i < 100; i++) {
const note = await prisma.note.create({
data: {
title: `AI Note ${i}`,
content: `AI content ${i}`,
userId: 'test-user-id',
autoGenerated: i % 2 === 0,
aiProvider: i % 3 === 0 ? 'openai' : 'ollama',
aiConfidence: 70 + i,
language: i % 2 === 0 ? 'en' : 'fr',
languageConfidence: 0.85 + (i * 0.001),
lastAiAnalysis: new Date(),
order: i
}
})
notes.push(note)
}
return notes
})
expect(result.length).toBe(100)
expect(duration).toBeLessThan(5000)
})
test('should query by AI fields efficiently (100 notes)', async () => {
// Create AI notes
const notes = []
for (let i = 0; i < 100; i++) {
const note = await prisma.note.create({
data: {
title: `AI Query Note ${i}`,
content: `Content ${i}`,
userId: 'test-user-id',
autoGenerated: i % 2 === 0,
aiProvider: i % 3 === 0 ? 'openai' : 'ollama',
order: i
}
})
notes.push(note)
}
// Query by autoGenerated
const { result: autoGenerated, duration: duration1 } = await measureExecutionTime(async () => {
return await prisma.note.findMany({
where: { autoGenerated: true }
})
})
expect(autoGenerated.length).toBeGreaterThan(0)
expect(duration1).toBeLessThan(500)
// Query by aiProvider
const { result: openaiNotes, duration: duration2 } = await measureExecutionTime(async () => {
return await prisma.note.findMany({
where: { aiProvider: 'openai' }
})
})
expect(openaiNotes.length).toBeGreaterThan(0)
expect(duration2).toBeLessThan(500)
})
test('should create AI feedback efficiently (100 feedback entries)', async () => {
// Create a note
const note = await prisma.note.create({
data: {
title: 'Feedback Performance Note',
content: 'Test content',
userId: 'test-user-id'
}
})
const { result, duration } = await measureExecutionTime(async () => {
const feedbacks = []
for (let i = 0; i < 100; i++) {
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: i % 3 === 0 ? 'thumbs_up' : 'thumbs_down',
feature: 'title_suggestion',
originalContent: `Feedback ${i}`,
metadata: JSON.stringify({
aiProvider: 'openai',
confidence: 70 + i,
timestamp: new Date().toISOString()
})
}
})
feedbacks.push(feedback)
}
return feedbacks
})
expect(result.length).toBe(100)
expect(duration).toBeLessThan(5000)
})
test('should query feedback by note efficiently (100 feedback entries)', async () => {
const note = await prisma.note.create({
data: {
title: 'Feedback Query Note',
content: 'Test content',
userId: 'test-user-id'
}
})
// Create feedback
for (let i = 0; i < 100; i++) {
await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: `Feedback ${i}`
}
})
}
// Query by noteId (should use index)
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.aiFeedback.findMany({
where: { noteId: note.id },
orderBy: { createdAt: 'asc' }
})
})
expect(result.length).toBe(100)
expect(duration).toBeLessThan(500)
})
})
describe('Database Size Performance', () => {
test('should track database size growth', async () => {
// Get initial size
const initialSize = await getDatabaseSize(prisma)
// Add 100 notes
const notesData = Array.from({ length: 100 }, (_, i) => ({
title: `Size Test Note ${i}`,
content: `Size test content ${i}`.repeat(10), // Larger content
userId: 'test-user-id',
order: i
}))
await prisma.note.createMany({ data: notesData })
// Get size after adding notes
const sizeAfter = await getDatabaseSize(prisma)
// Database should have grown
expect(sizeAfter).toBeGreaterThan(initialSize)
})
test('should handle large content efficiently', async () => {
const largeContent = 'A'.repeat(10000) // 10KB per note
const { result, duration } = await measureExecutionTime(async () => {
return await prisma.note.create({
data: {
title: 'Large Content Note',
content: largeContent,
userId: 'test-user-id'
}
})
})
expect(result.content).toHaveLength(10000)
expect(duration).toBeLessThan(1000)
})
})
describe('Concurrent Operations Performance', () => {
test('should handle multiple concurrent reads', async () => {
// Create test data
await createSampleNotes(prisma, 100)
// Measure concurrent read performance
const { duration } = await measureExecutionTime(async () => {
const promises = [
prisma.note.findMany({ take: 10 }),
prisma.note.findMany({ take: 10, skip: 10 }),
prisma.note.findMany({ take: 10, skip: 20 }),
prisma.note.findMany({ take: 10, skip: 30 }),
prisma.note.findMany({ take: 10, skip: 40 })
]
await Promise.all(promises)
})
// All concurrent reads should complete quickly
expect(duration).toBeLessThan(2000)
})
})
})

View File

@@ -0,0 +1,512 @@
/**
* Rollback Tests
* Validates that migrations can be safely rolled back
* Tests schema rollback, data recovery, and cleanup
*/
import { PrismaClient } from '@prisma/client'
import {
setupTestEnvironment,
createTestPrismaClient,
initializeTestDatabase,
cleanupTestDatabase,
createSampleNotes,
createSampleAINotes,
verifyDataIntegrity
} from './setup'
describe('Rollback Tests', () => {
let prisma: PrismaClient
beforeAll(async () => {
await setupTestEnvironment()
prisma = createTestPrismaClient()
await initializeTestDatabase(prisma)
})
afterAll(async () => {
await cleanupTestDatabase(prisma)
})
describe('Schema Rollback', () => {
test('should verify schema state before migration', async () => {
// Verify basic tables exist (pre-migration state)
const hasUser = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
'User'
)
expect(hasUser.length).toBeGreaterThan(0)
})
test('should verify AI tables exist after migration', async () => {
// Verify AI tables exist (post-migration state)
const hasAiFeedback = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
'AiFeedback'
)
expect(hasAiFeedback.length).toBeGreaterThan(0)
const hasMemoryEcho = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
'MemoryEchoInsight'
)
expect(hasMemoryEcho.length).toBeGreaterThan(0)
const hasUserAISettings = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
'UserAISettings'
)
expect(hasUserAISettings.length).toBeGreaterThan(0)
})
test('should verify Note AI columns exist after migration', async () => {
// Check if AI columns exist in Note table
const noteSchema = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`PRAGMA table_info(Note)`
)
const aiColumns = ['autoGenerated', 'aiProvider', 'aiConfidence', 'language', 'languageConfidence', 'lastAiAnalysis']
for (const column of aiColumns) {
const columnExists = noteSchema.some((col: any) => col.name === column)
expect(columnExists).toBe(true)
}
})
test('should simulate dropping AI columns (rollback scenario)', async () => {
// In a real rollback, you would execute ALTER TABLE DROP COLUMN
// For SQLite, this requires creating a new table and copying data
// This test verifies we can identify which columns would be dropped
const noteSchema = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`PRAGMA table_info(Note)`
)
const aiColumns = ['autoGenerated', 'aiProvider', 'aiConfidence', 'language', 'languageConfidence', 'lastAiAnalysis']
const allColumns = noteSchema.map((col: any) => col.name)
// Verify all AI columns exist
for (const column of aiColumns) {
expect(allColumns).toContain(column)
}
})
test('should simulate dropping AI tables (rollback scenario)', async () => {
// In a real rollback, you would execute DROP TABLE
// This test verifies we can identify which tables would be dropped
const aiTables = ['AiFeedback', 'MemoryEchoInsight', 'UserAISettings']
for (const table of aiTables) {
const exists = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
table
)
expect(exists.length).toBeGreaterThan(0)
}
})
})
describe('Data Recovery After Rollback', () => {
beforeEach(async () => {
// Clean up before each test
await prisma.note.deleteMany({})
await prisma.aiFeedback.deleteMany({})
})
test('should preserve basic note data if AI columns are dropped', async () => {
// Create notes with AI fields
const noteWithAI = await prisma.note.create({
data: {
title: 'Note with AI',
content: 'This note has AI fields',
userId: 'test-user-id',
autoGenerated: true,
aiProvider: 'openai',
aiConfidence: 95,
language: 'en',
languageConfidence: 0.98,
lastAiAnalysis: new Date()
}
})
// Verify basic fields are present
expect(noteWithAI.id).toBeDefined()
expect(noteWithAI.title).toBe('Note with AI')
expect(noteWithAI.content).toBe('This note has AI fields')
expect(noteWithAI.userId).toBe('test-user-id')
// In a rollback, AI columns would be dropped but basic data should remain
// This verifies basic data integrity independent of AI fields
const basicNote = await prisma.note.findUnique({
where: { id: noteWithAI.id },
select: {
id: true,
title: true,
content: true,
userId: true
}
})
expect(basicNote?.id).toBe(noteWithAI.id)
expect(basicNote?.title).toBe(noteWithAI.title)
expect(basicNote?.content).toBe(noteWithAI.content)
})
test('should preserve note relationships if AI tables are dropped', async () => {
// Create a user and note
const user = await prisma.user.create({
data: {
email: 'rollback-test@test.com',
name: 'Rollback User'
}
})
const notebook = await prisma.notebook.create({
data: {
name: 'Rollback Notebook',
order: 0,
userId: user.id
}
})
const note = await prisma.note.create({
data: {
title: 'Rollback Test Note',
content: 'Test content',
userId: user.id,
notebookId: notebook.id
}
})
// Verify relationships exist
expect(note.userId).toBe(user.id)
expect(note.notebookId).toBe(notebook.id)
// After rollback (dropping AI tables), basic relationships should be preserved
const retrievedNote = await prisma.note.findUnique({
where: { id: note.id },
include: {
notebook: true,
user: true
}
})
expect(retrievedNote?.userId).toBe(user.id)
expect(retrievedNote?.notebookId).toBe(notebook.id)
})
test('should handle orphaned records after table drop', async () => {
// Create a note with AI feedback
const note = await prisma.note.create({
data: {
title: 'Orphan Test Note',
content: 'Test content',
userId: 'test-user-id'
}
})
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Test feedback'
}
})
// Verify feedback is linked to note
expect(feedback.noteId).toBe(note.id)
// After rollback (dropping AiFeedback table), the note should still exist
// but feedback would be orphaned/deleted
const noteExists = await prisma.note.findUnique({
where: { id: note.id }
})
expect(noteExists).toBeDefined()
expect(noteExists?.id).toBe(note.id)
})
test('should verify no orphaned records exist after proper migration', async () => {
// Create note with feedback
const note = await prisma.note.create({
data: {
title: 'Orphan Check Note',
content: 'Test content',
userId: 'test-user-id'
}
})
await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Test feedback'
}
})
// Verify no orphaned feedback (all feedback should have valid noteId)
const allFeedback = await prisma.aiFeedback.findMany()
for (const fb of allFeedback) {
const noteExists = await prisma.note.findUnique({
where: { id: fb.noteId }
})
expect(noteExists).toBeDefined()
}
})
})
describe('Rollback Safety Checks', () => {
test('should verify data before attempting rollback', async () => {
// Create test data
await createSampleNotes(prisma, 10)
// Count data before rollback
const noteCountBefore = await prisma.note.count()
expect(noteCountBefore).toBe(10)
// In a real rollback scenario, you would:
// 1. Create backup of data
// 2. Verify backup integrity
// 3. Execute rollback migration
// 4. Verify data integrity after rollback
// For this test, we verify we can count and validate data
const notes = await prisma.note.findMany()
expect(notes.length).toBe(10)
for (const note of notes) {
expect(note.id).toBeDefined()
expect(note.title).toBeDefined()
expect(note.content).toBeDefined()
}
})
test('should identify tables created by migration', async () => {
// Get all tables in database
const allTables = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'`
)
const tableNames = allTables.map((t: any) => t.name)
// Identify AI-related tables (created by migration)
const aiTables = tableNames.filter((name: string) =>
name === 'AiFeedback' ||
name === 'MemoryEchoInsight' ||
name === 'UserAISettings'
)
// Verify AI tables exist
expect(aiTables.length).toBeGreaterThanOrEqual(3)
})
test('should identify columns added by migration', async () => {
// Get all columns in Note table
const noteSchema = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`PRAGMA table_info(Note)`
)
const allColumns = noteSchema.map((col: any) => col.name)
// Identify AI-related columns (added by migration)
const aiColumns = allColumns.filter((name: string) =>
name === 'autoGenerated' ||
name === 'aiProvider' ||
name === 'aiConfidence' ||
name === 'language' ||
name === 'languageConfidence' ||
name === 'lastAiAnalysis'
)
// Verify all AI columns exist
expect(aiColumns.length).toBe(6)
})
})
describe('Rollback with Data', () => {
test('should preserve essential note data', async () => {
// Create comprehensive test data
const notes = await createSampleAINotes(prisma, 20)
// Verify all notes have essential data
for (const note of notes) {
expect(note.id).toBeDefined()
expect(note.content).toBeDefined()
}
// After rollback, essential data should be preserved
const allNotes = await prisma.note.findMany()
expect(allNotes.length).toBe(20)
})
test('should handle rollback with complex data structures', async () => {
// Create note with complex data
const complexNote = await prisma.note.create({
data: {
title: 'Complex Note',
content: '**Markdown** content with [links](https://example.com)',
checkItems: JSON.stringify([
{ text: 'Task 1', done: false },
{ text: 'Task 2', done: true },
{ text: 'Task 3', done: false }
]),
images: JSON.stringify([
{ url: 'image1.jpg', caption: 'Caption 1' },
{ url: 'image2.jpg', caption: 'Caption 2' }
]),
labels: JSON.stringify(['label1', 'label2', 'label3']),
userId: 'test-user-id'
}
})
// Verify complex data is stored
const retrieved = await prisma.note.findUnique({
where: { id: complexNote.id }
})
expect(retrieved?.content).toContain('**Markdown**')
expect(retrieved?.checkItems).toBeDefined()
expect(retrieved?.images).toBeDefined()
expect(retrieved?.labels).toBeDefined()
// After rollback, complex data should be preserved
if (retrieved?.checkItems) {
const checkItems = JSON.parse(retrieved.checkItems)
expect(checkItems.length).toBe(3)
}
})
})
describe('Rollback Error Handling', () => {
test('should handle rollback when AI data exists', async () => {
// Create notes with AI data
await createSampleAINotes(prisma, 10)
// Verify AI data exists
const aiNotes = await prisma.note.findMany({
where: {
OR: [
{ autoGenerated: true },
{ aiProvider: { not: null } },
{ language: { not: null } }
]
}
})
expect(aiNotes.length).toBeGreaterThan(0)
// In a rollback scenario, this data would be lost
// This test verifies we can detect it before rollback
const hasAIData = await prisma.note.findFirst({
where: {
autoGenerated: true
}
})
expect(hasAIData).toBeDefined()
})
test('should handle rollback when feedback exists', async () => {
// Create note with feedback
const note = await prisma.note.create({
data: {
title: 'Feedback Note',
content: 'Test content',
userId: 'test-user-id'
}
})
await prisma.aiFeedback.createMany({
data: [
{
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Feedback 1'
},
{
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_down',
feature: 'semantic_search',
originalContent: 'Feedback 2'
}
]
})
// Verify feedback exists
const feedbackCount = await prisma.aiFeedback.count()
expect(feedbackCount).toBe(2)
// In a rollback scenario, this feedback would be lost
// This test verifies we can detect it before rollback
const feedbacks = await prisma.aiFeedback.findMany()
expect(feedbacks.length).toBe(2)
})
})
describe('Rollback Validation', () => {
test('should validate database state after simulated rollback', async () => {
// Create test data
await createSampleNotes(prisma, 5)
// Verify current state
const noteCount = await prisma.note.count()
expect(noteCount).toBe(5)
// In a real rollback, we would:
// 1. Verify data is backed up
// 2. Execute rollback migration
// 3. Verify AI tables/columns are removed
// 4. Verify core data is intact
// 5. Verify no orphaned records
// For this test, we verify we can validate current state
const notes = await prisma.note.findMany()
expect(notes.every(n => n.id && n.content)).toBe(true)
})
test('should verify no data corruption in core tables', async () => {
// Create comprehensive test data
const user = await prisma.user.create({
data: {
email: 'corruption-test@test.com',
name: 'Corruption Test User'
}
})
const notebook = await prisma.notebook.create({
data: {
name: 'Corruption Test Notebook',
order: 0,
userId: user.id
}
})
await prisma.note.create({
data: {
title: 'Corruption Test Note',
content: 'Test content',
userId: user.id,
notebookId: notebook.id
}
})
// Verify relationships are intact
const retrievedUser = await prisma.user.findUnique({
where: { id: user.id },
include: { notebooks: true, notes: true }
})
expect(retrievedUser?.notebooks.length).toBe(1)
expect(retrievedUser?.notes.length).toBe(1)
})
})
})

View File

@@ -0,0 +1,518 @@
/**
* Schema Migration Tests
* Validates that all schema migrations (SQL migrations) work correctly
* Tests database structure, indexes, and relationships
*/
import { PrismaClient } from '@prisma/client'
import {
setupTestEnvironment,
createTestPrismaClient,
initializeTestDatabase,
cleanupTestDatabase,
verifyTableExists,
verifyIndexExists,
verifyColumnExists,
getTableSchema
} from './setup'
describe('Schema Migration Tests', () => {
let prisma: PrismaClient
beforeAll(async () => {
await setupTestEnvironment()
prisma = createTestPrismaClient()
await initializeTestDatabase(prisma)
})
afterAll(async () => {
await cleanupTestDatabase(prisma)
})
describe('Core Table Existence', () => {
test('should have User table', async () => {
const exists = await verifyTableExists(prisma, 'User')
expect(exists).toBe(true)
})
test('should have Note table', async () => {
const exists = await verifyTableExists(prisma, 'Note')
expect(exists).toBe(true)
})
test('should have Notebook table', async () => {
const exists = await verifyTableExists(prisma, 'Notebook')
expect(exists).toBe(true)
})
test('should have Label table', async () => {
const exists = await verifyTableExists(prisma, 'Label')
expect(exists).toBe(true)
})
test('should have Account table', async () => {
const exists = await verifyTableExists(prisma, 'Account')
expect(exists).toBe(true)
})
test('should have Session table', async () => {
const exists = await verifyTableExists(prisma, 'Session')
expect(exists).toBe(true)
})
})
describe('AI Feature Tables', () => {
test('should have AiFeedback table', async () => {
const exists = await verifyTableExists(prisma, 'AiFeedback')
expect(exists).toBe(true)
})
test('should have MemoryEchoInsight table', async () => {
const exists = await verifyTableExists(prisma, 'MemoryEchoInsight')
expect(exists).toBe(true)
})
test('should have UserAISettings table', async () => {
const exists = await verifyTableExists(prisma, 'UserAISettings')
expect(exists).toBe(true)
})
})
describe('Note Table AI Fields Migration', () => {
test('should have autoGenerated column', async () => {
const exists = await verifyColumnExists(prisma, 'Note', 'autoGenerated')
expect(exists).toBe(true)
})
test('should have aiProvider column', async () => {
const exists = await verifyColumnExists(prisma, 'Note', 'aiProvider')
expect(exists).toBe(true)
})
test('should have aiConfidence column', async () => {
const exists = await verifyColumnExists(prisma, 'Note', 'aiConfidence')
expect(exists).toBe(true)
})
test('should have language column', async () => {
const exists = await verifyColumnExists(prisma, 'Note', 'language')
expect(exists).toBe(true)
})
test('should have languageConfidence column', async () => {
const exists = await verifyColumnExists(prisma, 'Note', 'languageConfidence')
expect(exists).toBe(true)
})
test('should have lastAiAnalysis column', async () => {
const exists = await verifyColumnExists(prisma, 'Note', 'lastAiAnalysis')
expect(exists).toBe(true)
})
})
describe('AiFeedback Table Structure', () => {
test('should have noteId column', async () => {
const exists = await verifyColumnExists(prisma, 'AiFeedback', 'noteId')
expect(exists).toBe(true)
})
test('should have userId column', async () => {
const exists = await verifyColumnExists(prisma, 'AiFeedback', 'userId')
expect(exists).toBe(true)
})
test('should have feedbackType column', async () => {
const exists = await verifyColumnExists(prisma, 'AiFeedback', 'feedbackType')
expect(exists).toBe(true)
})
test('should have feature column', async () => {
const exists = await verifyColumnExists(prisma, 'AiFeedback', 'feature')
expect(exists).toBe(true)
})
test('should have originalContent column', async () => {
const exists = await verifyColumnExists(prisma, 'AiFeedback', 'originalContent')
expect(exists).toBe(true)
})
test('should have correctedContent column', async () => {
const exists = await verifyColumnExists(prisma, 'AiFeedback', 'correctedContent')
expect(exists).toBe(true)
})
test('should have metadata column', async () => {
const exists = await verifyColumnExists(prisma, 'AiFeedback', 'metadata')
expect(exists).toBe(true)
})
test('should have createdAt column', async () => {
const exists = await verifyColumnExists(prisma, 'AiFeedback', 'createdAt')
expect(exists).toBe(true)
})
})
describe('MemoryEchoInsight Table Structure', () => {
test('should have userId column', async () => {
const exists = await verifyColumnExists(prisma, 'MemoryEchoInsight', 'userId')
expect(exists).toBe(true)
})
test('should have note1Id column', async () => {
const exists = await verifyColumnExists(prisma, 'MemoryEchoInsight', 'note1Id')
expect(exists).toBe(true)
})
test('should have note2Id column', async () => {
const exists = await verifyColumnExists(prisma, 'MemoryEchoInsight', 'note2Id')
expect(exists).toBe(true)
})
test('should have similarityScore column', async () => {
const exists = await verifyColumnExists(prisma, 'MemoryEchoInsight', 'similarityScore')
expect(exists).toBe(true)
})
test('should have insight column', async () => {
const exists = await verifyColumnExists(prisma, 'MemoryEchoInsight', 'insight')
expect(exists).toBe(true)
})
test('should have insightDate column', async () => {
const exists = await verifyColumnExists(prisma, 'MemoryEchoInsight', 'insightDate')
expect(exists).toBe(true)
})
test('should have viewed column', async () => {
const exists = await verifyColumnExists(prisma, 'MemoryEchoInsight', 'viewed')
expect(exists).toBe(true)
})
test('should have feedback column', async () => {
const exists = await verifyColumnExists(prisma, 'MemoryEchoInsight', 'feedback')
expect(exists).toBe(true)
})
test('should have dismissed column', async () => {
const exists = await verifyColumnExists(prisma, 'MemoryEchoInsight', 'dismissed')
expect(exists).toBe(true)
})
})
describe('UserAISettings Table Structure', () => {
test('should have userId column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'userId')
expect(exists).toBe(true)
})
test('should have titleSuggestions column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'titleSuggestions')
expect(exists).toBe(true)
})
test('should have semanticSearch column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'semanticSearch')
expect(exists).toBe(true)
})
test('should have paragraphRefactor column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'paragraphRefactor')
expect(exists).toBe(true)
})
test('should have memoryEcho column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'memoryEcho')
expect(exists).toBe(true)
})
test('should have memoryEchoFrequency column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'memoryEchoFrequency')
expect(exists).toBe(true)
})
test('should have aiProvider column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'aiProvider')
expect(exists).toBe(true)
})
test('should have preferredLanguage column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'preferredLanguage')
expect(exists).toBe(true)
})
test('should have fontSize column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'fontSize')
expect(exists).toBe(true)
})
test('should have demoMode column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'demoMode')
expect(exists).toBe(true)
})
test('should have showRecentNotes column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'showRecentNotes')
expect(exists).toBe(true)
})
test('should have emailNotifications column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'emailNotifications')
expect(exists).toBe(true)
})
test('should have desktopNotifications column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'desktopNotifications')
expect(exists).toBe(true)
})
test('should have anonymousAnalytics column', async () => {
const exists = await verifyColumnExists(prisma, 'UserAISettings', 'anonymousAnalytics')
expect(exists).toBe(true)
})
})
describe('Index Creation', () => {
test('should have indexes on AiFeedback.noteId', async () => {
const exists = await verifyIndexExists(prisma, 'AiFeedback', 'AiFeedback_noteId_idx')
expect(exists).toBe(true)
})
test('should have indexes on AiFeedback.userId', async () => {
const exists = await verifyIndexExists(prisma, 'AiFeedback', 'AiFeedback_userId_idx')
expect(exists).toBe(true)
})
test('should have indexes on AiFeedback.feature', async () => {
const exists = await verifyIndexExists(prisma, 'AiFeedback', 'AiFeedback_feature_idx')
expect(exists).toBe(true)
})
test('should have indexes on AiFeedback.createdAt', async () => {
const exists = await verifyIndexExists(prisma, 'AiFeedback', 'AiFeedback_createdAt_idx')
expect(exists).toBe(true)
})
test('should have indexes on Note table', async () => {
// Note table should have indexes on various columns
const schema = await getTableSchema(prisma, 'sqlite_master')
expect(schema).toBeDefined()
})
})
describe('Foreign Key Relationships', () => {
test('should maintain Note to AiFeedback relationship', async () => {
// Create a test note
const note = await prisma.note.create({
data: {
title: 'Test FK Note',
content: 'Test content',
userId: 'test-user-id'
}
})
// Create feedback linked to the note
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Test feedback'
}
})
expect(feedback.noteId).toBe(note.id)
})
test('should maintain User to AiFeedback relationship', async () => {
// Create a test note
const note = await prisma.note.create({
data: {
title: 'Test User FK Note',
content: 'Test content',
userId: 'test-user-id'
}
})
// Create feedback linked to user
const feedback = await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_down',
feature: 'semantic_search',
originalContent: 'Test feedback'
}
})
expect(feedback.userId).toBe('test-user-id')
})
test('should cascade delete AiFeedback when Note is deleted', async () => {
// Create a note with feedback
const note = await prisma.note.create({
data: {
title: 'Test Cascade Note',
content: 'Test content',
userId: 'test-user-id'
}
})
await prisma.aiFeedback.create({
data: {
noteId: note.id,
userId: 'test-user-id',
feedbackType: 'thumbs_up',
feature: 'title_suggestion',
originalContent: 'Test feedback'
}
})
// Verify feedback exists
const feedbacksBefore = await prisma.aiFeedback.findMany({
where: { noteId: note.id }
})
expect(feedbacksBefore.length).toBe(1)
// Delete the note
await prisma.note.delete({
where: { id: note.id }
})
// Verify feedback is cascade deleted
const feedbacksAfter = await prisma.aiFeedback.findMany({
where: { noteId: note.id }
})
expect(feedbacksAfter.length).toBe(0)
})
test('should maintain Note to Notebook relationship', async () => {
// Create a notebook
const notebook = await prisma.notebook.create({
data: {
name: 'Test Notebook',
order: 0,
userId: 'test-user-id'
}
})
// Create a note in the notebook
const note = await prisma.note.create({
data: {
title: 'Test Notebook Note',
content: 'Test content',
userId: 'test-user-id',
notebookId: notebook.id
}
})
expect(note.notebookId).toBe(notebook.id)
})
})
describe('Unique Constraints', () => {
test('should enforce unique constraint on User.email', async () => {
// First user should be created
await prisma.user.create({
data: {
email: 'unique@test.com',
name: 'Unique User'
}
})
// Second user with same email should fail
await expect(
prisma.user.create({
data: {
email: 'unique@test.com',
name: 'Duplicate User'
}
})
).rejects.toThrow()
})
test('should enforce unique constraint on Notebook userId+name', async () => {
const userId = 'test-user-unique'
// First notebook should be created
await prisma.notebook.create({
data: {
name: 'Unique Notebook',
order: 0,
userId
}
})
// Second notebook with same name for same user should fail
await expect(
prisma.notebook.create({
data: {
name: 'Unique Notebook',
order: 1,
userId
}
})
).rejects.toThrow()
})
})
describe('Default Values', () => {
test('should have default values for Note table', async () => {
const note = await prisma.note.create({
data: {
content: 'Test content',
userId: 'test-user-id'
}
})
expect(note.color).toBe('default')
expect(note.isPinned).toBe(false)
expect(note.isArchived).toBe(false)
expect(note.type).toBe('text')
expect(note.size).toBe('small')
expect(note.order).toBe(0)
})
test('should have default values for UserAISettings', async () => {
const user = await prisma.user.create({
data: {
email: 'default-settings@test.com'
}
})
const settings = await prisma.userAISettings.create({
data: {
userId: user.id
}
})
expect(settings.titleSuggestions).toBe(true)
expect(settings.semanticSearch).toBe(true)
expect(settings.paragraphRefactor).toBe(true)
expect(settings.memoryEcho).toBe(true)
expect(settings.memoryEchoFrequency).toBe('daily')
expect(settings.aiProvider).toBe('auto')
expect(settings.preferredLanguage).toBe('auto')
expect(settings.fontSize).toBe('medium')
expect(settings.demoMode).toBe(false)
expect(settings.showRecentNotes).toBe(false)
expect(settings.emailNotifications).toBe(false)
expect(settings.desktopNotifications).toBe(false)
expect(settings.anonymousAnalytics).toBe(false)
})
})
describe('Schema Version Tracking', () => {
test('should have all migrations applied', async () => {
// Check that the migration tables exist
const migrationsExist = await verifyTableExists(prisma, '_prisma_migrations')
// In SQLite with Prisma, migrations are tracked via _prisma_migrations table
// For this test, we just verify the schema is complete
const hasUser = await verifyTableExists(prisma, 'User')
const hasNote = await verifyTableExists(prisma, 'Note')
const hasAiFeedback = await verifyTableExists(prisma, 'AiFeedback')
expect(hasUser && hasNote && hasAiFeedback).toBe(true)
})
})
})

View File

@@ -0,0 +1,271 @@
/**
* Test database setup and teardown utilities for migration tests
* Provides isolated database environments for each test suite
*/
import { PrismaClient } from '@prisma/client'
import * as fs from 'fs'
import * as path from 'path'
// Environment variables
const DATABASE_DIR = path.join(process.cwd(), 'prisma', 'test-databases')
const TEST_DATABASE_PATH = path.join(DATABASE_DIR, 'migration-test.db')
/**
* Initialize test environment
* Creates test database directory if it doesn't exist
*/
export async function setupTestEnvironment() {
// Ensure test database directory exists
if (!fs.existsSync(DATABASE_DIR)) {
fs.mkdirSync(DATABASE_DIR, { recursive: true })
}
// Clean up any existing test database
if (fs.existsSync(TEST_DATABASE_PATH)) {
fs.unlinkSync(TEST_DATABASE_PATH)
}
}
/**
* Create a Prisma client instance connected to test database
*/
export function createTestPrismaClient(): PrismaClient {
return new PrismaClient({
datasources: {
db: {
url: `file:${TEST_DATABASE_PATH}`
}
}
})
}
/**
* Initialize test database schema from migrations
* This applies all migrations to create a clean schema
*/
export async function initializeTestDatabase(prisma: PrismaClient) {
// Connect to database
await prisma.$connect()
// Read and execute all migration files in order
const migrationsDir = path.join(process.cwd(), 'prisma', 'migrations')
const migrationFolders = fs.readdirSync(migrationsDir)
.filter(name => !name.includes('migration_lock') && fs.statSync(path.join(migrationsDir, name)).isDirectory())
.sort()
// Execute each migration
for (const folder of migrationFolders) {
const migrationSql = fs.readFileSync(path.join(migrationsDir, folder, 'migration.sql'), 'utf-8')
try {
await prisma.$executeRawUnsafe(migrationSql)
} catch (error) {
// Some migrations might fail if tables already exist, which is okay for test setup
console.log(`Migration ${folder} note:`, (error as Error).message)
}
}
}
/**
* Cleanup test database
* Disconnects Prisma client and removes test database file
*/
export async function cleanupTestDatabase(prisma: PrismaClient) {
try {
await prisma.$disconnect()
} catch (error) {
console.error('Error disconnecting Prisma:', error)
}
// Remove test database file
if (fs.existsSync(TEST_DATABASE_PATH)) {
fs.unlinkSync(TEST_DATABASE_PATH)
}
}
/**
* Create sample test data
* Generates test notes with various configurations
*/
export async function createSampleNotes(prisma: PrismaClient, count: number = 10) {
const notes = []
const userId = 'test-user-123'
for (let i = 0; i < count; i++) {
const note = await prisma.note.create({
data: {
title: `Test Note ${i + 1}`,
content: `This is test content for note ${i + 1}`,
userId,
color: `color-${i % 5}`,
order: i,
isPinned: i % 3 === 0,
isArchived: false,
type: 'text',
size: i % 3 === 0 ? 'small' : 'medium'
}
})
notes.push(note)
}
return notes
}
/**
* Create sample AI-enabled notes
* Tests AI field migration scenarios
*/
export async function createSampleAINotes(prisma: PrismaClient, count: number = 10) {
const notes = []
const userId = 'test-user-ai'
for (let i = 0; i < count; i++) {
const note = await prisma.note.create({
data: {
title: `AI Test Note ${i + 1}`,
content: `This is AI test content for note ${i + 1}`,
userId,
color: 'default',
order: i,
autoGenerated: i % 2 === 0,
aiProvider: i % 3 === 0 ? 'openai' : 'ollama',
aiConfidence: 70 + i * 2,
language: i % 2 === 0 ? 'en' : 'fr',
languageConfidence: 0.85 + (i * 0.01),
lastAiAnalysis: new Date()
}
})
notes.push(note)
}
return notes
}
/**
* Measure execution time for a function
* Useful for performance testing
*/
export async function measureExecutionTime<T>(fn: () => Promise<T>): Promise<{ result: T; duration: number }> {
const start = performance.now()
const result = await fn()
const end = performance.now()
return {
result,
duration: end - start
}
}
/**
* Verify data integrity after migration
* Checks for data loss or corruption
*/
export async function verifyDataIntegrity(prisma: PrismaClient, expectedNoteCount: number) {
const noteCount = await prisma.note.count()
if (noteCount !== expectedNoteCount) {
throw new Error(`Data integrity check failed: Expected ${expectedNoteCount} notes, found ${noteCount}`)
}
// Verify no null critical fields
const allNotes = await prisma.note.findMany()
for (const note of allNotes) {
if (!note.title && !note.content) {
throw new Error(`Data integrity check failed: Note ${note.id} has neither title nor content`)
}
}
return true
}
/**
* Check if database tables exist
* Verifies schema migration success
*/
export async function verifyTableExists(prisma: PrismaClient, tableName: string): Promise<boolean> {
try {
const result = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
tableName
)
return result.length > 0
} catch (error) {
return false
}
}
/**
* Check if index exists on a table
* Verifies index creation migration success
*/
export async function verifyIndexExists(prisma: PrismaClient, tableName: string, indexName: string): Promise<boolean> {
try {
const result = await prisma.$queryRawUnsafe<Array<{ name: string }>>(
`SELECT name FROM sqlite_master WHERE type='index' AND tbl_name=? AND name=?`,
tableName,
indexName
)
return result.length > 0
} catch (error) {
return false
}
}
/**
* Verify foreign key relationships
* Ensures cascade delete works correctly
*/
export async function verifyCascadeDelete(prisma: PrismaClient, parentTableName: string, childTableName: string): Promise<boolean> {
// This is a basic check - in a real migration test, you would:
// 1. Create a parent record
// 2. Create related child records
// 3. Delete the parent
// 4. Verify children are deleted
return true
}
/**
* Get table schema information
* Useful for verifying schema migration
*/
export async function getTableSchema(prisma: PrismaClient, tableName: string) {
try {
const result = await prisma.$queryRawUnsafe<Array<{
cid: number
name: string
type: string
notnull: number
dflt_value: string | null
pk: number
}>>(
`PRAGMA table_info(${tableName})`
)
return result
} catch (error) {
return null
}
}
/**
* Check if column exists in table
* Verifies column migration success
*/
export async function verifyColumnExists(prisma: PrismaClient, tableName: string, columnName: string): Promise<boolean> {
const schema = await getTableSchema(prisma, tableName)
if (!schema) return false
return schema.some(col => col.name === columnName)
}
/**
* Get database size in bytes
* Useful for performance monitoring
*/
export async function getDatabaseSize(prisma: PrismaClient): Promise<number> {
try {
const result = await prisma.$queryRawUnsafe<Array<{ size: number }>>(
`SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size()`
)
return result[0]?.size || 0
} catch (error) {
return 0
}
}

View File

@@ -0,0 +1,568 @@
import { test, expect } from '@playwright/test'
/**
* Tests complets pour les Settings UX (Story 11-2)
*
* Ce fichier teste toutes les fonctionnalités implémentées:
* - General Settings: Notifications (Email), Privacy (Analytics)
* - Profile Settings: Language, Font Size, Show Recent Notes
* - Appearance Settings: Theme Persistence (Light/Dark/Auto)
* - SettingsSearch: Functional search with filtering
*
* Prérequis:
* - Être connecté avec un compte utilisateur
* - Avoir accès aux pages de settings
* - Base de données avec les nouveaux champs (emailNotifications, anonymousAnalytics)
*/
test.describe('Settings UX - Story 11-2', () => {
// Variables pour stocker les credentials
let email: string
let password: string
test.beforeAll(async ({ browser }) => {
// Les credentials seront fournis par l'utilisateur
console.log('Credentials nécessaires pour exécuter les tests')
console.log('Veuillez fournir email et password')
})
test.beforeEach(async ({ page }) => {
// Se connecter avant chaque test
await page.goto('http://localhost:3000/login')
await page.fill('input[name="email"]', email)
await page.fill('input[name="password"]', password)
await page.click('button[type="submit"]')
// Attendre la connexion et vérifier qu'on est sur la page d'accueil
await page.waitForURL('**/main**')
await expect(page).toHaveURL(/\/main/)
})
/**
* Tests pour General Settings
*/
test.describe('General Settings', () => {
test('devrait afficher la page General Settings', async ({ page }) => {
await page.goto('http://localhost:3000/settings/general')
// Vérifier le titre de la page
const title = await page.textContent('h1')
expect(title).toContain('General')
// Vérifier que les sections sont présentes
await expect(page.locator('#language')).toBeVisible()
await expect(page.locator('#notifications')).toBeVisible()
await expect(page.locator('#privacy')).toBeVisible()
})
test('devrait avoir le toggle Email Notifications', async ({ page }) => {
await page.goto('http://localhost:3000/settings/general')
// Scroll vers la section notifications
await page.locator('#notifications').scrollIntoViewIfNeeded()
// Vérifier que le toggle est présent
const emailToggle = page.getByRole('switch', { name: /email notifications/i })
await expect(emailToggle).toBeVisible()
})
test('devrait pouvoir activer/désactiver Email Notifications', async ({ page }) => {
await page.goto('http://localhost:3000/settings/general')
// Scroll vers la section notifications
await page.locator('#notifications').scrollIntoViewIfNeeded()
// Récupérer l'état initial du toggle
const emailToggle = page.getByRole('switch', { name: /email notifications/i })
const initialState = await emailToggle.getAttribute('aria-checked')
const initialEnabled = initialState === 'true'
// Cliquer sur le toggle
await emailToggle.click()
// Attendre un peu pour l'opération asynchrone
await page.waitForTimeout(500)
// Vérifier que l'état a changé
const newState = await emailToggle.getAttribute('aria-checked')
const newEnabled = newState === 'true'
expect(newEnabled).toBe(!initialEnabled)
// Vérifier qu'un toast de succès apparaît
await expect(page.getByText(/success/i)).toBeVisible({ timeout: 3000 })
})
test('devrait avoir le toggle Anonymous Analytics', async ({ page }) => {
await page.goto('http://localhost:3000/settings/general')
// Scroll vers la section privacy
await page.locator('#privacy').scrollIntoViewIfNeeded()
// Vérifier que le toggle est présent
const analyticsToggle = page.getByRole('switch', { name: /anonymous analytics/i })
await expect(analyticsToggle).toBeVisible()
})
test('devrait pouvoir activer/désactiver Anonymous Analytics', async ({ page }) => {
await page.goto('http://localhost:3000/settings/general')
// Scroll vers la section privacy
await page.locator('#privacy').scrollIntoViewIfNeeded()
// Récupérer l'état initial du toggle
const analyticsToggle = page.getByRole('switch', { name: /anonymous analytics/i })
const initialState = await analyticsToggle.getAttribute('aria-checked')
const initialEnabled = initialState === 'true'
// Cliquer sur le toggle
await analyticsToggle.click()
// Attendre un peu pour l'opération asynchrone
await page.waitForTimeout(500)
// Vérifier que l'état a changé
const newState = await analyticsToggle.getAttribute('aria-checked')
const newEnabled = newState === 'true'
expect(newEnabled).toBe(!initialEnabled)
// Vérifier qu'un toast de succès apparaît
await expect(page.getByText(/success/i)).toBeVisible({ timeout: 3000 })
})
test('devrait avoir le composant SettingsSearch', async ({ page }) => {
await page.goto('http://localhost:3000/settings/general')
// Vérifier que la barre de recherche est présente
const searchInput = page.getByPlaceholder(/search/i)
await expect(searchInput).toBeVisible()
// Vérifier l'icône de recherche
await expect(page.locator('svg').filter({ hasText: '' }).first()).toBeVisible()
})
test('devrait filtrer les sections avec SettingsSearch', async ({ page }) => {
await page.goto('http://localhost:3000/settings/general')
// Cliquer sur la barre de recherche
const searchInput = page.getByPlaceholder(/search/i)
await searchInput.click()
// Taper "notification"
await searchInput.fill('notification')
// Vérifier que la section notifications est visible
await expect(page.locator('#notifications')).toBeVisible()
// Vérifier que les autres sections ne sont plus visibles (ou sont filtrées)
// Note: Cela dépend de l'implémentation exacte du filtrage
})
test('devrait effacer la recherche avec le bouton X', async ({ page }) => {
await page.goto('http://localhost:3000/settings/general')
const searchInput = page.getByPlaceholder(/search/i)
await searchInput.fill('test')
// Vérifier que le bouton X apparaît
const clearButton = page.getByRole('button', { name: /clear search/i })
await expect(clearButton).toBeVisible()
// Cliquer sur le bouton X
await clearButton.click()
// Vérifier que la recherche est vide
await expect(searchInput).toHaveValue('')
})
test('devrait effacer la recherche avec la touche Escape', async ({ page }) => {
await page.goto('http://localhost:3000/settings/general')
const searchInput = page.getByPlaceholder(/search/i)
await searchInput.fill('test')
// Appuyer sur Escape
await page.keyboard.press('Escape')
// Vérifier que la recherche est vide
await expect(searchInput).toHaveValue('')
})
})
/**
* Tests pour Profile Settings
*/
test.describe('Profile Settings', () => {
test('devrait afficher la page Profile Settings', async ({ page }) => {
await page.goto('http://localhost:3000/settings/profile')
// Vérifier le titre de la page
const title = await page.textContent('h1')
expect(title).toContain('Profile')
// Vérifier les sections
await expect(page.getByText(/display name/i)).toBeVisible()
await expect(page.getByText(/email/i)).toBeVisible()
await expect(page.getByText(/language preferences/i)).toBeVisible()
await expect(page.getByText(/display settings/i)).toBeVisible()
await expect(page.getByText(/change password/i)).toBeVisible()
})
test('devrait avoir le sélecteur de langue', async ({ page }) => {
await page.goto('http://localhost:3000/settings/profile')
// Scroll vers la section language
await page.getByText(/language preferences/i).scrollIntoViewIfNeeded()
// Vérifier que le sélecteur est présent
const languageSelect = page.locator('#language')
await expect(languageSelect).toBeVisible()
})
test('devrait pouvoir changer la langue', async ({ page }) => {
await page.goto('http://localhost:3000/settings/profile')
// Scroll vers la section language
await page.getByText(/language preferences/i).scrollIntoViewIfNeeded()
// Cliquer sur le sélecteur
const languageSelect = page.locator('#language')
await languageSelect.click()
// Sélectionner une langue (ex: français)
await page.getByRole('option', { name: /français/i }).click()
// Vérifier qu'un toast de succès apparaît
await expect(page.getByText(/success/i)).toBeVisible({ timeout: 3000 })
})
test('devrait avoir le sélecteur de taille de police', async ({ page }) => {
await page.goto('http://localhost:3000/settings/profile')
// Scroll vers la section display settings
await page.getByText(/display settings/i).scrollIntoViewIfNeeded()
// Vérifier que le sélecteur est présent
const fontSizeSelect = page.locator('#fontSize')
await expect(fontSizeSelect).toBeVisible()
})
test('devrait pouvoir changer la taille de police', async ({ page }) => {
await page.goto('http://localhost:3000/settings/profile')
// Scroll vers la section display settings
await page.getByText(/display settings/i).scrollIntoViewIfNeeded()
// Cliquer sur le sélecteur
const fontSizeSelect = page.locator('#fontSize')
await fontSizeSelect.click()
// Sélectionner une taille (ex: large)
await page.getByRole('option', { name: /large/i }).click()
// Vérifier qu'un toast de succès apparaît
await expect(page.getByText(/success/i)).toBeVisible({ timeout: 3000 })
// Vérifier que la taille de police a changé (vérifier la variable CSS)
const rootFontSize = await page.evaluate(() => {
return getComputedStyle(document.documentElement).getPropertyValue('--user-font-size')
})
expect(rootFontSize).toBeTruthy()
})
test('devrait avoir le toggle Show Recent Notes', async ({ page }) => {
await page.goto('http://localhost:3000/settings/profile')
// Scroll vers la section display settings
await page.getByText(/display settings/i).scrollIntoViewIfNeeded()
// Vérifier que le toggle est présent
const recentNotesToggle = page.getByRole('switch', { name: /show recent notes/i })
await expect(recentNotesToggle).toBeVisible()
})
test('devrait pouvoir activer/désactiver Show Recent Notes', async ({ page }) => {
await page.goto('http://localhost:3000/settings/profile')
// Scroll vers la section display settings
await page.getByText(/display settings/i).scrollIntoViewIfNeeded()
// Récupérer l'état initial du toggle
const recentNotesToggle = page.getByRole('switch', { name: /show recent notes/i })
const initialState = await recentNotesToggle.getAttribute('aria-checked')
const initialEnabled = initialState === 'true'
// Cliquer sur le toggle
await recentNotesToggle.click()
// Attendre un peu pour l'opération asynchrone
await page.waitForTimeout(500)
// Vérifier que l'état a changé
const newState = await recentNotesToggle.getAttribute('aria-checked')
const newEnabled = newState === 'true'
expect(newEnabled).toBe(!initialEnabled)
// Vérifier qu'un toast de succès apparaît
await expect(page.getByText(/success/i)).toBeVisible({ timeout: 3000 })
})
test('devrait pouvoir changer le nom d\'affichage', async ({ page }) => {
await page.goto('http://localhost:3000/settings/profile')
// Remplir le champ nom
const nameInput = page.getByLabel(/display name/i)
const newName = 'Test User ' + Date.now()
await nameInput.fill(newName)
// Soumettre le formulaire
await page.getByRole('button', { name: /save/i }).click()
// Vérifier qu'un toast de succès apparaît
await expect(page.getByText(/success/i)).toBeVisible({ timeout: 3000 })
// Recharger la page et vérifier que le nom a été sauvegardé
await page.reload()
await expect(nameInput).toHaveValue(newName)
})
})
/**
* Tests pour Appearance Settings
*/
test.describe('Appearance Settings', () => {
test('devrait afficher la page Appearance Settings', async ({ page }) => {
await page.goto('http://localhost:3000/settings/appearance')
// Vérifier le titre de la page
const title = await page.textContent('h1')
expect(title).toContain('Appearance')
// Vérifier les sections
await expect(page.getByText(/theme/i)).toBeVisible()
await expect(page.getByText(/typography/i)).toBeVisible()
})
test('devrait avoir le sélecteur de thème', async ({ page }) => {
await page.goto('http://localhost:3000/settings/appearance')
// Vérifier que le sélecteur est présent
const themeSelect = page.locator('#theme')
await expect(themeSelect).toBeVisible()
})
test('devrait pouvoir changer le thème pour Light', async ({ page }) => {
await page.goto('http://localhost:3000/settings/appearance')
// Cliquer sur le sélecteur de thème
const themeSelect = page.locator('#theme')
await themeSelect.click()
// Sélectionner "Light"
await page.getByRole('option', { name: /light/i }).click()
// Vérifier que le thème est appliqué immédiatement
await expect(page.locator('html')).toHaveClass(/light/)
await expect(page.locator('html')).not.toHaveClass(/dark/)
// Vérifier qu'un toast de succès apparaît
await expect(page.getByText(/success/i)).toBeVisible({ timeout: 3000 })
// Vérifier que le thème est sauvegardé dans localStorage
const localStorageTheme = await page.evaluate(() => {
return localStorage.getItem('theme')
})
expect(localStorageTheme).toBe('light')
})
test('devrait pouvoir changer le thème pour Dark', async ({ page }) => {
await page.goto('http://localhost:3000/settings/appearance')
// Cliquer sur le sélecteur de thème
const themeSelect = page.locator('#theme')
await themeSelect.click()
// Sélectionner "Dark"
await page.getByRole('option', { name: /dark/i }).click()
// Vérifier que le thème est appliqué immédiatement
await expect(page.locator('html')).toHaveClass(/dark/)
await expect(page.locator('html')).not.toHaveClass(/light/)
// Vérifier qu'un toast de succès apparaît
await expect(page.getByText(/success/i)).toBeVisible({ timeout: 3000 })
// Vérifier que le thème est sauvegardé dans localStorage
const localStorageTheme = await page.evaluate(() => {
return localStorage.getItem('theme')
})
expect(localStorageTheme).toBe('dark')
})
test('devrait pouvoir changer le thème pour Auto', async ({ page }) => {
await page.goto('http://localhost:3000/settings/appearance')
// Cliquer sur le sélecteur de thème
const themeSelect = page.locator('#theme')
await themeSelect.click()
// Sélectionner "Auto"
await page.getByRole('option', { name: /auto/i }).click()
// Vérifier qu'un toast de succès apparaît
await expect(page.getByText(/success/i)).toBeVisible({ timeout: 3000 })
// Vérifier que le thème est sauvegardé dans localStorage
const localStorageTheme = await page.evaluate(() => {
return localStorage.getItem('theme')
})
expect(localStorageTheme).toBe('auto')
})
test('devrait charger le thème depuis localStorage', async ({ page }) => {
// Définir le thème dans localStorage avant de charger la page
await page.goto('about:blank')
await page.evaluate(() => {
localStorage.setItem('theme', 'dark')
})
// Aller sur la page Appearance Settings
await page.goto('http://localhost:3000/settings/appearance')
// Attendre que la page charge
await page.waitForLoadState('networkidle')
// Vérifier que le thème est chargé et appliqué
const themeSelect = page.locator('#theme')
await expect(themeSelect).toHaveValue('dark')
// Vérifier que le thème est appliqué au DOM
await expect(page.locator('html')).toHaveClass(/dark/)
})
test('devrait persister le thème après rechargement de page', async ({ page }) => {
await page.goto('http://localhost:3000/settings/appearance')
// Changer le thème pour dark
const themeSelect = page.locator('#theme')
await themeSelect.click()
await page.getByRole('option', { name: /dark/i }).click()
// Attendre que le thème soit appliqué
await expect(page.locator('html')).toHaveClass(/dark/)
// Recharger la page
await page.reload()
await page.waitForLoadState('networkidle')
// Vérifier que le thème est toujours appliqué
await expect(page.locator('html')).toHaveClass(/dark/)
await expect(themeSelect).toHaveValue('dark')
})
})
/**
* Tests d'intégration cross-pages
*/
test.describe('Integration Tests', () => {
test('devrait naviguer entre les pages de settings', async ({ page }) => {
// Commencer sur General Settings
await page.goto('http://localhost:3000/settings/general')
// Cliquer sur Appearance dans la navigation
await page.getByRole('link', { name: /appearance/i }).click()
await expect(page).toHaveURL(/\/settings\/appearance/)
// Cliquer sur Profile dans la navigation
await page.getByRole('link', { name: /profile/i }).click()
await expect(page).toHaveURL(/\/settings\/profile/)
// Cliquer sur General dans la navigation
await page.getByRole('link', { name: /general/i }).click()
await expect(page).toHaveURL(/\/settings\/general/)
})
test('devrait persister les settings entre les pages', async ({ page }) => {
// Changer le thème sur Appearance Settings
await page.goto('http://localhost:3000/settings/appearance')
const themeSelect = page.locator('#theme')
await themeSelect.click()
await page.getByRole('option', { name: /dark/i }).click()
await expect(page.locator('html')).toHaveClass(/dark/)
// Aller sur General Settings et vérifier que le thème est toujours appliqué
await page.goto('http://localhost:3000/settings/general')
await expect(page.locator('html')).toHaveClass(/dark/)
// Aller sur Profile Settings et vérifier que le thème est toujours appliqué
await page.goto('http://localhost:3000/settings/profile')
await expect(page.locator('html')).toHaveClass(/dark/)
})
})
/**
* Tests de responsive design
*/
test.describe('Responsive Design', () => {
test('devrait fonctionner sur mobile', async ({ page }) => {
// Simuler un viewport mobile
await page.setViewportSize({ width: 375, height: 667 })
await page.goto('http://localhost:3000/settings/general')
// Vérifier que la page est utilisable
await expect(page.locator('h1')).toBeVisible()
await expect(page.getByPlaceholder(/search/i)).toBeVisible()
})
test('devrait fonctionner sur tablet', async ({ page }) => {
// Simuler un viewport tablet
await page.setViewportSize({ width: 768, height: 1024 })
await page.goto('http://localhost:3000/settings/general')
// Vérifier que la page est utilisable
await expect(page.locator('h1')).toBeVisible()
await expect(page.getByPlaceholder(/search/i)).toBeVisible()
})
test('devrait fonctionner sur desktop', async ({ page }) => {
// Simuler un viewport desktop
await page.setViewportSize({ width: 1280, height: 800 })
await page.goto('http://localhost:3000/settings/general')
// Vérifier que la page est utilisable
await expect(page.locator('h1')).toBeVisible()
await expect(page.getByPlaceholder(/search/i)).toBeVisible()
})
})
/**
* Tests de performance
*/
test.describe('Performance Tests', () => {
test('devrait charger rapidement General Settings', async ({ page }) => {
const startTime = Date.now()
await page.goto('http://localhost:3000/settings/general')
await page.waitForLoadState('networkidle')
const loadTime = Date.now() - startTime
// La page devrait charger en moins de 2 secondes
expect(loadTime).toBeLessThan(2000)
})
test('devrait appliquer le thème rapidement', async ({ page }) => {
await page.goto('http://localhost:3000/settings/appearance')
const themeSelect = page.locator('#theme')
const startTime = Date.now()
await themeSelect.click()
await page.getByRole('option', { name: /dark/i }).click()
await page.waitForSelector('html.dark', { timeout: 1000 })
const applyTime = Date.now() - startTime
// Le thème devrait être appliqué en moins de 500ms
expect(applyTime).toBeLessThan(500)
})
})
})

View File

@@ -0,0 +1,9 @@
/**
* Vitest setup file
* This file is loaded before all tests
*/
import { beforeAll, afterAll } from 'vitest'
// Global setup can be added here if needed
// For now, we keep it minimal as each test suite has its own setup