fix(provider): add backfill and Gemini security flags to switch function

The switch function was missing two important features after the SQLite
migration:

1. Backfill mechanism: Before switching providers, read the current live
   config and save it back to the current provider. This preserves any
   manual edits users made to the live config file.

2. Gemini security flags: When switching to a Gemini provider, set the
   appropriate security.auth.selectedType:
   - PackyCode providers: "gemini-api-key"
   - Google OAuth providers: "oauth-personal"

Also update tests to:
- Use the new unified MCP structure (mcp.servers) instead of the legacy
  per-app structure (mcp.codex.servers)
- Expect backfill behavior (was incorrectly marked as "no backfill")
- Remove assertions for provider-specific file deletion (v3.7.0+ uses
  SSOT, no longer creates per-provider config files)
This commit is contained in:
Jason
2025-11-25 11:16:50 +08:00
parent 1c87c8d253
commit 2a842e3b33
3 changed files with 70 additions and 28 deletions
+8 -13
View File
@@ -139,11 +139,11 @@ command = "say"
.and_then(|v| v.get("OPENAI_API_KEY"))
.and_then(|v| v.as_str())
.unwrap_or("");
// 注意:v3.7.0+ 的 switch 实现不再 backfill 旧供应商
// 旧供应商保持其原始配置不变
// 回填机制:切换前会将 live 配置回填到当前供应商
// 这保护了用户在 live 文件中的手动修改
assert_eq!(
legacy_auth_value, "stale",
"previous provider should retain its original auth (no backfill in v3.7.0+)"
legacy_auth_value, "legacy-key",
"previous provider should be backfilled with live auth"
);
}
@@ -251,16 +251,11 @@ fn switch_provider_updates_claude_live_and_state() {
let legacy_provider = providers
.get("old-provider")
.expect("legacy provider still exists");
// 注意:v3.7.0+ 的 switch 实现不再 backfill 旧供应商
// 旧供应商保持其原始配置不变
// 回填机制:切换前会将 live 配置回填到当前供应商
// 这保护了用户在 live 文件中的手动修改
assert_eq!(
legacy_provider
.settings_config
.get("env")
.and_then(|env| env.get("ANTHROPIC_API_KEY"))
.and_then(|key| key.as_str()),
Some("stale-key"),
"previous provider should retain its original config (no backfill in v3.7.0+)"
legacy_provider.settings_config, legacy_live,
"previous provider should be backfilled with live config"
);
let new_provider = providers
+30 -15
View File
@@ -2,7 +2,7 @@ use serde_json::json;
use cc_switch_lib::{
get_claude_settings_path, read_json_file, write_codex_live_atomic, AppError, AppType,
MultiAppConfig, Provider, ProviderMeta, ProviderService,
McpApps, McpServer, MultiAppConfig, Provider, ProviderMeta, ProviderService,
};
#[path = "support.rs"]
@@ -68,16 +68,27 @@ command = "say"
);
}
initial_config.mcp.codex.servers.insert(
// 使用新的统一 MCP 结构(v3.7.0+
let servers = initial_config.mcp.servers.get_or_insert_with(Default::default);
servers.insert(
"echo-server".into(),
json!({
"id": "echo-server",
"enabled": true,
"server": {
McpServer {
id: "echo-server".into(),
name: "Echo Server".into(),
server: json!({
"type": "stdio",
"command": "echo"
}
}),
}),
apps: McpApps {
claude: false,
codex: true,
gemini: false,
},
description: None,
homepage: None,
docs: None,
tags: Vec::new(),
},
);
let state = create_test_state_with_config(&initial_config).expect("create test state");
@@ -115,9 +126,15 @@ command = "say"
.get("config")
.and_then(|v| v.as_str())
.unwrap_or_default();
assert_eq!(
new_config_text, config_text,
"provider config snapshot should match live file"
// provider 存储的是原始配置,不包含 MCP 同步后的内容
assert!(
new_config_text.contains("mcp_servers.latest"),
"provider config should contain original MCP servers"
);
// live 文件额外包含同步的 MCP 服务器
assert!(
config_text.contains("mcp_servers.echo-server"),
"live config should include synced MCP servers"
);
let legacy = providers
@@ -570,10 +587,8 @@ fn provider_service_delete_claude_removes_provider_files() {
!providers.contains_key("delete"),
"claude provider should be removed"
);
assert!(
!by_name.exists() && !by_id.exists(),
"provider config files should be deleted"
);
// v3.7.0+ 不再使用供应商特定文件(如 settings-*.json
// 删除供应商只影响数据库记录,不清理这些旧格式文件
}
#[test]