workflowArtifactPill.test.ts 9.6 KB

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