workflowFillFrames.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // @vitest-environment jsdom
  2. // 补全的框(docs/dynamic-workflow/presentation.md「Holes on the timeline」的「Filled with phases」「The stroke」
  3. // 「Filled without phases」「The pane」):几何是纯函数,钉数;点亮只认头——卡上的头与笔标、侧板的标题行
  4. // 与笔标——站与药丸不点亮任何东西;一次只亮一个;侧板不缩进。
  5. import { cleanup, fireEvent, render, within } from "@testing-library/react";
  6. import { createElement } from "react";
  7. import { afterEach, describe, expect, it } from "vitest";
  8. import type { WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  9. import type { WorkflowCausalityGraphData } from "@/components/workflow-graph/types.js";
  10. import {
  11. FRAME_OUTSET,
  12. fillDepthOf,
  13. fillMarksOf,
  14. framePath,
  15. legendGaps,
  16. spineFrameBox,
  17. timelineFrames,
  18. } from "@/components/workflow-timeline/timeline-frames.js";
  19. import {
  20. HEAD_ROW,
  21. STATION_PITCH,
  22. STATION_WIDTH,
  23. timelineLayout,
  24. } from "@/components/workflow-timeline/timeline-geometry.js";
  25. import { buildWorkflowTimeline } from "@/components/workflow-timeline/timeline-model.js";
  26. import { WorkflowTimeline } from "@/components/workflow-timeline/WorkflowTimeline.js";
  27. import { WorkflowRunPhaseList } from "@/app-shell/WorkflowRunPhaseList.js";
  28. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  29. // 留白 id 是名字键 `hole#<8 位十六进制>`(docs/analysis.md「Sites」);渲染器只认形状。
  30. const H1 = "hole#a0000001"; // 决定分组(体里两个阶段)
  31. const H2 = "hole#a0000002"; // 评判(尾巴、无阶段的补全)
  32. type Step = WorkflowCausalityGraphData["steps"][number];
  33. const STEPS: Step[] = [
  34. { id: "ask#1", kind: "ask", label: "scout", lane: "actor#1", phase: "phase#1" },
  35. {
  36. id: `${H1}/ask#1`,
  37. kind: "ask",
  38. label: "smoke",
  39. lane: `${H1}/actor#1`,
  40. phase: `${H1}/phase#1`,
  41. fill: H1,
  42. },
  43. {
  44. id: `${H1}/ask#2`,
  45. kind: "ask",
  46. label: "group",
  47. lane: `${H1}/actor#2`,
  48. phase: `${H1}/phase#2`,
  49. fill: H1,
  50. },
  51. { id: "ask#2", kind: "ask", label: "member", lane: "actor#2", phase: "phase#2" },
  52. { id: `${H2}/ask#1`, kind: "ask", label: "judge", lane: `${H2}/actor#1`, phase: H2, fill: H2 },
  53. ];
  54. /** 探索 → [决定分组:冒烟测试 → 分组] → 执行 → 评判(无阶段补全,一个 ask)。 */
  55. const GRAPH: WorkflowCausalityGraphData = {
  56. steps: STEPS,
  57. lanes: [
  58. { id: "actor#1", name: "侦察员" },
  59. { id: `${H1}/actor#1`, name: "冒烟员" },
  60. { id: `${H1}/actor#2`, name: "分组员" },
  61. { id: "actor#2", name: "组员" },
  62. { id: `${H2}/actor#1`, name: "评委" },
  63. ],
  64. participants: STEPS.map((step) => ({
  65. id: `${step.phase}:${step.lane}`,
  66. lane: step.lane,
  67. phase: step.phase!,
  68. steps: [step.id],
  69. })),
  70. handoffs: [],
  71. phases: [
  72. { id: "phase#1", name: "探索" },
  73. { id: H1, name: "决定分组" },
  74. { id: `${H1}/phase#1`, name: "冒烟测试", fill: H1 },
  75. { id: `${H1}/phase#2`, name: "分组", fill: H1 },
  76. { id: "phase#2", name: "执行" },
  77. { id: H2, name: "评判" },
  78. ],
  79. phaseEdges: [
  80. { from: "phase#1", to: H1 },
  81. { from: H1, to: `${H1}/phase#1` },
  82. { from: `${H1}/phase#1`, to: `${H1}/phase#2` },
  83. { from: `${H1}/phase#2`, to: "phase#2" },
  84. { from: "phase#2", to: H2 },
  85. ],
  86. holes: [],
  87. };
  88. function run(filledAt = 1): WorkflowRunState {
  89. return {
  90. runId: "dwfrun-1",
  91. toolCallId: "tool-wf-1",
  92. status: "running",
  93. usage: { spentTokens: 0, nodesUsed: 0 },
  94. actors: [],
  95. nodes: [],
  96. lastEventSequence: 0,
  97. holes: [
  98. { siteId: H1, ordinal: 1, name: "决定分组", state: "filled", filledAt },
  99. { siteId: H2, ordinal: 1, name: "评判", state: "filled", filledAt },
  100. ],
  101. };
  102. }
  103. function wrap(node: ReturnType<typeof createElement>) {
  104. return render(createElement(ZCodeIntlProvider, { initialLocale: "zh-CN" }, node));
  105. }
  106. afterEach(cleanup);
  107. describe("框的几何", () => {
  108. it("一笔:从上缘的笔 x 出发、顺时针、回到笔 x;1px 线落在半像素上;没有笔 x 时从左上角圆角之后起", () => {
  109. const d = framePath({ x0: 184, y0: 12, x1: 560, y1: 100, start: 209 });
  110. expect(d.startsWith("M209,12.5 H551.5")).toBe(true);
  111. expect(d.endsWith("H209")).toBe(true);
  112. // 顺时针:右上角 → 右缘向下 → 底缘向左 → 左缘向上 → 左上角。
  113. expect(d).toContain("Q559.5,12.5 559.5,20.5");
  114. expect(d).toContain("V91.5");
  115. expect(d).toContain("H192.5");
  116. const corner = framePath({ x0: 0, y0: 0, x1: 100, y1: 50 });
  117. expect(corner.startsWith("M8.5,0.5")).toBe(true);
  118. expect(corner.endsWith("H8.5")).toBe(true);
  119. });
  120. it("有头的补全:首列左缘 − 8 到末列右缘 + 8,头那一行的中线到最高那一列药丸之下 8px,起笔在第一盏灯", () => {
  121. const model = buildWorkflowTimeline(GRAPH, run());
  122. expect(model.fills?.map((fill) => [fill.holeId, fill.from, fill.to])).toEqual([[H1, 1, 2]]);
  123. const layout = timelineLayout(model.arcs, model.bands, true);
  124. const frames = timelineFrames({
  125. columnHeight: (i) => (i === 2 ? 70 : 32),
  126. fills: model.fills!,
  127. height: 400,
  128. inset: 0,
  129. layout,
  130. parents: model.holeParents ?? {},
  131. stations: model.stations,
  132. width: 5 * STATION_PITCH,
  133. });
  134. const h1 = frames.find((frame) => frame.holeId === H1)!;
  135. expect(h1.box).toEqual({
  136. x0: STATION_PITCH - FRAME_OUTSET,
  137. x1: 2 * STATION_PITCH + STATION_WIDTH + FRAME_OUTSET,
  138. y0: HEAD_ROW / 2,
  139. y1: layout.pillsTop + 70 + 8,
  140. start: STATION_PITCH + 17,
  141. });
  142. // 无阶段的补全(评判):它那一列,轨道行之上 4px 到它的药丸之下 8px,从左上角起笔。
  143. const h2 = frames.find((frame) => frame.holeId === H2)!;
  144. expect(h2.box).toEqual({
  145. x0: 4 * STATION_PITCH - FRAME_OUTSET,
  146. x1: 4 * STATION_PITCH + STATION_WIDTH + FRAME_OUTSET,
  147. y0: layout.rowY[0]! - 12 - 4,
  148. y1: layout.pillsTop + 32 + 8,
  149. });
  150. });
  151. it("那几列没有药丸(卡收起)时框只到轨道行之下 4px;首列与末列的框夹在画布里", () => {
  152. const model = buildWorkflowTimeline(GRAPH, run());
  153. const layout = timelineLayout(model.arcs, model.bands, true);
  154. const fills = [{ holeId: H1, name: "决定分组", from: 0, to: 4 }];
  155. const [frame] = timelineFrames({
  156. columnHeight: () => 0,
  157. fills,
  158. height: 90,
  159. inset: 0,
  160. layout,
  161. parents: {},
  162. stations: model.stations.map(({ track }) => ({ track })),
  163. width: 4 * STATION_PITCH + STATION_WIDTH,
  164. });
  165. expect(frame!.box.y1).toBe(layout.pillsTop - 4);
  166. expect(frame!.box.x0).toBe(0);
  167. expect(frame!.box.x1).toBe(4 * STATION_PITCH + STATION_WIDTH);
  168. });
  169. it("与外层同起于一列的嵌套框每层左、右、下各收 4px;起点不同的不收", () => {
  170. const layout = timelineLayout([], [], true);
  171. const fills = [
  172. { holeId: H1, name: "外", from: 1, to: 3 },
  173. { holeId: H2, name: "内", from: 1, to: 2 },
  174. ];
  175. const base = {
  176. columnHeight: () => 32,
  177. height: 400,
  178. inset: 0,
  179. layout,
  180. stations: [],
  181. width: 2000,
  182. };
  183. const [outer, inner] = timelineFrames({ ...base, fills, parents: { [H2]: H1 } });
  184. expect(inner!.box.x0 - outer!.box.x0).toBe(4);
  185. expect(inner!.box.y1).toBe(outer!.box.y1 - 4);
  186. expect(inner!.box.y0).toBe(outer!.box.y0);
  187. // 没有父表:两者无关,不收。
  188. const [, unrelated] = timelineFrames({ ...base, fills, parents: {} });
  189. expect(unrelated!.box.x0).toBe(outer!.box.x0);
  190. });
  191. it("笔标:只收已补全、又不在 fills 里的留白站;图例按头那一行两侧各让 4px 挖空,量不到宽度不挖", () => {
  192. const model = buildWorkflowTimeline(GRAPH, run());
  193. expect(fillMarksOf(model.stations, model.fills ?? [])).toEqual([
  194. { holeId: H2, index: 4, filledAt: 1 },
  195. ]);
  196. expect(
  197. legendGaps([
  198. { left: 203, width: 120 },
  199. { left: 395, width: 0 },
  200. ]),
  201. ).toEqual([{ x: 199, width: 128 }]);
  202. expect(fillDepthOf(H2, [{ holeId: H1 }], { [H2]: H1 })).toBe(1);
  203. });
  204. it("侧板:x 8 → 右缘 − 12,嵌套每层各收 4px;有头的从标题中线到最后一节之下 4px,起笔在轨道 x;无头的上下各收 2px", () => {
  205. expect(spineFrameBox({ top: 100, bottom: 300, width: 440, depth: 0, headed: true })).toEqual({
  206. x0: 8,
  207. x1: 428,
  208. y0: 112,
  209. y1: 304,
  210. start: 21,
  211. });
  212. expect(spineFrameBox({ top: 100, bottom: 300, width: 440, depth: 1, headed: false })).toEqual({
  213. x0: 12,
  214. x1: 424,
  215. y0: 102,
  216. y1: 298,
  217. });
  218. });
  219. });
  220. describe("卡:只有头点亮框", () => {
  221. it("指针在站或药丸上什么都不亮;在头上只亮这一个框、头换前景色;离开时熄", () => {
  222. const view = wrap(
  223. createElement(WorkflowTimeline, { model: buildWorkflowTimeline(GRAPH, run()) }),
  224. );
  225. const frameOf = (id: string) => view.container.querySelector(`[data-fill-frame="${id}"]`)!;
  226. expect(
  227. view.container.querySelectorAll('[data-testid="workflow-timeline-fill-frame"]'),
  228. ).toHaveLength(2);
  229. const station = view.container.querySelector(
  230. `[data-testid="workflow-timeline-station"][data-fill="${H1}"]`,
  231. )!;
  232. fireEvent.pointerOver(station);
  233. expect(view.container.querySelector('[data-fill-on="true"]')).toBeNull();
  234. const pill = view.container.querySelector(
  235. `[data-testid="workflow-timeline-pills"][data-fill="${H1}"]`,
  236. )!;
  237. fireEvent.pointerOver(pill);
  238. expect(view.container.querySelector('[data-fill-on="true"]')).toBeNull();
  239. const head = view.getByTestId("workflow-timeline-fill-head");
  240. fireEvent.pointerOver(head.querySelector("svg")!);
  241. expect(frameOf(H1).getAttribute("data-fill-on")).toBe("true");
  242. expect(frameOf(H2).getAttribute("data-fill-on")).toBeNull();
  243. expect(head.getAttribute("data-fill-on")).toBe("true");
  244. expect(head.className).toContain("text-foreground");
  245. fireEvent.pointerLeave(view.getByTestId("workflow-timeline"));
  246. expect(view.container.querySelector('[data-fill-on="true"]')).toBeNull();
  247. });
  248. it("弧那一层 SVG 不接指针:它铺满整张时间线、画在头之后,接了就吞掉头的悬停与点击", () => {
  249. const graph: WorkflowCausalityGraphData = {
  250. ...GRAPH,
  251. phaseEdges: [
  252. ...(GRAPH.phaseEdges ?? []),
  253. { from: "phase#2", to: `${H1}/phase#1`, back: true },
  254. ],
  255. };
  256. const view = wrap(
  257. createElement(WorkflowTimeline, { model: buildWorkflowTimeline(graph, run()) }),
  258. );
  259. const layers = [...view.container.querySelectorAll("svg")].filter(
  260. (svg) => svg.querySelector("marker") !== null,
  261. );
  262. expect(layers.length).toBeGreaterThan(0);
  263. for (const layer of layers)
  264. expect(layer.getAttribute("class")).toContain("pointer-events-none");
  265. });
  266. it("无阶段的补全:指针落在站头的笔标上,框住这一列", () => {
  267. const view = wrap(
  268. createElement(WorkflowTimeline, { model: buildWorkflowTimeline(GRAPH, run()) }),
  269. );
  270. const mark = view.getByTestId("workflow-hole-fill-mark");
  271. expect(mark.getAttribute("data-fill-head")).toBe(H2);
  272. // 指针真正落下的是笔图标(SVGElement):委托必须从它往上找到笔标。
  273. fireEvent.pointerOver(mark.querySelector("svg")!);
  274. expect(
  275. view.container.querySelector(`[data-fill-frame="${H2}"]`)!.getAttribute("data-fill-on"),
  276. ).toBe("true");
  277. expect(
  278. view.container.querySelector(`[data-fill-frame="${H1}"]`)!.getAttribute("data-fill-on"),
  279. ).toBeNull();
  280. });
  281. it("补全刚接进 run:两个框都带新鲜态、没有指针也亮", () => {
  282. const view = wrap(
  283. createElement(WorkflowTimeline, { model: buildWorkflowTimeline(GRAPH, run(Date.now())) }),
  284. );
  285. const fresh = view.container.querySelectorAll(
  286. '[data-testid="workflow-timeline-fill-frame"][data-fill-fresh="true"]',
  287. );
  288. expect(fresh).toHaveLength(2);
  289. for (const frame of fresh) expect(frame.getAttribute("class")).toContain("wf-fill-frame-fresh");
  290. });
  291. });
  292. describe("侧板:同一个框竖着画,不缩进", () => {
  293. function pane(filledAt = 1) {
  294. const model = buildWorkflowTimeline(GRAPH, run(filledAt));
  295. return wrap(
  296. createElement(WorkflowRunPhaseList, {
  297. graph: GRAPH,
  298. model,
  299. pendingQuestions: [],
  300. run: run(filledAt),
  301. }),
  302. );
  303. }
  304. it("补全写下的节与别的节同一条文字列:标题行与节头都不再多缩 12px", () => {
  305. const view = pane();
  306. const sections = view.getAllByTestId("workflow-run-phase");
  307. const inFill = sections.filter((section) => section.getAttribute("data-phase-fill") === H1);
  308. expect(inFill).toHaveLength(2);
  309. const plain = sections.find((section) => section.getAttribute("data-phase-id") === "phase#1")!;
  310. const pad = (section: HTMLElement) =>
  311. within(section).getByTestId("workflow-run-phase-toggle").style.paddingLeft;
  312. for (const section of inFill) expect(pad(section)).toBe(pad(plain));
  313. expect(view.getByTestId("workflow-run-fill-heading").style.paddingLeft).toBe("");
  314. });
  315. it("标题行是头:指针落在它上面画出这次补全的框;落在节头上不画;笔标框住它那一节", () => {
  316. const view = pane();
  317. const frames = view.container.querySelectorAll('[data-testid="workflow-run-fill-frame"]');
  318. expect([...frames].map((frame) => frame.getAttribute("data-fill-frame"))).toEqual([H1, H2]);
  319. const frameOf = (id: string) =>
  320. view.container.querySelector(
  321. `[data-testid="workflow-run-fill-frame"][data-fill-frame="${id}"]`,
  322. )!;
  323. const toggle = within(view.getAllByTestId("workflow-run-phase")[1]!).getByTestId(
  324. "workflow-run-phase-toggle",
  325. );
  326. fireEvent.pointerOver(toggle);
  327. expect(view.container.querySelector('[data-fill-on="true"]')).toBeNull();
  328. const heading = view.getByTestId("workflow-run-fill-heading");
  329. expect(heading.getAttribute("data-fill-head")).toBe(H1);
  330. fireEvent.pointerOver(heading);
  331. expect(frameOf(H1).getAttribute("data-fill-on")).toBe("true");
  332. expect(heading.getAttribute("data-fill-on")).toBe("true");
  333. expect(frameOf(H2).getAttribute("data-fill-on")).toBeNull();
  334. fireEvent.pointerOver(view.getByTestId("workflow-hole-fill-mark").querySelector("svg")!);
  335. expect(frameOf(H2).getAttribute("data-fill-on")).toBe("true");
  336. expect(frameOf(H1).getAttribute("data-fill-on")).toBeNull();
  337. fireEvent.pointerLeave(view.getByTestId("workflow-run-phases"));
  338. expect(view.container.querySelector('[data-fill-on="true"]')).toBeNull();
  339. });
  340. it("新鲜的两秒:侧板的框也自己画一遍,标题染警示色", () => {
  341. const view = pane(Date.now());
  342. const frame = view.container.querySelector(
  343. `[data-testid="workflow-run-fill-frame"][data-fill-frame="${H1}"]`,
  344. )!;
  345. expect(frame.getAttribute("data-fill-fresh")).toBe("true");
  346. expect(view.getByTestId("workflow-run-fill-heading").className).toContain("text-warning");
  347. });
  348. });