rootOAuthReauthentication.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import { ZCODE_JWT_INVALID_BROADCAST_CHANNEL } from "@zcode/shared";
  3. const { effects, requestAlert } = vi.hoisted(() => ({
  4. effects: [] as Array<() => unknown>,
  5. requestAlert: vi.fn(),
  6. }));
  7. vi.mock("react", async (original) => ({
  8. ...(await original<typeof import("react")>()),
  9. useEffect: (effect: () => unknown) => effects.push(effect),
  10. useRef: (current: unknown) => ({ current }),
  11. }));
  12. vi.mock("@/hooks/useAlertDialog.js", () => ({ useAlertDialog: () => requestAlert }));
  13. vi.mock("@/i18n/IntlProvider.js", () => ({
  14. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  15. }));
  16. vi.mock("@/root/useAccountConnectionLossNotification.js", () => ({
  17. useAccountConnectionLossNotification: vi.fn(),
  18. }));
  19. vi.mock("@/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } }));
  20. import { useRootOAuthEffects } from "@/root/useRootOAuthEffects.js";
  21. describe("运行时登录失效进入重新登录", () => {
  22. beforeEach(() => {
  23. effects.length = 0;
  24. requestAlert.mockReset();
  25. });
  26. it.each([true, false])("提示结果 %s 都使用登录入口,不重启 App", async (confirmed) => {
  27. let finish!: (value: boolean) => void;
  28. requestAlert.mockReturnValue(
  29. new Promise<boolean>((resolve) => {
  30. finish = resolve;
  31. }),
  32. );
  33. let receive!: (message: { channel: string }) => void;
  34. const setUser = vi.fn(),
  35. onReauthenticationRequired = vi.fn(),
  36. executeDesktopCommand = vi.fn();
  37. useRootOAuthEffects({
  38. accountIntentKey: "synthetic",
  39. platform: { executeDesktopCommand } as never,
  40. services: {
  41. oauthService: { getActiveProvider: async () => null },
  42. broadcastService: {
  43. onMessage: (listener: typeof receive) => {
  44. receive = listener;
  45. return { dispose: vi.fn() };
  46. },
  47. },
  48. } as never,
  49. refreshProviderState: vi.fn(),
  50. setUser,
  51. setIsRestoringOAuthSession: vi.fn(),
  52. setOAuthError: vi.fn(),
  53. oauthPollingActive: false,
  54. setOAuthPollingActive: vi.fn(),
  55. markOAuthSuccess: vi.fn(),
  56. onReauthenticationRequired,
  57. });
  58. const dispose = effects[1]() as () => void;
  59. receive({ channel: ZCODE_JWT_INVALID_BROADCAST_CHANNEL });
  60. receive({ channel: ZCODE_JWT_INVALID_BROADCAST_CHANNEL });
  61. await vi.waitFor(() => expect(setUser).toHaveBeenCalledWith(null));
  62. expect(requestAlert).toHaveBeenCalledOnce();
  63. expect(requestAlert).toHaveBeenCalledWith(
  64. expect.objectContaining({ actionLabel: "login.expired.action" }),
  65. );
  66. expect(onReauthenticationRequired).not.toHaveBeenCalled();
  67. finish(confirmed);
  68. await vi.waitFor(() => expect(onReauthenticationRequired).toHaveBeenCalledOnce());
  69. expect(executeDesktopCommand).not.toHaveBeenCalled();
  70. dispose();
  71. });
  72. it.each(["before-alert", "during-alert"])(
  73. "新登录在 %s 完成时不显示或执行旧过期提示",
  74. async (timing) => {
  75. let finish!: (value: boolean) => void;
  76. requestAlert.mockReturnValue(
  77. new Promise<boolean>((resolve) => {
  78. finish = resolve;
  79. }),
  80. );
  81. let activeProvider: "zai" | null = timing === "before-alert" ? "zai" : null;
  82. const getActiveProvider = vi.fn(async () => activeProvider),
  83. setUser = vi.fn(),
  84. onReauthenticationRequired = vi.fn();
  85. let receive!: (message: { channel: string }) => void;
  86. useRootOAuthEffects({
  87. accountIntentKey: "synthetic",
  88. platform: {} as never,
  89. services: {
  90. oauthService: { getActiveProvider },
  91. broadcastService: {
  92. onMessage: (listener: typeof receive) => {
  93. receive = listener;
  94. return { dispose: vi.fn() };
  95. },
  96. },
  97. } as never,
  98. refreshProviderState: vi.fn(),
  99. setUser,
  100. setIsRestoringOAuthSession: vi.fn(),
  101. setOAuthError: vi.fn(),
  102. oauthPollingActive: false,
  103. setOAuthPollingActive: vi.fn(),
  104. markOAuthSuccess: vi.fn(),
  105. onReauthenticationRequired,
  106. });
  107. const dispose = effects[1]() as () => void;
  108. receive({ channel: ZCODE_JWT_INVALID_BROADCAST_CHANNEL });
  109. if (timing === "before-alert") {
  110. await vi.waitFor(() => expect(getActiveProvider).toHaveBeenCalledOnce());
  111. expect(requestAlert).not.toHaveBeenCalled();
  112. expect(setUser).not.toHaveBeenCalled();
  113. } else {
  114. await vi.waitFor(() => expect(requestAlert).toHaveBeenCalledOnce());
  115. activeProvider = "zai";
  116. finish(true);
  117. await vi.waitFor(() => expect(getActiveProvider).toHaveBeenCalledTimes(2));
  118. }
  119. expect(onReauthenticationRequired).not.toHaveBeenCalled();
  120. dispose();
  121. },
  122. );
  123. });