enterpriseProductsAuth.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { act, cleanup, render, screen, waitFor } from "@testing-library/react";
  4. import { afterEach, beforeEach, expect, it, vi } from "vitest";
  5. import { useEnterpriseCodingPlanProducts } from "@/settings/model-provider-section/useEnterpriseCodingPlanProducts.js";
  6. const mocks = vi.hoisted(() => ({
  7. auth: { user: { id: "account-a" } as { id: string } | null, authSessionSeq: 1 },
  8. getStaticTeamProducts: vi.fn(async () => ({})),
  9. getEnterprisePricing: vi.fn(),
  10. }));
  11. vi.mock("@/hooks/useServices.js", () => ({
  12. useOptionalServices: () => ({ codingPlanSubscriptionService: mocks }),
  13. }));
  14. vi.mock("@/store/StoreProvider.js", () => ({
  15. useZCodeStoreWithDefault: (selector: (state: typeof mocks.auth) => unknown) =>
  16. selector(mocks.auth),
  17. }));
  18. function Harness({
  19. enabled,
  20. authenticated,
  21. family = "bigmodel",
  22. }: {
  23. enabled: boolean;
  24. authenticated: boolean;
  25. family?: "bigmodel" | "zai";
  26. }) {
  27. const products = useEnterpriseCodingPlanProducts({ enabled, authenticated, family });
  28. return createElement(
  29. "div",
  30. null,
  31. products.snapshot?.raw.productList.map((p) => p.productId).join(","),
  32. );
  33. }
  34. beforeEach(() => {
  35. mocks.auth = { user: { id: "account-a" }, authSessionSeq: 1 };
  36. mocks.getEnterprisePricing.mockReset().mockResolvedValue({
  37. productList: [
  38. {
  39. productId: "private-team-a",
  40. subscribed: true,
  41. tier: "PRO",
  42. subscribeMode: "CONTINUOUS",
  43. subscribePeriod: "MONTHLY",
  44. payAmount: 100,
  45. },
  46. ],
  47. });
  48. });
  49. afterEach(cleanup);
  50. it("登录目录已加载后登出,立即清除团队;普通 disabled 仍保留同身份快照", async () => {
  51. const { rerender } = render(createElement(Harness, { enabled: true, authenticated: true }));
  52. await screen.findByText("private-team-a");
  53. rerender(createElement(Harness, { enabled: false, authenticated: true }));
  54. expect(screen.queryByText("private-team-a")).not.toBeNull();
  55. mocks.auth.user = null;
  56. rerender(createElement(Harness, { enabled: false, authenticated: false }));
  57. expect(screen.queryByText("private-team-a")).toBeNull();
  58. });
  59. it("登出后的迟到响应不能复活旧团队目录", async () => {
  60. let resolve!: (value: unknown) => void;
  61. mocks.getEnterprisePricing.mockReturnValueOnce(
  62. new Promise((r) => {
  63. resolve = r;
  64. }),
  65. );
  66. const { rerender } = render(createElement(Harness, { enabled: true, authenticated: true }));
  67. await waitFor(() => expect(mocks.getEnterprisePricing).toHaveBeenCalledTimes(1));
  68. mocks.auth.user = null;
  69. rerender(createElement(Harness, { enabled: false, authenticated: false }));
  70. await act(async () => {
  71. resolve({
  72. productList: [
  73. {
  74. productId: "private-team-a",
  75. tier: "PRO",
  76. subscribeMode: "CONTINUOUS",
  77. subscribePeriod: "MONTHLY",
  78. payAmount: 100,
  79. },
  80. ],
  81. });
  82. });
  83. expect(screen.queryByText("private-team-a")).toBeNull();
  84. });
  85. it.each(["account", "session", "family"] as const)(
  86. "身份边界 %s 变化时不能保留旧目录",
  87. async (boundary) => {
  88. const { rerender } = render(createElement(Harness, { enabled: true, authenticated: true }));
  89. await screen.findByText("private-team-a");
  90. mocks.getEnterprisePricing.mockReturnValue(new Promise(() => {}));
  91. if (boundary === "account") mocks.auth.user = { id: "account-b" };
  92. if (boundary === "session") mocks.auth.authSessionSeq++;
  93. rerender(
  94. createElement(Harness, {
  95. enabled: true,
  96. authenticated: true,
  97. family: boundary === "family" ? "zai" : "bigmodel",
  98. }),
  99. );
  100. expect(screen.queryByText("private-team-a")).toBeNull();
  101. },
  102. );