mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-11-20 07:52:52 +08:00
test(parity): check six more frontend mirrors against the backend fixture (#9580)
* test(parity): check six more frontend mirrors against the backend fixture These frontend copies were kept in step with Rust only by comments: providerNeedsRouting (tray::provider_needs_routing), WARN_BELOW_PERCENT, DISPLAY_CURRENCIES (TARGET_CURRENCIES), the custom icon prefix, length and size limits, APP_PROFILE_SCOPE (ProfileScope::for_app) and isValidUserAgentHeader (parse_custom_user_agent). The backend now writes their values into tests/config/backendConstants.json. For the two rules it writes the answers on sample inputs: 9 cards in each of the 5 tray apps and 12 User-Agent strings. The frontend test runs its own copy on the same inputs. tray's needs_routing_mirrors_the_frontend_rule restated the routing answers by hand; its cases are now fixture inputs, so it is deleted. icon.rs cited a frontend MAX_CUSTOM_ICON_DIMENSION that never existed; the frontend's OUTPUT_SIZE is exported and checked against the limit. * test(parity): compare display currencies with the backend as a set --------- Co-authored-by: Jason <farion1231@gmail.com>
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
//! intended change, regenerate with
|
||||
//! `CC_SWITCH_UPDATE_GOLDEN=1 cargo test backend_constants_match_shared_fixture`
|
||||
//! and update the frontend copy.
|
||||
use crate::app_config::AppType;
|
||||
use serde_json::{json, Map, Value};
|
||||
|
||||
/// Base URLs whose coding-plan detection both sides must agree on.
|
||||
@@ -38,6 +39,65 @@ const CODING_PLAN_URLS: &[&str] = &[
|
||||
"https://api.openai.com/v1",
|
||||
];
|
||||
|
||||
/// Header values whose validity both sides must agree on: trimmed-empty means
|
||||
/// unset, and only control characters other than tab are rejected.
|
||||
const USER_AGENT_SAMPLES: &[&str] = &[
|
||||
"",
|
||||
" ",
|
||||
"Mozilla/5.0 (X11; Linux x86_64)",
|
||||
" padded/1.0 ",
|
||||
"tab\tinside",
|
||||
"café/1.0",
|
||||
"客户端/2.0",
|
||||
"line\nbreak",
|
||||
"carriage\rreturn",
|
||||
"nul\u{0}byte",
|
||||
"escape\u{1b}[0m",
|
||||
"delete\u{7f}",
|
||||
];
|
||||
|
||||
/// Cards whose "must route through the local proxy" answer both sides must
|
||||
/// agree on, checked in every tray app.
|
||||
fn routing_cards() -> Vec<(&'static str, Value)> {
|
||||
let codex_config = |wire_api: &str| {
|
||||
json!({
|
||||
"auth": {"OPENAI_API_KEY": "sk"},
|
||||
"config": format!(
|
||||
"model_provider = \"x\"\n[model_providers.x]\nbase_url = \"https://x.example/v1\"\nwire_api = \"{wire_api}\"\n"
|
||||
)
|
||||
})
|
||||
};
|
||||
let card = |meta: Value, settings: Value| json!({"id": "p1", "name": "P1", "settingsConfig": settings, "meta": meta});
|
||||
let mut official_copilot = card(json!({"providerType": "github_copilot"}), json!({}));
|
||||
official_copilot["category"] = json!("official");
|
||||
vec![
|
||||
("plain", card(json!({}), json!({}))),
|
||||
(
|
||||
"copilot",
|
||||
card(json!({"providerType": "github_copilot"}), json!({})),
|
||||
),
|
||||
(
|
||||
"openai_chat",
|
||||
card(json!({"apiFormat": "openai_chat"}), json!({})),
|
||||
),
|
||||
(
|
||||
"anthropic",
|
||||
card(json!({"apiFormat": "anthropic"}), json!({})),
|
||||
),
|
||||
("full_url", card(json!({"isFullUrl": true}), json!({}))),
|
||||
("codex_chat_wire", card(json!({}), codex_config("chat"))),
|
||||
(
|
||||
"codex_responses_wire",
|
||||
card(json!({}), codex_config("responses")),
|
||||
),
|
||||
(
|
||||
"desktop_proxy_mode",
|
||||
card(json!({"claudeDesktopMode": "proxy"}), json!({})),
|
||||
),
|
||||
("official_copilot", official_copilot),
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backend_constants_match_shared_fixture() {
|
||||
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
@@ -49,6 +109,33 @@ fn backend_constants_match_shared_fixture() {
|
||||
(url.to_string(), json!(id))
|
||||
})
|
||||
.collect();
|
||||
let profile_scope: Map<String, Value> = AppType::all()
|
||||
.map(|app| {
|
||||
let scope = crate::services::profile::ProfileScope::for_app(&app);
|
||||
(app.as_str().to_string(), json!(scope.map(|s| s.as_str())))
|
||||
})
|
||||
.collect();
|
||||
let user_agent: Map<String, Value> = USER_AGENT_SAMPLES
|
||||
.iter()
|
||||
.map(|ua| {
|
||||
let valid = crate::provider::parse_custom_user_agent(Some(ua)).is_ok();
|
||||
(ua.to_string(), json!(valid))
|
||||
})
|
||||
.collect();
|
||||
let routing: Vec<Value> = routing_cards()
|
||||
.into_iter()
|
||||
.map(|(name, card)| {
|
||||
let provider: crate::provider::Provider = serde_json::from_value(card.clone()).unwrap();
|
||||
let by_app: Map<String, Value> = crate::tray::TRAY_APPS
|
||||
.iter()
|
||||
.map(|app| {
|
||||
let needs = crate::tray::provider_needs_routing(app, &provider);
|
||||
(app.as_str().to_string(), json!(needs))
|
||||
})
|
||||
.collect();
|
||||
json!({"name": name, "provider": card, "needsRouting": by_app})
|
||||
})
|
||||
.collect();
|
||||
let constants = json!({
|
||||
"copilotReasoningEfforts": crate::copilot_config::REASONING_EFFORTS,
|
||||
"codexReservedModelProviderIds": crate::codex_config::CODEX_RESERVED_MODEL_PROVIDER_IDS,
|
||||
@@ -62,6 +149,16 @@ fn backend_constants_match_shared_fixture() {
|
||||
"dict": crate::hermes_config::PROVIDER_SOURCE_DICT,
|
||||
},
|
||||
"codingPlanProviderByUrl": coding_plan,
|
||||
"quotaWarnBelowPercent": crate::tray::WARN_BELOW_PERCENT,
|
||||
"exchangeRateCurrencies": crate::services::exchange_rates::TARGET_CURRENCIES,
|
||||
"customIcon": {
|
||||
"prefix": crate::services::provider::icon::CUSTOM_ICON_PREFIX,
|
||||
"maxLength": crate::services::provider::icon::MAX_CUSTOM_ICON_LENGTH,
|
||||
"maxDimension": crate::services::provider::icon::MAX_CUSTOM_ICON_DIMENSION,
|
||||
},
|
||||
"profileScopeByApp": profile_scope,
|
||||
"userAgentValid": user_agent,
|
||||
"providerNeedsRouting": routing,
|
||||
});
|
||||
let expected = serde_json::to_string_pretty(&constants).unwrap() + "\n";
|
||||
if std::env::var_os("CC_SWITCH_UPDATE_GOLDEN").is_some() {
|
||||
|
||||
@@ -23,8 +23,8 @@ pub const CUSTOM_ICON_PREFIX: &str = "data:image/png;base64,";
|
||||
/// 前端输出 96×96 的 PNG,最坏情况(不可压缩)base64 后约 50 KB。
|
||||
pub const MAX_CUSTOM_ICON_LENGTH: usize = 64 * 1024;
|
||||
|
||||
/// PNG 宽高上限,和前端的 `MAX_CUSTOM_ICON_DIMENSION` 一致。前端只输出 96×96,留出余量即可。
|
||||
const MAX_CUSTOM_ICON_DIMENSION: u32 = 256;
|
||||
/// PNG 宽高上限。前端只输出 `OUTPUT_SIZE`(96×96),留出余量即可。
|
||||
pub(crate) const MAX_CUSTOM_ICON_DIMENSION: u32 = 256;
|
||||
|
||||
/// 解码时允许分配的内存:256×256 的 16 位 RGBA 是 512 KiB,留一倍余量
|
||||
const DECODE_LIMIT_BYTES: usize = 1024 * 1024;
|
||||
|
||||
+1
-80
@@ -74,7 +74,7 @@ const MAX_NAME_CHARS: usize = 32;
|
||||
/// 问题区里切换失败的原因最多几个字。
|
||||
const MAX_REASON_CHARS: usize = 60;
|
||||
/// 额度剩余不到这个百分比算「快用完」(和前端 `quotaRules.WARN_BELOW_PERCENT` 一致)。
|
||||
const WARN_BELOW_PERCENT: f64 = 20.0;
|
||||
pub(crate) const WARN_BELOW_PERCENT: f64 = 20.0;
|
||||
|
||||
/// 每个应用行的子菜单句柄,额度更新时就地改标题而不是整菜单重建(整建会关掉 macOS 上
|
||||
/// 正开着的菜单)。`create_tray_menu` 每次重建都整表覆盖写入。
|
||||
@@ -4520,85 +4520,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ─── 「需要路由」判定(和前端 providerNeedsRouting 对照)───
|
||||
|
||||
fn provider(meta: serde_json::Value, settings: serde_json::Value) -> Provider {
|
||||
serde_json::from_value(serde_json::json!({
|
||||
"id": "p1",
|
||||
"name": "P1",
|
||||
"settingsConfig": settings,
|
||||
"meta": meta,
|
||||
}))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn needs_routing_mirrors_the_frontend_rule() {
|
||||
let plain = provider(serde_json::json!({}), serde_json::json!({}));
|
||||
for app in TRAY_APPS {
|
||||
assert!(!provider_needs_routing(&app, &plain), "{app:?}");
|
||||
}
|
||||
|
||||
let copilot = provider(
|
||||
serde_json::json!({"providerType": "github_copilot"}),
|
||||
serde_json::json!({}),
|
||||
);
|
||||
for app in [
|
||||
AppType::Claude,
|
||||
AppType::ClaudeDesktop,
|
||||
AppType::Codex,
|
||||
AppType::GrokBuild,
|
||||
] {
|
||||
assert!(provider_needs_routing(&app, &copilot), "{app:?}");
|
||||
}
|
||||
assert!(!provider_needs_routing(&AppType::Gemini, &copilot));
|
||||
|
||||
let openai_chat = provider(
|
||||
serde_json::json!({"apiFormat": "openai_chat"}),
|
||||
serde_json::json!({}),
|
||||
);
|
||||
assert!(provider_needs_routing(&AppType::Claude, &openai_chat));
|
||||
assert!(provider_needs_routing(&AppType::Codex, &openai_chat));
|
||||
let anthropic = provider(
|
||||
serde_json::json!({"apiFormat": "anthropic"}),
|
||||
serde_json::json!({}),
|
||||
);
|
||||
assert!(!provider_needs_routing(&AppType::Claude, &anthropic));
|
||||
assert!(provider_needs_routing(&AppType::Codex, &anthropic));
|
||||
let full_url = provider(
|
||||
serde_json::json!({"isFullUrl": true}),
|
||||
serde_json::json!({}),
|
||||
);
|
||||
assert!(provider_needs_routing(&AppType::Claude, &full_url));
|
||||
|
||||
let codex_config = |wire_api: &str| {
|
||||
serde_json::json!({
|
||||
"auth": {"OPENAI_API_KEY": "sk"},
|
||||
"config": format!(
|
||||
"model_provider = \"x\"\n[model_providers.x]\nbase_url = \"https://x.example/v1\"\nwire_api = \"{wire_api}\"\n"
|
||||
)
|
||||
})
|
||||
};
|
||||
let chat_wire = provider(serde_json::json!({}), codex_config("chat"));
|
||||
assert!(provider_needs_routing(&AppType::Codex, &chat_wire));
|
||||
let responses_wire = provider(serde_json::json!({}), codex_config("responses"));
|
||||
assert!(!provider_needs_routing(&AppType::Codex, &responses_wire));
|
||||
|
||||
let desktop_mapping = provider(
|
||||
serde_json::json!({"claudeDesktopMode": "proxy"}),
|
||||
serde_json::json!({}),
|
||||
);
|
||||
assert!(provider_needs_routing(
|
||||
&AppType::ClaudeDesktop,
|
||||
&desktop_mapping
|
||||
));
|
||||
|
||||
// 官方卡永远不算。
|
||||
let mut official = copilot.clone();
|
||||
official.category = Some("official".to_string());
|
||||
assert!(!provider_needs_routing(&AppType::Claude, &official));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_wire_api_falls_back_to_a_single_assignment_when_toml_is_broken() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -19,7 +19,7 @@ export const MAX_UPLOAD_BYTES = 2 * 1024 * 1024;
|
||||
const MAX_SOURCE_DIMENSION = 4096;
|
||||
|
||||
/** 输出尺寸:32px 图标框在 3 倍屏上的像素数 */
|
||||
const OUTPUT_SIZE = 96;
|
||||
export const OUTPUT_SIZE = 96;
|
||||
|
||||
/**
|
||||
* 透明 logo 占输出画布的比例,和 ProviderIconBox 里透明 logo(22px)在 32px 框里的比例一致。
|
||||
|
||||
@@ -62,5 +62,220 @@
|
||||
"https://ollama.com.example.com/v1": null,
|
||||
"https://proxy.example.com/ollama.com/v1": null,
|
||||
"https://api.openai.com/v1": null
|
||||
}
|
||||
},
|
||||
"quotaWarnBelowPercent": 20.0,
|
||||
"exchangeRateCurrencies": [
|
||||
"CNY",
|
||||
"EUR",
|
||||
"JPY",
|
||||
"TWD",
|
||||
"TRY",
|
||||
"KRW"
|
||||
],
|
||||
"customIcon": {
|
||||
"prefix": "data:image/png;base64,",
|
||||
"maxLength": 65536,
|
||||
"maxDimension": 256
|
||||
},
|
||||
"profileScopeByApp": {
|
||||
"claude": "claude",
|
||||
"claude-desktop": "claude-desktop",
|
||||
"codex": "codex",
|
||||
"gemini": null,
|
||||
"grokbuild": null,
|
||||
"opencode": null,
|
||||
"openclaw": null,
|
||||
"hermes": null,
|
||||
"pi": null,
|
||||
"mcode": null,
|
||||
"kimi": null,
|
||||
"dsh": null,
|
||||
"agy": null,
|
||||
"omp": null,
|
||||
"copilot": null,
|
||||
"workbuddy": null
|
||||
},
|
||||
"userAgentValid": {
|
||||
"": true,
|
||||
" ": true,
|
||||
"Mozilla/5.0 (X11; Linux x86_64)": true,
|
||||
" padded/1.0 ": true,
|
||||
"tab\tinside": true,
|
||||
"café/1.0": true,
|
||||
"客户端/2.0": true,
|
||||
"line\nbreak": false,
|
||||
"carriage\rreturn": false,
|
||||
"nul\u0000byte": false,
|
||||
"escape\u001b[0m": false,
|
||||
"delete": false
|
||||
},
|
||||
"providerNeedsRouting": [
|
||||
{
|
||||
"name": "plain",
|
||||
"provider": {
|
||||
"id": "p1",
|
||||
"name": "P1",
|
||||
"settingsConfig": {},
|
||||
"meta": {}
|
||||
},
|
||||
"needsRouting": {
|
||||
"claude": false,
|
||||
"claude-desktop": false,
|
||||
"codex": false,
|
||||
"gemini": false,
|
||||
"grokbuild": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "copilot",
|
||||
"provider": {
|
||||
"id": "p1",
|
||||
"name": "P1",
|
||||
"settingsConfig": {},
|
||||
"meta": {
|
||||
"providerType": "github_copilot"
|
||||
}
|
||||
},
|
||||
"needsRouting": {
|
||||
"claude": true,
|
||||
"claude-desktop": true,
|
||||
"codex": true,
|
||||
"gemini": false,
|
||||
"grokbuild": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "openai_chat",
|
||||
"provider": {
|
||||
"id": "p1",
|
||||
"name": "P1",
|
||||
"settingsConfig": {},
|
||||
"meta": {
|
||||
"apiFormat": "openai_chat"
|
||||
}
|
||||
},
|
||||
"needsRouting": {
|
||||
"claude": true,
|
||||
"claude-desktop": false,
|
||||
"codex": true,
|
||||
"gemini": false,
|
||||
"grokbuild": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "anthropic",
|
||||
"provider": {
|
||||
"id": "p1",
|
||||
"name": "P1",
|
||||
"settingsConfig": {},
|
||||
"meta": {
|
||||
"apiFormat": "anthropic"
|
||||
}
|
||||
},
|
||||
"needsRouting": {
|
||||
"claude": false,
|
||||
"claude-desktop": false,
|
||||
"codex": true,
|
||||
"gemini": false,
|
||||
"grokbuild": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "full_url",
|
||||
"provider": {
|
||||
"id": "p1",
|
||||
"name": "P1",
|
||||
"settingsConfig": {},
|
||||
"meta": {
|
||||
"isFullUrl": true
|
||||
}
|
||||
},
|
||||
"needsRouting": {
|
||||
"claude": true,
|
||||
"claude-desktop": false,
|
||||
"codex": true,
|
||||
"gemini": false,
|
||||
"grokbuild": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "codex_chat_wire",
|
||||
"provider": {
|
||||
"id": "p1",
|
||||
"name": "P1",
|
||||
"settingsConfig": {
|
||||
"auth": {
|
||||
"OPENAI_API_KEY": "sk"
|
||||
},
|
||||
"config": "model_provider = \"x\"\n[model_providers.x]\nbase_url = \"https://x.example/v1\"\nwire_api = \"chat\"\n"
|
||||
},
|
||||
"meta": {}
|
||||
},
|
||||
"needsRouting": {
|
||||
"claude": false,
|
||||
"claude-desktop": false,
|
||||
"codex": true,
|
||||
"gemini": false,
|
||||
"grokbuild": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "codex_responses_wire",
|
||||
"provider": {
|
||||
"id": "p1",
|
||||
"name": "P1",
|
||||
"settingsConfig": {
|
||||
"auth": {
|
||||
"OPENAI_API_KEY": "sk"
|
||||
},
|
||||
"config": "model_provider = \"x\"\n[model_providers.x]\nbase_url = \"https://x.example/v1\"\nwire_api = \"responses\"\n"
|
||||
},
|
||||
"meta": {}
|
||||
},
|
||||
"needsRouting": {
|
||||
"claude": false,
|
||||
"claude-desktop": false,
|
||||
"codex": false,
|
||||
"gemini": false,
|
||||
"grokbuild": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "desktop_proxy_mode",
|
||||
"provider": {
|
||||
"id": "p1",
|
||||
"name": "P1",
|
||||
"settingsConfig": {},
|
||||
"meta": {
|
||||
"claudeDesktopMode": "proxy"
|
||||
}
|
||||
},
|
||||
"needsRouting": {
|
||||
"claude": false,
|
||||
"claude-desktop": true,
|
||||
"codex": false,
|
||||
"gemini": false,
|
||||
"grokbuild": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "official_copilot",
|
||||
"provider": {
|
||||
"id": "p1",
|
||||
"name": "P1",
|
||||
"settingsConfig": {},
|
||||
"meta": {
|
||||
"providerType": "github_copilot"
|
||||
},
|
||||
"category": "official"
|
||||
},
|
||||
"needsRouting": {
|
||||
"claude": false,
|
||||
"claude-desktop": false,
|
||||
"codex": false,
|
||||
"gemini": false,
|
||||
"grokbuild": false
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,4 +1,16 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { APP_PROFILE_SCOPE } from "@/components/profiles/scope";
|
||||
import { WARN_BELOW_PERCENT } from "@/components/quota/quotaRules";
|
||||
import { DISPLAY_CURRENCIES } from "@/components/usage/format";
|
||||
import type { AppId } from "@/lib/api/types";
|
||||
import { isValidUserAgentHeader } from "@/lib/userAgent";
|
||||
import type { Provider } from "@/types";
|
||||
import {
|
||||
CUSTOM_ICON_PREFIX,
|
||||
MAX_CUSTOM_ICON_LENGTH,
|
||||
OUTPUT_SIZE,
|
||||
} from "@/utils/customIcon";
|
||||
import { providerNeedsRouting } from "@/utils/providerCapabilities";
|
||||
import { detectCodingPlanProvider } from "@/config/codingPlanProviders";
|
||||
import { COPILOT_REASONING_EFFORTS } from "@/config/copilotConfig";
|
||||
import {
|
||||
@@ -60,4 +72,47 @@ describe("frontend copies of backend constants", () => {
|
||||
expect(detectCodingPlanProvider(url)).toBe(id);
|
||||
},
|
||||
);
|
||||
|
||||
it("warns about quota at the same percentage as the tray", () => {
|
||||
expect(WARN_BELOW_PERCENT).toBe(backend.quotaWarnBelowPercent);
|
||||
});
|
||||
|
||||
it("offers USD plus exactly the currencies the backend fetches rates for", () => {
|
||||
// Compared as sets: the menu order is a UI choice, not the backend's
|
||||
// fetch order.
|
||||
expect([...DISPLAY_CURRENCIES].sort()).toEqual(
|
||||
["USD", ...backend.exchangeRateCurrencies].sort(),
|
||||
);
|
||||
});
|
||||
|
||||
it("produces custom icons the backend accepts", () => {
|
||||
expect(CUSTOM_ICON_PREFIX).toBe(backend.customIcon.prefix);
|
||||
expect(MAX_CUSTOM_ICON_LENGTH).toBe(backend.customIcon.maxLength);
|
||||
expect(OUTPUT_SIZE).toBeLessThanOrEqual(backend.customIcon.maxDimension);
|
||||
});
|
||||
|
||||
it("groups the same apps into profile scopes", () => {
|
||||
for (const [app, scope] of Object.entries(backend.profileScopeByApp)) {
|
||||
expect(APP_PROFILE_SCOPE[app as AppId] ?? null, app).toBe(scope);
|
||||
}
|
||||
});
|
||||
|
||||
it.each(Object.entries(backend.userAgentValid))(
|
||||
"judges User-Agent %j the same way",
|
||||
(value, valid) => {
|
||||
expect(isValidUserAgentHeader(value)).toBe(valid);
|
||||
},
|
||||
);
|
||||
|
||||
it.each(backend.providerNeedsRouting)(
|
||||
"agrees whether the $name card needs routing",
|
||||
({ provider, needsRouting }) => {
|
||||
for (const [app, needs] of Object.entries(needsRouting)) {
|
||||
expect(
|
||||
providerNeedsRouting(app as AppId, provider as unknown as Provider),
|
||||
app,
|
||||
).toBe(needs);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user