startPlanEntitlementAccess.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // @vitest-environment jsdom
  2. import { cleanup, renderHook } from "@testing-library/react";
  3. import { afterEach, expect, it, vi } from "vitest";
  4. import { usePlanIdentitySnapshot } from "@/hooks/usePlanIdentitySnapshot.js";
  5. import { useV4SessionQuotaBanner } from "@/v4/useV4SessionQuotaBanner.js";
  6. const f = vi.hoisted(() => ({ options: [] as Array<Record<string, unknown>> }));
  7. vi.mock("@/hooks/useProviderSettingsView.js", () => ({
  8. useProviderSettingsView: () => ({
  9. state: {
  10. status: "ready",
  11. view: {
  12. revision: 1,
  13. providers: ["start-plan", "individual-coding-plan", "team-coding-plan"].map((mode) => ({
  14. providerId: `account:bigmodel-${mode}`,
  15. effectiveConfig: {
  16. access: { type: "zhipu-account", accountType: "bigmodel", mode, entitled: true },
  17. },
  18. })),
  19. },
  20. },
  21. }),
  22. }));
  23. vi.mock("@/hooks/useUsageEntitlement.js", () => ({
  24. useUsageEntitlementWithService: (_service: unknown, options: Record<string, unknown>) => {
  25. f.options.push(options);
  26. return { snapshot: null, error: null, loading: false, refresh: async () => {} };
  27. },
  28. }));
  29. afterEach(() => {
  30. cleanup();
  31. f.options = [];
  32. });
  33. it("冷缓存身份统计同时显式查询 Start 和所选团队,团队 scope 不丢失", () => {
  34. renderHook(() =>
  35. usePlanIdentitySnapshot("bigmodel", {
  36. kind: "team-coding-plan",
  37. productId: "p",
  38. organizationId: "o",
  39. projectId: "j",
  40. }),
  41. );
  42. expect(
  43. f.options.find((o) => o.preferredProviderId === "account:bigmodel-start-plan")?.accountAccess,
  44. ).toEqual({ type: "zhipu-account", family: "bigmodel", planKind: "start-plan" });
  45. expect(
  46. f.options.find((o) => o.preferredProviderId === "account:bigmodel-team-coding-plan")
  47. ?.accountAccess,
  48. ).toEqual({
  49. type: "zhipu-account",
  50. family: "bigmodel",
  51. planKind: "team-coding-plan",
  52. productId: "p",
  53. organizationId: "o",
  54. projectId: "j",
  55. });
  56. });
  57. it("会话反馈按执行 Start 补访问参数;切到其他执行 Provider 时停止 Start 查询", () => {
  58. const hook = renderHook(
  59. ({ providerId }) =>
  60. useV4SessionQuotaBanner({
  61. sessionId: "access",
  62. error: null,
  63. errorKey: null,
  64. phase: "idle",
  65. providerId,
  66. modelId: "glm-5",
  67. }),
  68. { initialProps: { providerId: "account:bigmodel-start-plan" } },
  69. );
  70. expect(f.options[0]).toMatchObject({
  71. enabled: true,
  72. accountAccess: { type: "zhipu-account", family: "bigmodel", planKind: "start-plan" },
  73. includeSubscription: true,
  74. });
  75. f.options = [];
  76. hook.rerender({ providerId: "account:bigmodel-team-coding-plan" });
  77. expect(f.options[0]?.enabled).toBe(false);
  78. });