taskWorkflowRunLines.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // @vitest-environment jsdom
  2. // 侧栏运行行的 DOM(docs/dynamic-workflow/presentation.md「The sidebar run line」):图标 + 迷你轨道 +
  3. // 当前 phase 名;隐含站点;结束行的中性词;确认折叠;点击 = 打开 run(不冒泡成普通选中)。
  4. import { act, cleanup, fireEvent, render } from "@testing-library/react";
  5. import { createElement, type ReactNode } from "react";
  6. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  7. import type {
  8. SessionWorkflowActivity,
  9. SessionWorkflowRunSummary,
  10. } from "@zcode/shared/zcode-protocol-v4";
  11. import { TaskWorkflowRunLines } from "@/components/workflow-run-line/TaskWorkflowRunLines.js";
  12. import { TooltipProvider } from "@/components/ui/tooltip.js";
  13. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  14. import {
  15. createWorkflowRunAckStore,
  16. setWorkflowRunAckStoreForTesting,
  17. type WorkflowRunAckStore,
  18. } from "@/lib/workflowRunAckStore.js";
  19. import {
  20. WorkflowRunOpenProvider,
  21. type WorkflowRunOpenTarget,
  22. } from "@/v4/workflowRunOpenContext.js";
  23. const intl = {
  24. formatMessage: ({ id }: { id: string }, values?: Record<string, string>) => {
  25. switch (id) {
  26. case "chat.toolCall.workflow.graph.phase.workflow":
  27. return "Workflow";
  28. case "chat.toolCall.workflow.run.status.running":
  29. return "Running";
  30. case "chat.toolCall.workflow.run.status.pending":
  31. return "Pending";
  32. case "chat.toolCall.workflow.run.status.completed":
  33. return "Completed";
  34. case "chat.toolCall.workflow.run.status.errored":
  35. return "Errored";
  36. case "chat.toolCall.workflow.run.status.stopped":
  37. return "Stopped";
  38. case "chat.toolCall.workflow.run.stopReason.user":
  39. return "by you";
  40. case "taskList.workflowRun.moreRuns":
  41. return `+${values?.count} more`;
  42. case "taskList.workflowRun.moreStations":
  43. return `+${values?.count}`;
  44. default:
  45. return id;
  46. }
  47. },
  48. };
  49. const SESSION = { workspacePath: "/ws", sessionId: "s1" };
  50. function run(
  51. overrides: Partial<SessionWorkflowRunSummary> & { runId: string },
  52. ): SessionWorkflowRunSummary {
  53. return { status: "running", phases: [], agentsWorking: 0, ...overrides };
  54. }
  55. function wrap(node: ReactNode, onOpenRun?: (target: WorkflowRunOpenTarget) => void) {
  56. const inner = onOpenRun ? createElement(WorkflowRunOpenProvider, { onOpenRun }, node) : node;
  57. return createElement(
  58. ZCodeIntlProvider,
  59. { initialLocale: "en-US" },
  60. createElement(TooltipProvider, null, inner),
  61. );
  62. }
  63. let store: WorkflowRunAckStore;
  64. beforeEach(() => {
  65. store = createWorkflowRunAckStore(null);
  66. setWorkflowRunAckStoreForTesting(store);
  67. });
  68. afterEach(() => {
  69. cleanup();
  70. setWorkflowRunAckStoreForTesting(null);
  71. });
  72. describe("TaskWorkflowRunLines", () => {
  73. it("draws the rail in declared order with the current phase name on the line", () => {
  74. const activity: SessionWorkflowActivity = {
  75. runs: [
  76. run({
  77. runId: "r1",
  78. toolCallId: "tc-1",
  79. name: "Deep research",
  80. phases: [
  81. { name: "Research", status: "done" },
  82. { name: "Draft", status: "running" },
  83. { name: "Review", status: "pending" },
  84. ],
  85. currentPhase: "Draft",
  86. agentsWorking: 2,
  87. }),
  88. ],
  89. };
  90. const { container } = render(
  91. wrap(
  92. createElement(TaskWorkflowRunLines, { activity, isActive: false, intl, session: SESSION }),
  93. ),
  94. );
  95. const line = container.querySelector<HTMLElement>("[data-workflow-run-line]");
  96. expect(line?.dataset.runStatus).toBe("running");
  97. expect(line?.tagName).toBe("SPAN");
  98. expect(line?.textContent).toBe("Draft");
  99. expect(
  100. Array.from(container.querySelectorAll<HTMLElement>("[data-rail-station]")).map(
  101. (station) => station.dataset.railStation,
  102. ),
  103. ).toEqual(["done", "running", "pending"]);
  104. expect(
  105. Array.from(container.querySelectorAll<HTMLElement>("[data-rail-segment]")).map(
  106. (segment) => segment.dataset.railSegment,
  107. ),
  108. ).toEqual(["strong", "faint"]);
  109. expect(container.querySelector("svg")).not.toBeNull();
  110. });
  111. it("a station entered beside the previous one rides a twin segment", () => {
  112. const activity: SessionWorkflowActivity = {
  113. runs: [
  114. run({
  115. runId: "r1",
  116. phases: [
  117. { name: "A", status: "done" },
  118. { name: "B", status: "running", alongside: [0] },
  119. { name: "C", status: "pending" },
  120. ],
  121. currentPhase: "B",
  122. }),
  123. ],
  124. };
  125. const { container } = render(
  126. wrap(createElement(TaskWorkflowRunLines, { activity, isActive: false, intl })),
  127. );
  128. const segments = Array.from(container.querySelectorAll<HTMLElement>("[data-rail-segment]"));
  129. // 段数不变(每站之前一段),墨色规则不变:进 B 的那段是双线,进 C 的那段是普通段。
  130. expect(segments.map((segment) => segment.dataset.railSegment)).toEqual(["strong", "faint"]);
  131. expect(segments.map((segment) => segment.dataset.railTwin)).toEqual(["true", undefined]);
  132. // 双线段真的是两条 1px 线(相距 2px),而不是一条加了属性的线。
  133. expect(segments[0]!.childElementCount).toBe(2);
  134. expect(segments[1]!.childElementCount).toBe(0);
  135. });
  136. it("a script without phases draws the implicit Workflow station", () => {
  137. const { container } = render(
  138. wrap(
  139. createElement(TaskWorkflowRunLines, {
  140. activity: { runs: [run({ runId: "r1" })] },
  141. isActive: false,
  142. intl,
  143. }),
  144. ),
  145. );
  146. expect(container.querySelector("[data-workflow-run-rail]")?.getAttribute("data-implicit")).toBe(
  147. "true",
  148. );
  149. expect(container.querySelector("[data-workflow-run-line]")?.textContent).toBe("Workflow");
  150. });
  151. it("past six stations the rail folds to five lamps and a +n tail", () => {
  152. const phases = Array.from({ length: 9 }, (_, index) => ({
  153. name: `p${index}`,
  154. status:
  155. index < 4 ? ("done" as const) : index === 4 ? ("running" as const) : ("pending" as const),
  156. }));
  157. const { container } = render(
  158. wrap(
  159. createElement(TaskWorkflowRunLines, {
  160. activity: { runs: [run({ runId: "r1", phases, currentPhase: "p4" })] },
  161. isActive: false,
  162. intl,
  163. }),
  164. ),
  165. );
  166. expect(container.querySelectorAll("[data-rail-station]")).toHaveLength(5);
  167. expect(container.querySelector("[data-workflow-run-rail]")?.textContent).toBe("+4");
  168. });
  169. it("ended runs show a neutral word, and linger only until acknowledged", () => {
  170. const activity: SessionWorkflowActivity = {
  171. runs: [
  172. run({ runId: "err", status: "errored", currentPhase: "Draft" }),
  173. run({ runId: "stop", status: "stopped", stopReason: "user" }),
  174. run({ runId: "done", status: "completed" }),
  175. ],
  176. };
  177. const { container, rerender } = render(
  178. wrap(createElement(TaskWorkflowRunLines, { activity, isActive: false, intl })),
  179. );
  180. const texts = () =>
  181. Array.from(container.querySelectorAll("[data-workflow-run-line]")).map(
  182. (line) => line.textContent,
  183. );
  184. expect(texts()).toEqual(["Errored · Draft", "Stopped · by you"]);
  185. expect(container.querySelector("[data-workflow-run-overflow]")?.textContent).toBe("+1 more");
  186. act(() => {
  187. store.acknowledge(["err"]);
  188. });
  189. expect(texts()).toEqual(["Stopped · by you", "Completed"]);
  190. // 会话被打开:剩下的已结束 run 整批确认,行随之消失。
  191. rerender(wrap(createElement(TaskWorkflowRunLines, { activity, isActive: true, intl })));
  192. expect(store.isAcknowledged("stop")).toBe(true);
  193. expect(store.isAcknowledged("done")).toBe(true);
  194. expect(container.querySelector("[data-workflow-run-lines]")).toBeNull();
  195. });
  196. it("a live run is never hidden by the acknowledgement set", () => {
  197. store.acknowledge(["live"]);
  198. const { container } = render(
  199. wrap(
  200. createElement(TaskWorkflowRunLines, {
  201. activity: { runs: [run({ runId: "live" })] },
  202. isActive: true,
  203. intl,
  204. }),
  205. ),
  206. );
  207. expect(container.querySelector("[data-workflow-run-line]")).not.toBeNull();
  208. });
  209. it("with an open handler the line is a button that opens the run without bubbling to the row", () => {
  210. const opened: WorkflowRunOpenTarget[] = [];
  211. const rowClicks = vi.fn();
  212. const activity: SessionWorkflowActivity = {
  213. runs: [run({ runId: "r1", toolCallId: "tc-1", name: "Deep research" })],
  214. };
  215. const { container } = render(
  216. wrap(
  217. createElement(
  218. "div",
  219. { onClick: rowClicks },
  220. createElement(TaskWorkflowRunLines, {
  221. activity,
  222. isActive: false,
  223. intl,
  224. session: { workspacePath: "/ws", workspaceIdentity: "id-1", sessionId: "s1" },
  225. }),
  226. ),
  227. (target) => opened.push(target),
  228. ),
  229. );
  230. const line = container.querySelector<HTMLElement>("[data-workflow-run-line]");
  231. expect(line?.tagName).toBe("BUTTON");
  232. fireEvent.click(line!);
  233. expect(rowClicks).not.toHaveBeenCalled();
  234. expect(opened).toEqual([
  235. {
  236. workspacePath: "/ws",
  237. workspaceIdentity: "id-1",
  238. sessionId: "s1",
  239. run: activity.runs[0],
  240. },
  241. ]);
  242. });
  243. });