webOAuthCallbackEntry.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // @vitest-environment jsdom
  2. // @vitest-environment-options {"url":"https://zcode.z.ai/"}
  3. import { act } from "react";
  4. import type { Root } from "react-dom/client";
  5. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  6. const roots = vi.hoisted(() => [] as Root[]);
  7. vi.mock("react-dom/client", async (importOriginal) => {
  8. const actual = await importOriginal<typeof import("react-dom/client")>();
  9. return {
  10. ...actual,
  11. createRoot: (...args: Parameters<typeof actual.createRoot>) => {
  12. const root = actual.createRoot(...args);
  13. roots.push(root);
  14. return root;
  15. },
  16. };
  17. });
  18. vi.mock("@zcode/ui", () => ({
  19. AppErrorBoundary: () => null,
  20. Root: () => null,
  21. ZCodeIntlProvider: () => null,
  22. Button: () => null,
  23. generateMobileDeviceFingerprint: () => "oauth-entry-test",
  24. installDocumentHiddenMotionPause: vi.fn(),
  25. playTaskNotificationSound: vi.fn(),
  26. setStreamClientId: vi.fn(),
  27. setWebRemoteControlTerminalTransportState: vi.fn(),
  28. }));
  29. vi.mock("@web/share/ConversationShareLandingPage.js", () => ({
  30. ConversationShareLandingLoader: () => "share-preview",
  31. ConversationShareLandingStatus: () => "invalid-share",
  32. }));
  33. beforeEach(() => {
  34. vi.resetModules();
  35. vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
  36. localStorage.clear();
  37. sessionStorage.clear();
  38. document.documentElement.lang = "en";
  39. document.body.innerHTML = '<div id="root"></div>';
  40. });
  41. afterEach(async () => {
  42. await act(async () => {
  43. for (const root of roots.splice(0)) root.unmount();
  44. });
  45. vi.restoreAllMocks();
  46. vi.unstubAllGlobals();
  47. });
  48. async function bootstrap(path: string, query: string) {
  49. window.history.replaceState(null, "", `${path}?${query}`);
  50. const { WebAuthService } = await import("@web/auth/webAuthService.js");
  51. // 保留真实入口和回调组件 effect;服务内部的 nonce/token 契约由 webAuthService.test 覆盖。
  52. const handleCallback = vi
  53. .spyOn(WebAuthService.prototype, "handleCallback")
  54. .mockResolvedValue(null);
  55. await act(async () => {
  56. await import("@web/main.js");
  57. });
  58. return handleCallback;
  59. }
  60. describe("Web OAuth callback entry", () => {
  61. for (const path of ["/share/callback", "/cn/share/callback", "/remote/v4", "/remote/v4/"]) {
  62. it.each(["authCode=bigmodel-code", "code=zai-code", "error=access_denied"])(
  63. `${path} 的 %s 渲染回调页并交给认证服务`,
  64. async (result) => {
  65. const handleCallback = await bootstrap(path, `state=entry-state&${result}`);
  66. expect(document.body.textContent).toContain("Finishing Sign-In");
  67. expect(handleCallback).toHaveBeenCalledExactlyOnceWith(window.location.href);
  68. },
  69. );
  70. }
  71. for (const path of ["/share/callback", "/cn/share/callback"]) {
  72. it.each(["authCode=code", "code=code", "error=access_denied", "state=entry-state"])(
  73. `${path} 缺少 state 或结果参数时不进入回调页:%s`,
  74. async (query) => {
  75. const handleCallback = await bootstrap(path, query);
  76. expect(handleCallback).not.toHaveBeenCalled();
  77. expect(document.body.textContent).toBe("share-preview");
  78. },
  79. );
  80. }
  81. it("普通分享路径即使携带 OAuth 参数也保持预览路由", async () => {
  82. const handleCallback = await bootstrap("/share/example", "state=entry-state&authCode=code");
  83. expect(handleCallback).not.toHaveBeenCalled();
  84. expect(document.body.textContent).toBe("share-preview");
  85. });
  86. });