Files
cc-switch/tests/components/UsageFooter.tokenPlanBalance.test.tsx
Jason bbcf0aeaec fix(usage): show account balance when a Zhipu account has no coding plan
Since the token plan template became the default for Zhipu cards, pay-as-you-go
accounts showed "query failed": the quota endpoint answers HTTP 200 with
{"code":500,"msg":"当前用户不存在coding plan","success":false}, which looked like
the provider was broken (#8465).

Show the plan when there is one, otherwise the account balance:

- coding_plan: query_coding_plan returns CodingPlanOutcome, with ZhipuNoPlan
  for the personal plan when msg says the account has no coding plan. Auth
  failures, network errors and other business errors are unchanged.
  get_coding_plan_quota keeps its old result (used by the modal test button).
  The team plan does not fall back: the balance endpoint ignores the
  organization/project headers.
- balance: query_zhipu_balance reads availableBalance from the undocumented
  /api/biz/account/query-customer-account-report, which accepts the inference
  API key (verified on open.bigmodel.cn, CNY). api.z.ai is assumed to share
  the shape and is shown in USD; not verified. Not added to detect_provider so
  it does not compete with the injected token plan template.
- The token_plan branch of query_provider_usage returns the balance result on
  ZhipuNoPlan; the tray already renders it as a balance line.
- UsageFooter only renders token plan data as tiers when every row is a
  percentage, so the fallback balance uses the balance layout.
2026-10-19 18:01:47 +08:00

71 lines
2.1 KiB
TypeScript

import { QueryClientProvider } from "@tanstack/react-query";
import { render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";
import UsageFooter from "@/components/UsageFooter";
import { usageKeys } from "@/lib/query/usage";
import type { Provider, UsageResult } from "@/types";
import { createTestQueryClient } from "../utils/testQueryClient";
function renderFooter(result: UsageResult) {
const provider: Provider = {
id: "zhipu",
name: "Zhipu GLM",
settingsConfig: {
env: { ANTHROPIC_BASE_URL: "https://open.bigmodel.cn/api/anthropic" },
},
meta: {
usage_script: {
enabled: true,
language: "javascript",
code: "",
templateType: "token_plan",
},
},
};
const queryClient = createTestQueryClient();
queryClient.setQueryData(usageKeys.script(provider.id, "claude"), result);
return render(
<QueryClientProvider client={queryClient}>
<UsageFooter
provider={provider}
providerId={provider.id}
appId="claude"
usageEnabled={true}
isCurrent={true}
/>
</QueryClientProvider>,
);
}
describe("UsageFooter token plan", () => {
// 智谱按量账户没有 Coding Plan 时,后端回落成账户余额(#8465)
it("shows a balance when the token plan query falls back to the account balance", () => {
renderFooter({
success: true,
data: [{ planName: "Zhipu GLM", remaining: 14.68, unit: "CNY" }],
});
expect(screen.getByText("Zhipu GLM")).toBeInTheDocument();
expect(screen.getByText("quota.balance")).toBeInTheDocument();
expect(screen.queryByText("quota.failed")).not.toBeInTheDocument();
});
it("keeps rendering percentage tiers for plan accounts", () => {
renderFooter({
success: true,
data: [
{
planName: "five_hour",
total: 100,
used: 20,
remaining: 80,
unit: "%",
},
],
});
expect(screen.queryByText("quota.balance")).not.toBeInTheDocument();
expect(screen.queryByText("Zhipu GLM")).not.toBeInTheDocument();
});
});