mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-06-16 13:34:04 +08:00
791ced0034
- Remove mergeCodexDefaultCatalogModelForSave implicit injection (P1) The model mapping table is now the single source of truth; no hidden entries are prepended on save. - Sync first catalog row model into config.toml on save Ensures Codex default request model matches the table's first entry instead of retaining a stale template value. - Remove API Format selector from CodexFormFields (P3) wire_api is always 'responses'; the selector confused users into thinking they were changing the upstream protocol. Only the 'Needs Local Routing' toggle remains. - Add restart hint to model mapping i18n text (P2) model_catalog_json is loaded at Codex startup; users are now informed that a restart is needed after changes. - Unify write_codex_live_with_catalog helper (P4) Replaces three scattered prepare+write call sites in config.rs, provider/live.rs, and proxy.rs with a single entry point. - Clean up useCodexConfigState dead state (P3 follow-up) Remove codexModelName, codexContextWindow, codexAutoCompactLimit and their handlers/effects since no component consumes them after the UI consolidation.
201 lines
5.7 KiB
TypeScript
201 lines
5.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
extractCodexBaseUrl,
|
|
extractCodexModelName,
|
|
extractCodexTopLevelInt,
|
|
removeCodexTopLevelField,
|
|
setCodexBaseUrl,
|
|
setCodexModelName,
|
|
setCodexTopLevelInt,
|
|
} from "@/utils/providerConfigUtils";
|
|
|
|
describe("Codex TOML utils", () => {
|
|
it("removes base_url line when set to empty", () => {
|
|
const input = [
|
|
'model_provider = "openai"',
|
|
'base_url = "https://api.example.com/v1"',
|
|
'model = "gpt-5-codex"',
|
|
"",
|
|
].join("\n");
|
|
|
|
const output = setCodexBaseUrl(input, "");
|
|
|
|
expect(output).not.toMatch(/^\s*base_url\s*=/m);
|
|
expect(extractCodexBaseUrl(output)).toBeUndefined();
|
|
expect(extractCodexModelName(output)).toBe("gpt-5-codex");
|
|
});
|
|
|
|
it("removes only the top-level model line when set to empty", () => {
|
|
const input = [
|
|
'model_provider = "openai"',
|
|
'base_url = "https://api.example.com/v1"',
|
|
'model = "gpt-5-codex"',
|
|
"",
|
|
"[profiles.default]",
|
|
'model = "profile-model"',
|
|
"",
|
|
].join("\n");
|
|
|
|
const output = setCodexModelName(input, "");
|
|
|
|
expect(output).not.toMatch(/^model\s*=\s*"gpt-5-codex"$/m);
|
|
expect(output).toMatch(/^\[profiles\.default\]\nmodel = "profile-model"$/m);
|
|
expect(extractCodexModelName(output)).toBeUndefined();
|
|
expect(extractCodexBaseUrl(output)).toBe("https://api.example.com/v1");
|
|
});
|
|
|
|
it("updates existing values when non-empty", () => {
|
|
const input = [
|
|
'model_provider = "openai"',
|
|
"base_url = 'https://old.example/v1'",
|
|
'model = "old-model"',
|
|
"",
|
|
].join("\n");
|
|
|
|
const output1 = setCodexBaseUrl(input, " https://new.example/v1 \n");
|
|
expect(extractCodexBaseUrl(output1)).toBe("https://new.example/v1");
|
|
|
|
const output2 = setCodexModelName(output1, " new-model \n");
|
|
expect(extractCodexModelName(output2)).toBe("new-model");
|
|
});
|
|
|
|
it("reads and writes base_url in the active provider section", () => {
|
|
const input = [
|
|
'model_provider = "custom"',
|
|
'model = "gpt-5.4"',
|
|
"",
|
|
"[model_providers.custom]",
|
|
'name = "custom"',
|
|
'wire_api = "responses"',
|
|
"",
|
|
"[profiles.default]",
|
|
'approval_policy = "never"',
|
|
"",
|
|
].join("\n");
|
|
|
|
const output = setCodexBaseUrl(input, "https://api.example.com/v1");
|
|
|
|
expect(output).toContain(
|
|
'[model_providers.custom]\nname = "custom"\nwire_api = "responses"\nbase_url = "https://api.example.com/v1"',
|
|
);
|
|
expect(extractCodexBaseUrl(output)).toBe("https://api.example.com/v1");
|
|
});
|
|
|
|
it("recovers a single misplaced base_url from another section", () => {
|
|
const input = [
|
|
'model_provider = "custom"',
|
|
'model = "gpt-5.4"',
|
|
"",
|
|
"[model_providers.custom]",
|
|
'name = "custom"',
|
|
'wire_api = "responses"',
|
|
"",
|
|
"[profiles.default]",
|
|
'approval_policy = "never"',
|
|
'base_url = "https://wrong.example/v1"',
|
|
"",
|
|
].join("\n");
|
|
|
|
expect(extractCodexBaseUrl(input)).toBe("https://wrong.example/v1");
|
|
|
|
const output = setCodexBaseUrl(input, "https://fixed.example/v1");
|
|
|
|
expect(output).toContain(
|
|
'[model_providers.custom]\nname = "custom"\nwire_api = "responses"\nbase_url = "https://fixed.example/v1"',
|
|
);
|
|
expect(output).not.toContain("https://wrong.example/v1");
|
|
expect(output.match(/base_url\s*=/g)).toHaveLength(1);
|
|
});
|
|
|
|
it("does not treat mcp_servers base_url as provider base_url", () => {
|
|
const input = [
|
|
'model_provider = "azure"',
|
|
'model = "gpt-4"',
|
|
"",
|
|
"[model_providers.azure]",
|
|
'name = "Azure OpenAI"',
|
|
'wire_api = "responses"',
|
|
"",
|
|
"[mcp_servers.my_server]",
|
|
'base_url = "http://localhost:8080"',
|
|
"",
|
|
].join("\n");
|
|
|
|
expect(extractCodexBaseUrl(input)).toBeUndefined();
|
|
|
|
const output = setCodexBaseUrl(input, "https://new.azure/v1");
|
|
|
|
expect(output).toContain(
|
|
'[model_providers.azure]\nname = "Azure OpenAI"\nwire_api = "responses"\nbase_url = "https://new.azure/v1"',
|
|
);
|
|
expect(output).toContain(
|
|
'[mcp_servers.my_server]\nbase_url = "http://localhost:8080"',
|
|
);
|
|
});
|
|
|
|
it("reads model only from the top-level config", () => {
|
|
const input = [
|
|
'model_provider = "custom"',
|
|
"",
|
|
"[profiles.default]",
|
|
'model = "profile-model"',
|
|
"",
|
|
].join("\n");
|
|
|
|
expect(extractCodexModelName(input)).toBeUndefined();
|
|
});
|
|
|
|
it("handles single-quoted values", () => {
|
|
const input = "base_url = 'https://api.example.com/v1'\nmodel = 'gpt-5'\n";
|
|
|
|
expect(extractCodexBaseUrl(input)).toBe("https://api.example.com/v1");
|
|
expect(extractCodexModelName(input)).toBe("gpt-5");
|
|
});
|
|
|
|
it("reads, writes, and removes top-level integer metadata fields", () => {
|
|
const input = [
|
|
'model_provider = "custom"',
|
|
'model = "deepseek-v4-flash"',
|
|
"",
|
|
"[model_providers.custom]",
|
|
'name = "DeepSeek"',
|
|
"",
|
|
].join("\n");
|
|
|
|
const withContext = setCodexTopLevelInt(
|
|
input,
|
|
"model_context_window",
|
|
128000,
|
|
);
|
|
const withCompact = setCodexTopLevelInt(
|
|
withContext,
|
|
"model_auto_compact_token_limit",
|
|
90000,
|
|
);
|
|
|
|
expect(extractCodexTopLevelInt(withCompact, "model_context_window")).toBe(
|
|
128000,
|
|
);
|
|
expect(
|
|
extractCodexTopLevelInt(
|
|
withCompact,
|
|
"model_auto_compact_token_limit",
|
|
),
|
|
).toBe(90000);
|
|
expect(withCompact).toMatch(/^model_context_window = 128000$/m);
|
|
expect(withCompact).toMatch(
|
|
/^model_auto_compact_token_limit = 90000$/m,
|
|
);
|
|
|
|
const removed = removeCodexTopLevelField(
|
|
withCompact,
|
|
"model_context_window",
|
|
);
|
|
|
|
expect(
|
|
extractCodexTopLevelInt(removed, "model_context_window"),
|
|
).toBeUndefined();
|
|
expect(removed).toContain("[model_providers.custom]");
|
|
});
|
|
});
|