Commit Graph

3 Commits

  • refactor(backend): phase 3 - add integration tests for config sync (partial)
    Add integration test suite with isolated test environment:
    - New test file: tests/import_export_sync.rs (149 lines, 3 test cases)
      * sync_claude_provider_writes_live_settings: validates SSOT sync to live settings
      * create_backup_skips_missing_file: edge case handling for missing config
      * create_backup_generates_snapshot_file: verifies backup snapshot creation
    - Test infrastructure:
      * OnceLock-based isolated HOME directory in temp folder
      * Mutex guard to ensure sequential test execution (avoid file system race)
      * Automatic cleanup between test runs
    
    Export core APIs for testing (lib.rs):
    - AppType, MultiAppConfig, Provider (data structures)
    - get_claude_settings_path, read_json_file (config utilities)
    - create_backup, sync_current_providers_to_live (sync operations)
    - update_settings, AppSettings (settings management)
    
    Adjust visibility:
    - import_export::sync_current_providers_to_live: fn -> pub fn
    
    Update documentation:
    - Mark Phase 3 as in-progress (๐Ÿšง) in BACKEND_REFACTOR_PLAN.md
    - Document current test coverage scope and pending scenarios
    
    Test results: 7/7 passed (4 unit + 3 integration)
    Build time: 0.16s
    
    Next steps:
    - Add Codex sync tests (auth.json + config.toml atomic writes)
    - Add MCP sync integration tests
    - Add end-to-end provider switching tests
  • refactor(backend): phase 2 - split commands.rs by domain (100%)
    Split monolithic commands.rs (1525 lines) into 7 domain-focused modules
    to improve maintainability and readability while preserving the external API.
    
    ## Changes
    
    ### Module Structure
    
    Created `commands/` directory with domain-based organization:
    
    - **provider.rs** (946 lines, 15 commands)
      - Provider CRUD operations (get, add, update, delete, switch)
      - Usage query integration
      - Endpoint speed testing and custom endpoint management
      - Sort order management
      - Largest file but highly cohesive (all provider-related)
    
    - **mcp.rs** (235 lines, 13 commands)
      - Claude MCP management (~/.claude.json)
      - SSOT MCP config management (config.json)
      - Sync operations (Claude โ†” Codex)
      - Import/export functionality
    
    - **config.rs** (153 lines, 8 commands)
      - Config path queries (Claude/Codex)
      - Directory operations (open, pick)
      - Config status checks
      - Parameter compatibility layer (app_type/app/appType)
    
    - **settings.rs** (40 lines, 5 commands)
      - App settings management
      - App restart functionality
      - app_config_dir override (Store integration)
    
    - **plugin.rs** (36 lines, 4 commands)
      - Claude plugin management (~/.claude/config.json)
      - Plugin status and config operations
    
    - **misc.rs** (45 lines, 3 commands)
      - External link handling
      - Update checks
      - Portable mode detection
    
    - **mod.rs** (15 lines)
      - Module exports via `pub use`
      - Preserves flat API structure
    
    ### API Preservation
    
    - Used `pub use` pattern to maintain external API
    - All commands still accessible as `commands::function_name`
    - Zero breaking changes for frontend code
    - lib.rs invoke_handler unchanged (48 commands registered)
    
    ## Statistics
    
    - Files: 1 โ†’ 7 (modular organization)
    - Lines: 1525 โ†’ 1470 (net -55 lines, -3.6%)
    - Commands: 48 โ†’ 48 (all preserved)
    - Average file size: 210 lines (excluding provider.rs)
    - Compilation: โœ… Success (6.92s, 0 warnings)
    - Tests: โœ… 4/4 passed
    
    ## Benefits
    
    - **Maintainability**: Easier to locate and modify domain-specific code
    - **Readability**: Smaller files (~200 lines) vs monolithic 1500+ lines
    - **Testability**: Can unit test individual modules in isolation
    - **Scalability**: Clear pattern for adding new command groups
    - **Zero Risk**: No API changes, all tests passing
    
    ## Design Decisions
    
    1. **Domain-based split**: Organized by business domain (provider, mcp, config)
       rather than technical layers (crud, query, sync)
    
    2. **Preserved provider.rs size**: Kept at 946 lines to maintain high cohesion
       (all provider-related operations together). Can be further split in Phase 2.1
       if needed.
    
    3. **Parameter compatibility**: Retained multiple parameter names (app_type, app,
       appType) for backward compatibility with different frontend call styles
    
    ## Phase 2 Status: โœ… 100% Complete
    
    Ready for Phase 3: Adding integration tests.
    
    Co-authored-by: Claude <noreply@anthropic.com>
  • refactor(backend): phase 1 - unified error handling with thiserror
    Introduce AppError enum to replace Result<T, String> pattern across
    the codebase, improving error context preservation and type safety.
    
    ## Changes
    
    ### Core Infrastructure
    - Add src/error.rs with AppError enum using thiserror
    - Add thiserror dependency to Cargo.toml
    - Implement helper functions: io(), json(), toml() for ergonomic error creation
    - Implement From<PoisonError> for automatic lock error conversion
    - Implement From<AppError> for String to maintain Tauri command compatibility
    
    ### Module Migrations (60% complete)
    - config.rs: Full migration to AppError
      - read_json_file, write_json_file, atomic_write
      - archive_file, copy_file, delete_file
    - claude_mcp.rs: Full migration to AppError
      - get_mcp_status, read_mcp_json, upsert_mcp_server
      - delete_mcp_server, validate_command_in_path
      - set_mcp_servers_map
    - codex_config.rs: Full migration to AppError
      - write_codex_live_atomic with rollback support
      - read_and_validate_codex_config_text
      - validate_config_toml
    - app_config.rs: Partial migration
      - MultiAppConfig::load, MultiAppConfig::save
    - store.rs: Partial migration
      - AppState::save now returns Result<(), AppError>
    - commands.rs: Minimal changes
      - Use .map_err(Into::into) for compatibility
    - mcp.rs: Minimal changes
      - sync_enabled_to_claude uses Into::into conversion
    
    ### Documentation
    - Add docs/BACKEND_REFACTOR_PLAN.md with detailed refactoring roadmap
    
    ## Benefits
    - Type-safe error handling with preserved error chains
    - Better error messages with file paths and context
    - Reduced boilerplate code (118 Result<T, String> instances to migrate)
    - Automatic error conversion for seamless integration
    
    ## Testing
    - All existing tests pass (4/4)
    - Compilation successful with no warnings
    - Build time: 0.61s (no performance regression)
    
    ## Remaining Work
    - claude_plugin.rs (7 functions)
    - migration.rs, import_export.rs
    - Add unit tests for error.rs
    - Complete commands.rs migration after dependent modules
    
    Co-authored-by: Claude <claude@anthropic.com>