Files
cc-switch/src-tauri/src/commands/mod.rs

18 lines
257 B
Rust
Raw Normal View History

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>
2025-10-27 22:18:05 +08:00
#![allow(non_snake_case)]
mod config;
refactor(backend): extract config and speedtest services (phase 4) This commit continues the backend refactoring initiative by extracting configuration management and API speedtest logic into dedicated service layers, completing phase 4 of the architectural improvement plan. ## Changes ### New Service Layers - **ConfigService** (`services/config.rs`): Consolidates all config import/export, backup management, and live sync operations - `create_backup()`: Creates timestamped backups with auto-cleanup - `export_config_to_path()`: Exports config to specified path - `load_config_for_import()`: Loads and validates imported config - `import_config_from_path()`: Full import with state update - `sync_current_providers_to_live()`: Syncs current providers to live files - Private helpers for Claude/Codex-specific sync logic - **SpeedtestService** (`services/speedtest.rs`): Encapsulates endpoint latency testing with proper validation and error handling - `test_endpoints()`: Tests multiple URLs concurrently - URL validation now unified in service layer - Includes 3 unit tests for edge cases (empty list, invalid URLs, timeout clamping) ### Command Layer Refactoring - Move all import/export commands to `commands/import_export.rs` - Commands become thin wrappers: parse params → call service → return JSON - Maintain `spawn_blocking` for I/O operations (phase 5 optimization) - Lock acquisition happens after I/O completes (minimize contention) ### File Organization - Delete: `import_export.rs`, `speedtest.rs` (root-level modules) - Create: `commands/import_export.rs`, `services/config.rs`, `services/speedtest.rs` - Update: Module declarations in `lib.rs`, `commands/mod.rs`, `services/mod.rs` ### Test Updates - Update 20 integration tests in `import_export_sync.rs` to use `ConfigService` APIs - All existing test cases pass without modification to test logic - Add 3 new unit tests for `SpeedtestService`: - `sanitize_timeout_clamps_values`: Boundary value testing - `test_endpoints_handles_empty_list`: Empty input handling - `test_endpoints_reports_invalid_url`: Invalid URL error reporting ## Benefits 1. **Improved Testability**: Service methods are `pub fn`, easily callable from tests without Tauri runtime 2. **Better Separation of Concerns**: Business logic isolated from command/transport layer 3. **Enhanced Maintainability**: Related operations grouped in cohesive service structs 4. **Consistent Error Handling**: Services return `Result<T, AppError>`, commands convert to `Result<T, String>` 5. **Performance**: I/O operations run in `spawn_blocking`, locks released before file operations ## Testing - ✅ All 43 tests passing (7 unit + 36 integration) - ✅ `cargo fmt --check` passes - ✅ `cargo clippy -- -D warnings` passes (zero warnings) ## Documentation Updated `BACKEND_REFACTOR_PLAN.md` to reflect completion of config and speedtest service extraction, marking phase 4 substantially complete. Co-authored-by: Claude Code <code@anthropic.com>
2025-10-28 15:58:04 +08:00
mod import_export;
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>
2025-10-27 22:18:05 +08:00
mod mcp;
mod misc;
mod plugin;
mod provider;
mod settings;
pub use config::*;
refactor(backend): extract config and speedtest services (phase 4) This commit continues the backend refactoring initiative by extracting configuration management and API speedtest logic into dedicated service layers, completing phase 4 of the architectural improvement plan. ## Changes ### New Service Layers - **ConfigService** (`services/config.rs`): Consolidates all config import/export, backup management, and live sync operations - `create_backup()`: Creates timestamped backups with auto-cleanup - `export_config_to_path()`: Exports config to specified path - `load_config_for_import()`: Loads and validates imported config - `import_config_from_path()`: Full import with state update - `sync_current_providers_to_live()`: Syncs current providers to live files - Private helpers for Claude/Codex-specific sync logic - **SpeedtestService** (`services/speedtest.rs`): Encapsulates endpoint latency testing with proper validation and error handling - `test_endpoints()`: Tests multiple URLs concurrently - URL validation now unified in service layer - Includes 3 unit tests for edge cases (empty list, invalid URLs, timeout clamping) ### Command Layer Refactoring - Move all import/export commands to `commands/import_export.rs` - Commands become thin wrappers: parse params → call service → return JSON - Maintain `spawn_blocking` for I/O operations (phase 5 optimization) - Lock acquisition happens after I/O completes (minimize contention) ### File Organization - Delete: `import_export.rs`, `speedtest.rs` (root-level modules) - Create: `commands/import_export.rs`, `services/config.rs`, `services/speedtest.rs` - Update: Module declarations in `lib.rs`, `commands/mod.rs`, `services/mod.rs` ### Test Updates - Update 20 integration tests in `import_export_sync.rs` to use `ConfigService` APIs - All existing test cases pass without modification to test logic - Add 3 new unit tests for `SpeedtestService`: - `sanitize_timeout_clamps_values`: Boundary value testing - `test_endpoints_handles_empty_list`: Empty input handling - `test_endpoints_reports_invalid_url`: Invalid URL error reporting ## Benefits 1. **Improved Testability**: Service methods are `pub fn`, easily callable from tests without Tauri runtime 2. **Better Separation of Concerns**: Business logic isolated from command/transport layer 3. **Enhanced Maintainability**: Related operations grouped in cohesive service structs 4. **Consistent Error Handling**: Services return `Result<T, AppError>`, commands convert to `Result<T, String>` 5. **Performance**: I/O operations run in `spawn_blocking`, locks released before file operations ## Testing - ✅ All 43 tests passing (7 unit + 36 integration) - ✅ `cargo fmt --check` passes - ✅ `cargo clippy -- -D warnings` passes (zero warnings) ## Documentation Updated `BACKEND_REFACTOR_PLAN.md` to reflect completion of config and speedtest service extraction, marking phase 4 substantially complete. Co-authored-by: Claude Code <code@anthropic.com>
2025-10-28 15:58:04 +08:00
pub use import_export::*;
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>
2025-10-27 22:18:05 +08:00
pub use mcp::*;
pub use misc::*;
pub use plugin::*;
pub use provider::*;
pub use settings::*;