workflowRoll.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // @vitest-environment jsdom
  2. // 门后的名单(docs/dynamic-workflow/presentation.md「The spine」):按状态分组、组头是计数行
  3. // 的那一项、行由调用方渲染、依次落地 8 ms 封顶 400。
  4. import { cleanup, render } from "@testing-library/react";
  5. import { createElement } from "react";
  6. import { afterEach, describe, expect, it } from "vitest";
  7. import type { StepRunStatus } from "@/components/workflow-graph/types.js";
  8. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  9. import { rosterRoll, stationRoster } from "@/components/workflow-timeline/roster-model.js";
  10. import type { TimelinePill } from "@/components/workflow-timeline/timeline-model.js";
  11. import { WorkflowAgentPill } from "@/components/workflow-timeline/WorkflowAgentPill.js";
  12. import {
  13. ROW_STAGGER_CAP_MS,
  14. ROW_STAGGER_MS,
  15. WorkflowRoll,
  16. } from "@/components/workflow-timeline/WorkflowRoll.js";
  17. afterEach(() => {
  18. cleanup();
  19. });
  20. function pill(index: number, status: StepRunStatus | undefined): TimelinePill {
  21. return {
  22. key: `phase#a:actor#1@${index}`,
  23. avatarIndex: index,
  24. lane: { id: "actor#1", laneClass: "agent" },
  25. laneClass: "agent",
  26. runtimeName: `review-${index}`,
  27. status,
  28. instance: { ordinal: index, siteId: "actor#1", sessionId: `s-${index}` },
  29. slot: { ordinal: index, siteId: "actor#1" },
  30. stepIds: ["ask#1"],
  31. };
  32. }
  33. const pills = (statuses: readonly (StepRunStatus | undefined)[]) =>
  34. statuses.map((status, index) => pill(index + 1, status));
  35. function renderRoll(list: TimelinePill[], locale: "en-US" | "zh-CN" = "en-US", unlisted?: number) {
  36. const roster = stationRoster(list, { pins: 5 })!;
  37. return render(
  38. createElement(
  39. ZCodeIntlProvider,
  40. { initialLocale: locale },
  41. createElement(WorkflowRoll, {
  42. groups: rosterRoll(roster),
  43. ...(unlisted === undefined ? {} : { unlisted }),
  44. renderRow: (entry: TimelinePill, enterDelayMs: number) =>
  45. createElement(WorkflowAgentPill, {
  46. enterDelayMs,
  47. key: entry.key,
  48. laneClass: entry.laneClass,
  49. name: entry.runtimeName ?? "",
  50. open: { label: `open ${entry.runtimeName}`, onOpen: () => {} },
  51. size: "row",
  52. status: entry.status,
  53. }),
  54. }),
  55. ),
  56. );
  57. }
  58. describe("WorkflowRoll", () => {
  59. it("组序 failed → running → pending → done、空组缺席;组头带人数与状态词、语义色;两列", () => {
  60. // 钉 5:running 3、8 → failed 4、9 → 补位 1;其余 2 5 6 7 10 11。
  61. const view = renderRoll(
  62. pills([
  63. "done",
  64. "done",
  65. "running",
  66. "failed",
  67. "pending",
  68. "done",
  69. "done",
  70. "running",
  71. "failed",
  72. undefined,
  73. "done",
  74. ]),
  75. );
  76. const roll = view.getByTestId("workflow-roster-roll");
  77. expect(roll.className).toContain("grid-cols-2");
  78. const headings = view.getAllByTestId("workflow-roll-heading");
  79. expect(headings.map((heading) => heading.getAttribute("data-roll-group"))).toEqual([
  80. "pending",
  81. "done",
  82. ]);
  83. expect(headings.map((heading) => heading.textContent)).toEqual(["2pending", "4done"]);
  84. expect(headings[0]!.className).toContain("text-foreground-subtlest");
  85. expect(headings[1]!.className).toContain("text-success");
  86. expect(headings[1]!.getAttribute("role")).toBe("heading");
  87. // 每组的人数加粗、状态词照常。
  88. expect(headings[1]!.querySelector(".font-medium")!.textContent).toBe("4");
  89. const rows = view.getAllByTestId("workflow-agent-pill");
  90. expect(rows.map((row) => row.getAttribute("title"))).toEqual([
  91. "review-5",
  92. "review-10",
  93. "review-2",
  94. "review-6",
  95. "review-7",
  96. "review-11",
  97. ]);
  98. expect(rows.every((row) => row.getAttribute("data-pill-size") === "row")).toBe(true);
  99. expect(rows.every((row) => row.tagName === "BUTTON")).toBe(true);
  100. // 行在组头之后、依次落地。
  101. expect(rows[0]!.style.animationDelay).toBe("");
  102. expect(rows[1]!.style.animationDelay).toBe(`${ROW_STAGGER_MS}ms`);
  103. expect(rows[5]!.style.animationDelay).toBe(`${ROW_STAGGER_MS * 5}ms`);
  104. });
  105. it("落地延迟封顶 400 ms;zh-CN 组头本地化", () => {
  106. const many = Array.from({ length: 70 }, (_, i) => pill(i + 1, "done"));
  107. const view = renderRoll(many, "zh-CN");
  108. const rows = view.getAllByTestId("workflow-agent-pill");
  109. expect(rows).toHaveLength(65);
  110. expect(rows[64]!.style.animationDelay).toBe(`${ROW_STAGGER_CAP_MS}ms`);
  111. expect(ROW_STAGGER_MS * 64).toBeGreaterThan(ROW_STAGGER_CAP_MS);
  112. const heading = view.getByTestId("workflow-roll-heading");
  113. expect(heading.textContent).toBe("65个已完成");
  114. expect(heading.querySelector(".font-medium")!.textContent).toBe("65");
  115. });
  116. // 表外的那些(docs/dynamic-workflow/presentation.md「The spine」):它们没有行,
  117. // 所以名单末尾说一句差额,而不是假装那些行在。
  118. it("表外的子代理在末尾一行淡字里交代,不占行、不进组头;没有时这一行缺席", () => {
  119. const list = pills(Array(9).fill("done"));
  120. expect(renderRoll(list).queryByTestId("workflow-roll-unlisted")).toBeNull();
  121. cleanup();
  122. expect(renderRoll(list, "en-US", 0).queryByTestId("workflow-roll-unlisted")).toBeNull();
  123. cleanup();
  124. const view = renderRoll(list, "en-US", 300);
  125. const line = view.getByTestId("workflow-roll-unlisted");
  126. expect(line.textContent).toBe("300 more agents not listed");
  127. expect(line.className).toContain("text-foreground-subtlest");
  128. // 组头照旧只数它下面的行;名单里也没有多出来的行。
  129. expect(view.getByTestId("workflow-roll-heading").textContent).toBe("4done");
  130. expect(view.getAllByTestId("workflow-agent-pill")).toHaveLength(4);
  131. cleanup();
  132. expect(renderRoll(list, "en-US", 1).getByTestId("workflow-roll-unlisted").textContent).toBe(
  133. "1 more agent not listed",
  134. );
  135. cleanup();
  136. expect(renderRoll(list, "zh-CN", 300).getByTestId("workflow-roll-unlisted").textContent).toBe(
  137. "另有 300 个子代理未列出",
  138. );
  139. });
  140. });