marketingTaskCompletionRefresh.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { act, cleanup, renderHook } from "@testing-library/react";
  4. import { afterEach, expect, it, vi } from "vitest";
  5. import type { SessionSummary } from "@zcode/shared/zcode-protocol-v4";
  6. const h = vi.hoisted(() => ({
  7. sessions: [] as Array<{ sessionId: string; phase: string }>,
  8. status: "live",
  9. listener: undefined as (() => void) | undefined,
  10. unsubscribe: vi.fn(),
  11. release: vi.fn(),
  12. acquire: vi.fn(),
  13. service: {},
  14. }));
  15. vi.mock("@/hooks/useServices.js", () => ({
  16. useServices: () => ({ zcodeAgentService: h.service }),
  17. }));
  18. vi.mock("@/v4/sessionsIndexRegistry.js", () => ({
  19. acquireSessionsIndex: h.acquire,
  20. releaseSessionsIndex: h.release,
  21. }));
  22. import {
  23. MarketingRefreshContext,
  24. useMarketingTaskCompletionRefresh,
  25. } from "@/components/marketing-touch/useMarketingTaskCompletionRefresh.js";
  26. function setup(rpcReady = true) {
  27. const refresh = vi.fn();
  28. h.acquire.mockReturnValue({
  29. getStatus: () => h.status,
  30. getSessions: () => h.sessions as SessionSummary[],
  31. subscribe: (listener: () => void) => {
  32. h.listener = listener;
  33. return h.unsubscribe;
  34. },
  35. });
  36. const hook = renderHook(
  37. ({ ready }) =>
  38. useMarketingTaskCompletionRefresh({
  39. workspacePath: "/workspace",
  40. workspaceIdentity: "remote:workspace",
  41. endpointKey: "remote-session",
  42. rpcReady: ready,
  43. }),
  44. {
  45. initialProps: { ready: rpcReady },
  46. wrapper: ({ children }) =>
  47. createElement(MarketingRefreshContext.Provider, { value: refresh }, children),
  48. },
  49. );
  50. return { ...hook, refresh };
  51. }
  52. function emit(phase: string, status = "live") {
  53. h.sessions = [{ sessionId: "task", phase }];
  54. h.status = status;
  55. act(() => h.listener?.());
  56. }
  57. afterEach(() => {
  58. cleanup();
  59. vi.clearAllMocks();
  60. h.sessions = [];
  61. h.status = "live";
  62. h.listener = undefined;
  63. });
  64. it("历史完成不刷新;真实成功边沿、重复和再次运行", () => {
  65. h.sessions = [{ sessionId: "task", phase: "completedSuccess" }];
  66. const { refresh } = setup();
  67. expect(refresh).not.toHaveBeenCalled();
  68. emit("completedSuccess");
  69. emit("running");
  70. emit("completedSuccess");
  71. emit("completedSuccess");
  72. expect(refresh).toHaveBeenCalledTimes(1);
  73. emit("running");
  74. emit("completedSuccess");
  75. expect(refresh).toHaveBeenCalledTimes(2);
  76. });
  77. it("失败和中断不刷新;新发现的历史任务不刷新", () => {
  78. const { refresh } = setup();
  79. emit("completedSuccess");
  80. emit("running");
  81. emit("error");
  82. emit("running");
  83. emit("completedInterrupted");
  84. expect(refresh).not.toHaveBeenCalled();
  85. });
  86. it("同批多任务完成合并;连接恢复建立新基线", () => {
  87. h.sessions = ["a", "b"].map((sessionId) => ({ sessionId, phase: "running" }));
  88. const { refresh } = setup();
  89. h.sessions = h.sessions.map((s) => ({ ...s, phase: "completedSuccess" }));
  90. act(() => h.listener?.());
  91. expect(refresh).toHaveBeenCalledTimes(1);
  92. emit("running");
  93. emit("running", "connecting");
  94. emit("completedSuccess");
  95. expect(refresh).toHaveBeenCalledTimes(1);
  96. });
  97. it("RPC 就绪后才订阅,传递隔离身份并在卸载释放", () => {
  98. const { rerender, unmount, refresh } = setup(false);
  99. expect(h.acquire).not.toHaveBeenCalled();
  100. rerender({ ready: true });
  101. expect(h.acquire).toHaveBeenCalledWith(
  102. {
  103. workspaceKey: "remote:workspace",
  104. workspaceIdentity: "remote:workspace",
  105. workspacePath: "/workspace",
  106. endpointKey: "remote-session",
  107. },
  108. h.service,
  109. );
  110. unmount();
  111. expect(h.unsubscribe).toHaveBeenCalledTimes(1);
  112. expect(h.release).toHaveBeenCalledTimes(1);
  113. expect(refresh).not.toHaveBeenCalled();
  114. });