workflowAgentPill.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // @vitest-environment jsdom
  2. // 子代理药丸(docs/dynamic-workflow/presentation.md「The pill」):可打开时整枚是按钮,
  3. // 悬停语法挂在 wf-pill-open 上、↗ 是可见证据;不可打开时是 span,inertTitle 解释为什么。
  4. import { cleanup, fireEvent, render } from "@testing-library/react";
  5. import { createElement } from "react";
  6. import { afterEach, describe, expect, it, vi } from "vitest";
  7. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  8. import {
  9. WorkflowAgentPill,
  10. avatarColor,
  11. } from "@/components/workflow-timeline/WorkflowAgentPill.js";
  12. afterEach(() => {
  13. cleanup();
  14. });
  15. function renderPill(props: Parameters<typeof WorkflowAgentPill>[0]) {
  16. return render(
  17. createElement(
  18. ZCodeIntlProvider,
  19. { initialLocale: "en-US" },
  20. createElement(WorkflowAgentPill, props),
  21. ),
  22. );
  23. }
  24. function view_state(props: { status: Parameters<typeof WorkflowAgentPill>[0]["status"] }) {
  25. const view = renderPill({ laneClass: "agent", name: "Ada", avatarIndex: 0, ...props });
  26. const state = view.container
  27. .querySelector("svg[data-subagent-avatar]")!
  28. .getAttribute("data-face-state");
  29. view.unmount();
  30. return state;
  31. }
  32. describe("WorkflowAgentPill", () => {
  33. it("前九个编号的脸色相互不相同,第十个循环;表情读 status", () => {
  34. const hues = Array.from({ length: 10 }, (_, avatarIndex) => {
  35. const view = renderPill({ laneClass: "agent", name: "same", avatarIndex, status: "done" });
  36. const face = view.container.querySelector("svg[data-subagent-avatar]") as SVGSVGElement;
  37. expect(face.getAttribute("data-face-state")).toBe("content");
  38. const hue = face.style.getPropertyValue("--wf-face-body");
  39. view.unmount();
  40. return hue;
  41. });
  42. expect(new Set(hues.slice(0, 9)).size).toBe(9);
  43. expect(hues[9]).toBe(hues[0]);
  44. expect(view_state({ status: undefined })).toBe("waiting");
  45. expect(view_state({ status: "running" })).toBe("scanning");
  46. expect(view_state({ status: "failed" })).toBe("sad");
  47. });
  48. it("可打开:<button> + wf-pill-open + aria-label + ↗,点击走 onOpen", () => {
  49. const onOpen = vi.fn();
  50. const view = renderPill({
  51. laneClass: "agent",
  52. name: "Ada",
  53. open: {
  54. label: "Open the transcript of Ada",
  55. onOpen,
  56. data: { "data-agent-key": "actor#1@1" },
  57. },
  58. status: "running",
  59. });
  60. const pill = view.getByTestId("workflow-agent-pill");
  61. expect(view.getByTestId("workflow-pill-status").className).toContain("text-foreground-subtle");
  62. expect(view.getByTestId("workflow-pill-status").className).not.toContain("text-warning");
  63. expect(pill.querySelector(":scope > svg[data-subagent-avatar]")).not.toBeNull();
  64. expect(pill.querySelector("img")).toBeNull();
  65. expect(pill.querySelector(".wf-pill-avatar")).toBeNull();
  66. expect(pill.className).toContain("rounded-full");
  67. expect(pill.tagName).toBe("BUTTON");
  68. expect(pill.getAttribute("type")).toBe("button");
  69. expect(pill.getAttribute("aria-label")).toBe("Open the transcript of Ada");
  70. expect(pill.getAttribute("data-agent-open")).toBe("true");
  71. expect(pill.className).toContain("wf-pill-open");
  72. const go = view.getByTestId("workflow-pill-open");
  73. expect(go.getAttribute("data-agent-key")).toBe("actor#1@1");
  74. // ↗ 与状态标记叠在同一个尾槽里(悬停时顶替它),不在标记旁边另占一格。
  75. const tail = view.getByTestId("workflow-pill-tail");
  76. expect(tail.contains(go)).toBe(true);
  77. expect(tail.contains(view.getByTestId("workflow-pill-status"))).toBe(true);
  78. fireEvent.click(pill);
  79. expect(onOpen).toHaveBeenCalledTimes(1);
  80. });
  81. it("尾槽只在有东西可放时在场:无标记且不可打开的药丸(草稿 / 工作区)没有尾槽", () => {
  82. const inert = renderPill({ laneClass: "workspace", name: "Workspace", status: undefined });
  83. expect(inert.queryByTestId("workflow-pill-tail")).toBeNull();
  84. inert.unmount();
  85. // 有标记、不可打开:尾槽只装标记——脚本药丸的对勾与子代理药丸的落在同一条右缘。
  86. const marked = renderPill({ laneClass: "workspace", name: "Workspace", status: "done" });
  87. const tail = marked.getByTestId("workflow-pill-tail");
  88. expect(tail.contains(marked.getByTestId("workflow-pill-status"))).toBe(true);
  89. expect(marked.queryByTestId("workflow-pill-open")).toBeNull();
  90. });
  91. it("不可打开:<span>、没有 ↗、没有悬停类;inertTitle 成为 tooltip", () => {
  92. const view = renderPill({
  93. inertTitle: "Transcript starts when the subagent does",
  94. laneClass: "agent",
  95. name: "checker",
  96. status: undefined,
  97. });
  98. const pill = view.getByTestId("workflow-agent-pill");
  99. expect(pill.tagName).toBe("SPAN");
  100. expect(pill.getAttribute("data-agent-open")).toBeNull();
  101. expect(pill.className).not.toContain("wf-pill-open");
  102. expect(view.queryByTestId("workflow-pill-open")).toBeNull();
  103. expect(pill.getAttribute("title")).toBe("Transcript starts when the subagent does");
  104. });
  105. it("到场与弹入:药丸带 wf-arrive,状态标记带 wf-mark 且带可读文字", () => {
  106. const view = renderPill({ laneClass: "agent", name: "Ada", status: "done" });
  107. expect(view.getByTestId("workflow-agent-pill").className).toContain("wf-arrive");
  108. const mark = view.getByTestId("workflow-pill-status");
  109. expect(mark.className).toContain("wf-mark");
  110. expect(mark.getAttribute("aria-label")).toBe("done");
  111. expect(mark.className).toContain("text-foreground-subtle");
  112. expect(mark.className).not.toContain("text-success");
  113. });
  114. it("脚本药丸没有头像色相;子代理悬停色相与脸同源:编号优先,无编号退回名字散列", () => {
  115. const view = renderPill({ laneClass: "workspace", name: "Workspace", status: undefined });
  116. expect(view.getByTestId("workflow-agent-pill").getAttribute("style")).toBeNull();
  117. expect(view.container.querySelector("svg[data-subagent-avatar]")).toBeNull();
  118. expect(avatarColor("Ada")).toBe(avatarColor("Ada"));
  119. expect(avatarColor("Ada")).not.toBe(avatarColor("Bob"));
  120. view.unmount();
  121. const numbered = renderPill({
  122. laneClass: "agent",
  123. name: "Ada",
  124. avatarIndex: 2,
  125. status: "done",
  126. });
  127. expect(numbered.getByTestId("workflow-agent-pill").getAttribute("style")).toContain("#6464EF");
  128. numbered.unmount();
  129. const unnumbered = renderPill({ laneClass: "agent", name: "Ada", status: "done" });
  130. expect(unnumbered.getByTestId("workflow-agent-pill").getAttribute("style")).toContain(
  131. avatarColor("Ada"),
  132. );
  133. });
  134. });