workflowTimelineGrowth.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // @vitest-environment jsdom
  2. // 补全的生长(docs/dynamic-workflow/presentation.md「Holes on the timeline」的「Growth」):补全接进 run 的那一帧,
  3. // 插入列右侧的站带 `wf-slide`、从 −插入宽度滑到位;320ms 后类摘掉;冷开一条已补全的时间线不滑。
  4. import { act, cleanup, render } from "@testing-library/react";
  5. import { createElement } from "react";
  6. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  7. import type { WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  8. import type { WorkflowCausalityGraphData } from "@/components/workflow-graph/types.js";
  9. import { STATION_PITCH } from "@/components/workflow-timeline/timeline-geometry.js";
  10. import { buildWorkflowTimeline } from "@/components/workflow-timeline/timeline-model.js";
  11. import { SLIDE_MS, fillGrowth } from "@/components/workflow-timeline/use-fill-growth.js";
  12. import { WorkflowTimeline } from "@/components/workflow-timeline/WorkflowTimeline.js";
  13. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  14. // 留白 id 是名字键 `hole#<8 位十六进制>`(docs/analysis.md「Sites」);渲染器只认形状。
  15. const H1 = "hole#a0000001"; // 决定分组
  16. const H2 = "hole#a0000002"; // 评判
  17. const OPEN: WorkflowCausalityGraphData = {
  18. steps: [
  19. { id: "ask#1", kind: "ask", label: "scout", lane: "actor#1", phase: "phase#1" },
  20. { id: H1, kind: "hole", label: "决定分组", lane: "main", phase: H1 },
  21. { id: "ask#2", kind: "ask", label: "member", lane: "actor#2", phase: "phase#2" },
  22. ],
  23. lanes: [
  24. { id: "actor#1", name: "侦察员" },
  25. { id: "actor#2", name: "组员" },
  26. ],
  27. participants: [
  28. { id: "p1", phase: "phase#1", lane: "actor#1", steps: ["ask#1"] },
  29. { id: "p2", phase: "phase#2", lane: "actor#2", steps: ["ask#2"] },
  30. ],
  31. handoffs: [],
  32. phases: [
  33. { id: "phase#1", name: "探索" },
  34. { id: H1, name: "决定分组" },
  35. { id: "phase#2", name: "执行" },
  36. { id: H2, name: "评判" },
  37. ],
  38. phaseEdges: [
  39. { from: "phase#1", to: H1 },
  40. { from: H1, to: "phase#2" },
  41. { from: "phase#2", to: H2 },
  42. ],
  43. holes: [
  44. { siteId: H1, name: "决定分组", type: "Plan" },
  45. { siteId: H2, name: "评判", type: "Verdict", tail: true },
  46. ],
  47. };
  48. const FILLED: WorkflowCausalityGraphData = {
  49. ...OPEN,
  50. steps: [
  51. OPEN.steps[0]!,
  52. {
  53. id: `${H1}/ask#1`,
  54. kind: "ask",
  55. label: "smoke",
  56. lane: `${H1}/actor#1`,
  57. phase: `${H1}/phase#1`,
  58. fill: H1,
  59. },
  60. {
  61. id: `${H1}/ask#2`,
  62. kind: "ask",
  63. label: "group",
  64. lane: `${H1}/actor#2`,
  65. phase: `${H1}/phase#2`,
  66. fill: H1,
  67. },
  68. OPEN.steps[2]!,
  69. ],
  70. lanes: [
  71. ...OPEN.lanes,
  72. { id: `${H1}/actor#1`, name: "冒烟员" },
  73. { id: `${H1}/actor#2`, name: "分组员" },
  74. ],
  75. participants: [
  76. OPEN.participants[0]!,
  77. { id: "p3", phase: `${H1}/phase#1`, lane: `${H1}/actor#1`, steps: [`${H1}/ask#1`] },
  78. { id: "p4", phase: `${H1}/phase#2`, lane: `${H1}/actor#2`, steps: [`${H1}/ask#2`] },
  79. OPEN.participants[1]!,
  80. ],
  81. // 体以标记开头:分析器留着 H1 自己的阶段(无成员),时间线把它交给头、不成站。
  82. phases: [
  83. { id: "phase#1", name: "探索" },
  84. { id: H1, name: "决定分组" },
  85. { id: `${H1}/phase#1`, name: "冒烟测试", fill: H1 },
  86. { id: `${H1}/phase#2`, name: "分组", fill: H1 },
  87. { id: "phase#2", name: "执行" },
  88. { id: H2, name: "评判" },
  89. ],
  90. phaseEdges: [
  91. { from: "phase#1", to: H1 },
  92. { from: H1, to: `${H1}/phase#1` },
  93. { from: `${H1}/phase#1`, to: `${H1}/phase#2` },
  94. { from: `${H1}/phase#2`, to: "phase#2" },
  95. { from: "phase#2", to: H2 },
  96. ],
  97. holes: [{ siteId: H2, name: "评判", type: "Verdict", tail: true }],
  98. };
  99. function run(overrides: Partial<WorkflowRunState> = {}): WorkflowRunState {
  100. return {
  101. runId: "run-1",
  102. toolCallId: "tc-1",
  103. status: "running",
  104. usage: { spentTokens: 0, nodesUsed: 0 },
  105. actors: [],
  106. nodes: [],
  107. lastEventSequence: 0,
  108. ...overrides,
  109. };
  110. }
  111. const WAITING = run({
  112. holes: [{ siteId: H1, ordinal: 1, name: "决定分组", type: "Plan", state: "waiting" }],
  113. });
  114. const AFTER = run({
  115. holes: [
  116. {
  117. siteId: H1,
  118. ordinal: 1,
  119. name: "决定分组",
  120. type: "Plan",
  121. state: "filled",
  122. filledAt: Date.now(),
  123. },
  124. ],
  125. });
  126. describe("fillGrowth", () => {
  127. it("新出现的补全头且站数增加才算生长:after 是新头的末站,shift 是增量 × 站距", () => {
  128. const before = buildWorkflowTimeline(OPEN, WAITING);
  129. const after = buildWorkflowTimeline(FILLED, AFTER);
  130. const seen = { stations: before.stations.length, fills: [] };
  131. expect(fillGrowth(seen, after)).toEqual({ after: 2, shift: STATION_PITCH });
  132. // 头没变(只是 run 进度变了)或站数没增(无阶段的补全)都不滑。
  133. expect(fillGrowth({ stations: 5, fills: [H1] }, after)).toBeUndefined();
  134. expect(fillGrowth({ stations: 5, fills: [] }, after)).toBeUndefined();
  135. });
  136. });
  137. describe("WorkflowTimeline · 生长的滑动", () => {
  138. beforeEach(() => {
  139. vi.useFakeTimers();
  140. });
  141. afterEach(() => {
  142. cleanup();
  143. vi.useRealTimers();
  144. });
  145. function view(model: ReturnType<typeof buildWorkflowTimeline>) {
  146. return createElement(
  147. ZCodeIntlProvider,
  148. { initialLocale: "zh-CN" },
  149. createElement(WorkflowTimeline, { model }),
  150. );
  151. }
  152. const slid = (container: HTMLElement) =>
  153. [
  154. ...container.querySelectorAll<HTMLElement>(
  155. '[data-testid="workflow-timeline-station"].wf-slide',
  156. ),
  157. ].map((station) => [
  158. station.dataset.stationStatus,
  159. station.style.getPropertyValue("--wf-slide"),
  160. ]);
  161. it("补全接进 run:插入列右侧的站带 wf-slide 与 −插入宽度;320ms 后摘掉;新站自己不滑", () => {
  162. const { container, rerender } = render(view(buildWorkflowTimeline(OPEN, WAITING)));
  163. expect(slid(container)).toEqual([]);
  164. rerender(view(buildWorkflowTimeline(FILLED, AFTER)));
  165. const stations = [
  166. ...container.querySelectorAll<HTMLElement>('[data-testid="workflow-timeline-station"]'),
  167. ];
  168. expect(stations.map((station) => station.classList.contains("wf-slide"))).toEqual([
  169. false,
  170. false,
  171. false,
  172. true,
  173. true,
  174. ]);
  175. expect(stations[3]!.style.getPropertyValue("--wf-slide")).toBe(`${-STATION_PITCH}px`);
  176. // 药丸列也随站滑。
  177. expect(
  178. container.querySelectorAll('[data-testid="workflow-timeline-pills"].wf-slide'),
  179. ).toHaveLength(1);
  180. act(() => {
  181. vi.advanceTimersByTime(SLIDE_MS);
  182. });
  183. expect(slid(container)).toEqual([]);
  184. });
  185. it("冷开一条已补全的时间线不滑", () => {
  186. const { container } = render(view(buildWorkflowTimeline(FILLED, AFTER)));
  187. expect(slid(container)).toEqual([]);
  188. });
  189. });