createWorkflowToolCallBlock.test.ts 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  1. // @vitest-environment jsdom
  2. // 启动前 ToolLayout 与旧宿主运行卡的渲染回归;v4 run 摘要另由 workflowToolSummary 测试覆盖。
  3. import { act, cleanup, fireEvent, render, within } from "@testing-library/react";
  4. import { createElement, type ReactNode } from "react";
  5. import { renderToStaticMarkup } from "react-dom/server";
  6. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  7. import type { WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  8. import type { WorkflowDraftPosition, WorkflowRunCardSummary } from "@/ToolCallBlocks/shared.js";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. vi.mock("@/components/ai-elements/code-block.js", () => ({
  11. CodeBlockHeader: () => null,
  12. CodeBlock: ({
  13. code,
  14. contentClassName,
  15. showLineNumbers,
  16. markedLines,
  17. }: {
  18. code: string;
  19. children?: ReactNode;
  20. contentClassName?: string;
  21. showLineNumbers?: boolean;
  22. markedLines?: readonly number[];
  23. }) =>
  24. createElement(
  25. "pre",
  26. {
  27. "data-testid": "code-block",
  28. className: contentClassName,
  29. "data-show-line-numbers": showLineNumbers === true ? "true" : undefined,
  30. "data-marked-lines": markedLines === undefined ? undefined : markedLines.join(","),
  31. },
  32. code,
  33. ),
  34. }));
  35. // ControlHintTooltip 依赖应用根部的 TooltipProvider;测试里按既有惯例替换为透传桩。
  36. vi.mock("@/ControlHintTooltip.js", () => ({
  37. ControlHintTooltip: ({ children }: { children?: ReactNode }) => children,
  38. }));
  39. // 接线用例要把整行渲染出来;行不碰 workspace 服务,给一个空实现免去搭一整份 Provider。
  40. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  41. useWorkspaceServices: () => ({}),
  42. }));
  43. // eslint-disable-next-line import/first -- 必须在 code-block mock 之后再引入被测 renderer。
  44. import {
  45. CreateWorkflowToolCallBlock,
  46. readWorkflowKindMessageId,
  47. } from "../src/ToolCallBlocks/renderers/create-workflow.js";
  48. // eslint-disable-next-line import/first -- 同上;反馈提示的纯函数与 renderer 同一读取规则。
  49. import { formatWorkflowFeedbackTooltip } from "../src/ToolCallBlocks/renderers/createWorkflowDisplay.js";
  50. // eslint-disable-next-line import/first -- 同上;卡片与确认窗共用的入参读取规则。
  51. import {
  52. readWorkflowAmendScriptInherited,
  53. readWorkflowRetuneCall,
  54. } from "../src/ToolCallBlocks/renderers/createWorkflowInput.js";
  55. // eslint-disable-next-line import/first -- 同上;接线层(行 → 宿主请求)只在这一节用到。
  56. import { ConversationRowView } from "@/v4/ConversationRowView.js";
  57. // eslint-disable-next-line import/first -- 同上。
  58. import { PlatformProvider } from "@/hooks/usePlatform.js";
  59. // eslint-disable-next-line import/first -- 同上;词条只用于双语言存在性断言。
  60. import enUS from "../src/i18n/locales/en-US.js";
  61. // eslint-disable-next-line import/first -- 同上。
  62. import zhCN from "../src/i18n/locales/zh-CN.js";
  63. type ToolCallOverrides = {
  64. /** 工具名(kind / title 同值);缺省 CreateWorkflow,修订行传 AmendWorkflow。 */
  65. kind?: string;
  66. input?: unknown;
  67. output?: unknown;
  68. raw?: unknown;
  69. status?: string;
  70. snapshotRefs?: readonly { field: string; previewBytes: number; fullBytes: number }[];
  71. };
  72. type ContextOverrides = {
  73. isRunning?: boolean;
  74. statusLabel?: string;
  75. errorText?: string;
  76. onOpenWorkflowRun?: (request: { workflowName?: string }) => void;
  77. onResumeWorkflowRun?: (request: { workflowName?: string }) => void;
  78. onOpenWorkflowActor?: (request: {
  79. runId: string;
  80. actorSessionId: string;
  81. siteId: string;
  82. ordinal: number;
  83. actorName?: string;
  84. }) => void;
  85. onLoadFullToolCallFields?: (toolId: string) => void;
  86. onOpenWorkflowArtifact?: (artifactId: string) => void;
  87. onOpenWorkflowWorkspace?: (request: { phaseId: string; workflowName?: string }) => void;
  88. workflowRun?: WorkflowRunCardSummary;
  89. workflowDraft?: WorkflowDraftPosition;
  90. forceOpen?: boolean;
  91. canToggle?: boolean;
  92. };
  93. function createBlock(
  94. toolCall: ToolCallOverrides,
  95. contextOverrides: ContextOverrides = {},
  96. locale: "en-US" | "zh-CN" = "en-US",
  97. ) {
  98. return createElement(
  99. ZCodeIntlProvider,
  100. { initialLocale: locale },
  101. createElement(CreateWorkflowToolCallBlock, {
  102. toolCallNode: {
  103. toolCall: {
  104. toolId: "tool-create-workflow",
  105. kind: toolCall.kind ?? "CreateWorkflow",
  106. title: toolCall.kind ?? "CreateWorkflow",
  107. input: toolCall.input,
  108. output: toolCall.output,
  109. status: toolCall.status ?? "completed",
  110. raw: toolCall.raw,
  111. snapshotRefs: toolCall.snapshotRefs,
  112. },
  113. childToolCalls: [],
  114. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 测试上下文只填 renderer 读取的字段。
  115. } as any,
  116. workspacePath: "/workspace",
  117. displayModel: {
  118. inlinePreview: { type: "none" },
  119. planResult: null,
  120. viewerSource: null,
  121. viewerLabelId: "codeViewer.viewCode",
  122. showSummaryFileLink: false,
  123. showInput: false,
  124. showOutput: true,
  125. showKind: false,
  126. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 同上。
  127. } as any,
  128. viewerSource: null,
  129. rawFileSummaries: [],
  130. isRunning: contextOverrides.isRunning ?? false,
  131. statusLabel: contextOverrides.statusLabel ?? "Completed",
  132. errorText: contextOverrides.errorText,
  133. childToolList: null,
  134. forceOpen: contextOverrides.forceOpen ?? true,
  135. canToggle: contextOverrides.canToggle ?? true,
  136. showIcon: true,
  137. onOpenWorkflowRun: contextOverrides.onOpenWorkflowRun,
  138. onResumeWorkflowRun: contextOverrides.onResumeWorkflowRun,
  139. onOpenWorkflowActor: contextOverrides.onOpenWorkflowActor,
  140. onOpenWorkflowArtifact: contextOverrides.onOpenWorkflowArtifact,
  141. onOpenWorkflowWorkspace: contextOverrides.onOpenWorkflowWorkspace,
  142. onLoadFullToolCallFields: contextOverrides.onLoadFullToolCallFields,
  143. workflowRun: contextOverrides.workflowRun,
  144. workflowDraft: contextOverrides.workflowDraft,
  145. }),
  146. );
  147. }
  148. function renderBlock(
  149. toolCall: ToolCallOverrides,
  150. contextOverrides: ContextOverrides = {},
  151. locale: "en-US" | "zh-CN" = "en-US",
  152. ): string {
  153. return renderToStaticMarkup(createBlock(toolCall, contextOverrides, locale));
  154. }
  155. function renderBlockDom(
  156. toolCall: ToolCallOverrides,
  157. contextOverrides: ContextOverrides = {},
  158. locale: "en-US" | "zh-CN" = "en-US",
  159. ) {
  160. return render(createBlock(toolCall, contextOverrides, locale));
  161. }
  162. // 本仓 vitest 没开 globals,@testing-library 的自动 cleanup 不会注册;不显式清理,
  163. // 上一条用例的 DOM 会留下来。
  164. afterEach(() => {
  165. cleanup();
  166. });
  167. const OK_DISPLAY = {
  168. kind: "create_workflow",
  169. ok: true,
  170. errorCount: 0,
  171. diagnostics: [],
  172. };
  173. // ── 图夹具:plan → verify,verify 回到 plan ─────────────────────────────────────
  174. const LOOP_GRAPH = {
  175. steps: [
  176. {
  177. id: "ask#1",
  178. kind: "ask",
  179. label: "plan",
  180. line: 2,
  181. column: 21,
  182. lane: "actor#1",
  183. phase: "phase#a",
  184. },
  185. {
  186. id: "ask#2",
  187. kind: "ask",
  188. label: "verify",
  189. line: 5,
  190. column: 21,
  191. lane: "actor#2",
  192. phase: "phase#b",
  193. },
  194. ],
  195. lanes: [
  196. { id: "actor#1", name: "planner", line: 1, column: 11 },
  197. { id: "actor#2", name: "checker", line: 4, column: 11 },
  198. ],
  199. participants: [
  200. { id: "phase#a:actor#1", phase: "phase#a", lane: "actor#1", steps: ["ask#1"] },
  201. { id: "phase#b:actor#2", phase: "phase#b", lane: "actor#2", steps: ["ask#2"] },
  202. ],
  203. handoffs: [],
  204. phases: [
  205. { id: "phase#a", name: "plan", line: 1 },
  206. { id: "phase#b", name: "verify", line: 4 },
  207. ],
  208. phaseEdges: [
  209. { from: "phase#a", to: "phase#b" },
  210. { from: "phase#b", to: "phase#a", back: true },
  211. ],
  212. exits: ["phase#b"],
  213. };
  214. const FLAT_GRAPH = {
  215. steps: [{ id: "ask#1", kind: "ask", label: "a", line: 1, column: 21, lane: "actor#1" }],
  216. lanes: [{ id: "actor#1", name: "a", line: 1, column: 11 }],
  217. participants: [{ id: "unphased:actor#1", phase: "unphased", lane: "actor#1", steps: ["ask#1"] }],
  218. handoffs: [],
  219. sink: ["ask#1"],
  220. };
  221. const LOOP_TOOL_CALL: ToolCallOverrides = {
  222. input: { name: "Research pipeline", script: 'phase("plan");\nconst p = agent("planner");' },
  223. raw: { display: { ...OK_DISPLAY, causalityGraph: LOOP_GRAPH } },
  224. };
  225. function runState(overrides: Partial<WorkflowRunState> = {}): WorkflowRunState {
  226. return {
  227. runId: "dwfrun-42",
  228. toolCallId: "tool-create-workflow",
  229. status: "running",
  230. usage: { spentTokens: 1_234, nodesUsed: 2 },
  231. actors: [
  232. { siteId: "actor#1", ordinal: 1, name: "Ada", sessionId: "s-1", status: "completed" },
  233. { siteId: "actor#2", ordinal: 1, name: "Bob", sessionId: "s-2", status: "running" },
  234. ],
  235. nodes: [
  236. {
  237. siteId: "ask#1",
  238. ordinal: 1,
  239. phase: "settled",
  240. outcome: "ok",
  241. actorSiteId: "actor#1",
  242. actorOrdinal: 1,
  243. },
  244. { siteId: "ask#2", ordinal: 1, phase: "executing", actorSiteId: "actor#2", actorOrdinal: 1 },
  245. ] as WorkflowRunState["nodes"],
  246. lastEventSequence: 7,
  247. ...overrides,
  248. };
  249. }
  250. function joined(
  251. overrides: Partial<WorkflowRunState> = {},
  252. extra: Partial<WorkflowRunCardSummary> = {},
  253. ) {
  254. const run = runState(overrides);
  255. const settled = run.nodes.filter((node) => node.phase === "settled").length;
  256. return {
  257. runId: run.runId,
  258. status: run.status,
  259. ...(run.stopReason === undefined ? {} : { stopReason: run.stopReason }),
  260. nodesSettled: settled,
  261. nodesTotal: run.nodes.length,
  262. run,
  263. ...extra,
  264. } satisfies WorkflowRunCardSummary;
  265. }
  266. describe("CreateWorkflowToolCallBlock 表头种类词", () => {
  267. it("扫光中的种类词可见:换词的 wf-swap 包在 animated-gradient-text 外面,不在里面", () => {
  268. // background-clip:text 只裁到自己这一层的文字;被 transform/opacity 动画提层的子元素会变透明,
  269. // 表头上「Workflow running」就成了一段空白(round 7 修订)。
  270. const view = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: joined() });
  271. const kind = view.getByTestId("workflow-card-kind");
  272. expect(kind.textContent).toBe("Workflow running");
  273. const gradient = kind.querySelector(".animated-gradient-text")!;
  274. expect(gradient).toBeTruthy();
  275. expect(gradient.querySelector(".wf-swap")).toBeNull();
  276. expect(gradient.closest(".wf-swap")).toBeTruthy();
  277. expect(gradient.textContent).toBe("Workflow running");
  278. });
  279. });
  280. describe("CreateWorkflowToolCallBlock 静态形态(编过、未联接 run)", () => {
  281. it("表头:种类词 + 名字,右侧空环灯「compiled」+ 等宽计数;卡体是时间线,脚本折叠", () => {
  282. const view = renderBlockDom(LOOP_TOOL_CALL);
  283. const card = view.getByTestId("workflow-card");
  284. expect(card.getAttribute("data-workflow-card-state")).toBe("static");
  285. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow");
  286. expect(view.getByTestId("workflow-card-name").textContent).toBe("Research pipeline");
  287. expect(view.getByTestId("workflow-card-static-status").textContent).toBe("compiled");
  288. expect(view.getByTestId("workflow-card-detail").textContent).toBe("2 phases · 2 agents");
  289. // 绿色留给等待确认态:compiled 不是成功色徽标。
  290. expect(card.innerHTML).not.toContain("text-success");
  291. const stations = view.getAllByTestId("workflow-timeline-station");
  292. expect(stations.map((station) => station.textContent)).toEqual([
  293. expect.stringContaining("plan"),
  294. expect.stringContaining("verify"),
  295. ]);
  296. expect(view.getAllByTestId("workflow-agent-pill").map((pill) => pill.textContent)).toEqual([
  297. expect.stringContaining("planner"),
  298. expect.stringContaining("checker"),
  299. ]);
  300. // 相邻的边是淡墨轨道段;回边是弧。
  301. expect(view.getByTestId("workflow-timeline-rail").getAttribute("data-rail-ink")).toBe("faint");
  302. const arc = view.getByTestId("workflow-timeline-arc");
  303. expect(arc.getAttribute("data-arc-from")).toBe("1");
  304. expect(arc.getAttribute("data-arc-to")).toBe("0");
  305. // 最低一道的弧也要有一段像样的下落:圆角之后的竖段向下且不短于箭头(回归:16px 的道高让最后
  306. // 一段向上走了一像素,`marker-end` 跟着朝上,箭头就不见了)。
  307. const d = arc.querySelector("path")!.getAttribute("d")!;
  308. const verticals = [...d.matchAll(/V(-?[\d.]+)/gu)].map((match) => Number(match[1]));
  309. const corner = Number(
  310. /Q[^ ]+ [^,]+,([\d.]+)$/u.exec(d.slice(0, d.lastIndexOf("V")).trimEnd())?.[1],
  311. );
  312. expect(verticals).toHaveLength(2);
  313. expect(verticals[1]! - corner).toBeGreaterThanOrEqual(8);
  314. // 没有汇点、没有「may terminate」。
  315. expect(card.textContent).not.toContain("terminate");
  316. // 脚本原文只能在这里读:默认收起,点开才进 DOM。
  317. expect(view.queryByTestId("code-block")).toBeNull();
  318. fireEvent.click(view.getByTestId("workflow-card-script-toggle"));
  319. expect(view.getByTestId("code-block").textContent).toContain('phase("plan")');
  320. });
  321. it("无标记脚本:一个隐式站「Workflow」,药丸是那条车道的作者原名", () => {
  322. const view = renderBlockDom({
  323. input: { script: "x" },
  324. raw: { display: { ...OK_DISPLAY, causalityGraph: FLAT_GRAPH } },
  325. });
  326. expect(view.getByTestId("workflow-timeline-station").textContent).toContain("Workflow");
  327. expect(view.getByTestId("workflow-agent-pill").textContent).toContain("a");
  328. expect(view.getByTestId("workflow-card-detail").textContent).toBe("1 phase · 1 agent");
  329. });
  330. it("没有图(或零 step 的图)时不画时间线,脚本折叠仍在", () => {
  331. const html = renderBlock({ input: { script: "const x = 1;" }, raw: { display: OK_DISPLAY } });
  332. expect(html).not.toContain("workflow-timeline");
  333. expect(html).toContain("workflow-card-script-toggle");
  334. expect(html).not.toContain("workflow-card-detail");
  335. const empty = renderBlock({
  336. input: { script: "x" },
  337. raw: {
  338. display: {
  339. ...OK_DISPLAY,
  340. causalityGraph: { steps: [], lanes: [], participants: [], handoffs: [] },
  341. },
  342. },
  343. });
  344. expect(empty).not.toContain("workflow-timeline");
  345. });
  346. it("联接不到 run 时没有页脚、没有 run 属性;回调在而 run 摘要不在时仍是静态卡", () => {
  347. const view = renderBlockDom(LOOP_TOOL_CALL, { onOpenWorkflowRun: () => {} });
  348. const card = view.getByTestId("workflow-card");
  349. expect(card.getAttribute("data-workflow-run-id")).toBeNull();
  350. expect(view.queryByTestId("workflow-card-footer")).toBeNull();
  351. expect(view.queryByTestId("workflow-card-status")).toBeNull();
  352. expect(html(view)).not.toContain("Workflow started");
  353. });
  354. it("站数超过一列时表头不再带秩带(看不见的站在时间线的边檐上)", () => {
  355. const phases = ["a", "b", "c", "d", "e", "f"].map((name, i) => ({
  356. id: `phase#${name}`,
  357. name,
  358. line: i + 1,
  359. }));
  360. const steps = phases.map((phase, i) => ({
  361. id: `ask#${i + 1}`,
  362. kind: "ask",
  363. label: phase.name,
  364. lane: "actor#1",
  365. phase: phase.id,
  366. }));
  367. const view = renderBlockDom({
  368. input: { script: "x" },
  369. raw: {
  370. display: {
  371. ...OK_DISPLAY,
  372. causalityGraph: {
  373. steps,
  374. lanes: [{ id: "actor#1", name: "worker" }],
  375. participants: steps.map((step) => ({
  376. id: `${step.phase}:actor#1`,
  377. phase: step.phase,
  378. lane: "actor#1",
  379. steps: [step.id],
  380. })),
  381. handoffs: [],
  382. phases,
  383. phaseEdges: phases.slice(1).map((phase, i) => ({ from: phases[i]!.id, to: phase.id })),
  384. exits: ["phase#f"],
  385. },
  386. },
  387. },
  388. });
  389. // 秩带已退役(追记「边檐」):看不见的站在时间线自己的边檐上,表头不再复述。
  390. expect(view.queryByTestId("workflow-rank-strip")).toBeNull();
  391. expect(view.getAllByTestId("workflow-timeline-station")).toHaveLength(6);
  392. });
  393. it("shows the graph truncation note when steps were omitted", () => {
  394. const view = renderBlockDom({
  395. input: { script: "x" },
  396. raw: { display: { ...OK_DISPLAY, causalityGraph: { ...FLAT_GRAPH, truncated: true } } },
  397. });
  398. expect(view.container.textContent).toContain("some participants were omitted");
  399. });
  400. it("falls back to the plain output text when no display payload exists", () => {
  401. const markup = renderBlock({
  402. input: { script: "export const meta = {};" },
  403. output: { diagnostics: [], ok: true, response: "The workflow script compiled cleanly." },
  404. raw: {},
  405. });
  406. expect(markup).toContain("The workflow script compiled cleanly.");
  407. expect(markup).toContain("workflow-card-script-toggle");
  408. // 无 display 时不应出现结构化反馈卡、compiled 词或待修正计数。
  409. expect(markup).not.toContain("Compiler feedback");
  410. expect(markup).not.toContain("workflow-draft-lamp");
  411. expect(markup).not.toContain("workflow-card-static-status");
  412. expect(markup).not.toContain("&quot;ok&quot;");
  413. });
  414. it("does not crash and shows a hint when input.script is missing", () => {
  415. const markup = renderBlock({ input: { name: "No script here" }, raw: {} });
  416. expect(markup).toContain("No workflow script provided.");
  417. expect(markup).not.toContain("code-block");
  418. });
  419. it("saved 来源行在时间线之上", () => {
  420. const view = renderBlockDom({
  421. input: {
  422. name: "Fan-out review",
  423. script: "x",
  424. saved: {
  425. name: "fan-out-review",
  426. scope: "project",
  427. path: "/workspace/.zcode/workflows/fan-out-review.dwf.ts",
  428. },
  429. },
  430. raw: { display: { ...OK_DISPLAY, causalityGraph: FLAT_GRAPH } },
  431. });
  432. const savedLine = view.container.querySelector('[data-workflow-card-saved-source="true"]')!;
  433. expect(savedLine.textContent).toContain("fan-out-review");
  434. const body = view.getByTestId("workflow-card-body");
  435. const order = Array.from(body.children);
  436. expect(order.indexOf(savedLine as HTMLElement)).toBeLessThan(
  437. order.indexOf(view.getByTestId("workflow-timeline")),
  438. );
  439. });
  440. });
  441. function html(view: ReturnType<typeof render>): string {
  442. return view.container.innerHTML;
  443. }
  444. // ── 编译反馈(docs/dynamic-workflow/presentation.md「Compiler feedback」)──────────────────
  445. // 编不过不是失败:什么都没跑,反馈交回了模型。行只用 ToolLayout 已有的槽位说这件事,全程不用 destructive。
  446. describe("CreateWorkflowToolCallBlock 编译反馈", () => {
  447. const FEEDBACK_DISPLAY = {
  448. kind: "create_workflow",
  449. ok: false,
  450. errorCount: 2,
  451. diagnostics: [
  452. { line: 3, column: 5, code: 2304, message: "Cannot find name 'agent'." },
  453. {
  454. line: 8,
  455. column: 1,
  456. code: 9003,
  457. message: "world.run's first argument must be a compile-time string literal.",
  458. },
  459. ],
  460. };
  461. const FEEDBACK_CALL: ToolCallOverrides = {
  462. input: { name: "Release notes digest", script: "const x: number = 'nope';" },
  463. output: { diagnostics: [], ok: false, response: "The workflow script has errors:" },
  464. raw: { display: FEEDBACK_DISPLAY },
  465. };
  466. const LATEST: WorkflowDraftPosition = { ordinal: 2, superseded: false };
  467. it("行:「Workflow draft」+ 名字 + 稿号 + 警示色空环灯 +「2 to fix · not run」;不是工作流卡", () => {
  468. const view = renderBlockDom(FEEDBACK_CALL, { workflowDraft: LATEST, forceOpen: false });
  469. const row = view.getByRole("button");
  470. expect(row.getAttribute("aria-expanded")).toBe("false");
  471. expect(row.querySelector(".tool-summary-kind-label")?.textContent).toBe("Workflow draft");
  472. expect(row.textContent).toContain("Release notes digest");
  473. expect(view.getByTestId("workflow-draft-ordinal").textContent).toBe("draft 2");
  474. expect(view.getByTestId("workflow-draft-status").textContent).toBe("2 to fix · not run");
  475. const lamp = view.getByTestId("workflow-draft-lamp");
  476. expect(lamp.getAttribute("data-draft-lamp")).toBe("open");
  477. expect(lamp.className).toContain("border-warning");
  478. expect(lamp.className).toContain("bg-transparent");
  479. // 灯在下划线的状态词之外:提示触发的只是词。
  480. expect(view.getByTestId("workflow-draft-status").contains(lamp)).toBe(false);
  481. expect(row.innerHTML).not.toContain("destructive");
  482. expect(view.queryByTestId("workflow-card")).toBeNull();
  483. expect(view.queryByTestId("workflow-timeline")).toBeNull();
  484. });
  485. it("有更新的一稿:灯褪成中性,稿号与词不变", () => {
  486. const view = renderBlockDom(FEEDBACK_CALL, {
  487. workflowDraft: { ordinal: 1, superseded: true },
  488. forceOpen: false,
  489. });
  490. const lamp = view.getByTestId("workflow-draft-lamp");
  491. expect(lamp.getAttribute("data-draft-lamp")).toBe("settled");
  492. expect(lamp.className).toContain("border-foreground-subtlest");
  493. expect(lamp.className).not.toContain("border-warning");
  494. expect(view.getByTestId("workflow-draft-ordinal").textContent).toBe("draft 1");
  495. expect(view.getByTestId("workflow-draft-status").textContent).toBe("2 to fix · not run");
  496. });
  497. it("宿主没有稿号联接:不编号,灯保持警示色", () => {
  498. const view = renderBlockDom(FEEDBACK_CALL, { forceOpen: false });
  499. expect(view.queryByTestId("workflow-draft-ordinal")).toBeNull();
  500. expect(view.getByTestId("workflow-draft-lamp").getAttribute("data-draft-lamp")).toBe("open");
  501. });
  502. it("展开:脚本带行号、被点名的行号着色;反馈卡中性边框,标题 + 条数 + 一句话 + 逐条", () => {
  503. const view = renderBlockDom(FEEDBACK_CALL, { workflowDraft: LATEST, forceOpen: true });
  504. // 展开后稿号仍在行上(不随展开隐藏)。
  505. expect(view.getByTestId("workflow-draft-ordinal").textContent).toBe("draft 2");
  506. const code = view.getByTestId("code-block");
  507. expect(code.getAttribute("data-show-line-numbers")).toBe("true");
  508. expect(code.getAttribute("data-marked-lines")).toBe("3,8");
  509. const card = view.getByTestId("workflow-compiler-feedback");
  510. expect(card.className).toContain("border-border");
  511. expect(card.outerHTML).not.toContain("destructive");
  512. expect(within(card).getByTestId("workflow-compiler-feedback-title").textContent).toBe(
  513. "Compiler feedback",
  514. );
  515. expect(within(card).getByTestId("workflow-compiler-feedback-count").textContent).toBe(
  516. "2 notes",
  517. );
  518. expect(within(card).getByTestId("workflow-compiler-feedback-lede").textContent).toBe(
  519. "Typecheck did not pass, so nothing ran. The feedback went back to the model to revise and resubmit.",
  520. );
  521. const lines = within(card).getAllByTestId("workflow-compiler-feedback-line");
  522. expect(lines).toHaveLength(2);
  523. expect(lines[0]!.textContent).toContain("L3:C5");
  524. expect(lines[0]!.textContent).toContain("Cannot find name");
  525. expect(lines[0]!.textContent).toContain("TS2304");
  526. // 分析器自有规则不是 TypeScript 码。
  527. expect(lines[1]!.textContent).toContain("rule 9003");
  528. expect(lines[1]!.textContent).not.toContain("TS9003");
  529. });
  530. it("单条反馈用单数;条数读 errorCount 而不是截断后的行数", () => {
  531. const one = renderBlockDom(
  532. {
  533. ...FEEDBACK_CALL,
  534. raw: {
  535. display: {
  536. ...FEEDBACK_DISPLAY,
  537. errorCount: 1,
  538. diagnostics: [FEEDBACK_DISPLAY.diagnostics[0]],
  539. },
  540. },
  541. },
  542. { forceOpen: true },
  543. );
  544. expect(one.getByTestId("workflow-compiler-feedback-count").textContent).toBe("1 note");
  545. expect(one.getByTestId("workflow-draft-status").textContent).toBe("1 to fix · not run");
  546. cleanup();
  547. const many = renderBlockDom(
  548. {
  549. ...FEEDBACK_CALL,
  550. raw: { display: { ...FEEDBACK_DISPLAY, errorCount: 150, truncated: true } },
  551. },
  552. { forceOpen: true },
  553. );
  554. expect(many.getByTestId("workflow-compiler-feedback-count").textContent).toBe("150 notes");
  555. expect(many.container.textContent).toContain("Some diagnostics were omitted.");
  556. });
  557. it("保存的来源编不过:那句话点名文件,而不是这次调用", () => {
  558. const view = renderBlockDom(
  559. {
  560. ...FEEDBACK_CALL,
  561. input: {
  562. name: "Release notes digest",
  563. script: "const x: number = 'nope';",
  564. saved: {
  565. name: "release-notes",
  566. scope: "project",
  567. path: ".zcode/workflows/release-notes.ts",
  568. },
  569. },
  570. },
  571. { forceOpen: true },
  572. );
  573. expect(view.getByTestId("workflow-compiler-feedback-lede").textContent).toBe(
  574. "The saved workflow file did not pass typecheck, so nothing ran. The feedback went back to the model to fix the file or save a corrected version.",
  575. );
  576. });
  577. it("zh-CN:「工作流草稿 · 第 1 稿 · 2 处待修正 · 未运行」与「编译反馈 · 2 条 · 规则 9003」", () => {
  578. const view = renderBlockDom(
  579. FEEDBACK_CALL,
  580. { workflowDraft: { ordinal: 1, superseded: false }, forceOpen: true },
  581. "zh-CN",
  582. );
  583. expect(view.container.querySelector(".tool-summary-kind-label")?.textContent).toBe(
  584. "工作流草稿",
  585. );
  586. expect(view.getByTestId("workflow-draft-ordinal").textContent).toBe("第 1 稿");
  587. expect(view.getByTestId("workflow-draft-status").textContent).toBe("2 处待修正 · 未运行");
  588. expect(view.getByTestId("workflow-compiler-feedback-title").textContent).toBe("编译反馈");
  589. expect(view.getByTestId("workflow-compiler-feedback-count").textContent).toBe("2 条");
  590. expect(view.getByTestId("workflow-compiler-feedback-lede").textContent).toBe(
  591. "类型检查未通过,脚本未运行;反馈已交回模型,由它修改后重新提交。",
  592. );
  593. expect(view.container.textContent).toContain("规则 9003");
  594. });
  595. it("提示:先说那一句话,再逐条列出位置与消息", () => {
  596. expect(formatWorkflowFeedbackTooltip("Nothing ran.", FEEDBACK_DISPLAY.diagnostics)).toBe(
  597. "Nothing ran.\nL3:C5 Cannot find name 'agent'.\nL8:C1 world.run's first argument must be a compile-time string literal.",
  598. );
  599. expect(formatWorkflowFeedbackTooltip("Nothing ran.", [])).toBe("Nothing ran.");
  600. });
  601. it("失败但没有 display(被拒、工具报错):仍是「Workflow」+ 工具状态词,没有灯与稿号", () => {
  602. const view = renderBlockDom(
  603. {
  604. input: { name: "Release notes digest", script: "x" },
  605. output: "run_not_found: no such run",
  606. status: "failed",
  607. },
  608. { statusLabel: "Failed", workflowDraft: LATEST, forceOpen: false },
  609. );
  610. expect(view.container.querySelector(".tool-summary-kind-label")?.textContent).toBe("Workflow");
  611. expect(view.container.textContent).toContain("Failed");
  612. expect(view.queryByTestId("workflow-draft-lamp")).toBeNull();
  613. expect(view.queryByTestId("workflow-draft-ordinal")).toBeNull();
  614. });
  615. it("编不过时即使宿主联接到了 run 也留在反馈行", () => {
  616. const markup = renderBlock(
  617. {
  618. input: { script: "x" },
  619. raw: {
  620. display: {
  621. kind: "create_workflow",
  622. ok: false,
  623. errorCount: 1,
  624. diagnostics: [{ line: 1, column: 1, code: 2304, message: "boom" }],
  625. },
  626. },
  627. },
  628. { workflowRun: joined() },
  629. );
  630. expect(markup).toContain("Compiler feedback");
  631. expect(markup).not.toContain('data-testid="workflow-card"');
  632. expect(markup).not.toContain("Workflow running");
  633. });
  634. });
  635. describe("CreateWorkflowToolCallBlock 启动前 ToolLayout", () => {
  636. const DRAFT =
  637. 'phase("plan");\nconst planner = agent("planner");\nawait world.run("pnpm test");\nphase("ver';
  638. // 草稿由笔逐字写出(追记「书写效果」):断言最终形态前先让笔写完。
  639. beforeEach(() => {
  640. vi.useFakeTimers();
  641. });
  642. afterEach(() => {
  643. vi.useRealTimers();
  644. });
  645. // 笔每写一字都是 setState → effect 再排下一枚定时器;一次 advance 只走一步,所以按步长逐段推进。
  646. const settle = () => {
  647. for (let i = 0; i < 120; i += 1) {
  648. act(() => {
  649. vi.advanceTimersByTime(24);
  650. });
  651. }
  652. };
  653. it("编写中:行不可展开(forceOpen 也不展开),草稿阶段线常驻行下,站随笔写出、没有药丸", () => {
  654. const view = renderBlockDom(
  655. {
  656. input: { name: "Research pipeline", script: DRAFT },
  657. raw: { v4Status: "inputStreaming" },
  658. status: "running",
  659. },
  660. { isRunning: true, forceOpen: true },
  661. );
  662. expect(view.container.textContent).toContain("Writing workflow");
  663. expect(view.container.textContent).toContain("Research pipeline");
  664. expect(view.container.querySelector(".tool-summary-kind-label")).toBeTruthy();
  665. expect(view.container.querySelector("[aria-expanded]")).toBeNull();
  666. expect(view.queryByTestId("workflow-card")).toBeNull();
  667. expect(view.queryByTestId("code-block")).toBeNull();
  668. // 笔刚落下:第一站已揭示但一个字还没写,光标稳住(正在写);第二站还不在轨道上。
  669. expect(view.getByTestId("workflow-draft-timeline")).toBeTruthy();
  670. expect(view.getAllByTestId("workflow-timeline-station")).toHaveLength(1);
  671. expect(view.getByTestId("workflow-timeline-caret").getAttribute("data-pen")).toBe("writing");
  672. settle();
  673. const stations = view.getAllByTestId("workflow-timeline-station");
  674. expect(stations.map((station) => station.textContent)).toEqual(["plan", "ver"]);
  675. // 笔追上了流:光标在最后一站的名字后面闪烁。
  676. const caret = view.getByTestId("workflow-timeline-caret");
  677. expect(stations[1]!.contains(caret)).toBe(true);
  678. expect(caret.getAttribute("data-pen")).toBe("idle");
  679. expect(caret.className).toContain("wf-caret");
  680. // 草稿只有阶段线:没有药丸、没有弧;阶段线不是展开内容,行上仍没有展开入口。
  681. expect(view.queryByTestId("workflow-agent-pill")).toBeNull();
  682. expect(view.queryByTestId("workflow-timeline-arc")).toBeNull();
  683. expect(view.container.querySelector("[aria-expanded]")).toBeNull();
  684. });
  685. it("display 一到,草稿站与光标离场:卡换成分析器的站与药丸(不变式 10)", () => {
  686. const view = renderBlockDom(
  687. {
  688. input: { name: "Research pipeline", script: DRAFT },
  689. raw: { v4Status: "inputStreaming" },
  690. status: "running",
  691. },
  692. { isRunning: true },
  693. );
  694. settle();
  695. expect(view.getAllByTestId("workflow-timeline-station")).toHaveLength(2);
  696. view.rerender(createBlock(LOOP_TOOL_CALL));
  697. expect(view.queryByTestId("workflow-draft-timeline")).toBeNull();
  698. expect(view.queryByTestId("workflow-timeline-caret")).toBeNull();
  699. const stations = view.getAllByTestId("workflow-timeline-station");
  700. expect(stations.map((station) => station.textContent)).toEqual([
  701. expect.stringContaining("plan"),
  702. expect.stringContaining("verify"),
  703. ]);
  704. expect(view.getAllByTestId("workflow-agent-pill")).toHaveLength(2);
  705. expect(view.getByTestId("workflow-timeline-arc")).toBeTruthy();
  706. });
  707. it("编写中脚本还没到一个字:只有行,没有阶段线块", () => {
  708. const view = renderBlockDom(
  709. {
  710. input: { name: "Research pipeline" },
  711. raw: { v4Status: "inputStreaming" },
  712. status: "running",
  713. },
  714. { isRunning: true },
  715. );
  716. expect(view.container.textContent).toContain("Writing workflow");
  717. expect(view.queryByTestId("workflow-draft-timeline")).toBeNull();
  718. expect(view.queryByTestId("workflow-timeline-station")).toBeNull();
  719. });
  720. it.each(["en-US", "zh-CN"] as const)("等待确认可展开脚本、再次收起:%s", (locale) => {
  721. const view = renderBlockDom(
  722. {
  723. ...LOOP_TOOL_CALL,
  724. raw: { ...(LOOP_TOOL_CALL.raw as object), v4Status: "pendingApproval" },
  725. status: "running",
  726. },
  727. { isRunning: true, forceOpen: false },
  728. locale,
  729. );
  730. expect(view.container.querySelector(".tool-summary-kind-label")).toBeTruthy();
  731. expect(
  732. view.container.querySelector(".tool-summary-kind-label.animated-gradient-text"),
  733. ).toBeTruthy();
  734. expect(view.container.querySelector(".truncate.font-mono")).toBeNull();
  735. expect(view.queryByTestId("workflow-card")).toBeNull();
  736. const trigger = view.getByRole("button");
  737. expect(trigger.getAttribute("aria-expanded")).toBe("false");
  738. expect(view.queryByTestId("code-block")).toBeNull();
  739. fireEvent.keyDown(trigger, { key: "Enter" });
  740. expect(trigger.getAttribute("aria-expanded")).toBe("true");
  741. expect(view.getByTestId("code-block").textContent).toContain("agent");
  742. expect(view.getByTestId("code-block").className).toContain("max-h-80");
  743. expect(view.getByTestId("workflow-script-codeblock").className).not.toContain("overflow-auto");
  744. fireEvent.click(trigger);
  745. expect(trigger.getAttribute("aria-expanded")).toBe("false");
  746. });
  747. });
  748. // ── 联接到 run:图整个留在聊天区,灯、药丸、墨迹随投影 ─────────────────────────────
  749. describe("CreateWorkflowToolCallBlock 运行态", () => {
  750. it("表头:种类词「Workflow running」、灯 + 状态词、步数细节;没有紧凑卡", () => {
  751. const view = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: joined() });
  752. const card = view.getByTestId("workflow-card");
  753. expect(card.getAttribute("data-workflow-card-state")).toBe("running");
  754. expect(card.getAttribute("data-workflow-run-id")).toBe("dwfrun-42");
  755. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow running");
  756. expect(view.getByTestId("workflow-card-status").textContent).toBe("Running");
  757. expect(view.getByTestId("workflow-card-detail").textContent).toBe("2 phases · 1 agent working");
  758. expect(view.queryByTestId("workflow-run-card")).toBeNull();
  759. expect(view.queryByTestId("workflow-card-static-status")).toBeNull();
  760. });
  761. it("站与药丸读投影:站灯、运行时名、状态标记;行进边是进入运行站的那一段", () => {
  762. const view = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: joined() });
  763. const stations = view.getAllByTestId("workflow-timeline-station");
  764. expect(stations.map((station) => station.getAttribute("data-station-status"))).toEqual([
  765. "done",
  766. "running",
  767. ]);
  768. expect(stations[0]!.textContent).toContain("1/1");
  769. const pills = view.getAllByTestId("workflow-agent-pill");
  770. expect(pills.map((pill) => pill.textContent)).toEqual([
  771. expect.stringContaining("Ada"),
  772. expect.stringContaining("Bob"),
  773. ]);
  774. expect(pills.map((pill) => pill.getAttribute("data-agent-status"))).toEqual([
  775. "done",
  776. "running",
  777. ]);
  778. // 状态不只靠颜色:标记带可读文字。
  779. expect(
  780. pills[1]!.querySelector('[data-testid="workflow-pill-status"]')?.getAttribute("aria-label"),
  781. ).toBe("running");
  782. expect(view.getByTestId("workflow-timeline-rail").getAttribute("data-rail-ink")).toBe("march");
  783. expect(view.getByTestId("workflow-timeline-arc").getAttribute("data-arc-ink")).toBe("faint");
  784. });
  785. it("running 同时有动画点和状态词,绝不只靠颜色或动画表达状态", () => {
  786. const view = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: joined() });
  787. const header = view.getByTestId("workflow-card-header");
  788. expect(header.querySelector(".animate-pulse")).toBeTruthy();
  789. expect(header.textContent).toContain("Running");
  790. });
  791. it("运行态没有页脚:卡上只说阶段与子代理,都在表头;步数、token、轮次不上卡(2026-09-09)", () => {
  792. const view = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: joined() });
  793. expect(view.queryByTestId("workflow-card-footer")).toBeNull();
  794. const card = view.getByTestId("workflow-card").textContent ?? "";
  795. expect(view.getByTestId("workflow-card-detail").textContent).toBe("2 phases · 1 agent working");
  796. expect(card).not.toContain("steps");
  797. expect(card).not.toContain("tokens");
  798. expect(view.container.querySelector("progress")).toBeNull();
  799. });
  800. it("第二轮:回边浓墨并成为行进边,站头显示 ⟳ 2;表头不数轮次,仍只说阶段与子代理", () => {
  801. const view = renderBlockDom(LOOP_TOOL_CALL, {
  802. workflowRun: joined({
  803. actors: [
  804. { siteId: "actor#1", ordinal: 2, name: "Ada", sessionId: "s-1b", status: "running" },
  805. { siteId: "actor#2", ordinal: 1, name: "Bob", sessionId: "s-2", status: "completed" },
  806. ],
  807. nodes: [
  808. {
  809. siteId: "ask#1",
  810. ordinal: 1,
  811. phase: "settled",
  812. outcome: "ok",
  813. actorSiteId: "actor#1",
  814. actorOrdinal: 1,
  815. },
  816. {
  817. siteId: "ask#2",
  818. ordinal: 1,
  819. phase: "settled",
  820. outcome: "ok",
  821. actorSiteId: "actor#2",
  822. actorOrdinal: 1,
  823. },
  824. {
  825. siteId: "ask#1",
  826. ordinal: 2,
  827. phase: "executing",
  828. actorSiteId: "actor#1",
  829. actorOrdinal: 2,
  830. },
  831. ] as WorkflowRunState["nodes"],
  832. }),
  833. });
  834. expect(view.getByTestId("workflow-timeline-arc").getAttribute("data-arc-ink")).toBe("march");
  835. expect(view.getByTestId("workflow-timeline-rail").getAttribute("data-rail-ink")).toBe("strong");
  836. const rounds = view.getAllByTestId("workflow-timeline-rounds");
  837. expect(rounds[0]!.textContent).toContain("2");
  838. expect(view.getByTestId("workflow-card-detail").textContent).toBe("2 phases · 1 agent working");
  839. expect(view.queryByTestId("workflow-card-footer")).toBeNull();
  840. });
  841. it("已完成:墨迹 = 轨迹,表头换成终态计数(阶段数 · 子代理数);没有页脚", () => {
  842. const onOpenWorkflowArtifact = vi.fn();
  843. const view = renderBlockDom(LOOP_TOOL_CALL, {
  844. onOpenWorkflowArtifact,
  845. workflowRun: joined({
  846. status: "completed",
  847. actors: [
  848. { siteId: "actor#1", ordinal: 1, name: "Ada", sessionId: "s-1", status: "completed" },
  849. { siteId: "actor#2", ordinal: 1, name: "Bob", sessionId: "s-2", status: "completed" },
  850. ],
  851. nodes: [
  852. { siteId: "ask#1", ordinal: 1, phase: "settled", outcome: "ok" },
  853. { siteId: "ask#2", ordinal: 1, phase: "settled", outcome: "ok" },
  854. ] as WorkflowRunState["nodes"],
  855. reports: [
  856. { id: "r1", kind: "text", title: "Summary", text: "done" },
  857. ] as WorkflowRunState["reports"],
  858. artifacts: [
  859. { id: "book", kind: "file", title: "Audit report", version: 2 },
  860. ] as WorkflowRunState["artifacts"],
  861. }),
  862. });
  863. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow completed");
  864. expect(view.getByTestId("workflow-card-status").textContent).toBe("Completed");
  865. expect(view.getByTestId("workflow-timeline-rail").getAttribute("data-rail-ink")).toBe("strong");
  866. expect(view.getByTestId("workflow-timeline-arc").getAttribute("data-arc-ink")).toBe("faint");
  867. expect(view.getByTestId("workflow-card-header").querySelector(".animate-pulse")).toBeNull();
  868. expect(view.queryByTestId("workflow-card-footer")).toBeNull();
  869. expect(view.getByTestId("workflow-card-detail").textContent).toBe("2 phases · 2 agents");
  870. const card = view.getByTestId("workflow-card").textContent ?? "";
  871. expect(card).not.toContain("steps");
  872. expect(card).not.toContain("tokens");
  873. // 产物只在产物条上出现(不再有「1 artifact」计数);「results」在屏幕上仍没有落点。
  874. expect(card).not.toContain("artifact");
  875. expect(card).not.toContain("result");
  876. expect(card).not.toContain("working");
  877. // 时间线下挂产物条:一枚可点的药丸,尾槽里是 v2。
  878. const strip = view.getByTestId("workflow-card-artifacts");
  879. const pills = within(strip).getAllByTestId("workflow-card-artifact");
  880. expect(pills).toHaveLength(1);
  881. expect(pills[0]?.tagName).toBe("BUTTON");
  882. expect(pills[0]?.getAttribute("data-artifact-open")).toBe("true");
  883. expect(within(strip).getByTestId("workflow-run-artifact-version").textContent).toBe("v2");
  884. fireEvent.click(pills[0]!);
  885. expect(onOpenWorkflowArtifact).toHaveBeenCalledWith("book");
  886. });
  887. it("宿主没注入产物打开能力时药丸禁用,产物条仍在场", () => {
  888. const view = renderBlockDom(LOOP_TOOL_CALL, {
  889. workflowRun: joined({
  890. status: "completed",
  891. artifacts: [
  892. { id: "book", kind: "file", title: "Audit report", version: 1 },
  893. ] as WorkflowRunState["artifacts"],
  894. }),
  895. });
  896. const pill = view.getByTestId("workflow-card-artifact") as HTMLButtonElement;
  897. expect(pill.disabled).toBe(true);
  898. expect(pill.getAttribute("data-artifact-open")).toBeNull();
  899. expect(view.queryByTestId("workflow-run-artifact-version")).toBeNull();
  900. });
  901. it("⤢ 打开详情页:只在宿主给了回调时在场,请求带展示名;点站头走同一条路", () => {
  902. const onOpenWorkflowRun = vi.fn();
  903. const view = renderBlockDom(LOOP_TOOL_CALL, { onOpenWorkflowRun, workflowRun: joined() });
  904. fireEvent.click(view.getByTestId("workflow-card-open-details"));
  905. expect(onOpenWorkflowRun).toHaveBeenCalledWith({ workflowName: "Research pipeline" });
  906. fireEvent.click(view.getAllByTestId("workflow-timeline-station")[1]!.querySelector("button")!);
  907. expect(onOpenWorkflowRun).toHaveBeenCalledTimes(2);
  908. cleanup();
  909. // 没有回调时站头不是控件(span,不是禁用的 button):禁用按钮会吞掉点击,轮尾摘要的整块开关
  910. // 就收不到(追记「点击语法修订」)。
  911. const gated = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: joined() });
  912. expect(gated.queryByTestId("workflow-card-open-details")).toBeNull();
  913. const station = gated.getAllByTestId("workflow-timeline-station")[0]!;
  914. expect(station.querySelector("button")).toBeNull();
  915. expect(station.querySelector('[data-testid="workflow-timeline-station-head"]')).not.toBeNull();
  916. });
  917. it("展示名缺席时请求里不带 workflowName", () => {
  918. const onOpenWorkflowRun = vi.fn();
  919. const view = renderBlockDom(
  920. { ...LOOP_TOOL_CALL, input: { script: "x" } },
  921. { onOpenWorkflowRun, workflowRun: joined() },
  922. );
  923. expect(view.getByTestId("workflow-card-name").textContent).toBe("Workflow script");
  924. fireEvent.click(view.getByTestId("workflow-card-open-details"));
  925. expect(onOpenWorkflowRun).toHaveBeenCalledWith({});
  926. });
  927. it("运行中折叠:卡体与页脚都收起;表头的灯与细节仍然在场", () => {
  928. const view = renderBlockDom(LOOP_TOOL_CALL, {
  929. forceOpen: false,
  930. canToggle: true,
  931. workflowRun: joined(),
  932. });
  933. expect(view.getByTestId("workflow-card-body")).toBeTruthy();
  934. fireEvent.click(view.getByTestId("workflow-card-toggle"));
  935. expect(view.queryByTestId("workflow-card-body")).toBeNull();
  936. expect(view.queryByTestId("workflow-card-footer")).toBeNull();
  937. expect(view.getByTestId("workflow-card-status").textContent).toBe("Running");
  938. expect(view.getByTestId("workflow-card-detail").textContent).toBe("2 phases · 1 agent working");
  939. fireEvent.click(view.getByTestId("workflow-card-toggle"));
  940. expect(view.getByTestId("workflow-card-body")).toBeTruthy();
  941. });
  942. it("forceOpen / canToggle=false 时没有折叠按钮", () => {
  943. const view = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: joined() });
  944. expect(view.queryByTestId("workflow-card-toggle")).toBeNull();
  945. cleanup();
  946. const locked = renderBlockDom(LOOP_TOOL_CALL, {
  947. forceOpen: false,
  948. canToggle: false,
  949. workflowRun: joined(),
  950. });
  951. expect(locked.queryByTestId("workflow-card-toggle")).toBeNull();
  952. expect(locked.getByTestId("workflow-card-body")).toBeTruthy();
  953. });
  954. it("联接到 run 后脚本折叠离开卡面:脚本原文去详情页的 Script 区读", () => {
  955. const view = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: joined() });
  956. expect(view.queryByTestId("workflow-card-script-toggle")).toBeNull();
  957. expect(view.queryByTestId("code-block")).toBeNull();
  958. });
  959. it("卡后面仍然挂着快照字段提示", () => {
  960. const view = renderBlockDom(
  961. {
  962. ...LOOP_TOOL_CALL,
  963. snapshotRefs: [{ field: "input", previewBytes: 100, fullBytes: 5_000 }],
  964. },
  965. { onLoadFullToolCallFields: () => {}, workflowRun: joined() },
  966. );
  967. expect(view.getByTestId("workflow-card")).toBeTruthy();
  968. expect(view.container.textContent).toContain("Load full tool data");
  969. });
  970. it("zh-CN 下卡面全部本地化", () => {
  971. const view = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: joined() }, "zh-CN");
  972. const card = view.getByTestId("workflow-card");
  973. expect(view.getByTestId("workflow-card-kind").textContent).toBe("工作流运行中");
  974. expect(view.getByTestId("workflow-card-status").textContent).toBe("运行中");
  975. expect(card.textContent).toContain("2 个阶段 · 1 个子代理工作中");
  976. expect(card.textContent).toContain("1 个子代理工作中");
  977. expect(card.textContent).not.toContain("Workflow running");
  978. expect(card.textContent).not.toContain("steps");
  979. });
  980. });
  981. // ── 药丸 → 子代理 transcript(docs/dynamic-workflow/presentation.md「The pill」)──
  982. describe("CreateWorkflowToolCallBlock 药丸点击", () => {
  983. it("带会话的药丸是按钮:点一下交出实例身份(run、会话、站点、序号、运行时名)", () => {
  984. const onOpenWorkflowActor = vi.fn();
  985. const view = renderBlockDom(LOOP_TOOL_CALL, { onOpenWorkflowActor, workflowRun: joined() });
  986. const pills = view.getAllByTestId("workflow-agent-pill");
  987. expect(pills.map((pill) => pill.tagName)).toEqual(["BUTTON", "BUTTON"]);
  988. expect(pills[1]!.getAttribute("aria-label")).toBe("Open the transcript of Bob");
  989. // ↗ 的在场就是「可打开」的可见证据;悬停语法挂在 wf-pill-open 上。
  990. expect(view.getAllByTestId("workflow-timeline-pill-open")).toHaveLength(2);
  991. expect(pills[1]!.className).toContain("wf-pill-open");
  992. fireEvent.click(pills[1]!);
  993. expect(onOpenWorkflowActor).toHaveBeenCalledWith({
  994. runId: "dwfrun-42",
  995. actorSessionId: "s-2",
  996. siteId: "actor#2",
  997. ordinal: 1,
  998. actorName: "Bob",
  999. });
  1000. });
  1001. it("能力门控:没有回调、或没有 run 时药丸不是按钮;活的 run 里没有会话的药丸也是按钮", () => {
  1002. const noCallback = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: joined() });
  1003. expect(
  1004. noCallback.getAllByTestId("workflow-agent-pill").every((pill) => pill.tagName === "SPAN"),
  1005. ).toBe(true);
  1006. expect(noCallback.queryByTestId("workflow-timeline-pill-open")).toBeNull();
  1007. cleanup();
  1008. // 还没启动的子代理(追记「点击语法修订」):与活药丸同一套悬停与点击,交出槽位——车道 +
  1009. // 将来的序号——不带会话 id;宿主开的是占位 tab。
  1010. const onOpenWorkflowActor = vi.fn();
  1011. const noSession = renderBlockDom(LOOP_TOOL_CALL, {
  1012. onOpenWorkflowActor,
  1013. workflowRun: joined({
  1014. actors: [
  1015. { siteId: "actor#1", ordinal: 1, name: "Ada", sessionId: "s-1", status: "completed" },
  1016. ],
  1017. }),
  1018. });
  1019. const pills = noSession.getAllByTestId("workflow-agent-pill");
  1020. expect(pills.map((pill) => pill.tagName)).toEqual(["BUTTON", "BUTTON"]);
  1021. expect(pills[1]!.className).toContain("wf-pill-open");
  1022. expect(pills[1]!.getAttribute("aria-label")).toBe("Open the transcript of checker");
  1023. fireEvent.click(pills[1]!);
  1024. expect(onOpenWorkflowActor).toHaveBeenCalledWith({
  1025. runId: "dwfrun-42",
  1026. siteId: "actor#2",
  1027. ordinal: 1,
  1028. actorName: "checker",
  1029. });
  1030. cleanup();
  1031. const static_ = renderBlockDom(LOOP_TOOL_CALL, { onOpenWorkflowActor });
  1032. expect(
  1033. static_.getAllByTestId("workflow-agent-pill").every((pill) => pill.tagName === "SPAN"),
  1034. ).toBe(true);
  1035. expect(static_.getAllByTestId("workflow-agent-pill")[0]!.getAttribute("title")).toBe("planner");
  1036. });
  1037. it("点药丸不触发站头(各自的路径互不串线)", () => {
  1038. const onOpenWorkflowActor = vi.fn();
  1039. const onOpenWorkflowRun = vi.fn();
  1040. const view = renderBlockDom(LOOP_TOOL_CALL, {
  1041. onOpenWorkflowActor,
  1042. onOpenWorkflowRun,
  1043. workflowRun: joined(),
  1044. });
  1045. fireEvent.click(view.getAllByTestId("workflow-agent-pill")[0]!);
  1046. expect(onOpenWorkflowActor).toHaveBeenCalledTimes(1);
  1047. expect(onOpenWorkflowRun).not.toHaveBeenCalled();
  1048. });
  1049. });
  1050. // 脚本药丸(docs/dynamic-workflow/transcript-and-notifications.md「Opening the tab」):与子代理药丸同一套打开语法,
  1051. // 交出的不是槽位而是这一站的阶段 id——脚本 transcript 一个 run 只有一份,落点才按站。
  1052. describe("CreateWorkflowToolCallBlock 脚本药丸", () => {
  1053. const WORKSPACE_GRAPH = {
  1054. ...LOOP_GRAPH,
  1055. steps: [
  1056. ...LOOP_GRAPH.steps,
  1057. {
  1058. id: "world-read#1",
  1059. kind: "world-read",
  1060. label: "run tests",
  1061. line: 6,
  1062. column: 3,
  1063. lane: "workspace",
  1064. phase: "phase#b",
  1065. },
  1066. ],
  1067. lanes: [...LOOP_GRAPH.lanes, { id: "workspace" }],
  1068. participants: [
  1069. ...LOOP_GRAPH.participants,
  1070. { id: "phase#b:workspace", phase: "phase#b", lane: "workspace", steps: ["world-read#1"] },
  1071. ],
  1072. };
  1073. const WORKSPACE_TOOL_CALL: ToolCallOverrides = {
  1074. ...LOOP_TOOL_CALL,
  1075. raw: { display: { ...OK_DISPLAY, causalityGraph: WORKSPACE_GRAPH } },
  1076. };
  1077. const workspacePill = (view: ReturnType<typeof renderBlockDom>) =>
  1078. view
  1079. .getAllByTestId("workflow-agent-pill")
  1080. .find((pill) => pill.textContent?.includes("Script"))!;
  1081. it("活的 run 里脚本药丸是按钮:点一下交出这一站的阶段 id 与展示名", () => {
  1082. const onOpenWorkflowWorkspace = vi.fn();
  1083. const onOpenWorkflowActor = vi.fn();
  1084. const view = renderBlockDom(WORKSPACE_TOOL_CALL, {
  1085. onOpenWorkflowWorkspace,
  1086. onOpenWorkflowActor,
  1087. workflowRun: joined(),
  1088. });
  1089. const pill = workspacePill(view);
  1090. expect(pill.tagName).toBe("BUTTON");
  1091. expect(pill.className).toContain("wf-pill-open");
  1092. expect(pill.getAttribute("aria-label")).toBe("Open the script's steps at verify");
  1093. expect(view.getAllByTestId("workflow-timeline-workspace-open")).toHaveLength(1);
  1094. fireEvent.click(pill);
  1095. expect(onOpenWorkflowWorkspace).toHaveBeenCalledWith({
  1096. phaseId: "phase#b",
  1097. workflowName: "Research pipeline",
  1098. });
  1099. // 与子代理那条路互不串线。
  1100. expect(onOpenWorkflowActor).not.toHaveBeenCalled();
  1101. });
  1102. it("能力门控:没有回调、或没有 run 时脚本药丸是 span;子代理回调在场也不借用", () => {
  1103. const noCallback = renderBlockDom(WORKSPACE_TOOL_CALL, {
  1104. onOpenWorkflowActor: vi.fn(),
  1105. workflowRun: joined(),
  1106. });
  1107. expect(workspacePill(noCallback).tagName).toBe("SPAN");
  1108. expect(noCallback.queryByTestId("workflow-timeline-workspace-open")).toBeNull();
  1109. cleanup();
  1110. const noRun = renderBlockDom(WORKSPACE_TOOL_CALL, { onOpenWorkflowWorkspace: vi.fn() });
  1111. expect(workspacePill(noRun).tagName).toBe("SPAN");
  1112. expect(noRun.queryByTestId("workflow-timeline-workspace-open")).toBeNull();
  1113. });
  1114. });
  1115. describe("CreateWorkflowToolCallBlock 失败态与 Resume", () => {
  1116. const failed = () =>
  1117. joined(
  1118. {
  1119. status: "errored",
  1120. error: { code: "actor_failed", message: "boom" } as WorkflowRunState["error"],
  1121. actors: [
  1122. { siteId: "actor#1", ordinal: 1, name: "Ada", sessionId: "s-1", status: "completed" },
  1123. { siteId: "actor#2", ordinal: 1, name: "Bob", sessionId: "s-2", status: "completed" },
  1124. ],
  1125. nodes: [
  1126. { siteId: "ask#1", ordinal: 1, phase: "settled", outcome: "ok" },
  1127. { siteId: "ask#2", ordinal: 1, phase: "settled", outcome: "failed" },
  1128. ] as WorkflowRunState["nodes"],
  1129. },
  1130. { resumable: true },
  1131. );
  1132. it("出错:表头「Workflow errored」,失败的站与药丸带失败标记,页脚有 Resume", () => {
  1133. const onResumeWorkflowRun = vi.fn();
  1134. const view = renderBlockDom(LOOP_TOOL_CALL, { onResumeWorkflowRun, workflowRun: failed() });
  1135. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow errored");
  1136. expect(view.getByTestId("workflow-card-status").textContent).toBe("Errored");
  1137. expect(
  1138. view.getAllByTestId("workflow-timeline-station")[1]!.getAttribute("data-station-status"),
  1139. ).toBe("failed");
  1140. expect(view.getAllByTestId("workflow-agent-pill")[1]!.getAttribute("data-agent-status")).toBe(
  1141. "failed",
  1142. );
  1143. const resume = view.getByTestId("workflow-card-resume");
  1144. expect(view.getByTestId("workflow-card-footer").contains(resume)).toBe(true);
  1145. fireEvent.click(resume);
  1146. expect(onResumeWorkflowRun).toHaveBeenCalledWith({ workflowName: "Research pipeline" });
  1147. });
  1148. it("Resume 是能力门控:没有回调、或摘要不说可恢复,都没有按钮", () => {
  1149. const noCallback = renderBlockDom(LOOP_TOOL_CALL, { workflowRun: failed() });
  1150. expect(noCallback.queryByTestId("workflow-card-resume")).toBeNull();
  1151. cleanup();
  1152. const { resumable: _off, ...notResumable } = failed();
  1153. const notOffered = renderBlockDom(LOOP_TOOL_CALL, {
  1154. onResumeWorkflowRun: vi.fn(),
  1155. workflowRun: notResumable,
  1156. });
  1157. expect(notOffered.queryByTestId("workflow-card-resume")).toBeNull();
  1158. cleanup();
  1159. const running = renderBlockDom(LOOP_TOOL_CALL, {
  1160. onResumeWorkflowRun: vi.fn(),
  1161. workflowRun: joined(),
  1162. });
  1163. expect(running.queryByTestId("workflow-card-resume")).toBeNull();
  1164. });
  1165. it("折叠的失败卡仍然留下页脚:Resume 必须够得着", () => {
  1166. const view = renderBlockDom(LOOP_TOOL_CALL, {
  1167. canToggle: true,
  1168. forceOpen: false,
  1169. onResumeWorkflowRun: vi.fn(),
  1170. workflowRun: failed(),
  1171. });
  1172. fireEvent.click(view.getByTestId("workflow-card-toggle"));
  1173. expect(view.queryByTestId("workflow-card-body")).toBeNull();
  1174. expect(view.getByTestId("workflow-card-resume")).toBeTruthy();
  1175. });
  1176. it("停止:种类词「Workflow stopped」+ 原因词,联接摘要标 resumable 时同样有 Resume", () => {
  1177. const view = renderBlockDom(LOOP_TOOL_CALL, {
  1178. onResumeWorkflowRun: vi.fn(),
  1179. workflowRun: joined({ status: "stopped", stopReason: "user" }, { resumable: true }),
  1180. });
  1181. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow stopped");
  1182. expect(view.getByTestId("workflow-card-status-reason").textContent).toContain("by you");
  1183. // 状态行的第三个词:停下的 run 还能接着跑。动词从「取消」改成「停止」之后,后果由这里说。
  1184. expect(view.getByTestId("workflow-card-status-resumable").textContent).toContain("resumable");
  1185. expect(view.getByTestId("workflow-card-resume")).toBeTruthy();
  1186. });
  1187. it("摘要不带 resumable 时状态行没有第三个词(UI 绝不按 status 推导)", () => {
  1188. const view = renderBlockDom(LOOP_TOOL_CALL, {
  1189. workflowRun: joined({ status: "stopped", stopReason: "user" }),
  1190. });
  1191. expect(view.getByTestId("workflow-card-status-reason").textContent).toContain("by you");
  1192. expect(view.queryByTestId("workflow-card-status-resumable")).toBeNull();
  1193. });
  1194. });
  1195. // ── kind 文案的相位化 ─────────────────────────────────────────────────────────────
  1196. // ToolLayout 只在失败态渲染 statusLabel,所以非失败态 kindLabel 是卡上唯一说明「在干什么」
  1197. // 的文字;而「校验」只在 running 相为真(脚本在 prepareApproval 里早于弹窗就被 analyze 过)。
  1198. // 相位读适配器挂在 raw 上的 v4 原状态,两层都钉:纯函数与实际渲染出来的那句话。
  1199. describe("readWorkflowKindMessageId", () => {
  1200. it("终态一律是「工作流」,不看 v4 状态", () => {
  1201. expect(readWorkflowKindMessageId({ v4Status: "success" }, false)).toBe(
  1202. "chat.toolCall.workflow.ran",
  1203. );
  1204. expect(readWorkflowKindMessageId({ v4Status: "inputStreaming" }, false)).toBe(
  1205. "chat.toolCall.workflow.ran",
  1206. );
  1207. expect(readWorkflowKindMessageId(undefined, false)).toBe("chat.toolCall.workflow.ran");
  1208. });
  1209. it("inputStreaming 是「正在编写」——模型还在写脚本,没人在校验", () => {
  1210. expect(readWorkflowKindMessageId({ v4Status: "inputStreaming" }, true)).toBe(
  1211. "chat.toolCall.workflow.writing",
  1212. );
  1213. });
  1214. it("pendingApproval 是「待确认」——球在用户手上", () => {
  1215. expect(readWorkflowKindMessageId({ v4Status: "pendingApproval" }, true)).toBe(
  1216. "chat.toolCall.workflow.awaitingConfirmation",
  1217. );
  1218. });
  1219. it("running 相才是「正在校验」,语义收窄到这一相", () => {
  1220. expect(readWorkflowKindMessageId({ v4Status: "running" }, true)).toBe(
  1221. "chat.toolCall.workflow.running",
  1222. );
  1223. });
  1224. it("v4Status 缺席(非 v4 宿主 / 老快照)时逐字保持今天的行为", () => {
  1225. // 新相位只在有权威状态时生效;没有状态就不猜,回落 isRunning → 校验。
  1226. expect(readWorkflowKindMessageId(undefined, true)).toBe("chat.toolCall.workflow.running");
  1227. expect(readWorkflowKindMessageId({}, true)).toBe("chat.toolCall.workflow.running");
  1228. expect(readWorkflowKindMessageId({ display: OK_DISPLAY }, true)).toBe(
  1229. "chat.toolCall.workflow.running",
  1230. );
  1231. // raw 不是普通对象(数组 / 字符串 / null)同样回落,不崩。
  1232. expect(readWorkflowKindMessageId([], true)).toBe("chat.toolCall.workflow.running");
  1233. expect(readWorkflowKindMessageId("running", true)).toBe("chat.toolCall.workflow.running");
  1234. expect(readWorkflowKindMessageId(null, true)).toBe("chat.toolCall.workflow.running");
  1235. // 认不出的状态字符串也走同一条回落。
  1236. expect(readWorkflowKindMessageId({ v4Status: "queued" }, true)).toBe(
  1237. "chat.toolCall.workflow.running",
  1238. );
  1239. });
  1240. });
  1241. describe("CreateWorkflowToolCallBlock kind 文案分相", () => {
  1242. const IN_FLIGHT_INPUT = { name: "Fan-out review", script: "const x = 1;" };
  1243. it("inputStreaming:卡上写「正在编写工作流」,不写校验", () => {
  1244. const html = renderBlock(
  1245. { input: IN_FLIGHT_INPUT, raw: { v4Status: "inputStreaming" }, status: "running" },
  1246. { isRunning: true },
  1247. );
  1248. expect(html).toContain("Writing workflow");
  1249. expect(html).not.toContain("Validating workflow");
  1250. });
  1251. it("pendingApproval:卡上写「工作流待确认」", () => {
  1252. const html = renderBlock(
  1253. { input: IN_FLIGHT_INPUT, raw: { v4Status: "pendingApproval" }, status: "running" },
  1254. { isRunning: true },
  1255. );
  1256. expect(html).toContain("Awaiting workflow confirmation");
  1257. expect(html).not.toContain("Validating workflow");
  1258. });
  1259. it("running:仍然是「正在校验工作流」", () => {
  1260. const html = renderBlock(
  1261. { input: IN_FLIGHT_INPUT, raw: { v4Status: "running" }, status: "running" },
  1262. { isRunning: true },
  1263. );
  1264. expect(html).toContain("Validating workflow");
  1265. expect(html).not.toContain("Writing workflow");
  1266. });
  1267. it("v4Status 缺席时卡面逐字保持今天的行为", () => {
  1268. const html = renderBlock(
  1269. { input: IN_FLIGHT_INPUT, raw: {}, status: "running" },
  1270. { isRunning: true },
  1271. );
  1272. expect(html).toContain("Validating workflow");
  1273. expect(html).not.toContain("Writing workflow");
  1274. expect(html).not.toContain("Awaiting workflow confirmation");
  1275. });
  1276. it("终态不带任何相位词:只写「工作流」", () => {
  1277. const html = renderBlock({
  1278. input: IN_FLIGHT_INPUT,
  1279. raw: { display: OK_DISPLAY, v4Status: "success" },
  1280. });
  1281. expect(html).not.toContain("Writing workflow");
  1282. expect(html).not.toContain("Awaiting workflow confirmation");
  1283. expect(html).not.toContain("Validating workflow");
  1284. });
  1285. it("zh-CN 下相位文案同样本地化", () => {
  1286. const writing = renderBlock(
  1287. { input: IN_FLIGHT_INPUT, raw: { v4Status: "inputStreaming" }, status: "running" },
  1288. { isRunning: true },
  1289. "zh-CN",
  1290. );
  1291. expect(writing).toContain("正在编写工作流");
  1292. expect(writing).not.toContain("正在校验工作流");
  1293. const awaiting = renderBlock(
  1294. { input: IN_FLIGHT_INPUT, raw: { v4Status: "pendingApproval" }, status: "running" },
  1295. { isRunning: true },
  1296. "zh-CN",
  1297. );
  1298. expect(awaiting).toContain("工作流待确认");
  1299. const validating = renderBlock(
  1300. { input: IN_FLIGHT_INPUT, raw: { v4Status: "running" }, status: "running" },
  1301. { isRunning: true },
  1302. "zh-CN",
  1303. );
  1304. expect(validating).toContain("正在校验工作流");
  1305. });
  1306. it("第 2 稿起编写中的种类词是「Revising workflow」、细节槽带稿号;第 1 稿照旧不编号", () => {
  1307. const revising = renderBlockDom(
  1308. { input: IN_FLIGHT_INPUT, raw: { v4Status: "inputStreaming" }, status: "running" },
  1309. { isRunning: true, workflowDraft: { ordinal: 2, superseded: false } },
  1310. );
  1311. expect(revising.container.textContent).toContain("Revising workflow");
  1312. expect(revising.container.textContent).not.toContain("Writing workflow");
  1313. expect(revising.getByTestId("workflow-draft-ordinal").textContent).toBe("draft 2");
  1314. expect(revising.queryByTestId("workflow-draft-lamp")).toBeNull();
  1315. cleanup();
  1316. const first = renderBlockDom(
  1317. { input: IN_FLIGHT_INPUT, raw: { v4Status: "inputStreaming" }, status: "running" },
  1318. { isRunning: true, workflowDraft: { ordinal: 1, superseded: false } },
  1319. );
  1320. expect(first.container.textContent).toContain("Writing workflow");
  1321. expect(first.queryByTestId("workflow-draft-ordinal")).toBeNull();
  1322. });
  1323. it("待确认与校验中:第 2 稿起同样带稿号,种类词不变", () => {
  1324. const awaiting = renderBlockDom(
  1325. { input: IN_FLIGHT_INPUT, raw: { v4Status: "pendingApproval" }, status: "running" },
  1326. { isRunning: true, workflowDraft: { ordinal: 2, superseded: false } },
  1327. );
  1328. expect(awaiting.container.textContent).toContain("Awaiting workflow confirmation");
  1329. expect(awaiting.getByTestId("workflow-draft-ordinal").textContent).toBe("draft 2");
  1330. cleanup();
  1331. const validating = renderBlockDom(
  1332. { input: IN_FLIGHT_INPUT, raw: { v4Status: "running" }, status: "running" },
  1333. { isRunning: true, workflowDraft: { ordinal: 3, superseded: false } },
  1334. );
  1335. expect(validating.getByTestId("workflow-card-kind").textContent).toBe("Validating workflow");
  1336. expect(validating.getByTestId("workflow-card-detail").textContent).toBe("draft 3");
  1337. cleanup();
  1338. // 编过、没启动的静态卡不是在途行:不带稿号。
  1339. const settled = renderBlockDom(LOOP_TOOL_CALL, {
  1340. workflowDraft: { ordinal: 2, superseded: false },
  1341. });
  1342. expect(settled.getByTestId("workflow-card-detail").textContent).not.toContain("draft");
  1343. });
  1344. it("zh-CN:第 2 稿起写「正在修改工作流」与「第 2 稿」", () => {
  1345. const view = renderBlockDom(
  1346. { input: IN_FLIGHT_INPUT, raw: { v4Status: "inputStreaming" }, status: "running" },
  1347. { isRunning: true, workflowDraft: { ordinal: 2, superseded: false } },
  1348. "zh-CN",
  1349. );
  1350. expect(view.container.textContent).toContain("正在修改工作流");
  1351. expect(view.getByTestId("workflow-draft-ordinal").textContent).toBe("第 2 稿");
  1352. });
  1353. it("编译反馈的新 key 在两种语言里都存在且已翻译;旧的错误计数与「诊断」键退场", () => {
  1354. for (const key of [
  1355. "chat.toolCall.workflow.draft",
  1356. "chat.toolCall.workflow.amend.draft",
  1357. "chat.toolCall.workflow.revising",
  1358. "chat.toolCall.workflow.amend.revising",
  1359. "chat.toolCall.workflow.draftOrdinal",
  1360. "chat.toolCall.workflow.toFix",
  1361. "chat.toolCall.workflow.notRun",
  1362. "chat.toolCall.workflow.feedback",
  1363. "chat.toolCall.workflow.feedback.lede",
  1364. "chat.toolCall.workflow.feedback.lede.saved",
  1365. "chat.toolCall.workflow.feedback.count",
  1366. "chat.toolCall.workflow.feedback.countOne",
  1367. "chat.toolCall.workflow.feedback.rule",
  1368. ]) {
  1369. expect((enUS as Record<string, string>)[key], key).toBeTruthy();
  1370. expect((zhCN as Record<string, string>)[key], key).toBeTruthy();
  1371. expect((zhCN as Record<string, string>)[key], key).not.toBe(
  1372. (enUS as Record<string, string>)[key],
  1373. );
  1374. }
  1375. for (const gone of [
  1376. "chat.toolCall.workflow.error",
  1377. "chat.toolCall.workflow.errors",
  1378. "chat.toolCall.workflow.diagnostics",
  1379. ]) {
  1380. expect((enUS as Record<string, string>)[gone], gone).toBeUndefined();
  1381. expect((zhCN as Record<string, string>)[gone], gone).toBeUndefined();
  1382. }
  1383. });
  1384. it("两个新 key 在两种语言里都存在,且不是英文占位", () => {
  1385. for (const key of [
  1386. "chat.toolCall.workflow.writing",
  1387. "chat.toolCall.workflow.awaitingConfirmation",
  1388. ]) {
  1389. expect(enUS[key]).toBeTruthy();
  1390. expect(zhCN[key]).toBeTruthy();
  1391. expect(zhCN[key]).not.toBe(enUS[key]);
  1392. }
  1393. });
  1394. });
  1395. // ── 五枚药丸与一扇门(docs/dynamic-workflow/presentation.md「Past six participants」)──
  1396. // 一站过了六个参与者:五枚钉住的药丸 + 一行「还有 n 个」;点那一行或站头开 run 详情、落到这一站。
  1397. describe("CreateWorkflowToolCallBlock 名册站", () => {
  1398. type FanStatus = "done" | "running" | "failed" | "pending";
  1399. // 真实的 fan-out 是一条车道跑出 N 个实例(`agent(\`reviewer-${i}\`)` 在循环里):静态图上是一张
  1400. // `many` 卡,run 一来按实例拆成 N 枚药丸(liveParticipantView)。display 的车道上限是 32,
  1401. // 所以名册的大数只能从这里来。
  1402. const FANOUT_GRAPH = {
  1403. steps: [
  1404. {
  1405. id: "ask#1",
  1406. kind: "ask",
  1407. label: "review",
  1408. line: 3,
  1409. column: 21,
  1410. lane: "actor#1",
  1411. phase: "phase#a",
  1412. },
  1413. ],
  1414. lanes: [{ id: "actor#1", namePattern: { head: "reviewer-" }, line: 2, column: 11 }],
  1415. participants: [
  1416. { id: "phase#a:actor#1", phase: "phase#a", lane: "actor#1", steps: ["ask#1"], many: true },
  1417. ],
  1418. handoffs: [],
  1419. phases: [{ id: "phase#a", name: "review", line: 1 }],
  1420. phaseEdges: [],
  1421. exits: ["phase#a"],
  1422. };
  1423. function fanoutToolCall(_n: number): ToolCallOverrides {
  1424. return {
  1425. input: { name: "bug-scan", script: 'phase("review");' },
  1426. raw: { display: { ...OK_DISPLAY, causalityGraph: FANOUT_GRAPH } },
  1427. };
  1428. }
  1429. function fanoutRun(
  1430. n: number,
  1431. statusOf: (index: number) => FanStatus,
  1432. unlistedByPhase?: WorkflowRunState["unlistedByPhase"],
  1433. ) {
  1434. const actors: WorkflowRunState["actors"] = [];
  1435. const nodes: WorkflowRunState["nodes"] = [];
  1436. for (let i = 1; i <= n; i += 1) {
  1437. const status = statusOf(i);
  1438. actors.push({
  1439. siteId: "actor#1",
  1440. ordinal: i,
  1441. name: `reviewer-${i}`,
  1442. sessionId: `s-${i}`,
  1443. status: status === "running" ? "running" : status === "pending" ? "waiting" : "completed",
  1444. });
  1445. if (status === "pending") continue;
  1446. nodes.push({
  1447. siteId: "ask#1",
  1448. ordinal: i,
  1449. actorSiteId: "actor#1",
  1450. actorOrdinal: i,
  1451. ...(status === "running"
  1452. ? { phase: "executing" }
  1453. : { phase: "settled", outcome: status === "failed" ? "failed" : "ok" }),
  1454. } as WorkflowRunState["nodes"][number]);
  1455. }
  1456. return joined({
  1457. actors,
  1458. nodes,
  1459. usage: { spentTokens: 10, nodesUsed: nodes.length },
  1460. ...(unlistedByPhase === undefined ? {} : { truncated: true, unlistedByPhase }),
  1461. });
  1462. }
  1463. const mid = (i: number): FanStatus =>
  1464. i === 4 ? "failed" : i === 6 ? "running" : i === 7 ? "pending" : "done";
  1465. it("七个参与者:钉住 running → failed → 补位五枚药丸,其余折成「还有 2 个」一行;没有计数行、量条、格子", () => {
  1466. const view = renderBlockDom(fanoutToolCall(7), { workflowRun: fanoutRun(7, mid) });
  1467. const column = view.getByTestId("workflow-timeline-pills");
  1468. expect(column.getAttribute("data-station-roster")).toBe("true");
  1469. const pills = within(column).getAllByTestId("workflow-agent-pill");
  1470. expect(pills.map((pill) => pill.textContent)).toEqual([
  1471. "reviewer-6",
  1472. "reviewer-4",
  1473. "reviewer-1",
  1474. "reviewer-2",
  1475. "reviewer-3",
  1476. ]);
  1477. expect(pills.map((pill) => pill.getAttribute("data-agent-status"))).toEqual([
  1478. "running",
  1479. "failed",
  1480. "done",
  1481. "done",
  1482. "done",
  1483. ]);
  1484. const row = within(column).getByTestId("workflow-roster-more-row");
  1485. expect(column.lastElementChild).toBe(row);
  1486. expect(row.getAttribute("data-more-count")).toBe("2");
  1487. expect(row.textContent).toBe("2 more");
  1488. expect(row.getAttribute("title")).toBe("2 more subagents · list everyone in the run pane");
  1489. // 叠上的脸按注意力序:pending 的 7 在 done 的 5 前面。
  1490. const faces = within(row)
  1491. .getByTestId("workflow-more-deck")
  1492. .querySelectorAll("[data-face-state]");
  1493. expect([...faces].map((face) => face.getAttribute("data-face-state"))).toEqual([
  1494. "waiting",
  1495. "content",
  1496. ]);
  1497. expect(within(row).queryByTestId("workflow-more-failed")).toBeNull();
  1498. expect(within(column).queryByTestId("workflow-roster-tally")).toBeNull();
  1499. expect(within(column).queryByTestId("workflow-roster-meter")).toBeNull();
  1500. expect(within(column).queryByTestId("workflow-agent-cell")).toBeNull();
  1501. // 表头照旧。
  1502. expect(view.getByTestId("workflow-card-detail").textContent).toContain("1 agent working");
  1503. });
  1504. // 表外的那些(docs/dynamic-workflow/presentation.md「Past six participants」):被归约淘汰的
  1505. // 子代理没有药丸也没有脸,但阈值、人数与红 ✕ 都算它们。
  1506. it("四枚药丸加三百个被淘汰的就是名册:药丸四枚、那一行说「还有 300 个」、红 ✕ 含表外失败的", () => {
  1507. const view = renderBlockDom(fanoutToolCall(4), {
  1508. workflowRun: fanoutRun(4, () => "running", [
  1509. { phaseName: "review", actors: 300, actorsSettled: 100, actorsFailed: 2, settled: 300 },
  1510. ]),
  1511. });
  1512. const column = view.getByTestId("workflow-timeline-pills");
  1513. expect(column.getAttribute("data-station-roster")).toBe("true");
  1514. expect(within(column).getAllByTestId("workflow-agent-pill")).toHaveLength(4);
  1515. const row = within(column).getByTestId("workflow-roster-more-row");
  1516. expect(row.getAttribute("data-more-count")).toBe("300");
  1517. expect(row.textContent).toContain("300 more");
  1518. expect(within(row).getByTestId("workflow-more-failed").textContent).toBe("2");
  1519. // 表外的没有脸:叠上一张都不该有。
  1520. expect(
  1521. within(row).getByTestId("workflow-more-deck").querySelectorAll("[data-face-state]"),
  1522. ).toHaveLength(0);
  1523. });
  1524. it("六个参与者仍是药丸列:没有名册", () => {
  1525. const view = renderBlockDom(fanoutToolCall(6), { workflowRun: fanoutRun(6, mid) });
  1526. const column = view.getByTestId("workflow-timeline-pills");
  1527. expect(column.getAttribute("data-station-roster")).toBeNull();
  1528. expect(within(column).getAllByTestId("workflow-agent-pill")).toHaveLength(6);
  1529. expect(view.queryByTestId("workflow-roster-more-row")).toBeNull();
  1530. });
  1531. it("失败多于五个:五枚红药丸之后那一行说藏了几个 failed", () => {
  1532. const view = renderBlockDom(fanoutToolCall(9), {
  1533. workflowRun: fanoutRun(9, (i) => (i <= 6 ? "failed" : "done")),
  1534. });
  1535. const column = view.getByTestId("workflow-timeline-pills");
  1536. const pills = within(column).getAllByTestId("workflow-agent-pill");
  1537. expect(pills.every((pill) => pill.getAttribute("data-agent-status") === "failed")).toBe(true);
  1538. const row = within(column).getByTestId("workflow-roster-more-row");
  1539. expect(row.getAttribute("data-more-count")).toBe("4");
  1540. expect(within(row).getByTestId("workflow-more-failed").textContent).toBe("1");
  1541. const faces = within(row)
  1542. .getByTestId("workflow-more-deck")
  1543. .querySelectorAll("[data-face-state]");
  1544. expect([...faces].map((face) => face.getAttribute("data-face-state"))).toEqual([
  1545. "sad",
  1546. "content",
  1547. "content",
  1548. ]);
  1549. });
  1550. it("那一行与站头交出站 id:开 run 详情、落到这一站;没有回调时那一行是静态的", () => {
  1551. const onOpenWorkflowRun = vi.fn();
  1552. const view = renderBlockDom(fanoutToolCall(60), {
  1553. onOpenWorkflowRun,
  1554. workflowRun: fanoutRun(60, () => "done"),
  1555. });
  1556. const row = view.getByTestId("workflow-roster-more-row");
  1557. expect(row.tagName).toBe("BUTTON");
  1558. expect(row.getAttribute("data-more-count")).toBe("55");
  1559. fireEvent.click(row);
  1560. expect(onOpenWorkflowRun).toHaveBeenCalledWith({ workflowName: "bug-scan", phaseId: "phase#a" });
  1561. // 站头(有回调时是按钮,title = 站名)同一条路。
  1562. fireEvent.click(view.getByTitle("review"));
  1563. expect(onOpenWorkflowRun).toHaveBeenCalledTimes(2);
  1564. expect(onOpenWorkflowRun).toHaveBeenLastCalledWith({
  1565. workflowName: "bug-scan",
  1566. phaseId: "phase#a",
  1567. });
  1568. cleanup();
  1569. const inert = renderBlockDom(fanoutToolCall(60), { workflowRun: fanoutRun(60, () => "done") });
  1570. expect(inert.getByTestId("workflow-roster-more-row").tagName).toBe("SPAN");
  1571. });
  1572. it("静态卡(无 run):`many` 卡还是一枚药丸;字面量基数的成员卡过了阈值是五枚 + 静态的「还有 3 个」", () => {
  1573. expect(renderBlock(fanoutToolCall(8))).not.toContain("data-station-roster");
  1574. const members = {
  1575. ...FANOUT_GRAPH,
  1576. participants: Array.from({ length: 8 }, (_, i) => ({
  1577. id: `phase#a:actor#1[${i}]`,
  1578. phase: "phase#a",
  1579. lane: "actor#1",
  1580. steps: ["ask#1"],
  1581. member: { index: i, of: 8 },
  1582. })),
  1583. };
  1584. const html = renderBlock({
  1585. input: { name: "bug-scan", script: 'phase("review");' },
  1586. raw: { display: { ...OK_DISPLAY, causalityGraph: members } },
  1587. });
  1588. expect(html).toContain('data-station-roster="true"');
  1589. expect(html).toContain('data-more-count="3"');
  1590. expect(html).toContain('<span aria-label="3 more subagents · list everyone in the run pane"');
  1591. expect(html).not.toContain('data-testid="workflow-roster-tally"');
  1592. });
  1593. it("zh-CN:「还有 n 个」与整句 title", () => {
  1594. const view = renderBlockDom(
  1595. fanoutToolCall(60),
  1596. { workflowRun: fanoutRun(60, () => "done") },
  1597. "zh-CN",
  1598. );
  1599. const row = view.getByTestId("workflow-roster-more-row");
  1600. expect(row.textContent).toBe("还有 55 个");
  1601. expect(row.getAttribute("title")).toBe("还有 55 个子代理 · 在运行侧栏里列出全部");
  1602. });
  1603. });
  1604. // ── AmendWorkflow 行(docs/dynamic-workflow/presentation.md「The tool row」第二张表)──
  1605. // 同一个渲染器换修订词汇:编写中「正在调整工作流」、待确认「等待调整确认」、编不过 / 静态卡
  1606. // 「工作流调整」;卡体多一行「调整 run X」。display 仍是 create_workflow,图与草稿笔一份实现。
  1607. describe("CreateWorkflowToolCallBlock as AmendWorkflow", () => {
  1608. const DRAFT = 'phase("plan");\nconst planner = agent("planner");\nphase("ver';
  1609. const AMEND_INPUT = { name: "Research pipeline", script: DRAFT, run_id: "dwfrun-prev" };
  1610. it.each([
  1611. ["en-US", "Amending workflow"],
  1612. ["zh-CN", "正在调整工作流"],
  1613. ] as const)("writing: %s", (locale, word) => {
  1614. const view = renderBlockDom(
  1615. {
  1616. kind: "AmendWorkflow",
  1617. input: AMEND_INPUT,
  1618. raw: { v4Status: "inputStreaming" },
  1619. status: "running",
  1620. },
  1621. { isRunning: true },
  1622. locale,
  1623. );
  1624. expect(view.container.textContent).toContain(word);
  1625. expect(view.container.textContent).not.toContain("Writing workflow");
  1626. // 草稿笔照旧:站从半截脚本里扫出来。
  1627. expect(view.getByTestId("workflow-draft-timeline")).toBeTruthy();
  1628. });
  1629. it.each([
  1630. ["en-US", "Awaiting amendment confirmation"],
  1631. ["zh-CN", "等待调整确认"],
  1632. ] as const)("awaiting confirmation: %s", (locale, word) => {
  1633. const view = renderBlockDom(
  1634. {
  1635. kind: "AmendWorkflow",
  1636. ...LOOP_TOOL_CALL,
  1637. input: { ...(LOOP_TOOL_CALL.input as object), run_id: "dwfrun-prev" },
  1638. raw: { ...(LOOP_TOOL_CALL.raw as object), v4Status: "pendingApproval" },
  1639. status: "running",
  1640. },
  1641. { isRunning: true },
  1642. locale,
  1643. );
  1644. expect(view.container.textContent).toContain(word);
  1645. });
  1646. it.each([
  1647. ["en-US", "Workflow amendment", "Amends run"],
  1648. ["zh-CN", "工作流调整", "调整 run"],
  1649. ] as const)("static card: kind word and the lineage row: %s", (locale, word, amends) => {
  1650. const view = renderBlockDom(
  1651. {
  1652. kind: "AmendWorkflow",
  1653. ...LOOP_TOOL_CALL,
  1654. input: { ...(LOOP_TOOL_CALL.input as object), run_id: "dwfrun-prev" },
  1655. },
  1656. {},
  1657. locale,
  1658. );
  1659. expect(view.getByTestId("workflow-card-kind").textContent).toBe(word);
  1660. const lineage = view.container.querySelector('[data-workflow-card-amends="true"]');
  1661. expect(lineage?.textContent).toContain(amends);
  1662. expect(lineage?.textContent).toContain("dwfrun-prev");
  1663. });
  1664. it("compile errors: the feedback row says「Amendment draft」, not the create word", () => {
  1665. const view = renderBlockDom({
  1666. kind: "AmendWorkflow",
  1667. input: AMEND_INPUT,
  1668. raw: {
  1669. display: {
  1670. kind: "create_workflow",
  1671. ok: false,
  1672. errorCount: 1,
  1673. diagnostics: [{ line: 1, column: 1, code: 2322, message: "Type error" }],
  1674. },
  1675. },
  1676. status: "failed",
  1677. });
  1678. expect(view.container.querySelector(".tool-summary-kind-label")?.textContent).toBe(
  1679. "Amendment draft",
  1680. );
  1681. expect(view.container.textContent).not.toContain("Amending workflow");
  1682. expect(view.container.textContent).not.toContain("Workflow draft");
  1683. });
  1684. it("from draft 2 on the streaming amend row says「Revising amendment」", () => {
  1685. const view = renderBlockDom(
  1686. {
  1687. kind: "AmendWorkflow",
  1688. input: AMEND_INPUT,
  1689. raw: { v4Status: "inputStreaming" },
  1690. status: "running",
  1691. },
  1692. { isRunning: true, workflowDraft: { ordinal: 2, superseded: false } },
  1693. );
  1694. expect(view.container.textContent).toContain("Revising amendment");
  1695. expect(view.getByTestId("workflow-draft-ordinal").textContent).toBe("draft 2");
  1696. });
  1697. // 省略 script 的修订(docs/dynamic-workflow/presentation.md「The tool row」):行上的入参是模型发出的
  1698. // 那一份,脚本是之后才回填的,所以卡上没有脚本可折叠。lineage 行说「脚本不变」,「未提供脚本」的
  1699. // 提示不出现——缺脚本正是这次调用的用意,不是故障。
  1700. const SETTINGS_ONLY_INPUT = { run_id: "dwfrun-prev", max_concurrency: 2 };
  1701. it.each([
  1702. ["en-US", "Amends run", "script unchanged"],
  1703. ["zh-CN", "调整 run", "脚本不变"],
  1704. ] as const)("static card of an amend that kept the script: %s", (locale, amends, unchanged) => {
  1705. const view = renderBlockDom(
  1706. { kind: "AmendWorkflow", input: SETTINGS_ONLY_INPUT, raw: LOOP_TOOL_CALL.raw },
  1707. {},
  1708. locale,
  1709. );
  1710. const lineage = view.container.querySelector('[data-workflow-card-amends="true"]');
  1711. expect(lineage?.textContent).toContain(amends);
  1712. expect(lineage?.textContent).toContain("dwfrun-prev");
  1713. expect(lineage?.textContent).toContain(unchanged);
  1714. expect(lineage?.getAttribute("data-workflow-card-amends-script-inherited")).toBe("true");
  1715. // 图照常画(display 来自回填后的脚本),但没有脚本折叠、也没有「未提供脚本」。
  1716. expect(view.getByTestId("workflow-timeline")).toBeTruthy();
  1717. expect(view.queryByTestId("workflow-card-script-toggle")).toBeNull();
  1718. expect(view.container.textContent).not.toContain("No workflow script provided.");
  1719. });
  1720. it("an amend that kept the script and has no display still shows no 'no script' notice", () => {
  1721. const view = renderBlockDom({ kind: "AmendWorkflow", input: SETTINGS_ONLY_INPUT, raw: {} });
  1722. expect(view.container.textContent).not.toContain("No workflow script provided.");
  1723. const lineage = view.container.querySelector('[data-workflow-card-amends="true"]');
  1724. expect(lineage?.textContent).toContain("script unchanged");
  1725. });
  1726. it("a static amend card that passed a script says nothing about the script", () => {
  1727. const view = renderBlockDom({
  1728. kind: "AmendWorkflow",
  1729. ...LOOP_TOOL_CALL,
  1730. input: { ...(LOOP_TOOL_CALL.input as object), run_id: "dwfrun-prev" },
  1731. });
  1732. const lineage = view.container.querySelector('[data-workflow-card-amends="true"]');
  1733. expect(lineage?.textContent).not.toContain("script unchanged");
  1734. expect(lineage?.getAttribute("data-workflow-card-amends-script-inherited")).toBeNull();
  1735. expect(view.getByTestId("workflow-card-script-toggle")).toBeTruthy();
  1736. });
  1737. it("an input trimmed to a snapshot preview is not read as a kept script", () => {
  1738. // 首屏快照把大入参裁成预览时,缺的是字节而不是脚本:不能把一个传了脚本的修订说成「脚本不变」。
  1739. const view = renderBlockDom({
  1740. kind: "AmendWorkflow",
  1741. input: { run_id: "dwfrun-prev" },
  1742. raw: LOOP_TOOL_CALL.raw,
  1743. snapshotRefs: [{ field: "input", previewBytes: 64, fullBytes: 40_000 }],
  1744. });
  1745. const lineage = view.container.querySelector('[data-workflow-card-amends="true"]');
  1746. expect(lineage?.textContent).toContain("dwfrun-prev");
  1747. expect(lineage?.textContent).not.toContain("script unchanged");
  1748. });
  1749. it("while the input streams nothing is said: the script may still be on its way", () => {
  1750. const view = renderBlockDom(
  1751. {
  1752. kind: "AmendWorkflow",
  1753. input: { run_id: "dwfrun-prev" },
  1754. raw: { v4Status: "inputStreaming" },
  1755. status: "running",
  1756. },
  1757. { isRunning: true },
  1758. );
  1759. expect(view.container.textContent).toContain("Amending workflow");
  1760. expect(view.container.textContent).not.toContain("script unchanged");
  1761. });
  1762. it("readWorkflowAmendScriptInherited: the resolved flag, or a finished input with neither script source", () => {
  1763. // 确认窗:CLI 回填过的入参带脚本,flag 说话。
  1764. expect(
  1765. readWorkflowAmendScriptInherited({
  1766. run_id: "r",
  1767. script: "x",
  1768. predecessor: { status: "completed", script_inherited: true },
  1769. }),
  1770. ).toBe(true);
  1771. // 聊天卡:模型发出的入参里没有脚本。
  1772. expect(readWorkflowAmendScriptInherited({ run_id: "r", max_concurrency: 2 })).toBe(true);
  1773. // 给了脚本、flag 不在:不是沿用。
  1774. expect(
  1775. readWorkflowAmendScriptInherited({
  1776. run_id: "r",
  1777. script: "x",
  1778. predecessor: { status: "completed" },
  1779. }),
  1780. ).toBe(false);
  1781. // `path` 修订(docs/dynamic-workflow/launch.md「Script files」的常态)同样不带 `script`,但它交上来的
  1782. // 是一份**改过的**脚本:说「脚本不变」就是对用户撒谎。
  1783. expect(
  1784. readWorkflowAmendScriptInherited({ run_id: "r", path: ".zcode/workflow-drafts/x.dwf.ts" }),
  1785. ).toBe(false);
  1786. // 沿用脚本且前驱的文件仍可记:回填的入参同时带 `path` 与 flag,flag 说了算。
  1787. expect(
  1788. readWorkflowAmendScriptInherited({
  1789. run_id: "r",
  1790. script: "x",
  1791. path: "/abs/.zcode/workflow-drafts/x.dwf.ts",
  1792. predecessor: { status: "completed", script_inherited: true },
  1793. }),
  1794. ).toBe(true);
  1795. // 非记录的入参(快照被裁掉、形状不明)什么都不断言。
  1796. expect(readWorkflowAmendScriptInherited(undefined)).toBe(false);
  1797. expect(readWorkflowAmendScriptInherited("{}")).toBe(false);
  1798. });
  1799. it("a CreateWorkflow row never grows a lineage row", () => {
  1800. const view = renderBlockDom(LOOP_TOOL_CALL);
  1801. expect(view.container.querySelector('[data-workflow-card-amends="true"]')).toBeNull();
  1802. });
  1803. it("readWorkflowKindMessageId switches to the amend vocabulary", () => {
  1804. expect(readWorkflowKindMessageId({ v4Status: "inputStreaming" }, true, true)).toBe(
  1805. "chat.toolCall.workflow.amend.writing",
  1806. );
  1807. expect(readWorkflowKindMessageId({ v4Status: "pendingApproval" }, true, true)).toBe(
  1808. "chat.toolCall.workflow.amend.awaitingConfirmation",
  1809. );
  1810. // 校验中与创建同一个词。
  1811. expect(readWorkflowKindMessageId({ v4Status: "running" }, true, true)).toBe(
  1812. "chat.toolCall.workflow.running",
  1813. );
  1814. expect(readWorkflowKindMessageId(undefined, false, true)).toBe(
  1815. "chat.toolCall.workflow.amend.ran",
  1816. );
  1817. });
  1818. it("every amend key exists in both locales", () => {
  1819. for (const key of [
  1820. "chat.toolCall.workflow.amend.writing",
  1821. "chat.toolCall.workflow.amend.awaitingConfirmation",
  1822. "chat.toolCall.workflow.amend.ran",
  1823. "chat.toolCall.workflow.amend.amended",
  1824. "chat.toolCall.workflow.amend.amends",
  1825. "chat.toolCall.workflow.card.superseded",
  1826. "chat.toolCall.workflow.run.stopReason.superseded",
  1827. "chat.toolCall.workflow.run.amends",
  1828. "chat.toolCall.workflow.run.supersededBy",
  1829. "chat.permission.workflow.amend.title",
  1830. "chat.permission.workflow.amends",
  1831. "chat.permission.workflow.amends.running",
  1832. "chat.permission.workflow.amends.scriptUnchanged",
  1833. "chat.toolCall.workflow.amend.scriptUnchanged",
  1834. ]) {
  1835. expect(enUS[key as keyof typeof enUS], key).toBeTruthy();
  1836. expect(zhCN[key as keyof typeof zhCN], key).toBeTruthy();
  1837. }
  1838. // 旧 lineage 键随 resume_from 一起退场;卡上的两句 lineage(调整自 / 已被替代)按用户裁决不画,键也不留。
  1839. for (const gone of ["chat.toolCall.workflow.card.supersededBy", "chat.toolCall.workflow.card.amends"]) {
  1840. expect((enUS as Record<string, string>)[gone], gone).toBeUndefined();
  1841. expect((zhCN as Record<string, string>)[gone], gone).toBeUndefined();
  1842. }
  1843. expect((enUS as Record<string, string>)["chat.permission.workflow.resumeFrom"]).toBeUndefined();
  1844. expect((zhCN as Record<string, string>)["chat.permission.workflow.resumeFrom"]).toBeUndefined();
  1845. });
  1846. // ── 就地生效的修订(docs/dynamic-workflow/concurrency.md)────────────────────────────
  1847. // 只改并发上限、run 又在飞时这次调用不编译、不铸新 run,结果连 display 都没有——整行退成设置行
  1848. // 是接线层(ConversationRowView)按入参形状裁的,见本文件末尾那一节。这里只钉渲染器这一侧:
  1849. // 在途的那几个词不能说「校验」,因为这种调用什么都不编译。
  1850. it("只改并发上限的调用在途时不说「正在校验」,从头到尾说「正在调整工作流」", () => {
  1851. expect(readWorkflowKindMessageId({ v4Status: "running" }, true, true, true)).toBe(
  1852. "chat.toolCall.workflow.amend.writing",
  1853. );
  1854. expect(readWorkflowKindMessageId({ v4Status: "inputStreaming" }, true, true, true)).toBe(
  1855. "chat.toolCall.workflow.amend.writing",
  1856. );
  1857. // 带脚本的修订不受影响:校验是真发生的。
  1858. expect(readWorkflowKindMessageId({ v4Status: "running" }, true, true, false)).toBe(
  1859. "chat.toolCall.workflow.running",
  1860. );
  1861. });
  1862. it.each([
  1863. [{ run_id: "r", max_concurrency: 4 }, { runId: "r", requested: 4 }],
  1864. [{ run_id: "r", max_concurrency: null }, { runId: "r", requested: null }],
  1865. // 带了脚本来源:这次修订要编译,不是就地生效的形状。
  1866. [{ run_id: "r", max_concurrency: 4, script: "phase('a')" }, undefined],
  1867. [{ run_id: "r", max_concurrency: 4, path: ".zcode/workflow-drafts/x.dwf.ts" }, undefined],
  1868. // 还改了模型或名字:那要换一条 run,不是就地生效。
  1869. [{ run_id: "r", max_concurrency: 4, subagent_model: "zhipu/glm-5.3" }, undefined],
  1870. [{ run_id: "r", max_concurrency: 4, name: "Research pipeline" }, undefined],
  1871. // 只有 run_id(脚本可能还在路上):不作判断。
  1872. [{ run_id: "r" }, undefined],
  1873. [{ max_concurrency: 4 }, undefined],
  1874. ["{}", undefined],
  1875. ])("readWorkflowRetuneCall %#", (input, expected) => {
  1876. expect(readWorkflowRetuneCall(input)).toEqual(expected);
  1877. });
  1878. it("在途的只改并发上限调用:行上说「正在调整工作流」,不说校验", () => {
  1879. const view = renderBlockDom(
  1880. {
  1881. kind: "AmendWorkflow",
  1882. input: { run_id: "dwfrun-prev", max_concurrency: 4 },
  1883. raw: { v4Status: "running" },
  1884. status: "running",
  1885. },
  1886. { isRunning: true },
  1887. );
  1888. expect(view.container.textContent).toContain("Amending workflow");
  1889. expect(view.container.textContent).not.toContain("Validating workflow");
  1890. });
  1891. });
  1892. // ── 接线(docs/dynamic-workflow/authoring.md「How the user sees them」)──
  1893. // 上面钉的是药丸把**产物 id** 交给上一层;这里钉 `ConversationRowView` 把 id 变成什么样的打开
  1894. // 请求。走到这张卡的是**没有被轮尾摘要接管**的那条路(联接到 run 但工具调用本身报错),联接到
  1895. // run 且成功的行只画一条 `WorkflowToolSummary`,卡在轮尾。
  1896. describe("ConversationRowView 把工具卡的产物药丸变成打开请求", () => {
  1897. const RUN = {
  1898. runId: "run-7",
  1899. toolCallId: "tool-5",
  1900. status: "completed",
  1901. usage: { spentTokens: 12, nodesUsed: 1 },
  1902. actors: [{ siteId: "actor#1", ordinal: 1, name: "Ada", sessionId: "s-1", status: "completed" }],
  1903. nodes: [{ siteId: "ask#1", ordinal: 1, phase: "settled", outcome: "ok" }],
  1904. artifacts: [
  1905. { id: "book", kind: "file", title: "Audit report", version: 2, contentType: "text/html" },
  1906. { id: "perf", kind: "chart", title: "Round timings", version: 1 },
  1907. ],
  1908. lastEventSequence: 3,
  1909. } as unknown as WorkflowRunState;
  1910. function renderRow(onOpenWorkflowArtifact: (request: unknown) => void) {
  1911. // 代码块桩会吃掉 matchMedia 的读者,但正文渲染器仍会问一次;jsdom 没有它。
  1912. Object.defineProperty(window, "matchMedia", {
  1913. configurable: true,
  1914. writable: true,
  1915. value: vi.fn(() => ({
  1916. matches: false,
  1917. addEventListener: vi.fn(),
  1918. removeEventListener: vi.fn(),
  1919. addListener: vi.fn(),
  1920. removeListener: vi.fn(),
  1921. })),
  1922. });
  1923. return render(
  1924. createElement(
  1925. ZCodeIntlProvider,
  1926. { initialLocale: "en-US" },
  1927. createElement(
  1928. PlatformProvider,
  1929. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 这条路径不碰平台服务。
  1930. { platform: {} as any },
  1931. createElement(ConversationRowView, {
  1932. row: {
  1933. rowId: 5,
  1934. turnId: "turn-1",
  1935. createdAt: 1_700_000_000_005,
  1936. createdAtSeq: 5,
  1937. kind: "toolCall",
  1938. toolCallId: "tool-5",
  1939. toolName: "CreateWorkflow",
  1940. // 成功的发起行被轮尾摘要接管,画不出这张卡;报错的行才落回完整工具卡。
  1941. status: "error",
  1942. input: { name: "implement-verify", script: "phase('a')" },
  1943. inputText: "{}",
  1944. output: {
  1945. text: "ok",
  1946. display: {
  1947. kind: "create_workflow",
  1948. ok: true,
  1949. errorCount: 0,
  1950. diagnostics: [],
  1951. causalityGraph: {
  1952. steps: [
  1953. {
  1954. id: "ask#1",
  1955. kind: "ask",
  1956. label: "plan",
  1957. line: 2,
  1958. column: 21,
  1959. lane: "actor#1",
  1960. },
  1961. ],
  1962. lanes: [{ id: "actor#1", name: "planner", line: 1, column: 11 }],
  1963. participants: [
  1964. {
  1965. id: "unphased:actor#1",
  1966. phase: "unphased",
  1967. lane: "actor#1",
  1968. steps: ["ask#1"],
  1969. },
  1970. ],
  1971. handoffs: [],
  1972. },
  1973. },
  1974. },
  1975. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 只填 renderer 读取的字段。
  1976. } as any,
  1977. context: {
  1978. workspacePath: "/workspace",
  1979. theme: "system",
  1980. sessionId: "parent-a",
  1981. onOpenWorkflowArtifact,
  1982. workflowRunByToolCallId: new Map([
  1983. [
  1984. "tool-5",
  1985. {
  1986. runId: "run-7",
  1987. toolCallId: "tool-5",
  1988. status: "completed",
  1989. nodesSettled: 1,
  1990. nodesTotal: 1,
  1991. run: RUN,
  1992. },
  1993. ],
  1994. ]),
  1995. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 同上。
  1996. } as any,
  1997. }),
  1998. ),
  1999. ),
  2000. );
  2001. }
  2002. it("带上展示名与 contentType(宿主据它把 html 产物直接开成浏览器 tab)", () => {
  2003. const spy = vi.fn();
  2004. const view = renderRow(spy);
  2005. fireEvent.click(view.getAllByTestId("workflow-card-artifact")[0]!);
  2006. expect(spy).toHaveBeenCalledWith({
  2007. parentSessionId: "parent-a",
  2008. runId: "run-7",
  2009. artifactId: "book",
  2010. title: "Audit report",
  2011. contentType: "text/html",
  2012. });
  2013. });
  2014. // 就地生效的修订行(docs/dynamic-workflow/concurrency.md):结果没有 display、也没有铸新 run,
  2015. // 所以判据全在入参形状上。默认并发从被调整那条 run 的投影读(run.concurrencyCeiling)。
  2016. function renderRetuneRow(
  2017. input: unknown,
  2018. options: {
  2019. ceiling?: number;
  2020. onOpenWorkflowRun?: (request: unknown) => void;
  2021. /** 退回真修订的那一条:编译过,所以有 display。 */
  2022. display?: unknown;
  2023. /** 同上:它铸了一条新 run,按**本行的** toolCallId 联接得上。 */
  2024. joinedByToolCallId?: boolean;
  2025. status?: string;
  2026. } = {},
  2027. ) {
  2028. Object.defineProperty(window, "matchMedia", {
  2029. configurable: true,
  2030. writable: true,
  2031. value: vi.fn(() => ({
  2032. matches: false,
  2033. addEventListener: vi.fn(),
  2034. removeEventListener: vi.fn(),
  2035. addListener: vi.fn(),
  2036. removeListener: vi.fn(),
  2037. })),
  2038. });
  2039. return render(
  2040. createElement(
  2041. ZCodeIntlProvider,
  2042. { initialLocale: "en-US" },
  2043. createElement(
  2044. PlatformProvider,
  2045. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 这条路径不碰平台服务。
  2046. { platform: {} as any },
  2047. createElement(ConversationRowView, {
  2048. row: {
  2049. rowId: 9,
  2050. turnId: "turn-1",
  2051. createdAt: 1_700_000_000_009,
  2052. createdAtSeq: 9,
  2053. kind: "toolCall",
  2054. toolCallId: "tool-9",
  2055. toolName: "AmendWorkflow",
  2056. status: options.status ?? "success",
  2057. input,
  2058. inputText: "{}",
  2059. // 就地生效的结果只有一句话:没有 display。
  2060. output: {
  2061. text: "Applied to the running run run-7; at most 4 subagents run at once.",
  2062. ...(options.display === undefined ? {} : { display: options.display }),
  2063. },
  2064. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 只填 renderer 读取的字段。
  2065. } as any,
  2066. context: {
  2067. workspacePath: "/workspace",
  2068. theme: "system",
  2069. sessionId: "parent-a",
  2070. onOpenWorkflowRun: options.onOpenWorkflowRun,
  2071. // 本行的 toolCallId 查不到任何 run(它没铸新 run);那条 run 按 runId 才在。
  2072. workflowRunByToolCallId: options.joinedByToolCallId
  2073. ? new Map([
  2074. [
  2075. "tool-9",
  2076. {
  2077. runId: "run-8",
  2078. toolCallId: "tool-9",
  2079. status: "running",
  2080. nodesSettled: 0,
  2081. nodesTotal: 1,
  2082. },
  2083. ],
  2084. ])
  2085. : new Map(),
  2086. workflowRunByRunId: new Map([
  2087. [
  2088. "run-7",
  2089. {
  2090. runId: "run-7",
  2091. toolCallId: "tool-5",
  2092. status: "running",
  2093. nodesSettled: 0,
  2094. nodesTotal: 1,
  2095. ...(options.ceiling === undefined
  2096. ? {}
  2097. : { run: { runId: "run-7", concurrencyCeiling: options.ceiling } }),
  2098. },
  2099. ],
  2100. ]),
  2101. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 同上。
  2102. } as any,
  2103. }),
  2104. ),
  2105. ),
  2106. );
  2107. }
  2108. it("就地生效的修订行退成设置行:点它开那条 run,带发起行的 toolCallId", () => {
  2109. const onOpenWorkflowRun = vi.fn();
  2110. const view = renderRetuneRow(
  2111. { run_id: "run-7", max_concurrency: 4 },
  2112. { ceiling: 13, onOpenWorkflowRun },
  2113. );
  2114. // 卡没了,只剩那一行;「已通过校验」「脚本不变」一个字都不在。
  2115. expect(view.queryByTestId("workflow-card")).toBeNull();
  2116. const row = view.getByTestId("workflow-retune-row");
  2117. expect(row.textContent).toContain("Settings changed");
  2118. expect(row.textContent).toContain("at most 4 at once");
  2119. for (const gone of ["compiled", "Amends run", "script unchanged"]) {
  2120. expect(view.container.textContent).not.toContain(gone);
  2121. }
  2122. fireEvent.click(row);
  2123. expect(onOpenWorkflowRun).toHaveBeenCalledWith({
  2124. parentSessionId: "parent-a",
  2125. toolCallId: "tool-5",
  2126. runId: "run-7",
  2127. });
  2128. });
  2129. // 默认并发 D(run.concurrencyCeiling)是起点不是上限:请求值正好等于 D 与 null 都是「解除本 run
  2130. // 自己的界」,高于或低于 D 的数照念。
  2131. it.each([
  2132. ["请求高于默认", { run_id: "run-7", max_concurrency: 40 }, 13, "at most 40 at once"],
  2133. ["请求正好是默认", { run_id: "run-7", max_concurrency: 13 }, 13, "limit back to the default"],
  2134. ["解除上限(null)", { run_id: "run-7", max_concurrency: null }, 13, "limit back to the default"],
  2135. ["请求低于默认", { run_id: "run-7", max_concurrency: 4 }, 13, "at most 4 at once"],
  2136. // 默认未知(那条 run 已被淘汰出投影):只能照念请求值。
  2137. ["默认未知", { run_id: "run-7", max_concurrency: 4 }, undefined, "at most 4 at once"],
  2138. ])("%s", (_case, input, ceiling, text) => {
  2139. const view = renderRetuneRow(input, ceiling === undefined ? {} : { ceiling });
  2140. expect(view.getByTestId("workflow-retune-row").textContent).toContain(text);
  2141. });
  2142. it("宿主没给打开回调(只读面):还是那一行,只是点不动", () => {
  2143. const view = renderRetuneRow({ run_id: "run-7", max_concurrency: 4 }, { ceiling: 13 });
  2144. expect(view.getByTestId("workflow-retune-row").tagName).not.toBe("BUTTON");
  2145. });
  2146. // run 已结算时同一个调用退回一次**真修订**:它编译过(有 display)、也铸出一条按本行 toolCallId
  2147. // 联接得上的 run。两条判据各自都足以把它挡在设置行之外——这里各钉一条。
  2148. it("退回真修订(有 display、有按 toolCallId 联接的 run):照旧走运行摘要,不退成设置行", () => {
  2149. const view = renderRetuneRow(
  2150. { run_id: "run-7", max_concurrency: 4 },
  2151. {
  2152. ceiling: 13,
  2153. joinedByToolCallId: true,
  2154. display: { kind: "create_workflow", ok: true, errorCount: 0, diagnostics: [] },
  2155. },
  2156. );
  2157. expect(view.queryByTestId("workflow-retune-row")).toBeNull();
  2158. });
  2159. it("只有 display(还没联接到 run)也不退成设置行:编译过就不是就地生效", () => {
  2160. const view = renderRetuneRow(
  2161. { run_id: "run-7", max_concurrency: 4 },
  2162. {
  2163. ceiling: 13,
  2164. display: { kind: "create_workflow", ok: true, errorCount: 0, diagnostics: [] },
  2165. },
  2166. );
  2167. expect(view.queryByTestId("workflow-retune-row")).toBeNull();
  2168. expect(view.queryByTestId("workflow-card")).not.toBeNull();
  2169. });
  2170. it("工具报错(拒绝也是 tool error):照旧走错误渲染", () => {
  2171. const view = renderRetuneRow({ run_id: "run-7", max_concurrency: 4 }, { status: "error" });
  2172. expect(view.queryByTestId("workflow-retune-row")).toBeNull();
  2173. });
  2174. it("活投影那份摘要上没有 contentType 的那一枚不编一个出来(它也从不带 sourcePath)", () => {
  2175. const spy = vi.fn();
  2176. const view = renderRow(spy);
  2177. fireEvent.click(view.getAllByTestId("workflow-card-artifact")[1]!);
  2178. expect(spy).toHaveBeenCalledWith({
  2179. parentSessionId: "parent-a",
  2180. runId: "run-7",
  2181. artifactId: "perf",
  2182. title: "Round timings",
  2183. });
  2184. });
  2185. });
  2186. // ── 接线:行上下文的稿号联接表 → 编译反馈行(docs/dynamic-workflow/presentation.md「Compiler feedback」)──
  2187. // 稿号由宿主从行窗口一遍建成(`workflowDraftJoin.ts`);这里钉 `ConversationRowView` 按 toolCallId 把
  2188. // 这一行自己的位置交给卡片,而不是让卡片自己去翻行窗口。
  2189. describe("ConversationRowView 把稿号交给编译反馈行", () => {
  2190. function renderFeedbackRow(context: Record<string, unknown>) {
  2191. Object.defineProperty(window, "matchMedia", {
  2192. configurable: true,
  2193. writable: true,
  2194. value: vi.fn(() => ({
  2195. matches: false,
  2196. addEventListener: vi.fn(),
  2197. removeEventListener: vi.fn(),
  2198. addListener: vi.fn(),
  2199. removeListener: vi.fn(),
  2200. })),
  2201. });
  2202. return render(
  2203. createElement(
  2204. ZCodeIntlProvider,
  2205. { initialLocale: "en-US" },
  2206. createElement(
  2207. PlatformProvider,
  2208. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 这条路径不碰平台服务。
  2209. { platform: {} as any },
  2210. createElement(ConversationRowView, {
  2211. row: {
  2212. rowId: 9,
  2213. turnId: "turn-1",
  2214. createdAt: 1_700_000_000_009,
  2215. createdAtSeq: 9,
  2216. kind: "toolCall",
  2217. toolCallId: "tool-9",
  2218. toolName: "CreateWorkflow",
  2219. status: "success",
  2220. input: { name: "Release notes digest", script: "const x: number = 'nope';" },
  2221. inputText: "{}",
  2222. output: {
  2223. text: "The workflow script has errors:",
  2224. display: {
  2225. kind: "create_workflow",
  2226. ok: false,
  2227. errorCount: 1,
  2228. diagnostics: [{ line: 1, column: 7, code: 2322, message: "Type error" }],
  2229. },
  2230. },
  2231. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 只填 renderer 读取的字段。
  2232. } as any,
  2233. context: {
  2234. workspacePath: "/workspace",
  2235. theme: "system",
  2236. sessionId: "parent-a",
  2237. ...context,
  2238. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 同上。
  2239. } as any,
  2240. }),
  2241. ),
  2242. ),
  2243. );
  2244. }
  2245. it("联接表里有这一行:稿号与被替代的灯随行上下文到达卡片", () => {
  2246. const view = renderFeedbackRow({
  2247. workflowDraftByToolCallId: new Map([["tool-9", { ordinal: 3, superseded: true }]]),
  2248. });
  2249. expect(view.getByTestId("workflow-draft-ordinal").textContent).toBe("draft 3");
  2250. expect(view.getByTestId("workflow-draft-lamp").getAttribute("data-draft-lamp")).toBe("settled");
  2251. });
  2252. it("联接表缺席(只读分享等宿主):不编号,灯保持警示色", () => {
  2253. const view = renderFeedbackRow({});
  2254. expect(view.queryByTestId("workflow-draft-ordinal")).toBeNull();
  2255. expect(view.getByTestId("workflow-draft-lamp").getAttribute("data-draft-lamp")).toBe("open");
  2256. });
  2257. });