Files
cc-switch/tests/hooks
Jason c8c4656e0e test: add MCP functionality tests achieving 100% hooks coverage
Milestone Achievement: 100% Hooks Coverage 🎉
- Hooks coverage: 87.5% (7/8) → 100% (8/8)
- Total tests: 81 → 105 (+29.6%)
- MCP functionality: 0 → 24 tests

useMcpActions Tests (8 tests):
- Test server reload with loading state management
  - Use deferred promise pattern to verify loading state transitions
  - Verify intermediate loading=true state during async operation
  - Verify error toast (duration: 6s) on reload failure
  - Ensure loading returns to false after error
- Test optimistic toggle with rollback on failure
  - Immediately update enabled flag before API confirmation
  - Verify success toast messages for enable/disable
  - Roll back state to original value on API failure
  - Show error toast (duration: 6s) when toggle fails
- Test server save with list refresh
  - Verify ID rewrite logic: saveServer(newId, {...input, id: oldId}) → {id: newId}
  - Verify syncOtherSide option correctly propagated to API
  - Refresh server list after successful save
  - Propagate errors to caller while showing error toast
  - Do not refresh list when save fails
- Test server delete with state management
  - Verify deleteServerInConfig called with correct parameters
  - Verify list refresh removes deleted server
  - Show success toast (duration: 1.5s) on delete
  - Keep state unchanged on delete failure
  - Propagate error to caller with error toast

useMcpValidation Tests (16 tests):
- Test JSON validation (4 tests)
  - Return empty string for blank/whitespace text
  - Return "mcp.error.jsonInvalid" for parsing failures
  - Reject non-object types (string, array)
  - Accept valid object payloads
- Test TOML error formatting (2 tests)
  - Map mustBeObject/parseError to "mcp.error.tomlInvalid"
  - Append error details for unknown errors
- Test TOML config validation (5 tests)
  - Propagate errors from validateToml utility
  - Return "mcp.error.commandRequired" for stdio without command
  - Return "mcp.wizard.urlRequired" for http without url
  - Catch and format tomlToMcpServer exceptions
  - Return empty string when validation passes
- Test JSON config validation (5 tests)
  - Reject invalid JSON syntax
  - Reject mcpServers array format (single object required)
  - Require command field for stdio type servers
  - Require url field for http type servers
  - Accept valid server configurations

Technical Highlights:
- Deferred Promise Pattern: Precise async timing control
- Optimistic Updates: Test immediate feedback + rollback
- Error Propagation: Distinguish caller errors vs toast notifications
- i18n Validation: All validators return translation keys
- Factory Functions: Reusable test data builders

All tests passing: 105/105
2025-10-26 11:56:24 +08:00
..