| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- // @vitest-environment jsdom
- import { createElement } from "react";
- import { renderToString } from "react-dom/server";
- import { renderHook } from "@testing-library/react";
- import { describe, expect, it, vi } from "vitest";
- import type { ProviderSettingsView } from "@zcode/services";
- import { BUILTIN_MODEL_PROVIDER_IDS } from "@zcode/shared";
- import {
- resolveCodingPlanProviderFingerprintAutoRefresh,
- useCodingPlanAccessRefresh,
- useCodingPlanEntitlements,
- } from "@/settings/model-provider-section/useCodingPlanEntitlements.js";
- const { usageEntitlementCalls, usageEntitlementRefresh } = vi.hoisted(() => ({
- usageEntitlementCalls: [] as Array<Record<string, unknown>>,
- usageEntitlementRefresh: vi.fn(async () => undefined),
- }));
- vi.mock("@/hooks/useUsageEntitlement.js", () => ({
- useUsageEntitlement: (options: Record<string, unknown>) => {
- usageEntitlementCalls.push(options);
- return {
- error: null,
- loading: false,
- refresh: usageEntitlementRefresh,
- snapshot: null,
- };
- },
- }));
- function HookProbe({
- connectionSelections,
- providerSettingsView,
- }: {
- connectionSelections?: Parameters<typeof useCodingPlanEntitlements>[0]["connectionSelections"];
- providerSettingsView: ProviderSettingsView | null;
- }) {
- useCodingPlanEntitlements({
- connectionSelections,
- providerSettingsView,
- });
- return null;
- }
- function createProviderSettingsView(
- providers: Array<{
- providerId: string;
- access?: Record<string, string | boolean>;
- }>,
- ): ProviderSettingsView {
- return {
- revision: 1,
- addableProviders: [],
- providerOrder: [],
- providers: providers.map(({ access, providerId }) => ({
- providerId,
- enabled: true,
- executable: Boolean(access),
- effectiveConfig: {
- ...(access
- ? {
- access: {
- type: "zhipu-account" as const,
- entitled: true,
- ...access,
- },
- }
- : {}),
- api: {
- type: "anthropic-messages" as const,
- baseUrl: "https://example.com",
- },
- builtinModelIds: [],
- },
- issues: [],
- models: [],
- })),
- };
- }
- describe("useCodingPlanEntitlements", () => {
- it("等价 Family 权益事实重新渲染时保持返回引用稳定", () => {
- usageEntitlementCalls.length = 0;
- const providerSettingsView = createProviderSettingsView([
- {
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
- access: {
- accountType: "bigmodel",
- mode: "team-coding-plan",
- },
- },
- ]);
- const initialSelection = {
- bigmodel: {
- kind: "team-coding-plan" as const,
- productId: "team-pro",
- organizationId: "org-a",
- projectId: "project-a",
- },
- };
- const hook = renderHook(
- ({ connectionSelections }) =>
- useCodingPlanEntitlements({ connectionSelections, providerSettingsView }),
- { initialProps: { connectionSelections: initialSelection } },
- );
- const firstResult = hook.result.current;
- hook.rerender({
- connectionSelections: {
- bigmodel: { ...initialSelection.bigmodel },
- },
- });
- expect(hook.result.current).toBe(firstResult);
- });
- it("BigModel Team Plan 选中时仍独立刷新 Start Plan entitlement", () => {
- usageEntitlementCalls.length = 0;
- renderToString(
- createElement(HookProbe, {
- connectionSelections: {
- bigmodel: {
- kind: "team-coding-plan",
- productId: "team-pro",
- organizationId: "org-a",
- projectId: "project-a",
- },
- },
- providerSettingsView: createProviderSettingsView([
- {
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
- access: {
- accountType: "bigmodel",
- mode: "team-coding-plan",
- },
- },
- {
- providerId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- access: {
- accountType: "bigmodel",
- mode: "start-plan",
- },
- },
- ]),
- }),
- );
- const bigmodelCall = usageEntitlementCalls.find(
- (call) => call.preferredProviderId === BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
- );
- expect(bigmodelCall).toMatchObject({
- allowDisabledPreferredProvider: true,
- enabled: true,
- preferredProviderId: BUILTIN_MODEL_PROVIDER_IDS.bigmodelTeamCodingPlan,
- accountAccess: expect.objectContaining({
- organizationId: "org-a",
- projectId: "project-a",
- }),
- requirePreferredProvider: true,
- refreshOnMount: false,
- });
- expect(String(bigmodelCall?.cacheKey)).toContain("bigmodel-team");
- const bigmodelStartCall = usageEntitlementCalls.find(
- (call) => call.preferredProviderId === BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan,
- );
- expect(bigmodelStartCall?.enabled).toBe(true);
- });
- it("Account Provider 没有已启用连接时不把旧静态入口当作可查询连接", () => {
- usageEntitlementCalls.length = 0;
- renderToString(
- createElement(HookProbe, {
- providerSettingsView: createProviderSettingsView([
- { providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan },
- { providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan },
- ]),
- }),
- );
- const calls = usageEntitlementCalls.filter((call) =>
- [
- BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan,
- BUILTIN_MODEL_PROVIDER_IDS.zaiStartPlan,
- ].includes(String(call.preferredProviderId)),
- );
- expect(calls).toHaveLength(2);
- expect(calls.every((call) => call.enabled === false)).toBe(true);
- });
- it("权益缓存身份跟随结构化 Account 连接而不是 Renderer 中的 API Key", () => {
- usageEntitlementCalls.length = 0;
- const providerId = BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan;
- renderToString(
- createElement(HookProbe, {
- providerSettingsView: createProviderSettingsView([
- {
- providerId,
- access: {
- accountType: "zai",
- mode: "individual-coding-plan",
- },
- },
- ]),
- }),
- );
- const firstCacheKey = usageEntitlementCalls.find(
- (call) => call.preferredProviderId === providerId,
- )?.cacheKey;
- usageEntitlementCalls.length = 0;
- renderToString(
- createElement(HookProbe, {
- connectionSelections: {
- zai: {
- kind: "team-coding-plan",
- productId: "team",
- organizationId: "org-a",
- projectId: "project-a",
- },
- },
- providerSettingsView: createProviderSettingsView([
- {
- providerId: BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan,
- access: {
- accountType: "zai",
- mode: "team-coding-plan",
- },
- },
- ]),
- }),
- );
- const secondCacheKey = usageEntitlementCalls.find(
- (call) => call.preferredProviderId === BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan,
- )?.cacheKey;
- expect(firstCacheKey).not.toBe(secondCacheKey);
- expect(String(firstCacheKey)).toContain(BUILTIN_MODEL_PROVIDER_IDS.zaiIndividualCodingPlan);
- // 缓存键包含规范化的请求身份;不依赖其中快照 JSON 的转义/序列化层数。
- expect(String(firstCacheKey)).toContain('"planKind":"individual-coding-plan"');
- expect(String(secondCacheKey)).toContain(BUILTIN_MODEL_PROVIDER_IDS.zaiTeamCodingPlan);
- expect(String(secondCacheKey)).toContain('"organizationId":"org-a"');
- });
- });
- describe("useCodingPlanAccessRefresh", () => {
- it("只在套餐选择身份变化时刷新权益", () => {
- const refresh = vi.fn(async () => undefined);
- const hook = renderHook(
- ({ selectedPlanKey }) => useCodingPlanAccessRefresh({ refresh, selectedPlanKey }),
- { initialProps: { selectedPlanKey: null as string | null } },
- );
- expect(refresh).not.toHaveBeenCalled();
- hook.rerender({ selectedPlanKey: "coding-plan:bigmodel-individual" });
- expect(refresh).toHaveBeenCalledTimes(1);
- expect(refresh).toHaveBeenLastCalledWith({ silent: true, reason: "access" });
- hook.rerender({ selectedPlanKey: "coding-plan:bigmodel-individual" });
- expect(refresh).toHaveBeenCalledTimes(1);
- hook.rerender({ selectedPlanKey: "team-plan:bigmodel:project-a" });
- expect(refresh).toHaveBeenCalledTimes(2);
- hook.rerender({ selectedPlanKey: null });
- expect(refresh).toHaveBeenCalledTimes(2);
- });
- });
- describe("resolveCodingPlanProviderFingerprintAutoRefresh", () => {
- it("连接方式同步期间跳过一次 provider 指纹自动刷新", () => {
- const skipped = resolveCodingPlanProviderFingerprintAutoRefresh({
- loading: false,
- providerFingerprint: "provider-key:v2",
- skippedProviderFingerprint: "",
- suppressAutoRefresh: true,
- });
- expect(skipped).toEqual({
- shouldRefresh: false,
- skippedProviderFingerprint: "provider-key:v2",
- });
- expect(
- resolveCodingPlanProviderFingerprintAutoRefresh({
- loading: false,
- providerFingerprint: "provider-key:v2",
- skippedProviderFingerprint: skipped.skippedProviderFingerprint,
- suppressAutoRefresh: false,
- }),
- ).toEqual({
- shouldRefresh: false,
- skippedProviderFingerprint: "",
- });
- });
- });
|