workflowTimelineArcTerminals.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // @vitest-environment jsdom
  2. // 弧端的槽位(docs/dynamic-workflow/presentation.md「Rails and arcs」):一站的起飞与落地各占一个槽,
  3. // 远端在左的在左、在右的在右,每一侧车道最低的在最外面;只有一个弧端的站仍用灯心;带的分叉点与
  4. // 汇合点不占槽。回归:一站既有进来的弧又有出去的弧时,出去的竖段穿过进来的箭头(testfield「工作流压力测试」)。
  5. import { cleanup, render } from "@testing-library/react";
  6. import { createElement } from "react";
  7. import { afterEach, describe, expect, it } from "vitest";
  8. import type { WorkflowCausalityGraphData } from "@/components/workflow-graph/types.js";
  9. import { buildWorkflowTimeline } from "@/components/workflow-timeline/timeline-model.js";
  10. import { arcTerminalOffsets, lampX } from "@/components/workflow-timeline/timeline-geometry.js";
  11. import { WorkflowTimeline } from "@/components/workflow-timeline/WorkflowTimeline.js";
  12. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  13. interface GraphSpec {
  14. names: readonly string[];
  15. /** 站下标 → 与它并行的站下标。 */
  16. alongside?: Record<number, readonly number[]>;
  17. edges: readonly (readonly [number, number])[];
  18. }
  19. function graphOf({ alongside = {}, edges, names }: GraphSpec): WorkflowCausalityGraphData {
  20. return {
  21. steps: names.map((name, i) => ({
  22. id: `ask#${i + 1}`,
  23. kind: "ask",
  24. label: name,
  25. line: i + 1,
  26. column: 1,
  27. lane: "actor#1",
  28. phase: `phase#${i + 1}`,
  29. })),
  30. lanes: [{ id: "actor#1", name: "worker", line: 1, column: 1 }],
  31. participants: names.map((_, i) => ({
  32. id: `phase#${i + 1}:actor#1`,
  33. phase: `phase#${i + 1}`,
  34. lane: "actor#1",
  35. steps: [`ask#${i + 1}`],
  36. })),
  37. handoffs: [],
  38. phases: names.map((name, i) => ({
  39. id: `phase#${i + 1}`,
  40. name,
  41. line: i + 1,
  42. column: 1,
  43. ...(alongside[i] === undefined
  44. ? {}
  45. : { alongside: alongside[i]!.map((j) => `phase#${j + 1}`) }),
  46. })),
  47. phaseEdges: edges.map(([from, to]) => ({ from: `phase#${from + 1}`, to: `phase#${to + 1}` })),
  48. exits: [`phase#${names.length}`],
  49. } as WorkflowCausalityGraphData;
  50. }
  51. const modelOf = (spec: GraphSpec) => buildWorkflowTimeline(graphOf(spec));
  52. /** 每条弧(`from>to`)的起飞与落地偏移。 */
  53. function offsetsOf(spec: GraphSpec): Record<string, { takeoff: number; landing: number }> {
  54. const model = modelOf(spec);
  55. const { landing, takeoff } = arcTerminalOffsets(model.arcs, model.bands, model.stations);
  56. return Object.fromEntries(
  57. model.arcs.map((arc, j) => [
  58. `${arc.from}>${arc.to}`,
  59. { landing: landing[j]! + 0, takeoff: takeoff[j]! + 0 },
  60. ]),
  61. );
  62. }
  63. const chain = (n: number): [number, number][] =>
  64. Array.from({ length: n - 1 }, (_, i) => [i, i + 1] as [number, number]);
  65. /** 两跳:plan → draft → publish,中间各隔一站。 */
  66. const HOPS: GraphSpec = {
  67. edges: [...chain(5), [0, 2], [2, 4]],
  68. names: ["plan", "gather", "draft", "review", "publish"],
  69. };
  70. /** 两个互不相干的二选一,复用同一对阶段名:left ⇄ right 两个方向都有边。 */
  71. const TWO_CHOICES: GraphSpec = {
  72. edges: [
  73. [0, 1],
  74. [0, 2],
  75. [1, 2],
  76. [1, 3],
  77. [2, 1],
  78. [2, 3],
  79. ],
  80. names: ["choose", "left", "right", "next"],
  81. };
  82. /** testfield「工作流压力测试」(dwfrun-c6d25e4f…)归约后的阶段图:十一站、十四条边、一条带。 */
  83. const STRESS: GraphSpec = {
  84. alongside: { 9: [8] },
  85. edges: [
  86. [0, 1],
  87. [1, 2],
  88. [2, 3],
  89. [3, 4],
  90. [3, 5],
  91. [4, 5],
  92. [4, 6],
  93. [5, 4],
  94. [5, 6],
  95. [6, 7],
  96. [7, 8],
  97. [7, 9],
  98. [8, 10],
  99. [9, 10],
  100. ],
  101. names: [
  102. "串行链:两步依赖",
  103. "并行扇出:四方同算",
  104. "回环门禁:修复到达标为止",
  105. "条件分支:按阈值选边",
  106. "支路甲:高门限汇总",
  107. "支路乙:低门限汇总",
  108. "混合扇出:逐项产出即复核",
  109. "并发相位:双翼交叠探针",
  110. "并发左翼:先行占位",
  111. "并发右翼:后行占位",
  112. "汇合收尾:成文与发布",
  113. ],
  114. };
  115. afterEach(cleanup);
  116. describe("arcTerminalOffsets", () => {
  117. it("a station with one terminal keeps the lamp's centre", () => {
  118. expect(offsetsOf({ edges: [...chain(4), [3, 0]], names: ["a", "b", "c", "d"] })).toEqual({
  119. "3>0": { landing: 0, takeoff: 0 },
  120. });
  121. // 两条互不相干的弧各在一头:每站只有一个弧端。
  122. expect(offsetsOf({ edges: [...chain(6), [0, 2], [5, 3]], names: "abcdef".split("") })).toEqual({
  123. "0>2": { landing: 0, takeoff: 0 },
  124. "5>3": { landing: 0, takeoff: 0 },
  125. });
  126. });
  127. it("an arc in from the left and an arc out to the right take the left and right slots", () => {
  128. const model = modelOf(HOPS);
  129. expect(model.arcs.map((arc) => [arc.from, arc.to, arc.lane])).toEqual([
  130. [0, 2, 0],
  131. [2, 4, 1],
  132. ]);
  133. expect(offsetsOf(HOPS)).toEqual({
  134. "0>2": { landing: -5, takeoff: 0 },
  135. "2>4": { landing: 0, takeoff: 5 },
  136. });
  137. });
  138. it("on one side the arc in the lowest lane stands outermost, landing or takeoff alike", () => {
  139. const model = modelOf(TWO_CHOICES);
  140. expect(model.arcs.map((arc) => [arc.from, arc.to, arc.lane])).toEqual([
  141. [2, 1, 0],
  142. [0, 2, 1],
  143. [1, 3, 2],
  144. ]);
  145. expect(offsetsOf(TWO_CHOICES)).toEqual({
  146. // left:两个弧端都朝右——落地(车道 0)在最右,起飞(车道 2)在它里面。
  147. // right:两个弧端都朝左——起飞(车道 0)在最左,落地(车道 1)在它里面。
  148. "0>2": { landing: 5, takeoff: 0 },
  149. "1>3": { landing: 0, takeoff: -5 },
  150. "2>1": { landing: 5, takeoff: -5 },
  151. });
  152. });
  153. it("a fan-out nests: three takeoffs to the right, lane 0 outermost", () => {
  154. const spec: GraphSpec = {
  155. edges: [...chain(5), [0, 2], [0, 3], [0, 4]],
  156. names: ["triage", "small", "medium", "large", "close"],
  157. };
  158. expect(offsetsOf(spec)).toEqual({
  159. "0>2": { landing: 0, takeoff: 10 },
  160. "0>3": { landing: 0, takeoff: 0 },
  161. "0>4": { landing: 0, takeoff: -10 },
  162. });
  163. });
  164. it("both sides at once: leftward terminals first, then rightward ones", () => {
  165. // c 有四个弧端:a → c 与 c → a 朝左,d → c 与 c → e 朝右。
  166. const spec: GraphSpec = {
  167. edges: [...chain(5), [0, 2], [2, 4], [3, 2], [2, 0]],
  168. names: ["a", "b", "c", "d", "e"],
  169. };
  170. const lanes = Object.fromEntries(
  171. modelOf(spec).arcs.map((arc) => [`${arc.from}>${arc.to}`, arc.lane]),
  172. );
  173. expect(lanes).toEqual({ "0>2": 1, "2>0": 3, "2>4": 2, "3>2": 0 });
  174. expect(offsetsOf(spec)).toEqual({
  175. // a:两个弧端都朝右——回边落地(车道 3)在里面、起飞(车道 1)在外面。
  176. "0>2": { landing: -15, takeoff: 5 },
  177. "2>0": { landing: -5, takeoff: -5 },
  178. // c:左侧 a → c(车道 1)−15、c → a(车道 3)−5;右侧 c → e(车道 2)+5、d → c(车道 0)+15。
  179. "2>4": { landing: 0, takeoff: 5 },
  180. "3>2": { landing: 15, takeoff: 0 },
  181. });
  182. });
  183. it("a band's fork and merge points take no slot and do not count at the lamp", () => {
  184. // 带 [[B, D], [C]],前驱 P、汇合 J。D → B 是主线上的回边,两端都是灯。S → B 与 T → C 被重挂到
  185. // 分叉点(下标仍是 B),D → X 被重挂到汇合点(下标仍是 D)——它们若也占 B、D 的槽,那两枚灯上的
  186. // 回边就会被挤出灯心。
  187. const spec: GraphSpec = {
  188. alongside: { 4: [3], 5: [4] },
  189. edges: [
  190. [0, 1],
  191. [1, 2],
  192. [2, 3],
  193. [2, 4],
  194. [3, 5],
  195. [4, 6],
  196. [5, 6],
  197. [6, 7],
  198. [5, 3],
  199. [0, 3],
  200. [1, 4],
  201. [5, 7],
  202. ],
  203. names: ["S", "T", "P", "B", "C", "D", "J", "X"],
  204. };
  205. const model = modelOf(spec);
  206. expect(
  207. model.bands.map((band) => [
  208. band.from,
  209. band.to,
  210. band.pred,
  211. band.join,
  212. band.tracks.map((track) => track.stations),
  213. ]),
  214. ).toEqual([[3, 5, 2, 6, [[3, 5], [4]]]]);
  215. expect(model.arcs.map((arc) => [arc.from, arc.to, arc.air])).toEqual(
  216. expect.arrayContaining([
  217. [5, 3, 0],
  218. [0, 3, 1],
  219. [1, 3, 1],
  220. [5, 7, 1],
  221. ]),
  222. );
  223. expect(offsetsOf(spec)).toEqual({
  224. "0>3": { landing: 0, takeoff: 0 },
  225. "1>3": { landing: 0, takeoff: 0 },
  226. "5>3": { landing: 0, takeoff: 0 },
  227. "5>7": { landing: 0, takeoff: 0 },
  228. });
  229. });
  230. });
  231. describe("WorkflowTimeline arc terminals", () => {
  232. function terminalsOf(spec: GraphSpec) {
  233. const model = modelOf(spec);
  234. const view = render(
  235. createElement(
  236. ZCodeIntlProvider,
  237. { initialLocale: "en-US" },
  238. createElement(WorkflowTimeline, { model }),
  239. ),
  240. );
  241. const byStation = new Map<number, number[]>();
  242. const add = (station: number, x: number) =>
  243. byStation.set(
  244. station,
  245. [...(byStation.get(station) ?? []), x].sort((l, r) => l - r),
  246. );
  247. for (const arc of view.getAllByTestId("workflow-timeline-arc")) {
  248. const d = arc.querySelector("path")!.getAttribute("d")!;
  249. // M xa,ya V … Q xa,ly … H … Q xb,ly xb,ly+8 V yb
  250. const xa = Number(/^M(-?[\d.]+),/u.exec(d)![1]);
  251. const xb = Number([...d.matchAll(/Q(-?[\d.]+),/gu)].at(-1)![1]);
  252. add(Number(arc.getAttribute("data-arc-from")), xa);
  253. add(Number(arc.getAttribute("data-arc-to")), xb);
  254. }
  255. return { byStation, model };
  256. }
  257. it("工作流压力测试: 支路甲 and 支路乙 each get two slots, no shared vertical", () => {
  258. const { byStation, model } = terminalsOf(STRESS);
  259. expect(model.arcs.map((arc) => [arc.from, arc.to, arc.lane])).toEqual([
  260. [5, 4, 0],
  261. [3, 5, 1],
  262. [4, 6, 2],
  263. ]);
  264. const lamp = (i: number) => lampX(i, 0);
  265. expect(Object.fromEntries(byStation)).toEqual({
  266. 3: [lamp(3)],
  267. // 支路甲:起飞到混合扇出(车道 2)在 −5,支路乙的回边(车道 0)落在 +5。
  268. 4: [lamp(4) - 5, lamp(4) + 5],
  269. // 支路乙:回边起飞(车道 0)在 −5,条件分支的弧(车道 1)落在 +5。
  270. 5: [lamp(5) - 5, lamp(5) + 5],
  271. 6: [lamp(6)],
  272. });
  273. });
  274. it("the two choices reusing phase names draw four distinct verticals", () => {
  275. const { byStation } = terminalsOf(TWO_CHOICES);
  276. for (const xs of byStation.values()) expect(new Set(xs).size).toBe(xs.length);
  277. expect(byStation.get(1)).toEqual([lampX(1) - 5, lampX(1) + 5]);
  278. expect(byStation.get(2)).toEqual([lampX(2) - 5, lampX(2) + 5]);
  279. });
  280. });