mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-02-15 23:33:15 +08:00
This commit introduces a new IPC channel and handler for fetching all user preferences at once, enhancing the efficiency of preference management. The PreferenceService is updated with a getAll method to retrieve all preferences from the memory cache, and the renderer-side PreferenceService is adjusted to support this new functionality. Additionally, type safety improvements are made across the preference handling code, ensuring better consistency and reducing potential errors. |
||
|---|---|---|
| .. | ||
| PreferenceService.simple.test.ts | ||
| PreferenceService.test.ts | ||
| README.md | ||
Main Process Preference System Tests
This directory contains unit tests for the main process PreferenceService.
Test Files
PreferenceService.simple.test.ts
Basic functionality tests for the main process PreferenceService:
- Singleton Pattern: Verifies proper singleton implementation
- Initialization: Tests database connection and cache loading
- Basic Operations: Tests get/set operations with defaults
- Bulk Operations: Tests getMultiple functionality
- Method Signatures: Verifies all required methods exist
- Type Safety: Ensures proper TypeScript integration
Architecture Coverage
✅ Main PreferenceService
- SQLite database integration with Drizzle ORM
- Memory caching with
PreferenceDefaultScopeType - Multi-window subscription management
- Batch operations and transactions
- IPC notification broadcasting
- Automatic cleanup and error handling
Test Statistics
- Total Test Files: 1
- Total Tests: 7
- Coverage Areas:
- Core functionality
- Database integration
- Type safety
- Method existence
- Error resilience
Testing Challenges & Solutions
Database Mocking
The main process PreferenceService heavily integrates with:
- Drizzle ORM
- SQLite database
- Electron BrowserWindow API
For comprehensive testing, these dependencies are mocked to focus on service logic rather than integration testing.
Mock Strategy
- Electron APIs: Mocked BrowserWindow for window management
- Database Layer: Mocked DbService and database queries
- Logger: Mocked logging to avoid setup complexity
Running Tests
# Run main process tests
yarn vitest run src/main/data/__tests__/
# Run specific test
yarn vitest run src/main/data/__tests__/PreferenceService.simple.test.ts
# Watch mode
yarn vitest watch src/main/data/__tests__/
Future Test Enhancements
For more comprehensive testing, consider:
- Integration Tests: Test actual database operations
- IPC Tests: Test cross-process communication
- Performance Tests: Measure cache performance vs database queries
- Subscription Tests: Test multi-window notification broadcasting
- Migration Tests: Test data migration scenarios
Key Testing Patterns
Service Lifecycle
- Singleton instance management
- Initialization state handling
- Default value provision
Error Resilience
- Database failure scenarios
- Invalid data handling
- Graceful degradation