cherry-studio/src/main/data/__tests__
fullex 72f32e4b8f feat(preferences): add getAll method and IPC handler for retrieving all preferences
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.
2025-08-12 01:01:22 +08:00
..
PreferenceService.simple.test.ts feat(preferences): add getAll method and IPC handler for retrieving all preferences 2025-08-12 01:01:22 +08:00
PreferenceService.test.ts feat(preferences): add getAll method and IPC handler for retrieving all preferences 2025-08-12 01:01:22 +08:00
README.md feat(preferences): add getAll method and IPC handler for retrieving all preferences 2025-08-12 01:01:22 +08:00

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:

  1. Integration Tests: Test actual database operations
  2. IPC Tests: Test cross-process communication
  3. Performance Tests: Measure cache performance vs database queries
  4. Subscription Tests: Test multi-window notification broadcasting
  5. 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