workflowRunDigest.test.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. // ── 表头挤不下的时候(docs/dynamic-workflow/presentation.md「When the header runs out of room」)──
  294. // 细节串曾是表头上唯一不肯让位的那段字:它一长(子代理模型名能让它比「2 phases · 1 agent working」
  295. // 长出一截),Configure 与 Stop 就被挤到卡片右边框外面。jsdom 不排版,所以这里钉住产生那个布局的
  296. // 类契约——细节串可缩、控件不可缩、右簇带 min-w-0(少了它整簇一个像素都不缩,见下面第三条)。
  297. describe("WorkflowRunDigest 表头溢出", () => {
  298. const LONG_MODEL = "4fc7f541-2382-4c32-be11-f021b8d7ed1d/GLM-5.3-Flash$high";
  299. function runningCard() {
  300. return renderDigest({
  301. onCancel: vi.fn(),
  302. onOpenRun: vi.fn(),
  303. settingsHost: { workspacePath: "/w", apply: vi.fn() },
  304. summary: joined({ subagentModel: LONG_MODEL }),
  305. });
  306. }
  307. it("让位的是细节串:min-w-0 + truncate,一行截断", () => {
  308. const detail = runningCard().getByTestId("workflow-card-detail");
  309. expect(detail.className).toContain("min-w-0");
  310. expect(detail.className).toContain("truncate");
  311. });
  312. it("控件不缩:Configure、Stop、⤢ 都带 shrink-0", () => {
  313. const view = runningCard();
  314. for (const id of [
  315. "workflow-digest-configure",
  316. "workflow-digest-cancel",
  317. "workflow-card-open-details",
  318. ]) {
  319. expect(view.getByTestId(id).className).toContain("shrink-0");
  320. }
  321. });
  322. it("右簇可压缩:不带 shrink-0,且必须带 min-w-0(否则地板是整串细节,一个像素都不缩)", () => {
  323. const view = runningCard();
  324. const cluster = view.getByTestId("workflow-card-detail").parentElement!;
  325. // shrink-0 会让整簇按内容宽定死——正是按钮被顶出卡外的老形状。
  326. expect(cluster.className).not.toContain("shrink-0");
  327. // 簇的 min-w-0 是这次修复的承重件:flex item 的 automatic minimum size 是 min-content,
  328. // 而 truncate 的 nowrap 让细节串的 min-content 等于整串字宽(overflow:hidden 和细节串
  329. // 自己的 min-w-0 都不会把它算小)。少了它,簇的地板就是「整串细节 + 按钮」。
  330. expect(cluster.className).toContain("min-w-0");
  331. // 三个控件与细节串同处一簇:负空间只能落在细节串上。
  332. for (const id of [
  333. "workflow-digest-configure",
  334. "workflow-digest-cancel",
  335. "workflow-card-open-details",
  336. ]) {
  337. expect(cluster.querySelector(`[data-testid="${id}"]`)).not.toBeNull();
  338. }
  339. });
  340. });
  341. describe("WorkflowRunDigest 收起态(默认)", () => {
  342. it("表头 + 阶段线:种类词、名字、灯 + 状态词、不含 tokens 的摘要;站与弧在场,药丸不在", () => {
  343. const view = renderDigest({ summary: joined() });
  344. const root = view.getByTestId("workflow-run-digest-t");
  345. expect(root.getAttribute("data-expanded")).toBe("true");
  346. expect(root.getAttribute("data-workflow-run-status")).toBe("running");
  347. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow running");
  348. expect(view.getByTestId("workflow-card-name").textContent).toBe("implement-verify");
  349. expect(view.queryByTestId("workflow-digest-status")).toBeNull();
  350. const detail = view.getByTestId("workflow-card-detail").textContent!;
  351. expect(detail).toBe("2 phases · 1 agent working");
  352. expect(view.getByTestId("workflow-card-detail").className).toContain("text-ui-base");
  353. expect(view.getByTestId("workflow-card-detail").className).not.toContain("font-mono");
  354. expect(detail).not.toContain("tokens");
  355. // 运行中的种类词扫光,wf-swap 包在渐变外面(round 7 的教训)。
  356. expect(root.querySelector(".wf-swap .animated-gradient-text")).toBeTruthy();
  357. expect(view.getAllByTestId("workflow-timeline-station")).toHaveLength(2);
  358. expect(view.getByTestId("workflow-timeline-arc")).toBeTruthy();
  359. expect(view.queryAllByTestId("workflow-agent-pill")).toHaveLength(2);
  360. expect(view.queryByTestId("workflow-card-footer")).toBeNull();
  361. // 没有回调就没有 ⤢、没有 Resume(回调的存在即门控)。
  362. expect(view.queryByTestId("workflow-card-open-details")).toBeNull();
  363. expect(view.queryByTestId("workflow-digest-resume")).toBeNull();
  364. });
  365. it("无箭头仍可点击及键盘折叠,子控件不切换", () => {
  366. const onOpenRun = vi.fn();
  367. const view = renderDigest({ summary: joined(), onOpenRun });
  368. const root = view.getByTestId("workflow-run-digest-t");
  369. expect(view.queryByTestId("workflow-card-toggle")).toBeNull();
  370. expect(root.getAttribute("role")).toBe("button");
  371. fireEvent.click(root);
  372. expect(view.queryAllByTestId("workflow-agent-pill")).toHaveLength(0);
  373. fireEvent.keyDown(root, { key: "Enter" });
  374. expect(view.getAllByTestId("workflow-agent-pill")).toHaveLength(2);
  375. fireEvent.click(view.getByTestId("workflow-card-open-details"));
  376. expect(onOpenRun).toHaveBeenCalledOnce();
  377. });
  378. it("还没启动的子代理药丸与活药丸同一套点击语法:按钮、↗,点它交出槽位而不切换整块", () => {
  379. // docs/dynamic-workflow/presentation.md「The pill」:此前它是 span,点击冒泡到整块
  380. // 开关,把卡折起来。
  381. const onOpenPill = vi.fn();
  382. const view = renderDigest({
  383. summary: joined({
  384. actors: [
  385. { siteId: "actor#1", ordinal: 1, name: "Ada", sessionId: "s-1", status: "completed" },
  386. ],
  387. }),
  388. onOpenPill,
  389. });
  390. const root = view.getByTestId("workflow-run-digest-t");
  391. const pills = view.getAllByTestId("workflow-agent-pill");
  392. expect(pills.map((pill) => pill.tagName)).toEqual(["BUTTON", "BUTTON"]);
  393. expect(pills[1]!.className).toContain("wf-pill-open");
  394. expect(pills[1]!.querySelector('[data-testid="workflow-timeline-pill-open"]')).not.toBeNull();
  395. fireEvent.click(pills[1]!);
  396. expect(onOpenPill).toHaveBeenCalledTimes(1);
  397. const handed = onOpenPill.mock.calls[0]![0] as TimelinePill;
  398. expect(handed.slot).toEqual({ siteId: "actor#2", ordinal: 1 });
  399. expect(handed.instance).toBeUndefined();
  400. expect(root.getAttribute("data-expanded")).toBe("true");
  401. });
  402. it("⤢ 与待答问题芯片都开 run 详情;芯片按数量计数、无回调时不可点", () => {
  403. const onOpenRun = vi.fn();
  404. const view = renderDigest({ summary: joined(), onOpenRun, pendingQuestions: 1 });
  405. fireEvent.click(view.getByTestId("workflow-card-open-details"));
  406. expect(onOpenRun).toHaveBeenCalledTimes(1);
  407. const chip = view.getByTestId("workflow-digest-questions");
  408. expect(chip.tagName).toBe("BUTTON");
  409. expect(chip.textContent).toBe("1 question");
  410. fireEvent.click(chip);
  411. expect(onOpenRun).toHaveBeenCalledTimes(2);
  412. cleanup();
  413. const inert = renderDigest({ summary: joined(), pendingQuestions: 2 });
  414. const span = inert.getByTestId("workflow-digest-questions");
  415. expect(span.tagName).toBe("SPAN");
  416. expect(span.textContent).toBe("2 questions");
  417. cleanup();
  418. expect(
  419. renderDigest({ summary: joined() }).queryByTestId("workflow-digest-questions"),
  420. ).toBeNull();
  421. });
  422. it("失败且可恢复:Resume 进表头,与详情页同一条命令;不可恢复或无回调时没有", () => {
  423. const onResume = vi.fn();
  424. const view = renderDigest({
  425. summary: joined({ status: "stopped", stopReason: "interrupted" }, { resumable: true }),
  426. onResume,
  427. });
  428. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow stopped");
  429. expect(view.getByTestId("workflow-digest-resume").getAttribute("data-size")).toBe("default");
  430. fireEvent.click(view.getByTestId("workflow-digest-resume"));
  431. expect(onResume).toHaveBeenCalledTimes(1);
  432. cleanup();
  433. expect(
  434. renderDigest({ summary: joined({ status: "errored" }), onResume }).queryByTestId(
  435. "workflow-digest-resume",
  436. ),
  437. ).toBeNull();
  438. });
  439. });
  440. describe("WorkflowRunDigest 产物条", () => {
  441. const ARTIFACTS = [
  442. { id: "book", kind: "file", title: "Audit report", version: 2 },
  443. { id: "perf", kind: "chart", title: "Round timings", version: 1 },
  444. ] as unknown as WorkflowRunState["artifacts"];
  445. it("收起态与展开态都挂产物条;点药丸交出产物 id,不切换整块", () => {
  446. const onOpenArtifact = vi.fn();
  447. const view = renderDigest({
  448. summary: joined({ status: "completed", artifacts: ARTIFACTS }),
  449. onOpenArtifact,
  450. });
  451. const root = view.getByTestId("workflow-run-digest-t");
  452. const strip = view.getByTestId("workflow-digest-artifacts");
  453. const pills = view.getAllByTestId("workflow-digest-artifact");
  454. expect(pills).toHaveLength(2);
  455. expect(pills[0]?.tagName).toBe("BUTTON");
  456. expect(pills[0]?.getAttribute("data-pill-size")).toBe("md");
  457. // 尾槽里的 v2 只在第 2 版的那枚上。
  458. expect(strip.querySelectorAll('[data-testid="workflow-run-artifact-version"]')).toHaveLength(1);
  459. expect(root.getAttribute("data-expanded")).toBe("true");
  460. fireEvent.click(pills[1]!);
  461. expect(onOpenArtifact).toHaveBeenCalledWith("perf");
  462. expect(root.getAttribute("data-expanded")).toBe("true");
  463. // 条上的空白处**是**整块开关的一部分(追记「点击语法修订」):点它切换,产物条仍在。
  464. fireEvent.click(strip);
  465. expect(root.getAttribute("data-expanded")).toBe("false");
  466. expect(view.getAllByTestId("workflow-digest-artifact")).toHaveLength(2);
  467. fireEvent.click(strip);
  468. expect(root.getAttribute("data-expanded")).toBe("true");
  469. });
  470. it("禁用的产物药丸(无回调)不透传:点它既不打开也不切换整块", () => {
  471. const view = renderDigest({ summary: joined({ status: "completed", artifacts: ARTIFACTS }) });
  472. const root = view.getByTestId("workflow-run-digest-t");
  473. const pill = view.getAllByTestId("workflow-digest-artifact")[0] as HTMLButtonElement;
  474. expect(pill.disabled).toBe(true);
  475. fireEvent.click(pill);
  476. expect(root.getAttribute("data-expanded")).toBe("true");
  477. });
  478. it("没有产物或没有活投影时条缺席;没有回调时药丸禁用", () => {
  479. expect(
  480. renderDigest({ summary: joined() }).queryByTestId("workflow-digest-artifacts"),
  481. ).toBeNull();
  482. cleanup();
  483. const cold = renderDigest({ summary: joined({ artifacts: ARTIFACTS }, { run: undefined }) });
  484. expect(cold.queryByTestId("workflow-digest-artifacts")).toBeNull();
  485. cleanup();
  486. const inert = renderDigest({ summary: joined({ artifacts: ARTIFACTS }) });
  487. const pill = inert.getAllByTestId("workflow-digest-artifact")[0] as HTMLButtonElement;
  488. expect(pill.disabled).toBe(true);
  489. });
  490. });
  491. // 实例表撞界(docs/dynamic-workflow/presentation.md「The run card」):表头的计数已经把表外的
  492. // 实例算进来了,时间线却只画得出表内那些——时间线下的这一句说的正是这个差额。
  493. describe("WorkflowRunDigest 撞界提示", () => {
  494. const truncated = (overrides: Partial<WorkflowRunState> = {}) =>
  495. joined({
  496. usage: { spentTokens: 10, nodesUsed: 2, nodesUnlisted: 1_500, nodesUnlistedSettled: 900 },
  497. truncated: true,
  498. ...overrides,
  499. });
  500. it("收起态与展开态都在:「仅展示 2/1502 步的详情」", () => {
  501. const view = renderDigest({ summary: truncated() });
  502. const root = view.getByTestId("workflow-run-digest-t");
  503. expect(view.getByTestId("workflow-digest-truncated").textContent).toBe(
  504. "Details shown for 2 of 1502 steps",
  505. );
  506. fireEvent.click(root);
  507. expect(root.getAttribute("data-expanded")).toBe("false");
  508. expect(view.getByTestId("workflow-digest-truncated")).not.toBeNull();
  509. });
  510. it("没撞界、只有小表撞界、没有活投影、以及没有轨道的单行卡上都没有这一句", () => {
  511. expect(
  512. renderDigest({ summary: joined() }).queryByTestId("workflow-digest-truncated"),
  513. ).toBeNull();
  514. cleanup();
  515. // `truncated` 也会被 reports / artifacts / phases 触界置位;那时步数一个不少,没有可说的。
  516. expect(
  517. renderDigest({ summary: joined({ truncated: true }) }).queryByTestId(
  518. "workflow-digest-truncated",
  519. ),
  520. ).toBeNull();
  521. cleanup();
  522. expect(
  523. renderDigest({ summary: truncated({}), graph: undefined }).queryByTestId(
  524. "workflow-digest-truncated",
  525. ),
  526. ).toBeNull();
  527. cleanup();
  528. expect(
  529. renderDigest({ summary: joined({ truncated: true }, { run: undefined }) }).queryByTestId(
  530. "workflow-digest-truncated",
  531. ),
  532. ).toBeNull();
  533. });
  534. it("zh-CN 念「仅展示 2/1502 步的详情」", () => {
  535. const view = renderDigest({ summary: truncated() }, "zh-CN");
  536. expect(view.getByTestId("workflow-digest-truncated").textContent).toBe(
  537. "仅展示 2/1502 步的详情",
  538. );
  539. });
  540. });
  541. describe("WorkflowRunDigest 冷态", () => {
  542. it("没有活投影:一行——种类词 + 名字 + 状态词,没有计数、没有阶段线、没有 chevron", () => {
  543. const summary: WorkflowRunCardSummary = {
  544. runId: "dwfrun-42",
  545. toolCallId: "tool-create-workflow",
  546. status: "completed",
  547. nodesSettled: 2,
  548. nodesTotal: 2,
  549. };
  550. const view = renderDigest({ summary, onOpenRun: vi.fn() });
  551. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow completed");
  552. expect(view.queryByTestId("workflow-digest-status")).toBeNull();
  553. expect(view.queryByTestId("workflow-card-detail")).toBeNull();
  554. expect(view.queryByTestId("workflow-digest-plot")).toBeNull();
  555. expect(view.queryByTestId("workflow-card-toggle")).toBeNull();
  556. const root = view.getByTestId("workflow-run-digest-t");
  557. expect(root.hasAttribute("data-expanded")).toBe(false);
  558. // 没有阶段线就没有开关:整块不是按钮。
  559. expect(root.hasAttribute("role")).toBe(false);
  560. expect(view.getByTestId("workflow-card-open-details")).toBeTruthy();
  561. });
  562. it("行窗口里没有图(resume 单独成轮):即使有活投影也只剩一行", () => {
  563. const view = renderDigest({ summary: joined(), graph: undefined });
  564. expect(view.queryByTestId("workflow-card-detail")).toBeNull();
  565. expect(view.queryByTestId("workflow-digest-plot")).toBeNull();
  566. expect(view.queryByTestId("workflow-card-toggle")).toBeNull();
  567. });
  568. });
  569. describe("WorkflowRunDigest 文案", () => {
  570. it("zh-CN 双写:芯片与 chevron 的词条在两种语言里都在", () => {
  571. for (const id of [
  572. "chat.toolCall.workflow.digest.question",
  573. "chat.toolCall.workflow.digest.questions",
  574. "chat.toolCall.workflow.digest.showAgents",
  575. "chat.toolCall.workflow.digest.hideAgents",
  576. ]) {
  577. expect(enUS[id as keyof typeof enUS]).toBeTruthy();
  578. expect(zhCN[id as keyof typeof zhCN]).toBeTruthy();
  579. }
  580. const view = renderDigest({ summary: joined(), pendingQuestions: 1 }, "zh-CN");
  581. expect(view.getByTestId("workflow-card-kind").textContent).toBe("工作流运行中");
  582. expect(view.getByTestId("workflow-digest-questions").textContent).toBe("1 个待答问题");
  583. expect(view.queryByTestId("workflow-card-toggle")).toBeNull();
  584. });
  585. });
  586. it.each(["running", "completed", "stopped"] as const)(
  587. "运行卡名称使用普通 UI 字体:%s",
  588. (status) => {
  589. const view = renderDigest({ summary: joined({ status }) });
  590. expect(view.getByTestId("workflow-card-name").className).not.toContain("font-mono");
  591. },
  592. );
  593. // 运行卡上的 Stop run(docs/dynamic-workflow/presentation.md):running 时
  594. // Resume 的位置换成 Stop,走详情页同一条命令;点下即禁用直到状态变化。
  595. describe("WorkflowRunDigest Stop run", () => {
  596. it("running + onCancel:Stop 进表头、可点;点后禁用并换成 Stopping…", () => {
  597. const onCancel = vi.fn();
  598. const view = renderDigest({ summary: joined(), onCancel });
  599. const cancel = view.getByTestId("workflow-digest-cancel");
  600. // 与 ⤢ 同款:ghost 图标钮,文字只在 aria-label / 提示里。
  601. expect(cancel.getAttribute("aria-label")).toBe("Stop run");
  602. expect(cancel.textContent).toBe("");
  603. expect(cancel.getAttribute("data-size")).toBe("icon-md");
  604. expect(cancel.getAttribute("data-variant")).toBe("ghost");
  605. expect(view.queryByTestId("workflow-digest-resume")).toBeNull();
  606. fireEvent.click(cancel);
  607. expect(onCancel).toHaveBeenCalledTimes(1);
  608. expect((cancel as HTMLButtonElement).disabled).toBe(true);
  609. expect(cancel.getAttribute("aria-label")).toBe("Stopping…");
  610. // 点 Cancel 不触发卡片折叠(按钮被 hitsControl 排除)。
  611. expect(
  612. view.container.querySelector("[data-workflow-run-digest]")?.getAttribute("data-expanded"),
  613. ).toBe("true");
  614. });
  615. it("Stopping… 的复位不再是一轮 effect setState:状态变化只提交一次", () => {
  616. // 修订 2026-09-12:复位曾是按 status 跑的 effect 里的 setCancelling(false)——每次状态变化都在
  617. // 投影帧的同步提交之后再补一笔更新(与工作流卡崩溃的抛点同形)。现在 Cancel 钮按 status 键控
  618. // 重挂,复位是重挂本身,状态变化那一帧只提交一次。用 Profiler 数提交。
  619. const onCancel = vi.fn();
  620. const onResume = vi.fn();
  621. let commits = 0;
  622. const tree = (summary: WorkflowRunCardSummary) =>
  623. createElement(
  624. Profiler,
  625. { id: "digest", onRender: () => (commits += 1) },
  626. createElement(
  627. ZCodeIntlProvider,
  628. { initialLocale: "en-US" },
  629. createElement(WorkflowRunDigest, {
  630. graph: LOOP_GRAPH as unknown as WorkflowRunDigestProps["graph"],
  631. name: "implement-verify",
  632. testIdKey: "t",
  633. summary,
  634. onCancel,
  635. onResume,
  636. }),
  637. ),
  638. );
  639. const view = render(tree(joined()));
  640. fireEvent.click(view.getByTestId("workflow-digest-cancel"));
  641. expect(view.getByTestId("workflow-digest-cancel").getAttribute("aria-label")).toBe("Stopping…");
  642. // 同状态的新帧:按钮保持 Stopping…。
  643. view.rerender(tree(joined({ status: "running" }, { nodesSettled: 3 })));
  644. expect((view.getByTestId("workflow-digest-cancel") as HTMLButtonElement).disabled).toBe(true);
  645. commits = 0;
  646. view.rerender(tree(joined({ status: "stopped" }, { resumable: true })));
  647. expect(commits).toBe(1);
  648. expect(view.queryByTestId("workflow-digest-cancel")).toBeNull();
  649. expect(view.getByTestId("workflow-digest-resume")).toBeTruthy();
  650. expect(onCancel).toHaveBeenCalledTimes(1);
  651. });
  652. it("状态一变按钮复位;stopped + resumable 时原位是 Resume;无回调或非 running 不渲染", () => {
  653. const onCancel = vi.fn();
  654. const onResume = vi.fn();
  655. const view = renderDigest({ summary: joined(), onCancel, onResume });
  656. fireEvent.click(view.getByTestId("workflow-digest-cancel"));
  657. view.rerender(
  658. createElement(
  659. ZCodeIntlProvider,
  660. { initialLocale: "en-US" },
  661. createElement(WorkflowRunDigest, {
  662. graph: LOOP_GRAPH as unknown as WorkflowRunDigestProps["graph"],
  663. name: "implement-verify",
  664. testIdKey: "t",
  665. summary: joined({ status: "stopped" }, { resumable: true }),
  666. onCancel,
  667. onResume,
  668. }),
  669. ),
  670. );
  671. expect(view.queryByTestId("workflow-digest-cancel")).toBeNull();
  672. expect(view.getByTestId("workflow-digest-resume")).toBeTruthy();
  673. cleanup();
  674. expect(renderDigest({ summary: joined() }).queryByTestId("workflow-digest-cancel")).toBeNull();
  675. cleanup();
  676. expect(
  677. renderDigest({ summary: joined({ status: "completed" }), onCancel }).queryByTestId(
  678. "workflow-digest-cancel",
  679. ),
  680. ).toBeNull();
  681. });
  682. it("zh-CN:停止运行 / 正在停止…", () => {
  683. const view = renderDigest({ summary: joined(), onCancel: vi.fn() }, "zh-CN");
  684. const cancel = view.getByTestId("workflow-digest-cancel");
  685. expect(cancel.getAttribute("aria-label")).toBe("停止运行");
  686. fireEvent.click(cancel);
  687. expect(cancel.getAttribute("aria-label")).toBe("正在停止…");
  688. expect(enUS["chat.toolCall.workflow.run.cancelling"]).toBeTruthy();
  689. expect(zhCN["chat.toolCall.workflow.run.cancelling"]).toBeTruthy();
  690. });
  691. // 动词改名的另一半:按下去之前就说清楚「停止 ≠ 丢弃」。这句话只活在提示的第二行里,
  692. // 所以断言走提示 props,而不是 DOM。
  693. it("提示第二行说明可恢复;正在停止时那句话撤走", () => {
  694. const view = renderDigest({ summary: joined(), onCancel: vi.fn() });
  695. const before = tooltips.calls.find((call) => call.title === "Stop run");
  696. expect(before?.description).toBe("You can resume later; finished steps are kept.");
  697. tooltips.calls.length = 0;
  698. fireEvent.click(view.getByTestId("workflow-digest-cancel"));
  699. const during = tooltips.calls.find((call) => call.title === "Stopping…");
  700. expect(during).toBeTruthy();
  701. expect(during?.description).toBeUndefined();
  702. });
  703. it("zh-CN 的提示第二行同样在场", () => {
  704. renderDigest({ summary: joined(), onCancel: vi.fn() }, "zh-CN");
  705. expect(tooltips.calls.find((call) => call.title === "停止运行")?.description).toBe(
  706. "停止后可以随时恢复,已完成的步骤会保留。",
  707. );
  708. });
  709. // run 控件的词表两边必须同集同序:一边加了词另一边忘了,用户看到的是英文兜底漏进中文界面。
  710. it("run 词表的键集在两种语言下完全一致", () => {
  711. const prefix = "chat.toolCall.workflow.run.";
  712. const zhKeys = Object.keys(zhCN).filter((key) => key.startsWith(prefix));
  713. const enKeys = Object.keys(enUS).filter((key) => key.startsWith(prefix));
  714. expect(zhKeys.length).toBeGreaterThan(0);
  715. expect(enKeys).toEqual(zhKeys);
  716. for (const key of [`${prefix}stopHint`, `${prefix}resumable`]) {
  717. expect(zhCN[key]).toBeTruthy();
  718. expect(enUS[key]).toBeTruthy();
  719. }
  720. });
  721. });
  722. // ── lineage(docs/dynamic-workflow/presentation.md「The run card」)──
  723. // 卡上只换种类词,不画「调整自 / 已被替代」两句(用户裁决:太吵);那两句只在详情页与确认窗。
  724. describe("WorkflowRunDigest lineage", () => {
  725. // 联接表把 run.stopReason 抄到摘要上(workflowRunCardJoin.ts);夹具照抄。
  726. const superseded = joined(
  727. { status: "stopped", stopReason: "superseded", supersededBy: "dwfrun-next" },
  728. { stopReason: "superseded" },
  729. );
  730. it.each([
  731. ["en-US", "Workflow superseded"],
  732. ["zh-CN", "工作流已被替代"],
  733. ] as const)("被替代的 run:只换种类词,没有 Resume,也没有后继链接:%s", (locale, kind) => {
  734. const onResume = vi.fn();
  735. const view = renderDigest({ summary: superseded, onResume }, locale);
  736. expect(view.getByTestId("workflow-card-kind").textContent).toBe(kind);
  737. expect(view.container.textContent).not.toContain("dwfrun-next");
  738. // 被替代的 run 永不可恢复(CLI 不给 resumable)。
  739. expect(view.queryByTestId("workflow-digest-resume")).toBeNull();
  740. // 灯仍是 stopped 的中性空环,不是 destructive。
  741. expect(view.container.innerHTML).not.toContain("bg-destructive");
  742. });
  743. it("修订出来的 run 卡上不提前驱", () => {
  744. const view = renderDigest({ summary: joined({ resumedFrom: "dwfrun-prev" }) });
  745. expect(view.container.textContent).not.toContain("dwfrun-prev");
  746. });
  747. it("普通 stopped 的 run 种类词照旧", () => {
  748. const view = renderDigest({
  749. summary: joined({ status: "stopped", stopReason: "user" }, { stopReason: "user" }),
  750. });
  751. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow stopped");
  752. });
  753. });
  754. // ── 接线(docs/dynamic-workflow/authoring.md「How the user sees them」)──
  755. // 上面钉的是药丸把**产物 id** 交给上一层;这里钉上一层把 id 变成什么样的打开请求。活投影里那份
  756. // 产物摘要带最新版的 `contentType`(宿主据它把 html 产物直接开成浏览器 tab),刻意不带
  757. // `sourcePath`(高频状态键),所以请求里也不该凭空出现一个出处。
  758. describe("ConversationWorkflowDigests 把药丸变成打开请求", () => {
  759. const ARTIFACTS = [
  760. { id: "book", kind: "file", title: "Audit report", version: 2, contentType: "text/html" },
  761. { id: "perf", kind: "chart", title: "Round timings", version: 1 },
  762. ] as unknown as WorkflowRunState["artifacts"];
  763. function renderDigests(onOpenWorkflowArtifact: (request: unknown) => void) {
  764. const summary = joined({ status: "completed", artifacts: ARTIFACTS });
  765. return render(
  766. createElement(
  767. ZCodeIntlProvider,
  768. { initialLocale: "en-US" },
  769. createElement(ConversationWorkflowDigests, {
  770. turnKey: "t",
  771. digests: [
  772. {
  773. key: "k",
  774. toolCallId: "tool-create-workflow",
  775. runId: "dwfrun-42",
  776. name: "implement-verify",
  777. graph: LOOP_GRAPH as unknown as WorkflowRunDigestProps["graph"],
  778. summary,
  779. },
  780. ],
  781. context: {
  782. sessionId: "parent-a",
  783. onOpenWorkflowArtifact,
  784. } as unknown as ConversationRowRenderContext,
  785. }),
  786. ),
  787. );
  788. }
  789. it("带上展示名与 contentType,不带出处(活投影那份摘要没有 sourcePath)", () => {
  790. const spy = vi.fn();
  791. const view = renderDigests(spy);
  792. fireEvent.click(view.getAllByTestId("workflow-digest-artifact")[0]!);
  793. expect(spy).toHaveBeenCalledWith({
  794. parentSessionId: "parent-a",
  795. runId: "dwfrun-42",
  796. artifactId: "book",
  797. title: "Audit report",
  798. contentType: "text/html",
  799. });
  800. });
  801. it("摘要上没有 contentType 的那一枚不编一个出来", () => {
  802. const spy = vi.fn();
  803. const view = renderDigests(spy);
  804. fireEvent.click(view.getAllByTestId("workflow-digest-artifact")[1]!);
  805. expect(spy).toHaveBeenCalledWith({
  806. parentSessionId: "parent-a",
  807. runId: "dwfrun-42",
  808. artifactId: "perf",
  809. title: "Round timings",
  810. });
  811. });
  812. });