webRemoteControlAuthSync.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // @vitest-environment jsdom
  2. import { cleanup, renderHook } from "@testing-library/react";
  3. import { afterEach, expect, it, vi } from "vitest";
  4. import { Emitter } from "@zcode/rpc";
  5. import { useWebRemoteControlAuthSync } from "@/web-remote-control/app/useWebRemoteControlAuthSync.js";
  6. afterEach(cleanup);
  7. it("初始恢复后转发凭据完成事件,不依赖 UI 用户或成功计数更新;卸载取消订阅", () => {
  8. const session = new Emitter<"signed-in" | "signed-out">();
  9. const oauth = { onDidChangeSession: session.event };
  10. const notifyWebRemoteControlAuthStateChanged = vi.fn();
  11. const platform = { notifyWebRemoteControlAuthStateChanged };
  12. const { rerender, unmount } = renderHook(
  13. ({ userId, restoring }) => useWebRemoteControlAuthSync(platform, oauth, userId, restoring),
  14. {
  15. initialProps: { userId: "" as string | undefined, restoring: true },
  16. },
  17. );
  18. expect(notifyWebRemoteControlAuthStateChanged).not.toHaveBeenCalled();
  19. rerender({ userId: "user", restoring: false });
  20. session.fire("signed-out");
  21. // 设置后处理失败时 UI 用户仍是 user,完成事件也必须立即送到 Main。
  22. session.fire("signed-in");
  23. session.fire("signed-in");
  24. rerender({ userId: undefined, restoring: false });
  25. unmount();
  26. session.fire("signed-out");
  27. expect(notifyWebRemoteControlAuthStateChanged.mock.calls).toEqual([
  28. [{ state: "signed-in" }],
  29. [{ state: "signed-out" }],
  30. [{ state: "signed-in" }],
  31. [{ state: "signed-in" }],
  32. ]);
  33. });
  34. it("没有桌面平台通知能力时不订阅远端 OAuth 事件", () => {
  35. const onDidChangeSession = vi.fn();
  36. renderHook(() => useWebRemoteControlAuthSync({}, { onDidChangeSession }, "user", false));
  37. expect(onDidChangeSession).not.toHaveBeenCalled();
  38. });