workflowRosterModel.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // 阶段名册的纯划分(docs/dynamic-workflow/presentation.md「Past six participants」):谁被钉住、谁是其余、
  2. // 各状态几人;「还有 n 个」那一行读什么(追记「五枚药丸与一扇门」)、门后的名单怎么分组(追记「一扇门
  3. // 与一卷名单」)。四处消费同一条规则,所以钉在这里而不是各自的渲染测试里。
  4. import { describe, expect, it } from "vitest";
  5. import type { StepRunStatus } from "@/components/workflow-graph/types.js";
  6. import {
  7. ROSTER_PINS_CARD,
  8. ROSTER_PINS_PANE,
  9. ROSTER_THRESHOLD,
  10. pillInstanceKey,
  11. rosterCounts,
  12. rosterMore,
  13. rosterRestCounts,
  14. rosterRoll,
  15. stationRoster,
  16. stationRosterOf,
  17. } from "@/components/workflow-timeline/roster-model.js";
  18. import type {
  19. TimelinePill,
  20. TimelineStation,
  21. } from "@/components/workflow-timeline/timeline-model.js";
  22. function pill(
  23. index: number,
  24. status: StepRunStatus | undefined,
  25. extra: Partial<TimelinePill> = {},
  26. ): TimelinePill {
  27. return {
  28. key: `phase#a:actor#1@${index}`,
  29. lane: { id: "actor#1", laneClass: "agent", name: "reviewer" },
  30. laneClass: "agent",
  31. status,
  32. instance: { ordinal: index, siteId: "actor#1", sessionId: `s-${index}` },
  33. stepIds: ["ask#1"],
  34. ...extra,
  35. };
  36. }
  37. function pills(statuses: readonly (StepRunStatus | undefined)[]): TimelinePill[] {
  38. return statuses.map((status, index) => pill(index + 1, status));
  39. }
  40. const keys = (list: readonly TimelinePill[]) => list.map((entry) => entry.instance!.ordinal);
  41. describe("stationRoster · 阈值", () => {
  42. it("参与者不多于阈值时没有名册(药丸列原样)", () => {
  43. expect(stationRoster(pills(Array(ROSTER_THRESHOLD).fill("done")), { pins: 3 })).toBeUndefined();
  44. expect(
  45. stationRoster(pills(Array(ROSTER_THRESHOLD + 1).fill("done")), { pins: 3 }),
  46. ).toBeDefined();
  47. });
  48. });
  49. describe("stationRoster · 钉位", () => {
  50. it("asking 先、running 次之、failed 再次,之后按参与者序补位,槽永不空", () => {
  51. const list = pills(["done", "running", "failed", "done", "running", "done", "failed", "done"]);
  52. list[3] = pill(4, "done", { asking: true });
  53. const roster = stationRoster(list, { pins: 5 })!;
  54. // asking 4 → running 2、5 → failed 3、7。
  55. expect(keys(roster.pinned)).toEqual([4, 2, 5, 3, 7]);
  56. // 其余按参与者序。
  57. expect(keys(roster.rest)).toEqual([1, 6, 8]);
  58. expect(roster.total).toBe(8);
  59. });
  60. it("还在跑的多于钉数时也不让位:钉位给参与者序里的前几个 running,failed 落进其余", () => {
  61. const list = pills([
  62. "failed",
  63. "running",
  64. "running",
  65. "running",
  66. "running",
  67. "running",
  68. "running",
  69. "done",
  70. ]);
  71. const roster = stationRoster(list, { pins: 5 })!;
  72. expect(keys(roster.pinned)).toEqual([2, 3, 4, 5, 6]);
  73. expect(keys(roster.rest)).toEqual([1, 7, 8]);
  74. });
  75. it("八个在跑、两个失败、其中一个在提问:钉位 = 提问者 + 四个 running,失败的藏在那一行后面", () => {
  76. const list = pills([
  77. "running",
  78. "running",
  79. "running",
  80. "running",
  81. "running",
  82. "running",
  83. "running",
  84. "running",
  85. "failed",
  86. "failed",
  87. ]);
  88. list[7] = pill(8, "running", { asking: true });
  89. const roster = stationRoster(list, { pins: 5 })!;
  90. expect(keys(roster.pinned)).toEqual([8, 1, 2, 3, 4]);
  91. // 两个 failed 一枚也没上钉位,红色 `✕ 2` 替它们说话。
  92. expect(rosterMore(roster).failed).toBe(2);
  93. });
  94. it("一枚 running 停下来时钉位只换一枚:让位的是它自己,其余原地不动", () => {
  95. const running: StepRunStatus[] = Array(6).fill("running").concat(Array(3).fill("done"));
  96. const before = stationRoster(pills(running), { pins: 5 })!;
  97. expect(keys(before.pinned)).toEqual([1, 2, 3, 4, 5]);
  98. // 第三个 running 结束:它让出钉位,第六个 running 顶上。
  99. const after = running.map((status, index) => (index === 2 ? "done" : status));
  100. const settled = stationRoster(pills(after), { pins: 5 })!;
  101. expect(keys(settled.pinned)).toEqual([1, 2, 4, 5, 6]);
  102. const left = keys(before.pinned).filter((key) => !keys(settled.pinned).includes(key));
  103. const entered = keys(settled.pinned).filter((key) => !keys(before.pinned).includes(key));
  104. expect([left, entered]).toEqual([[3], [6]]);
  105. });
  106. it("跑完的站没有 asking 也没有 running:这条序自己退化成 failed → 参与者序", () => {
  107. const roster = stationRoster(
  108. pills(["done", "failed", "done", "done", "failed", "done", "done"]),
  109. {
  110. pins: 3,
  111. },
  112. )!;
  113. expect(keys(roster.pinned)).toEqual([2, 5, 1]);
  114. expect(keys(roster.rest)).toEqual([3, 4, 6, 7]);
  115. });
  116. it("没有任何值得注意的参与者时按参与者序补位;钉数受 pins 上限约束", () => {
  117. const roster = stationRoster(pills(Array(9).fill("done")), { pins: 3 })!;
  118. expect(keys(roster.pinned)).toEqual([1, 2, 3]);
  119. expect(keys(roster.rest)).toEqual([4, 5, 6, 7, 8, 9]);
  120. });
  121. it("failed 多于钉数时只钉前几个,其余 failed 留在其余里(参与者序不变)", () => {
  122. const roster = stationRoster(
  123. pills(["failed", "done", "failed", "failed", "failed", "done", "done"]),
  124. {
  125. pins: 3,
  126. },
  127. )!;
  128. expect(keys(roster.pinned)).toEqual([1, 3, 4]);
  129. expect(keys(roster.rest)).toEqual([2, 5, 6, 7]);
  130. });
  131. it("其余不设上限:六十个参与者钉三枚,其余五十七个都在、按参与者序", () => {
  132. const roster = stationRoster(pills(Array(60).fill("done")), { pins: 3 })!;
  133. expect(roster.rest).toHaveLength(57);
  134. expect(keys(roster.rest).slice(0, 3)).toEqual([4, 5, 6]);
  135. expect(keys(roster.rest).at(-1)).toBe(60);
  136. });
  137. });
  138. describe("rosterCounts / pillInstanceKey", () => {
  139. it("计数含全部参与者;静态药丸(status undefined)计作 pending", () => {
  140. expect(rosterCounts(pills(["done", undefined, "running", "failed", "pending"]))).toEqual({
  141. done: 1,
  142. failed: 1,
  143. pending: 2,
  144. running: 1,
  145. });
  146. });
  147. it("实例键是 siteId@ordinal;合成车道没有", () => {
  148. expect(pillInstanceKey(pill(4, "done"))).toBe("actor#1@4");
  149. expect(pillInstanceKey({ instance: undefined })).toBeUndefined();
  150. });
  151. });
  152. // ── 「还有 n 个」那一行(docs/dynamic-workflow/presentation.md「Past six participants」)──
  153. describe("rosterMore", () => {
  154. it("卡上钉五枚:五枚 + 一行 = 六枚药丸的高度", () => {
  155. expect(ROSTER_PINS_CARD).toBe(5);
  156. });
  157. it("count 是其余人数;deck 按注意力序取前三(同档保持参与者序);failed 只数藏起来的", () => {
  158. const list = pills([
  159. "done",
  160. "failed",
  161. "running",
  162. "done",
  163. "pending",
  164. "failed",
  165. "done",
  166. "running",
  167. "failed",
  168. "done",
  169. ]);
  170. // 钉 3:running 3、8 → failed 2;其余 1 4 5 6 7 9 10 → failed 6、9 在前,pending 5 次之。
  171. const more = rosterMore(stationRoster(list, { pins: 3 })!);
  172. expect(more.count).toBe(7);
  173. expect(keys(more.deck)).toEqual([6, 9, 5]);
  174. expect(more.failed).toBe(2);
  175. // 钉位只够 running 时:三个 failed 全藏起来,都计入,且排在叠的最前。
  176. const two = rosterMore(stationRoster(list, { pins: 2 })!);
  177. expect(two.count).toBe(8);
  178. expect(keys(two.deck)).toEqual([2, 6, 9]);
  179. expect(two.failed).toBe(3);
  180. });
  181. it("叠的张数可调;六十人里叠上仍是全体其余里最要紧的,failed 数照旧准确", () => {
  182. const statuses: StepRunStatus[] = Array(60).fill("done");
  183. statuses[50] = "failed";
  184. statuses[55] = "running";
  185. statuses[57] = "failed";
  186. // 钉 1:running 56;其余里 failed 51、58 领先。
  187. const roster = stationRoster(pills(statuses), { pins: 1 })!;
  188. const more = rosterMore(roster, 2);
  189. expect(more.count).toBe(59);
  190. expect(keys(more.deck)).toEqual([51, 58]);
  191. expect(more.failed).toBe(2);
  192. });
  193. });
  194. // ── 门后的名单(docs/dynamic-workflow/presentation.md「The spine」)──
  195. describe("rosterRoll", () => {
  196. it("其余按状态分组,组序 failed → running → pending → done,组内参与者序,空组缺席,钉住的不在", () => {
  197. const list = pills([
  198. "done",
  199. "failed",
  200. "running",
  201. "done",
  202. "pending",
  203. "failed",
  204. "done",
  205. "running",
  206. "failed",
  207. undefined,
  208. ]);
  209. // 钉 1:running 3;其余 1 2 4 5 6 7 8 9 10。
  210. const groups = rosterRoll(stationRoster(list, { pins: 1 })!);
  211. expect(groups.map((group) => group.status)).toEqual(["failed", "running", "pending", "done"]);
  212. expect(groups.map((group) => keys(group.pills))).toEqual([[2, 6, 9], [8], [5, 10], [1, 4, 7]]);
  213. });
  214. it("全体完成时只有一组;名单里的人数 = 其余人数", () => {
  215. const roster = stationRoster(pills(Array(9).fill("done")), { pins: 5 })!;
  216. const groups = rosterRoll(roster);
  217. expect(groups).toHaveLength(1);
  218. expect(groups[0]!.status).toBe("done");
  219. expect(groups[0]!.pills).toHaveLength(roster.rest.length);
  220. });
  221. });
  222. // ── 表外的那些(docs/dynamic-workflow/presentation.md「Past six participants」):被归约淘汰的
  223. // 子代理没有药丸、没有脸、没有行,但这一站的每个数字都得把它们算回来。
  224. describe("stationRoster · 表外条目", () => {
  225. function station(list: TimelinePill[], unlisted?: TimelineStation["unlisted"]): TimelineStation {
  226. return {
  227. id: "phase#a",
  228. naming: { id: "phase#a", name: "review" },
  229. onLoop: false,
  230. pills: list,
  231. rounds: 1,
  232. status: "running",
  233. track: 0,
  234. visited: true,
  235. ...(unlisted === undefined ? {} : { unlisted }),
  236. };
  237. }
  238. it("阈值按表内 + 表外判:四枚药丸加三百个被淘汰的仍是名册,表外的一枚药丸都不占", () => {
  239. // 四枚药丸自己不成名册;补上两个表外的正好到阈值,仍不是。
  240. expect(stationRoster(pills(Array(4).fill("done")), { pins: 5 })).toBeUndefined();
  241. expect(
  242. stationRoster(pills(Array(4).fill("done")), {
  243. pins: 5,
  244. unlisted: { actors: 2, failed: 0, settled: 2 },
  245. }),
  246. ).toBeUndefined();
  247. const roster = stationRoster(pills(Array(4).fill("running")), {
  248. pins: 5,
  249. unlisted: { actors: 300, failed: 4, settled: 300 },
  250. })!;
  251. expect(roster.total).toBe(304);
  252. // 表外的没有药丸:钉位与其余里一枚都找不到它们。
  253. expect(keys(roster.pinned)).toEqual([1, 2, 3, 4]);
  254. expect(roster.rest).toEqual([]);
  255. // 三百个全跑完了:失败的算 failed,其余算 done。
  256. expect(roster.counts).toEqual({ done: 296, failed: 4, pending: 0, running: 4 });
  257. });
  258. // 表外 ≠ 已结束:出生就被拒、或排队时被淘汰的子代理还要跑,把它们记成 done 就是那句假话。
  259. it("只有 actorsSettled 那部分算结局,其余全进 pending", () => {
  260. const roster = stationRoster(pills(Array(4).fill("done")), {
  261. pins: 5,
  262. unlisted: { actors: 300, failed: 2, settled: 100 },
  263. })!;
  264. expect(roster.total).toBe(304);
  265. // 表内四个 done,加表外结算的 100 − 2;失败 2;剩下的 200 个还没跑。
  266. expect(roster.counts).toEqual({ done: 102, failed: 2, pending: 200, running: 0 });
  267. const more = rosterMore(roster);
  268. expect(more.count).toBe(300);
  269. expect(more.failed).toBe(2);
  270. });
  271. it("一格都没结算(actorsSettled 缺席归零):三百个全是 pending", () => {
  272. const roster = stationRoster(pills(Array(4).fill("running")), {
  273. pins: 5,
  274. unlisted: { actors: 300, failed: 0, settled: 0 },
  275. })!;
  276. expect(roster.counts).toEqual({ done: 0, failed: 0, pending: 300, running: 4 });
  277. expect(rosterRestCounts(roster)).toEqual({ done: 0, failed: 0, pending: 300, running: 0 });
  278. });
  279. it("「还有 n 个」把表外的算进人数与红 ✕;叠上的脸只有表内的", () => {
  280. const roster = stationRoster(
  281. pills(["running", "running", "running", "running", "running", "failed", "done"]),
  282. { pins: 5, unlisted: { actors: 10, failed: 3, settled: 10 } },
  283. )!;
  284. const more = rosterMore(roster);
  285. // 其余两枚药丸 + 十个表外的。
  286. expect(more.count).toBe(12);
  287. // 藏起来的一个 failed + 表外失败的三个。
  288. expect(more.failed).toBe(4);
  289. expect(keys(more.deck)).toEqual([6, 7]);
  290. });
  291. it("门关着的计数行 = 其余 + 表外;门后的名单只列得出表内的其余", () => {
  292. const roster = stationRoster(pills(["running", "failed", "done", "done", "pending"]), {
  293. pins: 2,
  294. unlisted: { actors: 10, failed: 3, settled: 10 },
  295. })!;
  296. // 钉 running 1、failed 2;其余 3 4 done、5 pending。
  297. expect(rosterRestCounts(roster)).toEqual({ done: 9, failed: 3, pending: 1, running: 0 });
  298. expect(rosterRoll(roster).map((group) => keys(group.pills))).toEqual([[5], [3, 4]]);
  299. });
  300. it("stationRosterOf 从站上取表外那一格:卡与侧板读同一个值,缺席即一条都没少", () => {
  301. const list = pills(Array(4).fill("done"));
  302. expect(stationRosterOf(station(list), ROSTER_PINS_PANE)).toBeUndefined();
  303. const roster = stationRosterOf(
  304. station(list, { actors: 300, failed: 2, nodesSettled: 300, settled: 100 }),
  305. ROSTER_PINS_CARD,
  306. )!;
  307. expect(roster.total).toBe(304);
  308. expect(rosterMore(roster).count).toBe(300);
  309. // 站带的是子代理的结局,不是节点的:`nodesSettled` 只进 fraction,一个人都不算。
  310. expect(roster.counts).toEqual({ done: 102, failed: 2, pending: 200, running: 0 });
  311. });
  312. });