workflowRunDigest.test.ts 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. // @vitest-environment jsdom
  2. // 轮尾摘要(docs/dynamic-workflow/presentation.md「The run card」):两态、默认收起——收起是表头 +
  3. // 阶段线,展开是药丸落到站下;冷态只剩一行。交互(chevron、⤢、Resume、芯片、药丸)走真实 DOM。
  4. import { cleanup, fireEvent, render } from "@testing-library/react";
  5. import { createElement, Profiler, type ReactNode } from "react";
  6. import { afterEach, describe, expect, it, vi } from "vitest";
  7. import type { WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  8. import type { WorkflowRunCardSummary } from "@/ToolCallBlocks/shared.js";
  9. import type { TimelinePill } from "@/components/workflow-timeline/timeline-model.js";
  10. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  11. // 提示只渲染 children(不改 DOM 形状),但把 title / description 记下来:Stop 钮的第二行
  12. // 「停止后可以随时恢复」只活在提示里,是这次改名要钉住的那半句话。
  13. const tooltips = vi.hoisted(() => ({
  14. calls: [] as Array<{ title: unknown; description?: string }>,
  15. }));
  16. vi.mock("@/ControlHintTooltip.js", () => ({
  17. ControlHintTooltip: ({
  18. children,
  19. title,
  20. description,
  21. }: {
  22. children?: ReactNode;
  23. title?: unknown;
  24. description?: string;
  25. }) => {
  26. tooltips.calls.push({ title, ...(description === undefined ? {} : { description }) });
  27. return children;
  28. },
  29. }));
  30. // 接线层的卡只读 workspace 作用域算子代理模型的 provider 名;这里不验证那行字,给一个空实现
  31. // 免去为它搭一整份 session store。
  32. vi.mock("@/hooks/useWorkflowSubagentModelProviderName.js", () => ({
  33. useWorkflowSubagentModelProviderName: () => undefined,
  34. }));
  35. // eslint-disable-next-line import/first -- 必须在 tooltip mock 之后再引入被测组件。
  36. import {
  37. WorkflowRunDigest,
  38. type WorkflowRunDigestProps,
  39. } from "@/components/workflow-timeline/WorkflowRunDigest.js";
  40. // eslint-disable-next-line import/first -- 同上。
  41. import { ConversationWorkflowDigests } from "@/v4/ConversationWorkflowDigests.js";
  42. // eslint-disable-next-line import/first -- 同上。
  43. import type { ConversationRowRenderContext } from "@/v4/conversationRowContext.js";
  44. // eslint-disable-next-line import/first -- 同上;词条只用于双语言存在性断言。
  45. import enUS from "../src/i18n/locales/en-US.js";
  46. // eslint-disable-next-line import/first -- 同上。
  47. import zhCN from "../src/i18n/locales/zh-CN.js";
  48. afterEach(() => {
  49. cleanup();
  50. tooltips.calls.length = 0;
  51. });
  52. // plan → verify,verify 回到 plan(与工具卡测试同一夹具)。
  53. const LOOP_GRAPH = {
  54. steps: [
  55. {
  56. id: "ask#1",
  57. kind: "ask",
  58. label: "plan",
  59. line: 2,
  60. column: 21,
  61. lane: "actor#1",
  62. phase: "phase#a",
  63. },
  64. {
  65. id: "ask#2",
  66. kind: "ask",
  67. label: "verify",
  68. line: 5,
  69. column: 21,
  70. lane: "actor#2",
  71. phase: "phase#b",
  72. },
  73. ],
  74. lanes: [
  75. { id: "actor#1", name: "planner", line: 1, column: 11 },
  76. { id: "actor#2", name: "checker", line: 4, column: 11 },
  77. ],
  78. participants: [
  79. { id: "phase#a:actor#1", phase: "phase#a", lane: "actor#1", steps: ["ask#1"] },
  80. { id: "phase#b:actor#2", phase: "phase#b", lane: "actor#2", steps: ["ask#2"] },
  81. ],
  82. handoffs: [],
  83. phases: [
  84. { id: "phase#a", name: "plan", line: 1 },
  85. { id: "phase#b", name: "verify", line: 4 },
  86. ],
  87. phaseEdges: [
  88. { from: "phase#a", to: "phase#b" },
  89. { from: "phase#b", to: "phase#a", back: true },
  90. ],
  91. exits: ["phase#b"],
  92. } as const;
  93. function runState(overrides: Partial<WorkflowRunState> = {}): WorkflowRunState {
  94. return {
  95. runId: "dwfrun-42",
  96. toolCallId: "tool-create-workflow",
  97. status: "running",
  98. usage: { spentTokens: 1_234, nodesUsed: 2 },
  99. actors: [
  100. { siteId: "actor#1", ordinal: 1, name: "Ada", sessionId: "s-1", status: "completed" },
  101. { siteId: "actor#2", ordinal: 1, name: "Bob", sessionId: "s-2", status: "running" },
  102. ],
  103. nodes: [
  104. {
  105. siteId: "ask#1",
  106. ordinal: 1,
  107. phase: "settled",
  108. outcome: "ok",
  109. actorSiteId: "actor#1",
  110. actorOrdinal: 1,
  111. },
  112. { siteId: "ask#2", ordinal: 1, phase: "executing", actorSiteId: "actor#2", actorOrdinal: 1 },
  113. ] as WorkflowRunState["nodes"],
  114. lastEventSequence: 7,
  115. ...overrides,
  116. };
  117. }
  118. function joined(
  119. overrides: Partial<WorkflowRunState> = {},
  120. extra: Partial<WorkflowRunCardSummary> = {},
  121. ): WorkflowRunCardSummary {
  122. const run = runState(overrides);
  123. return {
  124. runId: run.runId,
  125. toolCallId: run.toolCallId,
  126. status: run.status,
  127. nodesSettled: run.nodes.filter((node) => node.phase === "settled").length,
  128. nodesTotal: run.nodes.length,
  129. run,
  130. ...extra,
  131. };
  132. }
  133. function renderDigest(
  134. props: Partial<WorkflowRunDigestProps> & { summary: WorkflowRunCardSummary | undefined },
  135. locale: "en-US" | "zh-CN" = "en-US",
  136. ) {
  137. return render(
  138. createElement(
  139. ZCodeIntlProvider,
  140. { initialLocale: locale },
  141. createElement(WorkflowRunDigest, {
  142. graph: LOOP_GRAPH as unknown as WorkflowRunDigestProps["graph"],
  143. name: "implement-verify",
  144. runId: props.summary?.runId ?? "dwfrun-42",
  145. testIdKey: "t",
  146. ...props,
  147. }),
  148. ),
  149. );
  150. }
  151. // run 不在活投影里(八条上限淘汰 / 冷恢复无 journal 命中)的中性卡(docs/dynamic-workflow/presentation.md
  152. // 「The run card」):凡点名了 runId 的来源都出卡,联接不到就退成单行——种类词说「已结束」而不冒充终态。
  153. describe("WorkflowRunDigest 中性卡(summary 缺席)", () => {
  154. it("单行:种类词「Workflow ended」,无灯无轨道无 Cancel / Resume,⤢ 仍在;status 属性为 absent", () => {
  155. const onOpenRun = vi.fn();
  156. const view = renderDigest({
  157. summary: undefined,
  158. onOpenRun,
  159. onResume: vi.fn(),
  160. onCancel: vi.fn(),
  161. });
  162. const root = view.container.querySelector("[data-workflow-run-digest]")!;
  163. expect(root.getAttribute("data-workflow-run-id")).toBe("dwfrun-42");
  164. expect(root.getAttribute("data-workflow-run-status")).toBe("absent");
  165. expect(root.getAttribute("role")).toBeNull();
  166. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow ended");
  167. expect(view.getByTestId("workflow-card-name").textContent).toBe("implement-verify");
  168. expect(view.queryByTestId("workflow-digest-plot")).toBeNull();
  169. expect(view.queryByTestId("workflow-card-detail")).toBeNull();
  170. expect(view.queryByTestId("workflow-digest-cancel")).toBeNull();
  171. expect(view.queryByTestId("workflow-digest-resume")).toBeNull();
  172. fireEvent.click(view.getByTestId("workflow-card-open-details"));
  173. expect(onOpenRun).toHaveBeenCalledTimes(1);
  174. });
  175. it("双语言词条在场", () => {
  176. expect(enUS["chat.toolCall.workflow.card.ended"]).toBe("Workflow ended");
  177. expect(zhCN["chat.toolCall.workflow.card.ended"]).toBe("工作流已结束");
  178. const view = renderDigest({ summary: undefined }, "zh-CN");
  179. expect(view.getByTestId("workflow-card-kind").textContent).toBe("工作流已结束");
  180. });
  181. });
  182. // ── 名册站上「还有 n 个」那一行(docs/dynamic-workflow/presentation.md「Past six participants」)──
  183. // 桌面实测:轮尾摘要里那一行原本是静态 span,点它冒泡到整块开关、把卡折起来了。它是一扇门:开 run
  184. // 详情、落到这一站;站头照旧是开关的一部分。
  185. describe("WorkflowRunDigest 「还有 n 个」", () => {
  186. const FANOUT_GRAPH = {
  187. steps: [
  188. {
  189. id: "ask#1",
  190. kind: "ask",
  191. label: "review",
  192. line: 3,
  193. column: 21,
  194. lane: "actor#1",
  195. phase: "phase#a",
  196. },
  197. ],
  198. lanes: [{ id: "actor#1", namePattern: { head: "reviewer-" }, line: 2, column: 11 }],
  199. participants: [
  200. { id: "phase#a:actor#1", phase: "phase#a", lane: "actor#1", steps: ["ask#1"], many: true },
  201. ],
  202. handoffs: [],
  203. phases: [{ id: "phase#a", name: "review", line: 1 }],
  204. phaseEdges: [],
  205. exits: ["phase#a"],
  206. } as const;
  207. function fanout(n: number): WorkflowRunCardSummary {
  208. const actors: WorkflowRunState["actors"] = [];
  209. const nodes: WorkflowRunState["nodes"] = [];
  210. for (let i = 1; i <= n; i += 1) {
  211. actors.push({
  212. siteId: "actor#1",
  213. ordinal: i,
  214. name: `reviewer-${i}`,
  215. sessionId: `s-${i}`,
  216. status: "running",
  217. });
  218. nodes.push({
  219. siteId: "ask#1",
  220. ordinal: i,
  221. phase: "executing",
  222. actorSiteId: "actor#1",
  223. actorOrdinal: i,
  224. } as WorkflowRunState["nodes"][number]);
  225. }
  226. return joined({ actors, nodes, usage: { spentTokens: 10, nodesUsed: n } });
  227. }
  228. const renderFanout = (props: Partial<WorkflowRunDigestProps>) =>
  229. renderDigest({
  230. graph: FANOUT_GRAPH as unknown as WorkflowRunDigestProps["graph"],
  231. summary: fanout(9),
  232. ...props,
  233. });
  234. it("有 onOpenRun 时那一行是按钮:点它开 run 详情、带站 id,不切换折叠;站头仍是开关的一部分", () => {
  235. const onOpenRun = vi.fn();
  236. const view = renderFanout({ onOpenRun });
  237. const root = view.container.querySelector("[data-workflow-run-digest]")!;
  238. expect(root.getAttribute("data-expanded")).toBe("true");
  239. const row = view.getByTestId("workflow-roster-more-row");
  240. expect(row.tagName).toBe("BUTTON");
  241. fireEvent.click(row);
  242. expect(onOpenRun).toHaveBeenCalledWith({ phaseId: "phase#a" });
  243. expect(root.getAttribute("data-expanded")).toBe("true");
  244. // 站头没有回调:span,点它切换折叠(追记「点击语法修订」)。
  245. const head = view.getByTestId("workflow-timeline-station-head");
  246. expect(head.tagName).toBe("SPAN");
  247. fireEvent.click(head);
  248. expect(root.getAttribute("data-expanded")).toBe("false");
  249. expect(onOpenRun).toHaveBeenCalledTimes(1);
  250. // ⤢ 开整个 run,不带落点。
  251. fireEvent.click(view.getByTestId("workflow-card-open-details"));
  252. expect(onOpenRun).toHaveBeenLastCalledWith();
  253. });
  254. it("没有 onOpenRun 时那一行是静态的 span,点它与点空白一样切换折叠", () => {
  255. const view = renderFanout({});
  256. const root = view.container.querySelector("[data-workflow-run-digest]")!;
  257. const row = view.getByTestId("workflow-roster-more-row");
  258. expect(row.tagName).toBe("SPAN");
  259. fireEvent.click(row);
  260. expect(root.getAttribute("data-expanded")).toBe("false");
  261. });
  262. });
  263. // ── 子代理模型(docs/dynamic-workflow/presentation.md「The run card」)──
  264. // 细节串已经在说「几个子代理」,模型名跟在它后面当最后一段,同一段淡色文字——不加芯片、不加
  265. // 前缀。强度与规范串在 tooltip 里。没指定过模型的 run 没有这一段。
  266. describe("WorkflowRunDigest 子代理模型", () => {
  267. // 团队套餐的 provider id 是一串十六进制:这次改动要挡的就是它。
  268. const TEAM_PLAN_MODEL = "4fc7f541-2382-4c32-be11-f021b8d7ed1d/GLM-5.3-Flash$high";
  269. it("模型名是细节串的最后一段;provider id 不上屏,规范串与强度进 tooltip", () => {
  270. const view = renderDigest({ summary: joined({ subagentModel: TEAM_PLAN_MODEL }) });
  271. const detail = view.getByTestId("workflow-card-detail");
  272. expect(detail.textContent).toBe("2 phases · 1 agent working · GLM-5.3-Flash");
  273. expect(document.body.textContent).not.toContain("4fc7f541");
  274. expect(detail.getAttribute("title")).toContain(TEAM_PLAN_MODEL);
  275. expect(detail.getAttribute("title")).toContain("thinking High");
  276. });
  277. it("宿主给得出 provider 名时才拼「名字/模型」", () => {
  278. const view = renderDigest({
  279. subagentModelProviderName: (providerId) => (providerId === "myproxy" ? "MyProxy" : undefined),
  280. summary: joined({ subagentModel: "myproxy/glm-4.7$medium" }),
  281. });
  282. expect(view.getByTestId("workflow-card-detail").textContent).toBe(
  283. "2 phases · 1 agent working · MyProxy/glm-4.7",
  284. );
  285. });
  286. it("没指定过模型的 run:细节串一个字不变,也没有 tooltip", () => {
  287. const view = renderDigest({ summary: joined() });
  288. const detail = view.getByTestId("workflow-card-detail");
  289. expect(detail.textContent).toBe("2 phases · 1 agent working");
  290. expect(detail.getAttribute("title")).toBeNull();
  291. });
  292. });
  293. // 控件与细节串的 DOM 归属。
  294. describe("WorkflowRunDigest 表头控件归属", () => {
  295. const LONG_MODEL = "4fc7f541-2382-4c32-be11-f021b8d7ed1d/GLM-5.3-Flash$high";
  296. function runningCard() {
  297. return renderDigest({
  298. onCancel: vi.fn(),
  299. onOpenRun: vi.fn(),
  300. settingsHost: { workspacePath: "/w", apply: vi.fn() },
  301. summary: joined({ subagentModel: LONG_MODEL }),
  302. });
  303. }
  304. it("Configure、Stop、详情按钮与细节串位于同一右簇", () => {
  305. const view = runningCard();
  306. const cluster = view.getByTestId("workflow-card-detail").parentElement!;
  307. // 三个控件与细节串同处一簇。
  308. for (const id of [
  309. "workflow-digest-configure",
  310. "workflow-digest-cancel",
  311. "workflow-card-open-details",
  312. ]) {
  313. expect(cluster.querySelector(`[data-testid="${id}"]`)).not.toBeNull();
  314. }
  315. });
  316. });
  317. describe("WorkflowRunDigest 收起态(默认)", () => {
  318. it("表头 + 阶段线:种类词、名字、灯 + 状态词、不含 tokens 的摘要;站与弧在场,药丸不在", () => {
  319. const view = renderDigest({ summary: joined() });
  320. const root = view.getByTestId("workflow-run-digest-t");
  321. expect(root.getAttribute("data-expanded")).toBe("true");
  322. expect(root.getAttribute("data-workflow-run-status")).toBe("running");
  323. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow running");
  324. expect(view.getByTestId("workflow-card-name").textContent).toBe("implement-verify");
  325. expect(view.queryByTestId("workflow-digest-status")).toBeNull();
  326. const detail = view.getByTestId("workflow-card-detail").textContent!;
  327. expect(detail).toBe("2 phases · 1 agent working");
  328. // 运行中的种类词扫光,wf-swap 包在渐变外面(round 7 的教训)。
  329. expect(root.querySelector(".wf-swap .animated-gradient-text")).toBeTruthy();
  330. expect(view.getAllByTestId("workflow-timeline-station")).toHaveLength(2);
  331. expect(view.getByTestId("workflow-timeline-arc")).toBeTruthy();
  332. expect(view.queryAllByTestId("workflow-agent-pill")).toHaveLength(2);
  333. expect(view.queryByTestId("workflow-card-footer")).toBeNull();
  334. // 没有回调就没有 ⤢、没有 Resume(回调的存在即门控)。
  335. expect(view.queryByTestId("workflow-card-open-details")).toBeNull();
  336. expect(view.queryByTestId("workflow-digest-resume")).toBeNull();
  337. });
  338. it("无箭头仍可点击及键盘折叠,子控件不切换", () => {
  339. const onOpenRun = vi.fn();
  340. const view = renderDigest({ summary: joined(), onOpenRun });
  341. const root = view.getByTestId("workflow-run-digest-t");
  342. expect(view.queryByTestId("workflow-card-toggle")).toBeNull();
  343. expect(root.getAttribute("role")).toBe("button");
  344. fireEvent.click(root);
  345. expect(view.queryAllByTestId("workflow-agent-pill")).toHaveLength(0);
  346. fireEvent.keyDown(root, { key: "Enter" });
  347. expect(view.getAllByTestId("workflow-agent-pill")).toHaveLength(2);
  348. fireEvent.click(view.getByTestId("workflow-card-open-details"));
  349. expect(onOpenRun).toHaveBeenCalledOnce();
  350. });
  351. it("还没启动的子代理药丸与活药丸同一套点击语法:按钮、↗,点它交出槽位而不切换整块", () => {
  352. // docs/dynamic-workflow/presentation.md「The pill」:此前它是 span,点击冒泡到整块
  353. // 开关,把卡折起来。
  354. const onOpenPill = vi.fn();
  355. const view = renderDigest({
  356. summary: joined({
  357. actors: [
  358. { siteId: "actor#1", ordinal: 1, name: "Ada", sessionId: "s-1", status: "completed" },
  359. ],
  360. }),
  361. onOpenPill,
  362. });
  363. const root = view.getByTestId("workflow-run-digest-t");
  364. const pills = view.getAllByTestId("workflow-agent-pill");
  365. expect(pills.map((pill) => pill.tagName)).toEqual(["BUTTON", "BUTTON"]);
  366. expect(pills[1]!.className).toContain("wf-pill-open");
  367. expect(pills[1]!.querySelector('[data-testid="workflow-timeline-pill-open"]')).not.toBeNull();
  368. fireEvent.click(pills[1]!);
  369. expect(onOpenPill).toHaveBeenCalledTimes(1);
  370. const handed = onOpenPill.mock.calls[0]![0] as TimelinePill;
  371. expect(handed.slot).toEqual({ siteId: "actor#2", ordinal: 1 });
  372. expect(handed.instance).toBeUndefined();
  373. expect(root.getAttribute("data-expanded")).toBe("true");
  374. });
  375. it("⤢ 与待答问题芯片都开 run 详情;芯片按数量计数、无回调时不可点", () => {
  376. const onOpenRun = vi.fn();
  377. const view = renderDigest({ summary: joined(), onOpenRun, pendingQuestions: 1 });
  378. fireEvent.click(view.getByTestId("workflow-card-open-details"));
  379. expect(onOpenRun).toHaveBeenCalledTimes(1);
  380. const chip = view.getByTestId("workflow-digest-questions");
  381. expect(chip.tagName).toBe("BUTTON");
  382. expect(chip.textContent).toBe("1 question");
  383. fireEvent.click(chip);
  384. expect(onOpenRun).toHaveBeenCalledTimes(2);
  385. cleanup();
  386. const inert = renderDigest({ summary: joined(), pendingQuestions: 2 });
  387. const span = inert.getByTestId("workflow-digest-questions");
  388. expect(span.tagName).toBe("SPAN");
  389. expect(span.textContent).toBe("2 questions");
  390. cleanup();
  391. expect(
  392. renderDigest({ summary: joined() }).queryByTestId("workflow-digest-questions"),
  393. ).toBeNull();
  394. });
  395. it("失败且可恢复:Resume 进表头,与详情页同一条命令;不可恢复或无回调时没有", () => {
  396. const onResume = vi.fn();
  397. const view = renderDigest({
  398. summary: joined({ status: "stopped", stopReason: "interrupted" }, { resumable: true }),
  399. onResume,
  400. });
  401. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow stopped");
  402. expect(view.getByTestId("workflow-digest-resume").getAttribute("data-size")).toBe("default");
  403. fireEvent.click(view.getByTestId("workflow-digest-resume"));
  404. expect(onResume).toHaveBeenCalledTimes(1);
  405. cleanup();
  406. expect(
  407. renderDigest({ summary: joined({ status: "errored" }), onResume }).queryByTestId(
  408. "workflow-digest-resume",
  409. ),
  410. ).toBeNull();
  411. });
  412. });
  413. describe("WorkflowRunDigest 产物条", () => {
  414. const ARTIFACTS = [
  415. { id: "book", kind: "file", title: "Audit report", version: 2 },
  416. { id: "perf", kind: "chart", title: "Round timings", version: 1 },
  417. ] as unknown as WorkflowRunState["artifacts"];
  418. it("收起态与展开态都挂产物条;点药丸交出产物 id,不切换整块", () => {
  419. const onOpenArtifact = vi.fn();
  420. const view = renderDigest({
  421. summary: joined({ status: "completed", artifacts: ARTIFACTS }),
  422. onOpenArtifact,
  423. });
  424. const root = view.getByTestId("workflow-run-digest-t");
  425. const strip = view.getByTestId("workflow-digest-artifacts");
  426. const pills = view.getAllByTestId("workflow-digest-artifact");
  427. expect(pills).toHaveLength(2);
  428. expect(pills[0]?.tagName).toBe("BUTTON");
  429. expect(pills[0]?.getAttribute("data-pill-size")).toBe("md");
  430. // 尾槽里的 v2 只在第 2 版的那枚上。
  431. expect(strip.querySelectorAll('[data-testid="workflow-run-artifact-version"]')).toHaveLength(1);
  432. expect(root.getAttribute("data-expanded")).toBe("true");
  433. fireEvent.click(pills[1]!);
  434. expect(onOpenArtifact).toHaveBeenCalledWith("perf");
  435. expect(root.getAttribute("data-expanded")).toBe("true");
  436. // 条上的空白处**是**整块开关的一部分(追记「点击语法修订」):点它切换,产物条仍在。
  437. fireEvent.click(strip);
  438. expect(root.getAttribute("data-expanded")).toBe("false");
  439. expect(view.getAllByTestId("workflow-digest-artifact")).toHaveLength(2);
  440. fireEvent.click(strip);
  441. expect(root.getAttribute("data-expanded")).toBe("true");
  442. });
  443. it("禁用的产物药丸(无回调)不透传:点它既不打开也不切换整块", () => {
  444. const view = renderDigest({ summary: joined({ status: "completed", artifacts: ARTIFACTS }) });
  445. const root = view.getByTestId("workflow-run-digest-t");
  446. const pill = view.getAllByTestId("workflow-digest-artifact")[0] as HTMLButtonElement;
  447. expect(pill.disabled).toBe(true);
  448. fireEvent.click(pill);
  449. expect(root.getAttribute("data-expanded")).toBe("true");
  450. });
  451. it("没有产物或没有活投影时条缺席;没有回调时药丸禁用", () => {
  452. expect(
  453. renderDigest({ summary: joined() }).queryByTestId("workflow-digest-artifacts"),
  454. ).toBeNull();
  455. cleanup();
  456. const cold = renderDigest({ summary: joined({ artifacts: ARTIFACTS }, { run: undefined }) });
  457. expect(cold.queryByTestId("workflow-digest-artifacts")).toBeNull();
  458. cleanup();
  459. const inert = renderDigest({ summary: joined({ artifacts: ARTIFACTS }) });
  460. const pill = inert.getAllByTestId("workflow-digest-artifact")[0] as HTMLButtonElement;
  461. expect(pill.disabled).toBe(true);
  462. });
  463. });
  464. // 实例表撞界(docs/dynamic-workflow/presentation.md「The run card」):表头的计数已经把表外的
  465. // 实例算进来了,时间线却只画得出表内那些——时间线下的这一句说的正是这个差额。
  466. describe("WorkflowRunDigest 撞界提示", () => {
  467. const truncated = (overrides: Partial<WorkflowRunState> = {}) =>
  468. joined({
  469. usage: { spentTokens: 10, nodesUsed: 2, nodesUnlisted: 1_500, nodesUnlistedSettled: 900 },
  470. truncated: true,
  471. ...overrides,
  472. });
  473. it("收起态与展开态都在:「仅展示 2/1502 步的详情」", () => {
  474. const view = renderDigest({ summary: truncated() });
  475. const root = view.getByTestId("workflow-run-digest-t");
  476. expect(view.getByTestId("workflow-digest-truncated").textContent).toBe(
  477. "Details shown for 2 of 1502 steps",
  478. );
  479. fireEvent.click(root);
  480. expect(root.getAttribute("data-expanded")).toBe("false");
  481. expect(view.getByTestId("workflow-digest-truncated")).not.toBeNull();
  482. });
  483. it("没撞界、只有小表撞界、没有活投影、以及没有轨道的单行卡上都没有这一句", () => {
  484. expect(
  485. renderDigest({ summary: joined() }).queryByTestId("workflow-digest-truncated"),
  486. ).toBeNull();
  487. cleanup();
  488. // `truncated` 也会被 reports / artifacts / phases 触界置位;那时步数一个不少,没有可说的。
  489. expect(
  490. renderDigest({ summary: joined({ truncated: true }) }).queryByTestId(
  491. "workflow-digest-truncated",
  492. ),
  493. ).toBeNull();
  494. cleanup();
  495. expect(
  496. renderDigest({ summary: truncated({}), graph: undefined }).queryByTestId(
  497. "workflow-digest-truncated",
  498. ),
  499. ).toBeNull();
  500. cleanup();
  501. expect(
  502. renderDigest({ summary: joined({ truncated: true }, { run: undefined }) }).queryByTestId(
  503. "workflow-digest-truncated",
  504. ),
  505. ).toBeNull();
  506. });
  507. it("zh-CN 念「仅展示 2/1502 步的详情」", () => {
  508. const view = renderDigest({ summary: truncated() }, "zh-CN");
  509. expect(view.getByTestId("workflow-digest-truncated").textContent).toBe(
  510. "仅展示 2/1502 步的详情",
  511. );
  512. });
  513. });
  514. describe("WorkflowRunDigest 冷态", () => {
  515. it("没有活投影:一行——种类词 + 名字 + 状态词,没有计数、没有阶段线、没有 chevron", () => {
  516. const summary: WorkflowRunCardSummary = {
  517. runId: "dwfrun-42",
  518. toolCallId: "tool-create-workflow",
  519. status: "completed",
  520. nodesSettled: 2,
  521. nodesTotal: 2,
  522. };
  523. const view = renderDigest({ summary, onOpenRun: vi.fn() });
  524. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow completed");
  525. expect(view.queryByTestId("workflow-digest-status")).toBeNull();
  526. expect(view.queryByTestId("workflow-card-detail")).toBeNull();
  527. expect(view.queryByTestId("workflow-digest-plot")).toBeNull();
  528. expect(view.queryByTestId("workflow-card-toggle")).toBeNull();
  529. const root = view.getByTestId("workflow-run-digest-t");
  530. expect(root.hasAttribute("data-expanded")).toBe(false);
  531. // 没有阶段线就没有开关:整块不是按钮。
  532. expect(root.hasAttribute("role")).toBe(false);
  533. expect(view.getByTestId("workflow-card-open-details")).toBeTruthy();
  534. });
  535. it("行窗口里没有图(resume 单独成轮):即使有活投影也只剩一行", () => {
  536. const view = renderDigest({ summary: joined(), graph: undefined });
  537. expect(view.queryByTestId("workflow-card-detail")).toBeNull();
  538. expect(view.queryByTestId("workflow-digest-plot")).toBeNull();
  539. expect(view.queryByTestId("workflow-card-toggle")).toBeNull();
  540. });
  541. });
  542. describe("WorkflowRunDigest 文案", () => {
  543. it("zh-CN 双写:芯片与 chevron 的词条在两种语言里都在", () => {
  544. for (const id of [
  545. "chat.toolCall.workflow.digest.question",
  546. "chat.toolCall.workflow.digest.questions",
  547. "chat.toolCall.workflow.digest.showAgents",
  548. "chat.toolCall.workflow.digest.hideAgents",
  549. ]) {
  550. expect(enUS[id as keyof typeof enUS]).toBeTruthy();
  551. expect(zhCN[id as keyof typeof zhCN]).toBeTruthy();
  552. }
  553. const view = renderDigest({ summary: joined(), pendingQuestions: 1 }, "zh-CN");
  554. expect(view.getByTestId("workflow-card-kind").textContent).toBe("工作流运行中");
  555. expect(view.getByTestId("workflow-digest-questions").textContent).toBe("1 个待答问题");
  556. expect(view.queryByTestId("workflow-card-toggle")).toBeNull();
  557. });
  558. });
  559. // 运行卡上的 Stop run(docs/dynamic-workflow/presentation.md):running 时
  560. // Resume 的位置换成 Stop,走详情页同一条命令;点下即禁用直到状态变化。
  561. describe("WorkflowRunDigest Stop run", () => {
  562. it("running + onCancel:Stop 进表头、可点;点后禁用并换成 Stopping…", () => {
  563. const onCancel = vi.fn();
  564. const view = renderDigest({ summary: joined(), onCancel });
  565. const cancel = view.getByTestId("workflow-digest-cancel");
  566. // 与 ⤢ 同款:ghost 图标钮,文字只在 aria-label / 提示里。
  567. expect(cancel.getAttribute("aria-label")).toBe("Stop run");
  568. expect(cancel.textContent).toBe("");
  569. expect(cancel.getAttribute("data-size")).toBe("icon-md");
  570. expect(cancel.getAttribute("data-variant")).toBe("ghost");
  571. expect(view.queryByTestId("workflow-digest-resume")).toBeNull();
  572. fireEvent.click(cancel);
  573. expect(onCancel).toHaveBeenCalledTimes(1);
  574. expect((cancel as HTMLButtonElement).disabled).toBe(true);
  575. expect(cancel.getAttribute("aria-label")).toBe("Stopping…");
  576. // 点 Cancel 不触发卡片折叠(按钮被 hitsControl 排除)。
  577. expect(
  578. view.container.querySelector("[data-workflow-run-digest]")?.getAttribute("data-expanded"),
  579. ).toBe("true");
  580. });
  581. it("Stopping… 的复位不再是一轮 effect setState:状态变化只提交一次", () => {
  582. // 修订 2026-09-12:复位曾是按 status 跑的 effect 里的 setCancelling(false)——每次状态变化都在
  583. // 投影帧的同步提交之后再补一笔更新(与工作流卡崩溃的抛点同形)。现在 Cancel 钮按 status 键控
  584. // 重挂,复位是重挂本身,状态变化那一帧只提交一次。用 Profiler 数提交。
  585. const onCancel = vi.fn();
  586. const onResume = vi.fn();
  587. let commits = 0;
  588. const tree = (summary: WorkflowRunCardSummary) =>
  589. createElement(
  590. Profiler,
  591. { id: "digest", onRender: () => (commits += 1) },
  592. createElement(
  593. ZCodeIntlProvider,
  594. { initialLocale: "en-US" },
  595. createElement(WorkflowRunDigest, {
  596. graph: LOOP_GRAPH as unknown as WorkflowRunDigestProps["graph"],
  597. name: "implement-verify",
  598. testIdKey: "t",
  599. summary,
  600. onCancel,
  601. onResume,
  602. }),
  603. ),
  604. );
  605. const view = render(tree(joined()));
  606. fireEvent.click(view.getByTestId("workflow-digest-cancel"));
  607. expect(view.getByTestId("workflow-digest-cancel").getAttribute("aria-label")).toBe("Stopping…");
  608. // 同状态的新帧:按钮保持 Stopping…。
  609. view.rerender(tree(joined({ status: "running" }, { nodesSettled: 3 })));
  610. expect((view.getByTestId("workflow-digest-cancel") as HTMLButtonElement).disabled).toBe(true);
  611. commits = 0;
  612. view.rerender(tree(joined({ status: "stopped" }, { resumable: true })));
  613. expect(commits).toBe(1);
  614. expect(view.queryByTestId("workflow-digest-cancel")).toBeNull();
  615. expect(view.getByTestId("workflow-digest-resume")).toBeTruthy();
  616. expect(onCancel).toHaveBeenCalledTimes(1);
  617. });
  618. it("状态一变按钮复位;stopped + resumable 时原位是 Resume;无回调或非 running 不渲染", () => {
  619. const onCancel = vi.fn();
  620. const onResume = vi.fn();
  621. const view = renderDigest({ summary: joined(), onCancel, onResume });
  622. fireEvent.click(view.getByTestId("workflow-digest-cancel"));
  623. view.rerender(
  624. createElement(
  625. ZCodeIntlProvider,
  626. { initialLocale: "en-US" },
  627. createElement(WorkflowRunDigest, {
  628. graph: LOOP_GRAPH as unknown as WorkflowRunDigestProps["graph"],
  629. name: "implement-verify",
  630. testIdKey: "t",
  631. summary: joined({ status: "stopped" }, { resumable: true }),
  632. onCancel,
  633. onResume,
  634. }),
  635. ),
  636. );
  637. expect(view.queryByTestId("workflow-digest-cancel")).toBeNull();
  638. expect(view.getByTestId("workflow-digest-resume")).toBeTruthy();
  639. cleanup();
  640. expect(renderDigest({ summary: joined() }).queryByTestId("workflow-digest-cancel")).toBeNull();
  641. cleanup();
  642. expect(
  643. renderDigest({ summary: joined({ status: "completed" }), onCancel }).queryByTestId(
  644. "workflow-digest-cancel",
  645. ),
  646. ).toBeNull();
  647. });
  648. it("zh-CN:停止运行 / 正在停止…", () => {
  649. const view = renderDigest({ summary: joined(), onCancel: vi.fn() }, "zh-CN");
  650. const cancel = view.getByTestId("workflow-digest-cancel");
  651. expect(cancel.getAttribute("aria-label")).toBe("停止运行");
  652. fireEvent.click(cancel);
  653. expect(cancel.getAttribute("aria-label")).toBe("正在停止…");
  654. expect(enUS["chat.toolCall.workflow.run.cancelling"]).toBeTruthy();
  655. expect(zhCN["chat.toolCall.workflow.run.cancelling"]).toBeTruthy();
  656. });
  657. // 动词改名的另一半:按下去之前就说清楚「停止 ≠ 丢弃」。这句话只活在提示的第二行里,
  658. // 所以断言走提示 props,而不是 DOM。
  659. it("提示第二行说明可恢复;正在停止时那句话撤走", () => {
  660. const view = renderDigest({ summary: joined(), onCancel: vi.fn() });
  661. const before = tooltips.calls.find((call) => call.title === "Stop run");
  662. expect(before?.description).toBe("You can resume later; finished steps are kept.");
  663. tooltips.calls.length = 0;
  664. fireEvent.click(view.getByTestId("workflow-digest-cancel"));
  665. const during = tooltips.calls.find((call) => call.title === "Stopping…");
  666. expect(during).toBeTruthy();
  667. expect(during?.description).toBeUndefined();
  668. });
  669. it("zh-CN 的提示第二行同样在场", () => {
  670. renderDigest({ summary: joined(), onCancel: vi.fn() }, "zh-CN");
  671. expect(tooltips.calls.find((call) => call.title === "停止运行")?.description).toBe(
  672. "停止后可以随时恢复,已完成的步骤会保留。",
  673. );
  674. });
  675. // run 控件的词表两边必须同集同序:一边加了词另一边忘了,用户看到的是英文兜底漏进中文界面。
  676. it("run 词表的键集在两种语言下完全一致", () => {
  677. const prefix = "chat.toolCall.workflow.run.";
  678. const zhKeys = Object.keys(zhCN).filter((key) => key.startsWith(prefix));
  679. const enKeys = Object.keys(enUS).filter((key) => key.startsWith(prefix));
  680. expect(zhKeys.length).toBeGreaterThan(0);
  681. expect(enKeys).toEqual(zhKeys);
  682. for (const key of [`${prefix}stopHint`, `${prefix}resumable`]) {
  683. expect(zhCN[key]).toBeTruthy();
  684. expect(enUS[key]).toBeTruthy();
  685. }
  686. });
  687. });
  688. // ── lineage(docs/dynamic-workflow/presentation.md「The run card」)──
  689. // 卡上只换种类词,不画「调整自 / 已被替代」两句(用户裁决:太吵);那两句只在详情页与确认窗。
  690. describe("WorkflowRunDigest lineage", () => {
  691. // 联接表把 run.stopReason 抄到摘要上(workflowRunCardJoin.ts);夹具照抄。
  692. const superseded = joined(
  693. { status: "stopped", stopReason: "superseded", supersededBy: "dwfrun-next" },
  694. { stopReason: "superseded" },
  695. );
  696. it.each([
  697. ["en-US", "Workflow superseded"],
  698. ["zh-CN", "工作流已被替代"],
  699. ] as const)("被替代的 run:只换种类词,没有 Resume,也没有后继链接:%s", (locale, kind) => {
  700. const onResume = vi.fn();
  701. const view = renderDigest({ summary: superseded, onResume }, locale);
  702. expect(view.getByTestId("workflow-card-kind").textContent).toBe(kind);
  703. expect(view.container.textContent).not.toContain("dwfrun-next");
  704. // 被替代的 run 永不可恢复(CLI 不给 resumable)。
  705. expect(view.queryByTestId("workflow-digest-resume")).toBeNull();
  706. // 灯仍是 stopped 的中性空环,不是 destructive。
  707. expect(view.container.innerHTML).not.toContain("bg-destructive");
  708. });
  709. it("修订出来的 run 卡上不提前驱", () => {
  710. const view = renderDigest({ summary: joined({ resumedFrom: "dwfrun-prev" }) });
  711. expect(view.container.textContent).not.toContain("dwfrun-prev");
  712. });
  713. it("普通 stopped 的 run 种类词照旧", () => {
  714. const view = renderDigest({
  715. summary: joined({ status: "stopped", stopReason: "user" }, { stopReason: "user" }),
  716. });
  717. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow stopped");
  718. });
  719. });
  720. // ── 接线(docs/dynamic-workflow/authoring.md「How the user sees them」)──
  721. // 上面钉的是药丸把**产物 id** 交给上一层;这里钉上一层把 id 变成什么样的打开请求。活投影里那份
  722. // 产物摘要带最新版的 `contentType`(宿主据它把 html 产物直接开成浏览器 tab),刻意不带
  723. // `sourcePath`(高频状态键),所以请求里也不该凭空出现一个出处。
  724. describe("ConversationWorkflowDigests 把药丸变成打开请求", () => {
  725. const ARTIFACTS = [
  726. { id: "book", kind: "file", title: "Audit report", version: 2, contentType: "text/html" },
  727. { id: "perf", kind: "chart", title: "Round timings", version: 1 },
  728. ] as unknown as WorkflowRunState["artifacts"];
  729. function renderDigests(onOpenWorkflowArtifact: (request: unknown) => void) {
  730. const summary = joined({ status: "completed", artifacts: ARTIFACTS });
  731. return render(
  732. createElement(
  733. ZCodeIntlProvider,
  734. { initialLocale: "en-US" },
  735. createElement(ConversationWorkflowDigests, {
  736. turnKey: "t",
  737. digests: [
  738. {
  739. key: "k",
  740. toolCallId: "tool-create-workflow",
  741. runId: "dwfrun-42",
  742. name: "implement-verify",
  743. graph: LOOP_GRAPH as unknown as WorkflowRunDigestProps["graph"],
  744. summary,
  745. },
  746. ],
  747. context: {
  748. sessionId: "parent-a",
  749. onOpenWorkflowArtifact,
  750. } as unknown as ConversationRowRenderContext,
  751. }),
  752. ),
  753. );
  754. }
  755. it("带上展示名与 contentType,不带出处(活投影那份摘要没有 sourcePath)", () => {
  756. const spy = vi.fn();
  757. const view = renderDigests(spy);
  758. fireEvent.click(view.getAllByTestId("workflow-digest-artifact")[0]!);
  759. expect(spy).toHaveBeenCalledWith({
  760. parentSessionId: "parent-a",
  761. runId: "dwfrun-42",
  762. artifactId: "book",
  763. title: "Audit report",
  764. contentType: "text/html",
  765. });
  766. });
  767. it("摘要上没有 contentType 的那一枚不编一个出来", () => {
  768. const spy = vi.fn();
  769. const view = renderDigests(spy);
  770. fireEvent.click(view.getAllByTestId("workflow-digest-artifact")[1]!);
  771. expect(spy).toHaveBeenCalledWith({
  772. parentSessionId: "parent-a",
  773. runId: "dwfrun-42",
  774. artifactId: "perf",
  775. title: "Round timings",
  776. });
  777. });
  778. });