workflowArtifactPill.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // @vitest-environment jsdom
  2. // 产物药丸与产物条(docs/dynamic-workflow/presentation.md「The run card」):与子代理药丸同一套
  3. // 语法——方瓦片代替圆头像,尾槽放版本号;永远是 <button>,没有回调即禁用;条 ≤ 3 枚 + `+N`,
  4. // 事件在条这一层止步。四处(工具卡、轮尾摘要、通知行、中枢)共用这两个组件。
  5. import { cleanup, fireEvent, render } from "@testing-library/react";
  6. import { createElement } from "react";
  7. import { afterEach, describe, expect, it, vi } from "vitest";
  8. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  9. import {
  10. WorkflowArtifactPill,
  11. type ArtifactPillData,
  12. } from "@/components/workflow-timeline/WorkflowArtifactPill.js";
  13. import { WorkflowArtifactStrip } from "@/components/workflow-timeline/WorkflowArtifactStrip.js";
  14. afterEach(() => {
  15. cleanup();
  16. });
  17. const BOOK: ArtifactPillData = { id: "book", kind: "file", title: "Audit report", version: 2 };
  18. function renderPill(
  19. props: Partial<Parameters<typeof WorkflowArtifactPill>[0]> = {},
  20. locale: "en-US" | "zh-CN" = "en-US",
  21. ) {
  22. return render(
  23. createElement(
  24. ZCodeIntlProvider,
  25. { initialLocale: locale },
  26. createElement(WorkflowArtifactPill, { artifact: BOOK, ...props }),
  27. ),
  28. );
  29. }
  30. function renderStrip(
  31. props: Partial<Parameters<typeof WorkflowArtifactStrip>[0]> & {
  32. artifacts: readonly ArtifactPillData[];
  33. },
  34. locale: "en-US" | "zh-CN" = "en-US",
  35. ) {
  36. return render(
  37. createElement(
  38. ZCodeIntlProvider,
  39. { initialLocale: locale },
  40. createElement(WorkflowArtifactStrip, {
  41. testId: "strip",
  42. pillTestId: "pill",
  43. moreTestId: "more",
  44. ...props,
  45. }),
  46. ),
  47. );
  48. }
  49. describe("WorkflowArtifactPill", () => {
  50. it("可打开:真按钮、瓦片带 kind 图标、尾槽里版本号与 ↗ 同格;点了交出产物 id", () => {
  51. const onOpen = vi.fn();
  52. const view = renderPill({ onOpen });
  53. const pill = view.getByTestId("workflow-artifact-pill") as HTMLButtonElement;
  54. expect(pill.tagName).toBe("BUTTON");
  55. expect(pill.disabled).toBe(false);
  56. expect(pill.className).toContain("wf-pill-open");
  57. expect(pill.getAttribute("data-artifact-open")).toBe("true");
  58. expect(pill.getAttribute("data-pill-size")).toBe("md");
  59. expect(pill.getAttribute("title")).toBe("File · Audit report");
  60. expect(pill.getAttribute("aria-label")).toBe("Open artifact: Audit report");
  61. expect(pill.querySelector(":scope > svg")).not.toBeNull();
  62. const tail = view.getByTestId("workflow-pill-tail");
  63. expect(tail.querySelector('[data-testid="workflow-run-artifact-version"]')?.textContent).toBe(
  64. "v2",
  65. );
  66. expect(tail.querySelector('[data-testid="workflow-artifact-pill-open"]')).not.toBeNull();
  67. fireEvent.click(pill);
  68. expect(onOpen).toHaveBeenCalledWith("book");
  69. });
  70. it("没有回调:禁用、没有 aria-label、没有 ↗;版本号仍在(事实与能力分开)", () => {
  71. const view = renderPill();
  72. const pill = view.getByTestId("workflow-artifact-pill") as HTMLButtonElement;
  73. expect(pill.disabled).toBe(true);
  74. expect(pill.className).not.toContain("wf-pill-open");
  75. expect(pill.getAttribute("aria-label")).toBeNull();
  76. expect(view.queryByTestId("workflow-artifact-pill-open")).toBeNull();
  77. expect(view.getByTestId("workflow-run-artifact-version").textContent).toBe("v2");
  78. });
  79. it("v1 不写版本;v1 且不可打开时尾槽整个缺席", () => {
  80. const view = renderPill({ artifact: { ...BOOK, version: 1 } });
  81. expect(view.queryByTestId("workflow-run-artifact-version")).toBeNull();
  82. expect(view.queryByTestId("workflow-pill-tail")).toBeNull();
  83. cleanup();
  84. const noVersion = renderPill({ artifact: { id: "x", kind: "markdown" }, onOpen: vi.fn() });
  85. expect(noVersion.queryByTestId("workflow-run-artifact-version")).toBeNull();
  86. expect(noVersion.getByTestId("workflow-pill-tail")).toBeTruthy();
  87. });
  88. it("小号尺寸透传;标题缺席退回 id;截字只在要求时发生", () => {
  89. const long = { id: "long", kind: "table" as const, title: "一".repeat(40), version: 1 };
  90. const view = renderPill({ artifact: long, size: "sm", truncateTitle: true });
  91. const pill = view.getByTestId("workflow-artifact-pill");
  92. expect(pill.getAttribute("data-pill-size")).toBe("sm");
  93. expect(pill.querySelector(".wf-pill-name")?.textContent?.length).toBeLessThan(40);
  94. expect(pill.getAttribute("title")).toBe(`Table · ${"一".repeat(40)}`);
  95. cleanup();
  96. const bare = renderPill({ artifact: { id: "bare", kind: "file" } });
  97. expect(
  98. bare.getByTestId("workflow-artifact-pill").querySelector(".wf-pill-name")?.textContent,
  99. ).toBe("bare");
  100. });
  101. it("zh-CN:tooltip 用中文 kind 词,尾槽仍是 v2,长写「第 2 版」进 title", () => {
  102. const view = renderPill({}, "zh-CN");
  103. expect(view.getByTestId("workflow-artifact-pill").getAttribute("title")).toBe(
  104. "文件 · Audit report",
  105. );
  106. const version = view.getByTestId("workflow-run-artifact-version");
  107. expect(version.textContent).toBe("v2");
  108. expect(version.getAttribute("title")).toBe("第 2 版");
  109. });
  110. it("入场延迟写成 animation-delay + backwards 填充", () => {
  111. const view = renderPill({ enterDelayMs: 60 });
  112. const style = view.getByTestId("workflow-artifact-pill").getAttribute("style") ?? "";
  113. expect(style).toContain("animation-delay: 60ms");
  114. expect(style).toContain("animation-fill-mode: backwards");
  115. });
  116. });
  117. describe("WorkflowArtifactStrip", () => {
  118. const FOUR: ArtifactPillData[] = [
  119. BOOK,
  120. { id: "perf", kind: "chart", title: "Round timings", version: 1 },
  121. { id: "notes", kind: "markdown", title: "Notes", version: 1 },
  122. { id: "kpi", kind: "metrics", title: "KPIs", version: 3 },
  123. ];
  124. it("至多三枚,其余折成 +N;每枚依次错开入场", () => {
  125. const view = renderStrip({ artifacts: FOUR, onOpenArtifact: vi.fn() });
  126. const pills = view.getAllByTestId("pill");
  127. expect(pills).toHaveLength(3);
  128. expect(pills.map((pill) => pill.getAttribute("data-artifact-id"))).toEqual([
  129. "book",
  130. "perf",
  131. "notes",
  132. ]);
  133. expect(pills[0]?.getAttribute("style")).toBeNull();
  134. expect(pills[2]?.getAttribute("style")).toContain("animation-delay: 60ms");
  135. expect(view.getByTestId("more").textContent).toBe("+1");
  136. });
  137. it("发射侧砍过:不足三枚也标「+…」;零件时整条缺席", () => {
  138. const view = renderStrip({ artifacts: FOUR.slice(0, 2), truncated: true });
  139. expect(view.getByTestId("more").textContent).toBe("+…");
  140. cleanup();
  141. expect(renderStrip({ artifacts: [] }).queryByTestId("strip")).toBeNull();
  142. cleanup();
  143. expect(renderStrip({ artifacts: FOUR.slice(0, 2) }).queryByTestId("more")).toBeNull();
  144. });
  145. it("事件只替药丸止步:药丸的点击与键盘不冒到外面的开关;条上的空白照常冒泡", () => {
  146. // docs/dynamic-workflow/presentation.md「The pill」:条的空白与 `+N` 是宿主可点区域
  147. // (轮尾摘要的整块开关)的一部分,只有药丸是自己的按钮。
  148. const outer = vi.fn();
  149. const onOpenArtifact = vi.fn();
  150. const view = render(
  151. createElement(
  152. ZCodeIntlProvider,
  153. { initialLocale: "en-US" },
  154. createElement(
  155. "div",
  156. { onClick: outer, onKeyDown: outer, "data-testid": "outer" },
  157. createElement(WorkflowArtifactStrip, {
  158. artifacts: FOUR,
  159. onOpenArtifact,
  160. testId: "strip",
  161. pillTestId: "pill",
  162. moreTestId: "more",
  163. }),
  164. ),
  165. ),
  166. );
  167. const pill = view.getAllByTestId("pill")[0]!;
  168. fireEvent.click(pill);
  169. expect(onOpenArtifact).toHaveBeenCalledWith("book");
  170. fireEvent.keyDown(pill, { key: "Enter" });
  171. fireEvent.keyDown(pill, { key: " " });
  172. expect(outer).not.toHaveBeenCalled();
  173. // 空白、`+N` 与别的键照旧冒泡(Escape 之类归外面管)。
  174. fireEvent.click(view.getByTestId("strip"));
  175. fireEvent.click(view.getByTestId("more"));
  176. fireEvent.keyDown(view.getByTestId("strip"), { key: "Escape" });
  177. expect(outer).toHaveBeenCalledTimes(3);
  178. });
  179. it("禁用的药丸(无回调)同样止步:点它不把外面的开关点开", () => {
  180. const outer = vi.fn();
  181. const view = render(
  182. createElement(
  183. ZCodeIntlProvider,
  184. { initialLocale: "en-US" },
  185. createElement(
  186. "div",
  187. { onClick: outer, "data-testid": "outer" },
  188. createElement(WorkflowArtifactStrip, { artifacts: FOUR.slice(0, 1), pillTestId: "pill" }),
  189. ),
  190. ),
  191. );
  192. fireEvent.click(view.getByTestId("pill"));
  193. expect(outer).not.toHaveBeenCalled();
  194. });
  195. it("小号条把尺寸交给每枚药丸", () => {
  196. const view = renderStrip({ artifacts: FOUR.slice(0, 1), size: "sm" });
  197. expect(view.getByTestId("pill").getAttribute("data-pill-size")).toBe("sm");
  198. });
  199. });
  200. it("Markdown 产物使用 FileDisplay 文件图标", () => {
  201. const view = renderPill({ artifact: { id: "md", kind: "markdown", title: "报告" } });
  202. expect(view.container.querySelector("img")?.getAttribute("src")).toMatch(/markdown/i);
  203. expect(view.container.querySelector("img")?.parentElement?.tagName).toBe("BUTTON");
  204. expect(view.container.querySelector(".wf-pill-tile")).toBeNull();
  205. });