workflowHoleSurfaces.test.ts 16 KB

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