workflowTimelineBands.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // 带的折叠(docs/dynamic-workflow/presentation.md「The timeline model」):把分析器的
  2. // `alongside` 事实折成画面上的带与轨道。纯下标、纯函数——时间线模型与侧栏的迷你运行线共用它,
  3. // 所以规则钉在这里,而不是各自的渲染测试里。
  4. import { describe, expect, it } from "vitest";
  5. import { bandOf, foldPhaseBands, trackOf } from "@/components/workflow-timeline/timeline-bands.js";
  6. describe("foldPhaseBands", () => {
  7. it("没有 alongside 的时间线没有带", () => {
  8. expect(foldPhaseBands(3, [[], [], []])).toEqual([]);
  9. expect(foldPhaseBands(3, [])).toEqual([]);
  10. expect(foldPhaseBands(0, [])).toEqual([]);
  11. });
  12. it("A ∥ B → C:两站折成一条带,各占一条轨道,先声明的在主线上", () => {
  13. // strand-fanout-two-phases-join:进入 B 时 A 的 fan-out 还在跑。
  14. expect(foldPhaseBands(3, [[], [0], []])).toEqual([{ from: 0, to: 1, tracks: [[0], [1]] }]);
  15. });
  16. it("重叠链 A ∥ B、B ∥ C:一条带两条轨道,A 与 C 同轨(它们并不并行)", () => {
  17. // strand-overlap-chain:没有哪一条带能同时容下三者的两两并行,贪心着色把 C 放回主线。
  18. expect(foldPhaseBands(4, [[], [0], [1], []])).toEqual([
  19. { from: 0, to: 2, tracks: [[0, 2], [1]] },
  20. ]);
  21. });
  22. it("一条主线带两个分支:两个分支彼此不并行,叠在同一条分支轨道上", () => {
  23. expect(foldPhaseBands(3, [[], [0], [0]])).toEqual([{ from: 0, to: 2, tracks: [[0], [1, 2]] }]);
  24. });
  25. it("三者两两并行:三条轨道", () => {
  26. expect(foldPhaseBands(3, [[], [0], [0, 1]])).toEqual([
  27. { from: 0, to: 2, tracks: [[0], [1], [2]] },
  28. ]);
  29. });
  30. it("两条带各自折叠,按 from 升序", () => {
  31. expect(foldPhaseBands(6, [[], [0], [], [], [3], []])).toEqual([
  32. { from: 0, to: 1, tracks: [[0], [1]] },
  33. { from: 3, to: 4, tracks: [[3], [4]] },
  34. ]);
  35. });
  36. it("只有一头报出来也算数:`alongside` 先对称化", () => {
  37. // 进入 B 时 A 还在跑,只有 B 报得出这件事;画面上两站是对等的。
  38. expect(foldPhaseBands(2, [[1], []])).toEqual([{ from: 0, to: 1, tracks: [[0], [1]] }]);
  39. });
  40. it("自指、越界与未列出的引用当没说", () => {
  41. expect(foldPhaseBands(2, [[0], [1]])).toEqual([]);
  42. expect(foldPhaseBands(2, [[7, -1, 2], []])).toEqual([]);
  43. expect(foldPhaseBands(3, [[0, 9], [0], []])).toEqual([{ from: 0, to: 1, tracks: [[0], [1]] }]);
  44. });
  45. it("区间里的空档也算成员(鲁棒起见;没有循环时不会出现)", () => {
  46. expect(foldPhaseBands(3, [[2], [], []])).toEqual([{ from: 0, to: 2, tracks: [[0, 1], [2]] }]);
  47. });
  48. it("相交的区间并成一条带", () => {
  49. expect(foldPhaseBands(4, [[2], [3], [], []])).toEqual([
  50. {
  51. from: 0,
  52. to: 3,
  53. tracks: [
  54. [0, 1],
  55. [2, 3],
  56. ],
  57. },
  58. ]);
  59. });
  60. });
  61. describe("bandOf / trackOf", () => {
  62. const bands = foldPhaseBands(5, [[], [], [1], [1], []]);
  63. it("带外的站没有带,轨道一律是主线", () => {
  64. expect(bands).toEqual([{ from: 1, to: 3, tracks: [[1], [2, 3]] }]);
  65. expect(bandOf(bands, 0)).toBeUndefined();
  66. expect(bandOf(bands, 4)).toBeUndefined();
  67. expect(trackOf(bands, 0)).toBe(0);
  68. expect(trackOf(bands, 4)).toBe(0);
  69. });
  70. it("带内的站报出它所在的带与轨道", () => {
  71. expect(bandOf(bands, 2)).toBe(bands[0]);
  72. expect([1, 2, 3].map((i) => trackOf(bands, i))).toEqual([0, 1, 1]);
  73. });
  74. });