useWorkflowRunJournalSummaries.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // @vitest-environment jsdom
  2. // 工具卡 join 回退的信源:journal-backed 的发现查询(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md
  3. // 「发现查询」+「UI 回退的重试语义」)。
  4. //
  5. // 这个 hook 存在的理由就是一个实测失效:查询原本一次性发出、失败即静默 null,而它恰好
  6. // 在会话还冷的时候发出(effect 声明顺序 + CLI 串行派发),于是重启后工具卡永远退回编译态。
  7. // 冷会话前置已在 CLI 侧修好;这里钉住第二半——可重试的那一跳真的重试了。
  8. import { renderHook, waitFor } from "@testing-library/react";
  9. import { describe, expect, it, vi } from "vitest";
  10. import type {
  11. V4ConversationWorkflowRunsParams,
  12. V4ConversationWorkflowRunsResult,
  13. } from "@zcode/shared/zcode-protocol-v4";
  14. vi.mock("@/logger.js", () => ({
  15. logger: { debug: vi.fn(), warn: vi.fn() },
  16. }));
  17. const workflowRuns =
  18. vi.fn<(params: V4ConversationWorkflowRunsParams) => Promise<V4ConversationWorkflowRunsResult>>();
  19. vi.mock("@/v4/V4ConversationContext.js", () => ({
  20. useV4Conversation: () => ({ workflowRuns }),
  21. }));
  22. // eslint-disable-next-line import/first -- 必须在 context mock 之后再引入被测 hook。
  23. import {
  24. isWorkflowRunsCapabilityMissing,
  25. useWorkflowRunJournalSummaries,
  26. } from "@/hooks/useWorkflowRunJournalSummaries.js";
  27. function result(runId: string): V4ConversationWorkflowRunsResult {
  28. return {
  29. runs: [{ runId, toolCallId: `call-${runId}`, status: "failed", resumable: true }],
  30. };
  31. }
  32. describe("isWorkflowRunsCapabilityMissing", () => {
  33. it("能力缺席与普通失败必须分开:前者终局,后者值得再问一次", () => {
  34. expect(
  35. isWorkflowRunsCapabilityMissing(
  36. new Error(
  37. "capability not supported by this session runtime: listDynamicWorkflowRuns (session s1)",
  38. ),
  39. ),
  40. ).toBe(true);
  41. expect(isWorkflowRunsCapabilityMissing(new Error("fault.command.capabilityUnsupported"))).toBe(
  42. true,
  43. );
  44. expect(
  45. isWorkflowRunsCapabilityMissing(new Error("fault.workflowRuns.sessionNotFound: s1")),
  46. ).toBe(false);
  47. expect(isWorkflowRunsCapabilityMissing(new Error("socket closed"))).toBe(false);
  48. });
  49. });
  50. describe("useWorkflowRunJournalSummaries", () => {
  51. it("首查成功即收口:live 抬升不再多打一次 RPC", async () => {
  52. workflowRuns.mockReset();
  53. workflowRuns.mockResolvedValue(result("dwfrun-1"));
  54. const hook = renderHook(
  55. (props: { live: boolean }) =>
  56. useWorkflowRunJournalSummaries({ sessionId: "sess-1", live: props.live }),
  57. { initialProps: { live: false } },
  58. );
  59. await waitFor(() => expect(hook.result.current).toHaveLength(1));
  60. expect(workflowRuns).toHaveBeenCalledTimes(1);
  61. hook.rerender({ live: true });
  62. await waitFor(() => expect(hook.result.current).toHaveLength(1));
  63. expect(workflowRuns).toHaveBeenCalledTimes(1);
  64. });
  65. it("冷开首查失败 → live 抬升时重试并补上摘要(这就是那个失效的修复)", async () => {
  66. workflowRuns.mockReset();
  67. workflowRuns
  68. // 会话还没在册:CLI 侧回结构化的 sessionNotFound。
  69. .mockRejectedValueOnce(new Error("fault.workflowRuns.sessionNotFound: sess-1"))
  70. .mockResolvedValueOnce(result("dwfrun-1"));
  71. const hook = renderHook(
  72. (props: { live: boolean }) =>
  73. useWorkflowRunJournalSummaries({ sessionId: "sess-1", live: props.live }),
  74. { initialProps: { live: false } },
  75. );
  76. await waitFor(() => expect(workflowRuns).toHaveBeenCalledTimes(1));
  77. expect(hook.result.current).toBeNull();
  78. hook.rerender({ live: true });
  79. await waitFor(() => expect(hook.result.current).toHaveLength(1));
  80. expect(hook.result.current?.[0]?.runId).toBe("dwfrun-1");
  81. });
  82. it("能力缺席不重试:旧 CLI / journal 不可用是稳定事实", async () => {
  83. workflowRuns.mockReset();
  84. workflowRuns.mockRejectedValue(
  85. new Error(
  86. "capability not supported by this session runtime: listDynamicWorkflowRuns (session sess-1)",
  87. ),
  88. );
  89. const hook = renderHook(
  90. (props: { live: boolean }) =>
  91. useWorkflowRunJournalSummaries({ sessionId: "sess-1", live: props.live }),
  92. { initialProps: { live: false } },
  93. );
  94. await waitFor(() => expect(workflowRuns).toHaveBeenCalledTimes(1));
  95. hook.rerender({ live: true });
  96. await waitFor(() => expect(hook.result.current).toBeNull());
  97. expect(workflowRuns).toHaveBeenCalledTimes(1);
  98. });
  99. it("没有 sessionId(草稿会话)时根本不发请求", async () => {
  100. workflowRuns.mockReset();
  101. const hook = renderHook(() => useWorkflowRunJournalSummaries({ sessionId: null, live: true }));
  102. await waitFor(() => expect(hook.result.current).toBeNull());
  103. expect(workflowRuns).not.toHaveBeenCalled();
  104. });
  105. it("enabled=false(嵌套只读 transcript)不发请求:journal 按父会话建键,拿 actor id 去问只会造出幽灵 runtime", async () => {
  106. // Bug 根因(2026-08-24 实测):actor transcript 的嵌套 SessionPane 曾带着 actor 会话 id
  107. // 发这条查询,CLI 的冷会话前置随即对一条**正在运行**的 detached 会话物化第二个 runtime,
  108. // 直播冻结在「已工作 xx 秒」。CLI 侧已按 hasLiveConversation 收口;这里钉住 UI 侧的那半:
  109. // 只读嵌套 pane(actor / subagent transcript)根本不该发出这条父会话专属的查询。
  110. workflowRuns.mockReset();
  111. const hook = renderHook(() =>
  112. useWorkflowRunJournalSummaries({ sessionId: "sess-actor-1", live: true, enabled: false }),
  113. );
  114. await waitFor(() => expect(hook.result.current).toBeNull());
  115. expect(workflowRuns).not.toHaveBeenCalled();
  116. });
  117. it("换会话:丢弃旧摘要并重查——上一个会话的 run 绝不能给新会话的卡片做 join", async () => {
  118. workflowRuns.mockReset();
  119. workflowRuns
  120. .mockResolvedValueOnce(result("dwfrun-1"))
  121. .mockResolvedValueOnce(result("dwfrun-2"));
  122. const hook = renderHook(
  123. (props: { sessionId: string }) =>
  124. useWorkflowRunJournalSummaries({ sessionId: props.sessionId, live: true }),
  125. { initialProps: { sessionId: "sess-1" } },
  126. );
  127. await waitFor(() => expect(hook.result.current?.[0]?.runId).toBe("dwfrun-1"));
  128. hook.rerender({ sessionId: "sess-2" });
  129. await waitFor(() => expect(hook.result.current?.[0]?.runId).toBe("dwfrun-2"));
  130. expect(workflowRuns).toHaveBeenCalledTimes(2);
  131. });
  132. it("切回一个问过的会话:重查一次而不是空着(收口记的是会话 + 答案,不是一个裸的「问过了」)", async () => {
  133. // 收口只记**最近**那个会话及其答案:只记一个 id 而把答案丢在 state 里的话,切回来会
  134. // 两头落空——早退不重查,换会话又清了状态,卡片于是永远没有入口。重查一页 journal 很便宜,
  135. // 空着的入口不便宜。
  136. workflowRuns.mockReset();
  137. workflowRuns
  138. .mockResolvedValueOnce(result("dwfrun-1"))
  139. .mockResolvedValueOnce(result("dwfrun-2"))
  140. .mockResolvedValueOnce(result("dwfrun-1"));
  141. const hook = renderHook(
  142. (props: { sessionId: string }) =>
  143. useWorkflowRunJournalSummaries({ sessionId: props.sessionId, live: true }),
  144. { initialProps: { sessionId: "sess-1" } },
  145. );
  146. await waitFor(() => expect(hook.result.current?.[0]?.runId).toBe("dwfrun-1"));
  147. hook.rerender({ sessionId: "sess-2" });
  148. await waitFor(() => expect(hook.result.current?.[0]?.runId).toBe("dwfrun-2"));
  149. hook.rerender({ sessionId: "sess-1" });
  150. await waitFor(() => expect(hook.result.current?.[0]?.runId).toBe("dwfrun-1"));
  151. expect(workflowRuns).toHaveBeenCalledTimes(3);
  152. });
  153. // ── refreshKey:run 目录页与任务岛计数的新鲜度来源 ──
  154. // 摘要是会话中途也会变的(跑完一个 run 就多一条已结束),但收口语义又必须挡住无谓重查。
  155. // `refreshKey` 就是这条缝:调用方按「已结算 run 的单调计数」给,跑完一个才抬一次。
  156. it("refreshKey 不变:仍然只查一次(渲染再多也不多打 RPC)", async () => {
  157. workflowRuns.mockReset();
  158. workflowRuns.mockResolvedValue(result("dwfrun-1"));
  159. const hook = renderHook(
  160. (props: { refreshKey: number }) =>
  161. useWorkflowRunJournalSummaries({ sessionId: "sess-1", live: true, ...props }),
  162. { initialProps: { refreshKey: 2 } },
  163. );
  164. await waitFor(() => expect(hook.result.current).toHaveLength(1));
  165. hook.rerender({ refreshKey: 2 });
  166. hook.rerender({ refreshKey: 2 });
  167. await waitFor(() => expect(hook.result.current).toHaveLength(1));
  168. expect(workflowRuns).toHaveBeenCalledTimes(1);
  169. });
  170. it("refreshKey 抬升:恰好重取一次并换上新摘要(会话中途跑完一个 run)", async () => {
  171. workflowRuns.mockReset();
  172. workflowRuns
  173. .mockResolvedValueOnce(result("dwfrun-1"))
  174. .mockResolvedValueOnce(result("dwfrun-2"));
  175. const hook = renderHook(
  176. (props: { refreshKey: number }) =>
  177. useWorkflowRunJournalSummaries({ sessionId: "sess-1", live: true, ...props }),
  178. { initialProps: { refreshKey: 0 } },
  179. );
  180. await waitFor(() => expect(hook.result.current?.[0]?.runId).toBe("dwfrun-1"));
  181. hook.rerender({ refreshKey: 1 });
  182. await waitFor(() => expect(hook.result.current?.[0]?.runId).toBe("dwfrun-2"));
  183. expect(workflowRuns).toHaveBeenCalledTimes(2);
  184. });
  185. it("能力缺席在 refreshKey 抬升后**仍然**不重探:那是稳定事实,不是新鲜度问题", async () => {
  186. workflowRuns.mockReset();
  187. workflowRuns.mockRejectedValue(
  188. new Error(
  189. "capability not supported by this session runtime: listDynamicWorkflowRuns (session sess-1)",
  190. ),
  191. );
  192. const hook = renderHook(
  193. (props: { refreshKey: number }) =>
  194. useWorkflowRunJournalSummaries({ sessionId: "sess-1", live: true, ...props }),
  195. { initialProps: { refreshKey: 0 } },
  196. );
  197. await waitFor(() => expect(workflowRuns).toHaveBeenCalledTimes(1));
  198. hook.rerender({ refreshKey: 1 });
  199. hook.rerender({ refreshKey: 2 });
  200. await waitFor(() => expect(hook.result.current).toBeNull());
  201. expect(workflowRuns).toHaveBeenCalledTimes(1);
  202. });
  203. it("limit 原样传给查询;缺省时不带这个键(由 CLI 侧定缺省)", async () => {
  204. workflowRuns.mockReset();
  205. workflowRuns.mockResolvedValue(result("dwfrun-1"));
  206. const withLimit = renderHook(() =>
  207. useWorkflowRunJournalSummaries({ sessionId: "sess-1", live: true, limit: 64 }),
  208. );
  209. await waitFor(() => expect(withLimit.result.current).toHaveLength(1));
  210. expect(workflowRuns).toHaveBeenLastCalledWith({ sessionId: "sess-1", limit: 64 });
  211. const withoutLimit = renderHook(() =>
  212. useWorkflowRunJournalSummaries({ sessionId: "sess-2", live: true }),
  213. );
  214. await waitFor(() => expect(withoutLimit.result.current).toHaveLength(1));
  215. expect(workflowRuns).toHaveBeenLastCalledWith({ sessionId: "sess-2" });
  216. });
  217. });