workflowHoleSurfaces.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // 留白在各个面上的画法(docs/dynamic-workflow/presentation.md「Holes on the timeline」):时间线的留白站
  2. // (虚线灯、虚线轨道、尾巴残段、补全的头与框)、run 卡的「{n} 处留白待补全」芯片、
  3. // 确认窗的留白行与「2 个阶段 · 2 处留白」、侧栏迷你轨道的虚线灯与「{name} · 等待补全」、
  4. // 已接进 run 的补全行摘要。静态渲染(renderToStaticMarkup),交互不在此。
  5. import { createElement, type ReactNode } from "react";
  6. import { renderToStaticMarkup } from "react-dom/server";
  7. import { describe, expect, it, vi } from "vitest";
  8. import type { WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  9. import type { WorkflowCausalityGraphData } from "@/components/workflow-graph/types.js";
  10. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  11. vi.mock("@/ControlHintTooltip.js", async (importOriginal) => ({
  12. ...(await importOriginal<typeof import("@/ControlHintTooltip.js")>()),
  13. ControlHintTooltip: ({ children }: { children?: ReactNode }) => children,
  14. }));
  15. vi.mock("@/hooks/useWorkflowSubagentModelProviderName.js", () => ({
  16. useWorkflowSubagentModelProviderName: () => undefined,
  17. }));
  18. vi.mock("@/components/ai-elements/code-block.js", () => ({
  19. CodeBlock: ({ code }: { code: string }) => createElement("pre", null, code),
  20. CodeBlockHeader: () => null,
  21. }));
  22. vi.mock("@/lib/workflowRunAckStore.js", () => ({
  23. getWorkflowRunAckStore: () => ({ acknowledge: () => undefined }),
  24. useWorkflowRunAcknowledged: () => () => false,
  25. }));
  26. vi.mock("@/v4/workflowRunOpenContext.js", () => ({ useWorkflowRunOpen: () => null }));
  27. // eslint-disable-next-line import/first -- 必须在 mock 之后再引入被测组件。
  28. import { buildWorkflowTimeline } from "@/components/workflow-timeline/timeline-model.js";
  29. // eslint-disable-next-line import/first -- 同上。
  30. import { WorkflowTimeline } from "@/components/workflow-timeline/WorkflowTimeline.js";
  31. // eslint-disable-next-line import/first -- 同上。
  32. import { WorkflowRunDigest } from "@/components/workflow-timeline/WorkflowRunDigest.js";
  33. // eslint-disable-next-line import/first -- 同上。
  34. import { WorkflowTimelineFills } from "@/components/workflow-timeline/WorkflowTimelineFills.js";
  35. // eslint-disable-next-line import/first -- 同上。
  36. import { HoleWaitingBody } from "@/app-shell/WorkflowRunHoleParts.js";
  37. // eslint-disable-next-line import/first -- 同上。
  38. import { workflowPhasesDetail } from "@/components/workflow-timeline/timeline-summary.js";
  39. // eslint-disable-next-line import/first -- 同上。
  40. import { WorkflowPermissionHoles } from "@/WorkflowPermissionHoles.js";
  41. // eslint-disable-next-line import/first -- 同上。
  42. import { TaskWorkflowRunLines } from "@/components/workflow-run-line/TaskWorkflowRunLines.js";
  43. // eslint-disable-next-line import/first -- 同上。
  44. import { foldWorkflowRunRail } from "@/lib/workflowRunLine.js";
  45. // eslint-disable-next-line import/first -- 同上。
  46. import { WorkflowToolSummary } from "@/v4/WorkflowToolSummary.js";
  47. // eslint-disable-next-line import/first -- 同上。
  48. import { workflowFillCounts } from "@/ToolCallBlocks/renderers/createWorkflowHoleInput.js";
  49. // 留白 id 是名字键 `hole#<8 位十六进制>`(docs/analysis.md「Sites」);渲染器只认形状。
  50. const H1 = "hole#a0000001"; // 决定分组
  51. const H2 = "hole#a0000002"; // 评判
  52. const H11 = "hole#a0000011"; // 挑选(在 H1 的体里)
  53. function html(node: ReactNode, locale: "zh-CN" | "en-US" = "zh-CN"): string {
  54. return renderToStaticMarkup(createElement(ZCodeIntlProvider, { initialLocale: locale }, node));
  55. }
  56. const OPEN: WorkflowCausalityGraphData = {
  57. steps: [{ id: "ask#1", kind: "ask", label: "scout", lane: "actor#1", phase: "phase#1" }],
  58. lanes: [{ id: "actor#1", name: "侦察员" }],
  59. participants: [{ id: "p1", phase: "phase#1", lane: "actor#1", steps: ["ask#1"] }],
  60. handoffs: [],
  61. phases: [
  62. { id: "phase#1", name: "探索" },
  63. { id: H1, name: "决定分组" },
  64. { id: H2, name: "评判" },
  65. ],
  66. phaseEdges: [
  67. { from: "phase#1", to: H1 },
  68. { from: H1, to: H2 },
  69. ],
  70. holes: [
  71. { siteId: H1, name: "决定分组", type: "Plan" },
  72. { siteId: H2, name: "评判", type: "Verdict", tail: true },
  73. ],
  74. };
  75. const FILLED: WorkflowCausalityGraphData = {
  76. steps: [
  77. { id: "ask#1", kind: "ask", label: "scout", lane: "actor#1", phase: "phase#1" },
  78. {
  79. id: `${H1}/ask#1`,
  80. kind: "ask",
  81. label: "smoke",
  82. lane: `${H1}/actor#1`,
  83. phase: `${H1}/phase#1`,
  84. fill: H1,
  85. },
  86. ],
  87. lanes: [
  88. { id: "actor#1", name: "侦察员" },
  89. { id: `${H1}/actor#1`, name: "冒烟员" },
  90. ],
  91. participants: [
  92. { id: "p1", phase: "phase#1", lane: "actor#1", steps: ["ask#1"] },
  93. { id: "p2", phase: `${H1}/phase#1`, lane: `${H1}/actor#1`, steps: [`${H1}/ask#1`] },
  94. ],
  95. handoffs: [],
  96. // 体以标记开头:H1 自己的阶段留在表里(无成员),时间线把它交给头、不成站。
  97. phases: [
  98. { id: "phase#1", name: "探索" },
  99. { id: H1, name: "决定分组" },
  100. { id: `${H1}/phase#1`, name: "冒烟测试", fill: H1 },
  101. ],
  102. phaseEdges: [
  103. { from: "phase#1", to: H1 },
  104. { from: H1, to: `${H1}/phase#1` },
  105. ],
  106. };
  107. function run(overrides: Partial<WorkflowRunState> = {}): WorkflowRunState {
  108. return {
  109. runId: "run-1",
  110. toolCallId: "tc-1",
  111. status: "running",
  112. usage: { spentTokens: 0, nodesUsed: 0 },
  113. actors: [],
  114. nodes: [],
  115. lastEventSequence: 0,
  116. ...overrides,
  117. };
  118. }
  119. describe("时间线上的留白站", () => {
  120. it("开着的留白:虚线灯、只画名字不画类型、两侧虚线轨道段、尾巴留白的 40px 残段", () => {
  121. const out = html(
  122. createElement(WorkflowTimeline, { model: buildWorkflowTimeline(OPEN, undefined) }),
  123. );
  124. expect(out).toContain('data-station-hole="open"');
  125. expect(out).toContain('data-lamp="hole-open"');
  126. expect(out).toContain("border-dashed");
  127. // 类型是编译契约,不上界面:站头只有名字(2026-09-28 实测里等宽类型徽标把站名挤到零宽)。
  128. expect(out).not.toContain("workflow-hole-type");
  129. expect(out).toContain("决定分组");
  130. expect(out).toContain("评判");
  131. expect(out).not.toContain("Plan");
  132. expect(out).not.toContain("Verdict");
  133. expect(out).toContain('data-rail-dashed="true"');
  134. expect(out).toContain('data-testid="workflow-timeline-tail-stub"');
  135. expect(out).not.toContain("workflow-timeline-fill-head");
  136. });
  137. it("等待中:警示色虚线灯(常亮光晕、不搏动)与「等待补全」", () => {
  138. const out = html(
  139. createElement(WorkflowTimeline, {
  140. model: buildWorkflowTimeline(
  141. OPEN,
  142. run({
  143. holes: [{ siteId: H1, ordinal: 1, name: "决定分组", type: "Plan", state: "waiting" }],
  144. }),
  145. ),
  146. }),
  147. );
  148. expect(out).toContain('data-lamp="hole-waiting"');
  149. expect(out).toContain("wf-lamp-hole-waiting");
  150. expect(out).toContain("等待补全");
  151. // 等待的灯不是 running 灯:没有心跳。
  152. expect(out).not.toContain("wf-lamp-running");
  153. });
  154. it("补全后:头(笔 · 名,不画类型)与框,体的站带 data-fill;tooltip 说由主代理补全;框静止时不亮", () => {
  155. const out = html(
  156. createElement(WorkflowTimeline, {
  157. model: buildWorkflowTimeline(
  158. FILLED,
  159. run({
  160. holes: [
  161. {
  162. siteId: H1,
  163. ordinal: 1,
  164. name: "决定分组",
  165. type: "Plan",
  166. state: "filled",
  167. filledAt: 1,
  168. },
  169. ],
  170. }),
  171. ),
  172. }),
  173. );
  174. expect(out).toContain('data-testid="workflow-timeline-fill-head"');
  175. expect(out).toContain("决定分组");
  176. expect(out).not.toContain("Plan");
  177. expect(out).toContain("由主代理补全");
  178. // 区域换成了框(docs/dynamic-workflow/presentation.md「Filled with phases」):一条 pathLength 为 1 的路径。
  179. expect(out).not.toContain("workflow-timeline-fill-region");
  180. expect(out).toContain('data-testid="workflow-timeline-fill-frame"');
  181. expect(out).toContain(`data-fill-frame="${H1}"`);
  182. expect(out).toContain('pathLength="1"');
  183. expect(out).not.toContain('data-fill-on="true"');
  184. // 头是唯一的点亮入口;站仍带 data-fill(测试与生长用),但不再是悬停的目标。
  185. expect(out).toContain(`data-fill-head="${H1}"`);
  186. expect(out).toContain(`data-fill="${H1}"`);
  187. });
  188. });
  189. describe("侧板的等待体", () => {
  190. it("写「等待主代理补全 · 等了多久」与主代理收到的提示语;没有提示语时不留空段", () => {
  191. const now = 10 * 60_000;
  192. const out = html(
  193. createElement(HoleWaitingBody, {
  194. hole: {
  195. siteId: H1,
  196. state: "waiting",
  197. type: "Plan",
  198. since: now - 3 * 60_000,
  199. prompt: "侦察结果如下,请补全这一段。",
  200. },
  201. now,
  202. }),
  203. );
  204. expect(out).toContain("等待主代理补全");
  205. expect(out).toContain('data-testid="workflow-run-hole-prompt"');
  206. expect(out).toContain("侦察结果如下,请补全这一段。");
  207. const bare = html(
  208. createElement(HoleWaitingBody, { hole: { siteId: H1, state: "waiting" }, now }),
  209. );
  210. expect(bare).not.toContain("workflow-run-hole-prompt");
  211. });
  212. });
  213. describe("嵌套的补全框", () => {
  214. const frame = (holeId: string, x0: number, x1: number) => ({
  215. holeId,
  216. box: { x0, x1, y0: 12, y1: 60, start: x0 + 25 },
  217. });
  218. const fresh: ReadonlySet<string> = new Set();
  219. it("一次只亮一个框:指针在内层的头上只亮内层,在外层的头上只亮外层(框不叠)", () => {
  220. const fills = [
  221. { holeId: H1, name: "决定分组", from: 1, to: 3 },
  222. { holeId: H11, name: "挑选", from: 2, to: 2 },
  223. ];
  224. const frames = [frame(H1, 184, 752), frame(H11, 376, 552)];
  225. const props = { fills, frames, fresh, height: 80, inset: 0, width: 900 };
  226. const inner = html(createElement(WorkflowTimelineFills, { ...props, active: H11 }));
  227. expect(inner.match(/data-fill-on="true"/gu)).toHaveLength(2); // 框 + 它的头
  228. expect(inner).toMatch(new RegExp(`data-fill-frame="${H11}"[^>]*data-fill-on="true"`, "u"));
  229. expect(inner).not.toMatch(new RegExp(`data-fill-frame="${H1}"[^>]*data-fill-on="true"`, "u"));
  230. const outer = html(createElement(WorkflowTimelineFills, { ...props, active: H1 }));
  231. expect(outer).toMatch(new RegExp(`data-fill-frame="${H1}"[^>]*data-fill-on="true"`, "u"));
  232. expect(outer).not.toMatch(new RegExp(`data-fill-frame="${H11}"[^>]*data-fill-on="true"`, "u"));
  233. expect(outer.match(/data-testid="workflow-timeline-fill-head"/gu)).toHaveLength(2);
  234. // 起点不同:两个头各自对准自己的第一盏灯(两行)。
  235. expect(outer.match(/data-testid="workflow-timeline-fill-heads"/gu)).toHaveLength(2);
  236. const rest = html(createElement(WorkflowTimelineFills, { ...props, active: undefined }));
  237. expect(rest).not.toContain('data-fill-on="true"');
  238. });
  239. it("内层与外层同起于一列:内层的头排在外层头的右缘再过 12px,同一行;顺序外层在前", () => {
  240. const fills = [
  241. { holeId: H1, name: "决定分组", from: 1, to: 3 },
  242. { holeId: H11, name: "挑选", from: 1, to: 1 },
  243. ];
  244. const out = html(
  245. createElement(WorkflowTimelineFills, {
  246. active: undefined,
  247. fills,
  248. frames: [],
  249. fresh,
  250. height: 40,
  251. inset: 0,
  252. width: 900,
  253. }),
  254. );
  255. const rows = out.match(
  256. /<span class="absolute flex items-center gap-3"[^>]*data-testid="workflow-timeline-fill-heads"[^>]*>/gu,
  257. );
  258. expect(rows).toHaveLength(1);
  259. expect(out.indexOf(`data-fill-head="${H1}"`)).toBeLessThan(
  260. out.indexOf(`data-fill-head="${H11}"`),
  261. );
  262. // 两个头在同一个容器里:内层头不再自己带 left。
  263. expect(out.match(/data-testid="workflow-timeline-fill-head"/gu)).toHaveLength(2);
  264. // 行的左缘 = 第 1 站的 x(192)+ 灯心 17 − 笔的半宽退让 6。
  265. expect(out).toContain("left:203px");
  266. expect(out.match(/left:/gu)).toHaveLength(1);
  267. });
  268. it("新鲜的两秒:框带 wf-fill-frame-fresh 自己画出(没有指针也亮),头染警示色", () => {
  269. const fills = [{ holeId: H1, name: "决定分组", from: 1, to: 2, filledAt: Date.now() }];
  270. const out = html(
  271. createElement(WorkflowTimelineFills, {
  272. active: undefined,
  273. fills,
  274. frames: [frame(H1, 184, 560)],
  275. fresh: new Set([H1]),
  276. height: 80,
  277. inset: 0,
  278. width: 900,
  279. }),
  280. );
  281. expect(out).toContain("wf-fill-frame-fresh");
  282. expect(out).toMatch(/data-fill-fresh="true"[^>]*data-fill-on="true"/u);
  283. expect(out).toContain("text-warning");
  284. });
  285. });
  286. describe("run 卡与确认窗", () => {
  287. it("卡表头长出「{n} 处留白待补全」芯片(与问题芯片并存)", () => {
  288. const out = html(
  289. createElement(WorkflowRunDigest, {
  290. graph: OPEN,
  291. name: "flaky-hunt",
  292. pendingQuestions: 1,
  293. runId: "run-1",
  294. summary: {
  295. runId: "run-1",
  296. status: "running",
  297. nodesSettled: 0,
  298. nodesTotal: 1,
  299. agents: 1,
  300. run: run({ holes: [{ siteId: H1, ordinal: 1, name: "决定分组", state: "waiting" }] }),
  301. },
  302. testIdKey: "t",
  303. }),
  304. );
  305. expect(out).toContain('data-testid="workflow-digest-holes"');
  306. expect(out).toContain("1 处留白待补全");
  307. expect(out).toContain('data-testid="workflow-digest-questions"');
  308. });
  309. it("细节数「1 个阶段 · 2 处留白」:开着的留白不算阶段", () => {
  310. const format = (descriptor: { id: string }, values?: Record<string, string | number>) =>
  311. descriptor.id.endsWith("card.holes")
  312. ? `${values?.count} 处留白`
  313. : descriptor.id.endsWith("card.phase") || descriptor.id.endsWith("card.phases")
  314. ? `${values?.count} 个阶段`
  315. : descriptor.id;
  316. expect(workflowPhasesDetail(format, buildWorkflowTimeline(OPEN, undefined), OPEN)).toBe(
  317. "1 个阶段 · 2 处留白",
  318. );
  319. });
  320. it("确认窗的留白行:每处留白一枚芯片(笔、名;不画类型)加一句会暂停", () => {
  321. const out = html(createElement(WorkflowPermissionHoles, { holes: OPEN.holes }));
  322. expect(out).toContain('data-testid="workflow-permission-holes"');
  323. expect(out.match(/data-testid="workflow-permission-hole"/gu)).toHaveLength(2);
  324. expect(out).toContain("运行到留白处会暂停,等主代理补全后继续。");
  325. expect(out).not.toContain("Plan");
  326. expect(html(createElement(WorkflowPermissionHoles, { holes: [] }))).toBe("");
  327. });
  328. it("已接进 run 的补全行:「留白已补全 · {name} · {k} 个阶段 · {m} 个子代理」", () => {
  329. const counts = workflowFillCounts(FILLED, H1);
  330. expect(counts).toEqual({ agents: 1, phases: 1 });
  331. const out = html(
  332. createElement(WorkflowToolSummary, {
  333. toolCallId: "tc-fill",
  334. summary: { runId: "run-1", status: "running", nodesSettled: 0, nodesTotal: 0, agents: 3 },
  335. fill: { name: "决定分组", ...counts },
  336. }),
  337. );
  338. expect(out).toContain("留白已补全");
  339. expect(out).toContain("决定分组");
  340. expect(out).toContain("1 个阶段");
  341. expect(out).toContain("1 个子代理");
  342. });
  343. });
  344. describe("侧栏运行行", () => {
  345. it("迷你轨道把留白画成虚线灯(等待时警示色),行上写「{name} · 等待补全」", () => {
  346. const rail = foldWorkflowRunRail([
  347. { name: "探索", status: "done" },
  348. { name: "决定分组", status: "pending", hole: "waiting" },
  349. { name: "评判", status: "pending", hole: "open" },
  350. ]);
  351. expect(rail.stations.map((station) => station.hole)).toEqual([undefined, "waiting", "open"]);
  352. expect(rail.stations[1]!.reached).toBe(true);
  353. const out = html(
  354. createElement(TaskWorkflowRunLines, {
  355. activity: {
  356. runs: [
  357. {
  358. runId: "run-1",
  359. status: "running",
  360. phases: [
  361. { name: "探索", status: "done" },
  362. { name: "决定分组", status: "pending", hole: "waiting" },
  363. ],
  364. currentPhase: "探索",
  365. agentsWorking: 0,
  366. waitingHole: "决定分组",
  367. },
  368. ],
  369. },
  370. isActive: false,
  371. intl: {
  372. formatMessage: (desc: { id: string }, values?: Record<string, string>) =>
  373. desc.id === "taskList.workflowRun.holeWaiting" ? `${values?.name} · 等待补全` : desc.id,
  374. },
  375. }),
  376. );
  377. expect(out).toContain('data-rail-hole="waiting"');
  378. expect(out).toContain("border-warning");
  379. expect(out).toContain("决定分组 · 等待补全");
  380. });
  381. });