|
| 1 | +use cc_switch_lib::AppType; |
| 2 | + |
| 3 | +#[path = "support.rs"] |
| 4 | +mod support; |
| 5 | +use support::test_mutex; |
| 6 | + |
| 7 | +// 注意:remote 模块目前是私有的,无法在单元测试中直接访问 |
| 8 | +// 以下测试验证了相关的类型和配置 |
| 9 | + |
| 10 | +// 测试 AppType 可以正确序列化(远程服务器需要使用 AppType) |
| 11 | +#[test] |
| 12 | +fn test_app_type_for_remote() { |
| 13 | + let _guard = test_mutex().lock().expect("acquire test mutex"); |
| 14 | + |
| 15 | + // 验证 AppType 可以序列化(用于远程 API) |
| 16 | + let app_type = AppType::Claude; |
| 17 | + let serialized = serde_json::to_string(&app_type).expect("serialize AppType"); |
| 18 | + assert!(serialized.contains("Claude") || serialized.contains("claude")); |
| 19 | + |
| 20 | + // 验证反序列化 |
| 21 | + let deserialized: AppType = serde_json::from_str(&serialized).expect("deserialize AppType"); |
| 22 | + assert_eq!(deserialized, AppType::Claude); |
| 23 | +} |
| 24 | + |
| 25 | +// 测试远程配置的 JSON 结构 |
| 26 | +#[test] |
| 27 | +fn test_remote_config_json_structure() { |
| 28 | + let _guard = test_mutex().lock().expect("acquire test mutex"); |
| 29 | + |
| 30 | + // 模拟 RemoteConfig 的 JSON 结构 |
| 31 | + let config_json = r#"{ |
| 32 | + "enabled": true, |
| 33 | + "port": 4000, |
| 34 | + "tailscale_enabled": false |
| 35 | + }"#; |
| 36 | + |
| 37 | + // 验证可以解析为通用值 |
| 38 | + let config: serde_json::Value = serde_json::from_str(config_json).expect("parse config"); |
| 39 | + assert_eq!(config["enabled"], true); |
| 40 | + assert_eq!(config["port"], 4000); |
| 41 | + assert_eq!(config["tailscale_enabled"], false); |
| 42 | +} |
| 43 | + |
| 44 | +// 测试端口号验证 |
| 45 | +#[test] |
| 46 | +fn test_port_validation() { |
| 47 | + let _guard = test_mutex().lock().expect("acquire test mutex"); |
| 48 | + |
| 49 | + // 有效端口 |
| 50 | + let valid_ports = [1024, 4000, 8080, 65535]; |
| 51 | + for port in valid_ports { |
| 52 | + assert!(port >= 1024 && port <= 65535); |
| 53 | + } |
| 54 | + |
| 55 | + // 无效端口(这些应该在 UI 层验证) |
| 56 | + let invalid_ports = [0, 100, 1023, 65536, 70000]; |
| 57 | + for port in invalid_ports { |
| 58 | + assert!(port < 1024 || port > 65535); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// 注意:需要完整 Tauri 环境的测试应该在集成测试中运行: |
| 63 | +// - start_remote / stop_remote 完整流程 |
| 64 | +// - broadcast_provider_switch 在异步上下文中不阻塞 |
| 65 | +// - 实际的 HTTP 端点测试(health、providers、switch、SSE events) |
| 66 | +// - Tailscale toggle 时的服务器重启 |
| 67 | +// - 端口变更时的服务器重启 |
| 68 | +// |
| 69 | +// 手动测试步骤: |
| 70 | +// 1. 启动 CC Switch |
| 71 | +// 2. 打开设置页面,启用远程管理 |
| 72 | +// 3. 访问 http://localhost:4000 验证 Web UI 可用 |
| 73 | +// 4. 切换 provider,验证所有连接的浏览器收到 SSE 事件 |
| 74 | +// 5. 在服务器运行时切换 Tailscale 开关,验证服务器重启(黄色指示器) |
| 75 | +// 6. 在服务器运行时修改端口,验证服务器重启且新端口生效 |
| 76 | +// 7. 观察日志确认没有 "blocking_read()" 相关的 panic |
0 commit comments