mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-11-20 07:52:52 +08:00
959e4a955f
The usage dialog resolves the provider's key and base URL on the frontend
per app, but had no branch for MiniMax Code or Kimi, so both came back
empty. Testing the official balance or coding plan template then failed
with "API key is empty", the template was not auto-detected from the
endpoint, and the custom template showed {{apiKey}} and {{baseUrl}} as
unset, while the backend resolver (Provider::resolve_usage_credentials)
already reads both apps and saved queries worked. Same gap as DeepSeek
Harness in #9108.
MiniMax Code stores credentials in options.{baseURL, apiKey} like
OpenCode and DeepSeek Harness, so it joins that branch. Kimi keeps them
in provider.{base_url, api_key} and gets its own branch; like the
backend it reads only api_key, not api_key_env.
438 lines
12 KiB
TypeScript
438 lines
12 KiB
TypeScript
import type { ReactNode } from "react";
|
|
import { render, screen } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
import UsageScriptModal from "@/components/UsageScriptModal";
|
|
import type { Provider, UsageScript } from "@/types";
|
|
|
|
vi.mock("@/components/common/FullScreenPanel", () => ({
|
|
FullScreenPanel: ({
|
|
isOpen,
|
|
children,
|
|
footer,
|
|
}: {
|
|
isOpen: boolean;
|
|
children: ReactNode;
|
|
footer?: ReactNode;
|
|
}) =>
|
|
isOpen ? (
|
|
<div>
|
|
<div>{children}</div>
|
|
<div>{footer}</div>
|
|
</div>
|
|
) : null,
|
|
}));
|
|
|
|
vi.mock("@/components/JsonEditor", () => ({
|
|
default: ({ value }: { value: string }) => (
|
|
<textarea data-testid="usage-script-code" value={value} readOnly />
|
|
),
|
|
}));
|
|
|
|
const provider: Provider = {
|
|
id: "sub2api-site",
|
|
name: "Example Sub2API",
|
|
settingsConfig: {
|
|
env: {
|
|
ANTHROPIC_BASE_URL: "https://api.example.com",
|
|
ANTHROPIC_AUTH_TOKEN: "sk-test",
|
|
},
|
|
},
|
|
meta: {
|
|
usage_script: {
|
|
enabled: true,
|
|
language: "javascript",
|
|
code: "({ request: { url: '' }, extractor: function () { return {}; } })",
|
|
},
|
|
},
|
|
};
|
|
|
|
function renderModal(
|
|
onSave: (script: UsageScript) => void,
|
|
usageScript?: UsageScript,
|
|
) {
|
|
const client = new QueryClient({
|
|
defaultOptions: { queries: { retry: false } },
|
|
});
|
|
render(
|
|
<QueryClientProvider client={client}>
|
|
<UsageScriptModal
|
|
provider={
|
|
usageScript
|
|
? { ...provider, meta: { usage_script: usageScript } }
|
|
: provider
|
|
}
|
|
appId="claude"
|
|
isOpen
|
|
onClose={() => {}}
|
|
onSave={onSave}
|
|
/>
|
|
</QueryClientProvider>,
|
|
);
|
|
}
|
|
|
|
describe("UsageScriptModal Sub2API template", () => {
|
|
it("fills the /v1/usage script and saves it as the sub2api template", async () => {
|
|
const onSave = vi.fn();
|
|
renderModal(onSave);
|
|
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "usageScript.templateSub2API" }),
|
|
);
|
|
|
|
const code = (
|
|
screen.getByTestId("usage-script-code") as HTMLTextAreaElement
|
|
).value;
|
|
expect(code).toContain('+ "/v1/usage"');
|
|
expect(code).toContain("Bearer {{apiKey}}");
|
|
// 与通用模板一样只用 API Key 和地址,不显示访问令牌、用户 ID
|
|
expect(screen.getByLabelText(/API Key/)).toBeInTheDocument();
|
|
expect(
|
|
screen.queryByLabelText("usageScript.accessToken"),
|
|
).not.toBeInTheDocument();
|
|
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "usageScript.saveConfig" }),
|
|
);
|
|
expect(onSave).toHaveBeenCalledWith(
|
|
expect.objectContaining({ templateType: "sub2api", code }),
|
|
);
|
|
});
|
|
|
|
it("reopens a script saved with the sub2api template as that template", async () => {
|
|
const onSave = vi.fn();
|
|
renderModal(onSave, {
|
|
...provider.meta!.usage_script!,
|
|
templateType: "sub2api",
|
|
});
|
|
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "usageScript.saveConfig" }),
|
|
);
|
|
expect(onSave).toHaveBeenCalledWith(
|
|
expect.objectContaining({ templateType: "sub2api" }),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("UsageScriptModal official subscription hint", () => {
|
|
// agy 的额度靠它自己的 /usage 命令查,不读登录凭据
|
|
it("explains Antigravity's quota check instead of reading OAuth credentials", () => {
|
|
const client = new QueryClient({
|
|
defaultOptions: { queries: { retry: false } },
|
|
});
|
|
render(
|
|
<QueryClientProvider client={client}>
|
|
<UsageScriptModal
|
|
provider={{
|
|
id: "agy-official",
|
|
name: "Google Official",
|
|
category: "official",
|
|
settingsConfig: { env: {}, config: {} },
|
|
meta: {
|
|
usage_script: {
|
|
enabled: true,
|
|
language: "javascript",
|
|
code: "",
|
|
templateType: "official_subscription",
|
|
},
|
|
},
|
|
}}
|
|
appId="agy"
|
|
isOpen
|
|
onClose={() => {}}
|
|
onSave={vi.fn()}
|
|
/>
|
|
</QueryClientProvider>,
|
|
);
|
|
expect(
|
|
screen.getByText("usageScript.officialSubscriptionHintAgy"),
|
|
).toBeInTheDocument();
|
|
expect(
|
|
screen.queryByText("usageScript.officialSubscriptionHint"),
|
|
).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("UsageScriptModal Claude official card bound to an account", () => {
|
|
// 卡片和托盘在没保存开关时默认查这个号的额度,配置页也要显示为已开
|
|
it("shows usage enabled by default when nothing was saved", async () => {
|
|
const onSave = vi.fn();
|
|
const client = new QueryClient({
|
|
defaultOptions: { queries: { retry: false } },
|
|
});
|
|
render(
|
|
<QueryClientProvider client={client}>
|
|
<UsageScriptModal
|
|
provider={{
|
|
id: "claude-bound",
|
|
name: "Claude Official",
|
|
category: "official",
|
|
settingsConfig: { env: {} },
|
|
meta: {
|
|
authBinding: {
|
|
source: "managed_account",
|
|
authProvider: "claude_official",
|
|
accountId: "acct-1",
|
|
},
|
|
},
|
|
}}
|
|
appId="claude"
|
|
isOpen
|
|
onClose={() => {}}
|
|
onSave={onSave}
|
|
/>
|
|
</QueryClientProvider>,
|
|
);
|
|
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "usageScript.saveConfig" }),
|
|
);
|
|
expect(onSave).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
enabled: true,
|
|
templateType: "official_subscription",
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
/**
|
|
* Oh My Pi has its own `AppId` ("omp"), so it never matched the `pi` branch
|
|
* and both built-in tests reported "API key is empty" on an omp card. The
|
|
* provider key and base URL must reach the balance API like they do for Pi.
|
|
*/
|
|
describe("UsageScriptModal Oh My Pi credentials", () => {
|
|
it("passes the omp provider's key and base URL to the balance test", async () => {
|
|
const getBalance = vi.fn().mockResolvedValue({ success: true, data: [] });
|
|
vi.doMock("@/lib/api/subscription", () => ({
|
|
subscriptionApi: { getBalance },
|
|
}));
|
|
const { default: Modal } = await import("@/components/UsageScriptModal");
|
|
const client = new QueryClient({
|
|
defaultOptions: { queries: { retry: false } },
|
|
});
|
|
render(
|
|
<QueryClientProvider client={client}>
|
|
<Modal
|
|
provider={{
|
|
id: "cc-switch-demo",
|
|
name: "Demo",
|
|
// A base URL the balance template recognises, so the test reaches
|
|
// `getBalance` instead of stopping at the template picker.
|
|
settingsConfig: {
|
|
baseUrl: "https://api.deepseek.com/v1",
|
|
apiKey: "sk-omp",
|
|
api: "openai-completions",
|
|
},
|
|
}}
|
|
appId="omp"
|
|
isOpen
|
|
onClose={() => {}}
|
|
onSave={() => {}}
|
|
/>
|
|
</QueryClientProvider>,
|
|
);
|
|
|
|
// The usage query is off by default; enabling it asks for confirmation once.
|
|
await userEvent.click(screen.getByRole("switch"));
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "confirm.usage.confirm" }),
|
|
);
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "usageScript.testScript" }),
|
|
);
|
|
|
|
expect(getBalance).toHaveBeenCalledWith(
|
|
"https://api.deepseek.com/v1",
|
|
"sk-omp",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("UsageScriptModal DeepSeek Harness credentials", () => {
|
|
const dshProvider: Provider = {
|
|
id: "deepseek-dsh",
|
|
name: "DeepSeek",
|
|
settingsConfig: {
|
|
api: "openai-completions",
|
|
options: {
|
|
baseURL: "https://api.deepseek.com/v1/",
|
|
apiKey: "sk-test-dsh",
|
|
},
|
|
models: {},
|
|
},
|
|
meta: {
|
|
usage_script: {
|
|
enabled: true,
|
|
language: "javascript",
|
|
code: "",
|
|
},
|
|
},
|
|
};
|
|
|
|
function renderDshModal() {
|
|
const client = new QueryClient({
|
|
defaultOptions: { queries: { retry: false } },
|
|
});
|
|
render(
|
|
<QueryClientProvider client={client}>
|
|
<UsageScriptModal
|
|
provider={dshProvider}
|
|
appId="dsh"
|
|
isOpen
|
|
onClose={() => {}}
|
|
onSave={() => {}}
|
|
/>
|
|
</QueryClientProvider>,
|
|
);
|
|
}
|
|
|
|
it("detects the official balance template from the DSH provider endpoint", () => {
|
|
renderDshModal();
|
|
|
|
expect(screen.getByText("usageScript.balanceHint")).toBeInTheDocument();
|
|
expect(screen.queryByTestId("usage-script-code")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("passes the saved DSH key and normalized endpoint to the balance test", async () => {
|
|
const { subscriptionApi } = await import("@/lib/api/subscription");
|
|
const getBalance = vi
|
|
.spyOn(subscriptionApi, "getBalance")
|
|
.mockResolvedValue({
|
|
success: true,
|
|
data: [{ isValid: true, remaining: 1, unit: "CNY" }],
|
|
});
|
|
|
|
try {
|
|
renderDshModal();
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "usageScript.templateBalance" }),
|
|
);
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "usageScript.testScript" }),
|
|
);
|
|
|
|
expect(getBalance).toHaveBeenCalledWith(
|
|
"https://api.deepseek.com/v1",
|
|
"sk-test-dsh",
|
|
);
|
|
} finally {
|
|
getBalance.mockRestore();
|
|
}
|
|
});
|
|
|
|
it("shows inherited DSH credentials in the custom template variables", async () => {
|
|
renderDshModal();
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "usageScript.templateCustom" }),
|
|
);
|
|
|
|
expect(screen.getByText("https://api.deepseek.com/v1")).toBeInTheDocument();
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "apiKeyInput.show" }),
|
|
);
|
|
expect(screen.getByText("sk-test-dsh")).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe.each([
|
|
{
|
|
appId: "mcode" as const,
|
|
settingsConfig: {
|
|
kind: "custom",
|
|
api: "openai-completions",
|
|
options: {
|
|
baseURL: "https://api.deepseek.com/v1/",
|
|
apiKey: "sk-test-mcode",
|
|
},
|
|
models: {},
|
|
},
|
|
apiKey: "sk-test-mcode",
|
|
},
|
|
{
|
|
appId: "kimi" as const,
|
|
settingsConfig: {
|
|
provider: {
|
|
type: "openai",
|
|
base_url: "https://api.deepseek.com/v1/",
|
|
api_key: "sk-test-kimi",
|
|
},
|
|
},
|
|
apiKey: "sk-test-kimi",
|
|
},
|
|
])(
|
|
"UsageScriptModal $appId credentials",
|
|
({ appId, settingsConfig, apiKey }) => {
|
|
function renderAppModal() {
|
|
const client = new QueryClient({
|
|
defaultOptions: { queries: { retry: false } },
|
|
});
|
|
render(
|
|
<QueryClientProvider client={client}>
|
|
<UsageScriptModal
|
|
provider={{
|
|
id: `deepseek-${appId}`,
|
|
name: "DeepSeek",
|
|
settingsConfig,
|
|
meta: {
|
|
usage_script: {
|
|
enabled: true,
|
|
language: "javascript",
|
|
code: "",
|
|
},
|
|
},
|
|
}}
|
|
appId={appId}
|
|
isOpen
|
|
onClose={() => {}}
|
|
onSave={() => {}}
|
|
/>
|
|
</QueryClientProvider>,
|
|
);
|
|
}
|
|
|
|
it("passes the saved key and normalized endpoint to the balance test", async () => {
|
|
const { subscriptionApi } = await import("@/lib/api/subscription");
|
|
const getBalance = vi
|
|
.spyOn(subscriptionApi, "getBalance")
|
|
.mockResolvedValue({
|
|
success: true,
|
|
data: [{ isValid: true, remaining: 1, unit: "CNY" }],
|
|
});
|
|
|
|
try {
|
|
renderAppModal();
|
|
// The DeepSeek endpoint auto-selects the official balance template.
|
|
expect(screen.getByText("usageScript.balanceHint")).toBeInTheDocument();
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "usageScript.testScript" }),
|
|
);
|
|
|
|
expect(getBalance).toHaveBeenCalledWith(
|
|
"https://api.deepseek.com/v1",
|
|
apiKey,
|
|
);
|
|
} finally {
|
|
getBalance.mockRestore();
|
|
}
|
|
});
|
|
|
|
it("shows inherited credentials in the custom template variables", async () => {
|
|
renderAppModal();
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "usageScript.templateCustom" }),
|
|
);
|
|
|
|
expect(
|
|
screen.getByText("https://api.deepseek.com/v1"),
|
|
).toBeInTheDocument();
|
|
await userEvent.click(
|
|
screen.getByRole("button", { name: "apiKeyInput.show" }),
|
|
);
|
|
expect(screen.getByText(apiKey)).toBeInTheDocument();
|
|
});
|
|
},
|
|
);
|