workflowRunLine.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // 侧栏工作流运行行的纯模型与确认集合(docs/dynamic-workflow/presentation.md「The sidebar run line」)。
  2. import { describe, expect, it } from "vitest";
  3. import type {
  4. SessionWorkflowActivity,
  5. SessionWorkflowPhaseSummary,
  6. SessionWorkflowRunSummary,
  7. } from "@zcode/shared/zcode-protocol-v4";
  8. import {
  9. WORKFLOW_RUN_ACK_LIMIT,
  10. createWorkflowRunAckStore,
  11. WORKFLOW_RUN_ACK_STORAGE_KEY,
  12. } from "@/lib/workflowRunAckStore.js";
  13. import {
  14. WORKFLOW_RUN_LINE_MAX_LINES,
  15. countLiveWorkflowRuns,
  16. foldWorkflowRunRail,
  17. selectWorkflowRunLines,
  18. settledWorkflowRunIds,
  19. workflowRunParallelPhaseLabel,
  20. } from "@/lib/workflowRunLine.js";
  21. function run(
  22. overrides: Partial<SessionWorkflowRunSummary> & { runId: string },
  23. ): SessionWorkflowRunSummary {
  24. return { status: "running", phases: [], agentsWorking: 0, ...overrides };
  25. }
  26. function phases(statuses: SessionWorkflowPhaseSummary["status"][]): SessionWorkflowPhaseSummary[] {
  27. return statuses.map((status, index) => ({ name: `p${index}`, status }));
  28. }
  29. describe("foldWorkflowRunRail", () => {
  30. it("no phase vocabulary → one implicit station", () => {
  31. expect(foldWorkflowRunRail([])).toEqual({ stations: [], hidden: 0, implicit: true });
  32. });
  33. it("up to six stations are all drawn; the segment into a reached station is strong", () => {
  34. const rail = foldWorkflowRunRail(phases(["done", "running", "pending"]));
  35. expect(rail.hidden).toBe(0);
  36. expect(rail.implicit).toBe(false);
  37. expect(rail.stations.map((station) => [station.status, station.reached])).toEqual([
  38. ["done", true],
  39. ["running", true],
  40. ["pending", false],
  41. ]);
  42. });
  43. it("past six stations folds to the running station ± 2 with a +n tail", () => {
  44. const rail = foldWorkflowRunRail(
  45. phases(["done", "done", "done", "done", "running", "pending", "pending", "pending"]),
  46. );
  47. expect(rail.stations.map((station) => station.name)).toEqual(["p2", "p3", "p4", "p5", "p6"]);
  48. expect(rail.hidden).toBe(3);
  49. });
  50. it("the window clamps at either end and anchors on the last reached station when nothing runs", () => {
  51. const start = foldWorkflowRunRail(phases(["running", ...Array<"pending">(7).fill("pending")]));
  52. expect(start.stations.map((station) => station.name)).toEqual(["p0", "p1", "p2", "p3", "p4"]);
  53. const ended = foldWorkflowRunRail(phases([...Array<"done">(7).fill("done"), "failed"]));
  54. expect(ended.stations.map((station) => station.name)).toEqual(["p3", "p4", "p5", "p6", "p7"]);
  55. expect(ended.hidden).toBe(3);
  56. });
  57. });
  58. describe("foldWorkflowRunRail · 并行站点", () => {
  59. /** 声明表 + 每站的 alongside(下标指向同一张表)。 */
  60. function band(alongside: (number[] | undefined)[]): SessionWorkflowPhaseSummary[] {
  61. return alongside.map((beside, index) => ({
  62. name: `p${index}`,
  63. status: "pending" as const,
  64. ...(beside === undefined ? {} : { alongside: beside }),
  65. }));
  66. }
  67. it("a station beside the previous one rides a twin segment; the one after the band does not", () => {
  68. // A ∥ B → C:B 与 A 同带不同轨(进入 B 的那段是双线),C 在带外(普通段)。
  69. const rail = foldWorkflowRunRail(band([undefined, [0], undefined]));
  70. expect(rail.stations.map((station) => station.twin)).toEqual([undefined, true, undefined]);
  71. });
  72. it("an overlap chain keeps every branch station on a twin segment", () => {
  73. // 重叠链:进 B 时 A 还在跑、进 C 时 B 还在跑。三站连成一条带,轨道是
  74. // [[A, C], [B]](贪心着色),于是进 B 与进 C 的两段都跨轨 —— 都是双线段。
  75. const rail = foldWorkflowRunRail(band([undefined, [0], [1]]));
  76. expect(rail.stations.map((station) => station.twin)).toEqual([undefined, true, true]);
  77. });
  78. it("without any alongside no station is twinned", () => {
  79. const rail = foldWorkflowRunRail(band([undefined, undefined, undefined]));
  80. expect(rail.stations.every((station) => station.twin === undefined)).toBe(true);
  81. expect(rail.stations.every((station) => !Object.hasOwn(station, "twin"))).toBe(true);
  82. });
  83. it("the band is folded on the full table, so the window cannot break it apart", () => {
  84. // 9 站、窗口只留 5 站:带在全表上折,双线标志随站进窗口(按窗口内下标重折会把带拆断)。
  85. const phases = band([
  86. undefined,
  87. undefined,
  88. undefined,
  89. undefined,
  90. undefined,
  91. [6],
  92. [5],
  93. undefined,
  94. undefined,
  95. ]);
  96. phases[6] = { ...phases[6]!, status: "running" };
  97. const rail = foldWorkflowRunRail(phases);
  98. expect(rail.stations.map((station) => station.name)).toEqual(["p4", "p5", "p6", "p7", "p8"]);
  99. expect(rail.stations.map((station) => station.twin)).toEqual([
  100. undefined,
  101. undefined,
  102. true,
  103. undefined,
  104. undefined,
  105. ]);
  106. });
  107. });
  108. describe("workflowRunParallelPhaseLabel", () => {
  109. it("joins every running phase with ∥, and stays silent for a single one", () => {
  110. expect(
  111. workflowRunParallelPhaseLabel([
  112. { name: "A", status: "running" },
  113. { name: "B", status: "running" },
  114. { name: "C", status: "pending" },
  115. ]),
  116. ).toBe("A ∥ B");
  117. expect(
  118. workflowRunParallelPhaseLabel([
  119. { name: "A", status: "done" },
  120. { name: "B", status: "running" },
  121. ]),
  122. ).toBeUndefined();
  123. expect(workflowRunParallelPhaseLabel([])).toBeUndefined();
  124. });
  125. });
  126. describe("selectWorkflowRunLines", () => {
  127. const activity: SessionWorkflowActivity = {
  128. runs: [
  129. run({ runId: "live-1" }),
  130. run({ runId: "live-2", status: "pending" }),
  131. run({ runId: "done-new", status: "completed" }),
  132. run({ runId: "done-old", status: "errored" }),
  133. ],
  134. };
  135. it("live runs always show; settled ones only while unacknowledged; at most two lines", () => {
  136. const none = selectWorkflowRunLines(activity, () => false);
  137. expect(none.lines.map((line) => line.runId)).toEqual(["live-1", "live-2"]);
  138. expect(none.overflow).toBe(2);
  139. expect(WORKFLOW_RUN_LINE_MAX_LINES).toBe(2);
  140. });
  141. it("acknowledged settled runs disappear, live ones do not", () => {
  142. const acked = new Set(["done-new", "done-old", "live-1"]);
  143. const selection = selectWorkflowRunLines(activity, (runId) => acked.has(runId));
  144. expect(selection.lines.map((line) => line.runId)).toEqual(["live-1", "live-2"]);
  145. expect(selection.overflow).toBe(0);
  146. });
  147. it("a single unacknowledged settled run lingers as the only line", () => {
  148. const selection = selectWorkflowRunLines(
  149. { runs: [run({ runId: "done", status: "stopped", stopReason: "user" })] },
  150. () => false,
  151. );
  152. expect(selection.lines.map((line) => line.runId)).toEqual(["done"]);
  153. expect(selectWorkflowRunLines(undefined, () => false)).toEqual({ lines: [], overflow: 0 });
  154. });
  155. it("settledWorkflowRunIds / countLiveWorkflowRuns split the activity by liveness", () => {
  156. expect(settledWorkflowRunIds(activity)).toEqual(["done-new", "done-old"]);
  157. expect(countLiveWorkflowRuns(activity)).toBe(2);
  158. expect(countLiveWorkflowRuns(undefined)).toBe(0);
  159. });
  160. });
  161. describe("workflowRunAckStore", () => {
  162. function memoryStorage(seed?: string) {
  163. const map = new Map<string, string>();
  164. if (seed !== undefined) map.set(WORKFLOW_RUN_ACK_STORAGE_KEY, seed);
  165. return {
  166. getItem: (key: string) => map.get(key) ?? null,
  167. setItem: (key: string, value: string) => {
  168. map.set(key, value);
  169. },
  170. dump: () => map.get(WORKFLOW_RUN_ACK_STORAGE_KEY),
  171. };
  172. }
  173. it("acknowledges, persists and notifies once per change", () => {
  174. const storage = memoryStorage();
  175. const store = createWorkflowRunAckStore(storage);
  176. let notified = 0;
  177. store.subscribe(() => {
  178. notified += 1;
  179. });
  180. expect(store.isAcknowledged("r1")).toBe(false);
  181. store.acknowledge(["r1", "r2"]);
  182. expect(store.isAcknowledged("r1")).toBe(true);
  183. expect(store.getVersion()).toBe(1);
  184. expect(notified).toBe(1);
  185. // 重复确认无变化:不抬版本、不通知、不写盘。
  186. store.acknowledge(["r1"]);
  187. expect(store.getVersion()).toBe(1);
  188. expect(notified).toBe(1);
  189. expect(JSON.parse(storage.dump() ?? "[]")).toEqual(["r1", "r2"]);
  190. });
  191. it("reads the persisted set back and survives garbage", () => {
  192. expect(createWorkflowRunAckStore(memoryStorage('["a","b",3]')).isAcknowledged("b")).toBe(true);
  193. expect(createWorkflowRunAckStore(memoryStorage("not json")).isAcknowledged("a")).toBe(false);
  194. expect(createWorkflowRunAckStore(null).isAcknowledged("a")).toBe(false);
  195. });
  196. it("is bounded: the oldest acknowledgement is evicted past the limit", () => {
  197. const store = createWorkflowRunAckStore(memoryStorage());
  198. store.acknowledge(["first"]);
  199. store.acknowledge(Array.from({ length: WORKFLOW_RUN_ACK_LIMIT }, (_, index) => `r${index}`));
  200. expect(store.isAcknowledged("first")).toBe(false);
  201. expect(store.isAcknowledged("r0")).toBe(true);
  202. expect(store.isAcknowledged(`r${WORKFLOW_RUN_ACK_LIMIT - 1}`)).toBe(true);
  203. });
  204. });