workflowCompletionCard.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // @vitest-environment jsdom
  2. // 完成卡(docs/dynamic-workflow/transcript-and-notifications.md):表头 → 交付物行 + 产物索引 → 四格数字。纯展示层——
  3. // 预览由调用方交进来,这里只钉形态、布局随件数的变化、数字的诚实(`—` 不是 0)与门控。
  4. import { cleanup, fireEvent, render, within } from "@testing-library/react";
  5. import { createElement, type ReactNode } from "react";
  6. import { afterEach, describe, expect, it, vi } from "vitest";
  7. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  8. vi.mock("@/ControlHintTooltip.js", () => ({
  9. ControlHintTooltip: ({ children }: { children?: ReactNode }) => children,
  10. }));
  11. // eslint-disable-next-line import/first -- 必须在 tooltip mock 之后再引入被测组件。
  12. import {
  13. COMPLETION_INDEX_MAX,
  14. WorkflowCompletionCard,
  15. formatCompactCount,
  16. type WorkflowCompletionCardProps,
  17. } from "@/components/workflow-timeline/WorkflowCompletionCard.js";
  18. // eslint-disable-next-line import/first -- 同上。
  19. import type { WorkflowCompletionArtifact } from "@/components/workflow-timeline/WorkflowArtifactTile.js";
  20. // eslint-disable-next-line import/first -- 同上。
  21. import { formatWorkDuration, workDurationParts } from "@/lib/workDuration.js";
  22. afterEach(() => {
  23. cleanup();
  24. });
  25. const ARTIFACTS: WorkflowCompletionArtifact[] = [
  26. { id: "brief", kind: "markdown", title: "research-brief.md", version: 1, bytes: 12_288 },
  27. {
  28. id: "sources",
  29. kind: "file",
  30. title: "sources.csv",
  31. version: 2,
  32. bytes: 6_144,
  33. contentType: "text/csv",
  34. sourcePath: "out/sources.csv",
  35. },
  36. { id: "coverage", kind: "chart", title: "source coverage", version: 1, itemCount: 4 },
  37. ];
  38. function renderCard(
  39. overrides: Partial<WorkflowCompletionCardProps> = {},
  40. locale: "en-US" | "zh-CN" = "en-US",
  41. ) {
  42. const props: WorkflowCompletionCardProps = {
  43. name: "deep-research",
  44. figures: { durationMs: 708_000, tokens: 386_412, subagents: 10, phases: 16 },
  45. artifacts: ARTIFACTS,
  46. testIdKey: "turn-1",
  47. ...overrides,
  48. };
  49. return render(
  50. createElement(
  51. ZCodeIntlProvider,
  52. { initialLocale: locale },
  53. createElement(WorkflowCompletionCard, props),
  54. ),
  55. );
  56. }
  57. describe("WorkflowCompletionCard · 形态", () => {
  58. it("表头 = 完成的种类词 + 名字 + 灯与状态词;没有 chevron;有回调才有 ⤢", () => {
  59. const onOpenRun = vi.fn();
  60. const view = renderCard({ onOpenRun });
  61. const card = view.getByTestId("workflow-completion-card-turn-1");
  62. expect(card.getAttribute("aria-label")).toBe("Workflow completed");
  63. expect(view.getByTestId("workflow-card-kind").textContent).toBe("Workflow completed");
  64. expect(view.getByTestId("workflow-card-name").textContent).toBe("deep-research");
  65. expect(view.getByTestId("workflow-completion-status").textContent).toBe("Completed");
  66. expect(view.queryByTestId("workflow-card-toggle")).toBeNull();
  67. fireEvent.click(view.getByTestId("workflow-card-open-details"));
  68. expect(onOpenRun).toHaveBeenCalledTimes(1);
  69. cleanup();
  70. const cold = renderCard();
  71. expect(cold.queryByTestId("workflow-card-open-details")).toBeNull();
  72. });
  73. it("四格:时间拆成数字 + 单位、tokens 紧凑写法并把全值放进 title、子代理与阶段数原样", () => {
  74. const view = renderCard();
  75. expect(view.getByTestId("workflow-completion-figure-time").getAttribute("data-value")).toBe(
  76. "11m 48s",
  77. );
  78. const tokens = view.getByTestId("workflow-completion-figure-tokens");
  79. expect(tokens.getAttribute("data-value")).toBe("386.4k");
  80. expect(tokens.querySelector("[title]")?.getAttribute("title")).toBe("386,412 tokens");
  81. expect(
  82. view.getByTestId("workflow-completion-figure-subagents").getAttribute("data-value"),
  83. ).toBe("10");
  84. expect(view.getByTestId("workflow-completion-figure-phases").getAttribute("data-value")).toBe(
  85. "16",
  86. );
  87. // 标签四个都在。
  88. const figures = view.getByTestId("workflow-completion-figures");
  89. expect(figures.textContent).toContain("time");
  90. expect(figures.textContent).toContain("subagents");
  91. expect(figures.textContent).toContain("phases");
  92. expect(figures.textContent).not.toContain("steps");
  93. });
  94. it("拿不到的格写 `—`(带无障碍名),不写 0;时间独立于其余三格", () => {
  95. const view = renderCard({ figures: { durationMs: 3_000 } });
  96. expect(view.getByTestId("workflow-completion-figure-time").getAttribute("data-value")).toBe(
  97. "3s",
  98. );
  99. for (const key of ["tokens", "subagents", "phases"]) {
  100. const figure = view.getByTestId(`workflow-completion-figure-${key}`);
  101. expect(figure.getAttribute("data-value")).toBeNull();
  102. expect(figure.textContent).toContain("—");
  103. expect(figure.querySelector("[aria-label]")?.getAttribute("aria-label")).toBe(
  104. "Not available",
  105. );
  106. expect(figure.textContent).not.toContain("0");
  107. }
  108. });
  109. it("zh-CN:时长单位前留空格并渲染子代理标签", () => {
  110. const view = renderCard({ figures: { durationMs: 708_000, tokens: 5 } }, "zh-CN");
  111. expect(view.getByTestId("workflow-completion-figure-time").getAttribute("data-value")).toBe(
  112. "11 分 48 秒",
  113. );
  114. expect(view.getByTestId("workflow-completion-figures").textContent).toContain("子代理");
  115. });
  116. });
  117. describe("WorkflowCompletionCard · 产物索引", () => {
  118. it("没有交付物:索引独占产物区;细节与打开身份保真", () => {
  119. const onOpenArtifact = vi.fn();
  120. const view = renderCard({ onOpenArtifact });
  121. const index = view.getByTestId("workflow-completion-index");
  122. expect(index.className).not.toContain("border-t");
  123. expect(index.getAttribute("data-columns")).toBe("auto");
  124. const lines = view.getAllByTestId("workflow-completion-line");
  125. expect(lines).toHaveLength(3);
  126. expect(lines.every((line) => line.getAttribute("data-variant") === "line")).toBe(true);
  127. // 三件、无旗子:没有交付物,也没有交付物行;索引不画框,所以整卡没有纸页字形。
  128. expect(view.queryByTestId("workflow-completion-row")).toBeNull();
  129. expect(view.queryByTestId("workflow-artifact-tile-frame")).toBeNull();
  130. expect(view.queryByTestId("workflow-artifact-sheet")).toBeNull();
  131. // 细节就在行上:文档只有大小;文件 `CSV · 6.0 KB`;看板是条数。
  132. const details = view.getAllByTestId("workflow-artifact-line-detail");
  133. expect(details[0]!.textContent).toBe("12.0 KB");
  134. expect(details[1]!.textContent).toContain("CSV");
  135. expect(details[1]!.textContent).toContain("6.0 KB");
  136. expect(details[2]!.textContent).toBe("4 items");
  137. // v2 尾槽只在第二件上。
  138. const versions = view.getAllByTestId("workflow-artifact-tile-version");
  139. expect(versions).toHaveLength(1);
  140. expect(versions[0]!.textContent).toBe("v2");
  141. // 可点:真按钮、aria-label、↗ 在场、交出 id;tooltip 是「种类词 · 标题」。
  142. expect(lines[1]!.tagName).toBe("BUTTON");
  143. expect(lines[1]!.hasAttribute("disabled")).toBe(false);
  144. expect(lines[1]!.getAttribute("aria-label")).toBe("Open artifact: sources.csv");
  145. expect(lines[1]!.getAttribute("title")).toBe("File · sources.csv");
  146. expect(view.getAllByTestId("workflow-artifact-tile-open")).toHaveLength(3);
  147. fireEvent.click(lines[1]!);
  148. expect(onOpenArtifact).toHaveBeenCalledWith("sources");
  149. // 没有门。
  150. expect(view.queryByTestId("workflow-completion-more")).toBeNull();
  151. });
  152. it("没有回调:行禁用、无 aria-label、无 ↗;版本仍在", () => {
  153. const view = renderCard();
  154. const lines = view.getAllByTestId("workflow-completion-line");
  155. expect(lines.every((line) => line.hasAttribute("disabled"))).toBe(true);
  156. expect(lines[0]!.getAttribute("aria-label")).toBeNull();
  157. expect(view.queryByTestId("workflow-artifact-tile-open")).toBeNull();
  158. expect(view.getAllByTestId("workflow-artifact-tile-version")).toHaveLength(1);
  159. });
  160. it("一件产物即交付物:保留预览框,没有索引", () => {
  161. const one = renderCard({ artifacts: ARTIFACTS.slice(0, 1) });
  162. expect(one.queryByTestId("workflow-completion-index")).toBeNull();
  163. const row = one.getByTestId("workflow-completion-row");
  164. expect(row.getAttribute("data-artifact-id")).toBe("brief");
  165. expect(one.getByTestId("workflow-artifact-row-frame")).toBeTruthy();
  166. });
  167. it("超过六件:五行 + 一行「N more」——一扇门(↗ 常在,点开 run 侧板);六件全画;砍过时「… more」且无回调则禁用", () => {
  168. const many = Array.from({ length: 7 }, (_unused, index) => ({
  169. id: `a${index}`,
  170. kind: "file" as const,
  171. title: `产物 ${index}`,
  172. version: 1,
  173. }));
  174. const onOpenRun = vi.fn();
  175. const view = renderCard({ artifacts: many, onOpenRun });
  176. expect(view.getAllByTestId("workflow-completion-line")).toHaveLength(COMPLETION_INDEX_MAX - 1);
  177. const more = view.getByTestId("workflow-completion-more") as HTMLButtonElement;
  178. expect(more.textContent).toBe("2 more");
  179. expect(more.getAttribute("data-variant")).toBe("more");
  180. expect(more.getAttribute("title")).toBe("Open run details");
  181. expect(within(more).getByTestId("workflow-artifact-tile-open").className).toContain(
  182. "wf-pill-go-rest",
  183. );
  184. fireEvent.click(more);
  185. expect(onOpenRun).toHaveBeenCalledTimes(1);
  186. cleanup();
  187. const six = renderCard({ artifacts: many.slice(0, 6) });
  188. expect(six.getAllByTestId("workflow-completion-line")).toHaveLength(COMPLETION_INDEX_MAX);
  189. expect(six.queryByTestId("workflow-completion-more")).toBeNull();
  190. cleanup();
  191. const truncated = renderCard({ artifacts: many.slice(0, 4), artifactsTruncated: true });
  192. expect(truncated.getAllByTestId("workflow-completion-line")).toHaveLength(4);
  193. const dumb = truncated.getByTestId("workflow-completion-more") as HTMLButtonElement;
  194. expect(dumb.textContent).toBe("… more");
  195. expect(dumb.disabled).toBe(true);
  196. expect(within(dumb).queryByTestId("workflow-artifact-tile-open")).toBeNull();
  197. });
  198. it("预览只交给交付物的框:没有交付物时不问预览;零产物没有产物区、卡仍画", () => {
  199. const renderPreview = vi.fn((_artifact: WorkflowCompletionArtifact) => undefined);
  200. const view = renderCard({ renderPreview });
  201. expect(renderPreview).not.toHaveBeenCalled();
  202. expect(view.getByTestId("workflow-completion-index")).toBeTruthy();
  203. cleanup();
  204. const empty = renderCard({ artifacts: [] });
  205. expect(empty.queryByTestId("workflow-completion-index")).toBeNull();
  206. expect(empty.queryByTestId("workflow-completion-row")).toBeNull();
  207. expect(empty.getByTestId("workflow-completion-figures")).toBeTruthy();
  208. });
  209. it("行依次落地:第 k 行延迟 k × 30 ms 且 backwards 填充;四格排在行之后", () => {
  210. const view = renderCard();
  211. const lines = view.getAllByTestId("workflow-completion-line");
  212. expect(lines[0]!.style.animationDelay).toBe("30ms");
  213. expect(lines[2]!.style.animationDelay).toBe("90ms");
  214. expect(lines[2]!.style.animationFillMode).toBe("backwards");
  215. expect(view.getByTestId("workflow-completion-figure-time").style.animationDelay).toBe("120ms");
  216. expect(view.getByTestId("workflow-completion-figure-phases").style.animationDelay).toBe(
  217. "210ms",
  218. );
  219. });
  220. });
  221. // 交付物(docs/dynamic-workflow/transcript-and-notifications.md「Artifact tiles」「The index」):打了 primary 旗子的那件
  222. // 领头成一行,其余是细线之下的索引行;第七件起五行 + 「N more」。UI 上没有「primary」这个词。
  223. describe("WorkflowCompletionCard · 交付物", () => {
  224. const REPORT: WorkflowCompletionArtifact = {
  225. id: "report",
  226. kind: "markdown",
  227. title: "季度基准报告",
  228. description: "对比十二个服务的 p50 / p99 延迟与上季度基线;确认三处回归,给出两项修复建议。",
  229. version: 3,
  230. bytes: 24_576,
  231. primary: true,
  232. };
  233. const others = (count: number): WorkflowCompletionArtifact[] =>
  234. Array.from({ length: count }, (_unused, index) => ({
  235. id: `n${index}`,
  236. kind: index === 0 ? "chart" : "markdown",
  237. title: `产物 ${index}`,
  238. version: 1,
  239. ...(index === 0 ? { itemCount: 12 } : { bytes: 2_048 }),
  240. }));
  241. it("交付物行保留说明、细节、版本与打开身份", () => {
  242. const onOpenArtifact = vi.fn();
  243. // 旗子在清单**中间**也领头:布局按旗子选,不按位置。
  244. const view = renderCard({ artifacts: [...others(2), REPORT, ...others(1)], onOpenArtifact });
  245. const row = view.getByTestId("workflow-completion-row") as HTMLButtonElement;
  246. expect(row.tagName).toBe("BUTTON");
  247. expect(row.getAttribute("data-artifact-id")).toBe("report");
  248. expect(row.getAttribute("data-variant")).toBe("row");
  249. expect(view.getByTestId("workflow-artifact-row-title").textContent).toBe("季度基准报告");
  250. const description = view.getByTestId("workflow-artifact-row-description");
  251. expect(description.textContent).toContain("确认三处回归");
  252. expect(view.getByTestId("workflow-artifact-row-detail").textContent).toBe("Document·24.0 KB");
  253. expect(within(row).getByTestId("workflow-artifact-tile-version").textContent).toBe("v3");
  254. expect(row.getAttribute("aria-label")).toBe("Open artifact: 季度基准报告");
  255. fireEvent.click(row);
  256. expect(onOpenArtifact).toHaveBeenCalledWith("report");
  257. // 没有任何「primary」字样——形态本身在说话。
  258. expect(view.getByTestId("workflow-completion-card-turn-1").textContent).not.toMatch(
  259. /primary/iu,
  260. );
  261. });
  262. it("说明缺席时那一行消失、行仍在;没有回调时整行禁用", () => {
  263. const { description: _dropped, ...bare } = REPORT;
  264. const view = renderCard({ artifacts: [bare, ...others(1)] });
  265. expect(view.queryByTestId("workflow-artifact-row-description")).toBeNull();
  266. const row = view.getByTestId("workflow-completion-row") as HTMLButtonElement;
  267. expect(row.disabled).toBe(true);
  268. expect(row.getAttribute("aria-label")).toBeNull();
  269. expect(view.queryByTestId("workflow-artifact-tile-open")).toBeNull();
  270. });
  271. it("其余产物在分隔线后按原序列出:六件全画且没有门", () => {
  272. const view = renderCard({ artifacts: [REPORT, ...others(6)] });
  273. const index = view.getByTestId("workflow-completion-index");
  274. expect(index.className).toContain("border-t");
  275. const lines = view.getAllByTestId("workflow-completion-line");
  276. expect(lines).toHaveLength(6);
  277. expect(lines.map((line) => line.getAttribute("data-artifact-id"))).toEqual([
  278. "n0",
  279. "n1",
  280. "n2",
  281. "n3",
  282. "n4",
  283. "n5",
  284. ]);
  285. const details = view.getAllByTestId("workflow-artifact-line-detail");
  286. expect(details[0]!.textContent).toBe("12 items");
  287. expect(details[1]!.textContent).toBe("2.0 KB");
  288. expect(lines[0]!.getAttribute("title")).toBe("Chart · 产物 0");
  289. expect(view.queryByTestId("workflow-completion-more")).toBeNull();
  290. // 索引不画框:整卡只有交付物那一个框(这里没交预览,所以是一张纸页字形)。
  291. expect(view.getAllByTestId("workflow-artifact-sheet")).toHaveLength(1);
  292. });
  293. it("第七件起:五行 + 「N more」(点开 run 侧板);zh「还有 N 个」;砍过时 `…`;无回调则禁用", () => {
  294. const onOpenRun = vi.fn();
  295. const view = renderCard({ artifacts: [REPORT, ...others(7)], onOpenRun });
  296. expect(view.getAllByTestId("workflow-completion-line")).toHaveLength(5);
  297. const more = view.getByTestId("workflow-completion-more") as HTMLButtonElement;
  298. expect(more.textContent).toBe("2 more");
  299. expect(more.getAttribute("title")).toBe("Open run details");
  300. fireEvent.click(more);
  301. expect(onOpenRun).toHaveBeenCalledTimes(1);
  302. cleanup();
  303. const zh = renderCard({ artifacts: [REPORT, ...others(7)], onOpenRun }, "zh-CN");
  304. expect(zh.getByTestId("workflow-completion-more").textContent).toBe("还有 2 个");
  305. cleanup();
  306. const truncated = renderCard({ artifacts: [REPORT, ...others(3)], artifactsTruncated: true });
  307. expect(truncated.getAllByTestId("workflow-completion-line")).toHaveLength(3);
  308. const dumb = truncated.getByTestId("workflow-completion-more") as HTMLButtonElement;
  309. expect(dumb.textContent).toBe("… more");
  310. expect(dumb.disabled).toBe(true);
  311. });
  312. it("预览只问交付物一次;节拍:行 30 ms、索引行依次、门在最后一行之后、四格接在门之后", () => {
  313. const renderPreview = vi.fn((_artifact: WorkflowCompletionArtifact) => undefined);
  314. const view = renderCard({ artifacts: [REPORT, ...others(7)], renderPreview });
  315. expect(renderPreview.mock.calls.map(([artifact]) => artifact.id)).toEqual(["report"]);
  316. expect(view.getByTestId("workflow-completion-row").style.animationDelay).toBe("30ms");
  317. const lines = view.getAllByTestId("workflow-completion-line");
  318. expect(lines[0]!.style.animationDelay).toBe("60ms");
  319. expect(lines[4]!.style.animationDelay).toBe("180ms");
  320. expect(view.getByTestId("workflow-completion-more").style.animationDelay).toBe("210ms");
  321. // 7 格(行 + 5 行 + 门)之后才是四格。
  322. expect(view.getByTestId("workflow-completion-figure-time").style.animationDelay).toBe("240ms");
  323. // 没交预览时行里是纸页字形——整卡唯一的一张。
  324. expect(view.getAllByTestId("workflow-artifact-sheet")).toHaveLength(1);
  325. });
  326. });
  327. describe("formatCompactCount / workDuration", () => {
  328. it("tokens 紧凑写法:千位一位小数、百万两位;千以下原样", () => {
  329. expect(formatCompactCount(812)).toEqual({ value: "812", unit: "" });
  330. expect(formatCompactCount(386_412)).toEqual({ value: "386.4", unit: "k" });
  331. expect(formatCompactCount(1_304_118)).toEqual({ value: "1.30", unit: "M" });
  332. });
  333. it("时长拆段:最多两段、至少 1 秒;与「工作了」的写法同源", () => {
  334. const format = ({ id }: { id: string }) => id.split(".").at(-1)!.charAt(0);
  335. expect(workDurationParts(0, format)).toEqual([{ value: 1, unit: "s" }]);
  336. expect(workDurationParts(3_725_000, format)).toEqual([
  337. { value: 1, unit: "h" },
  338. { value: 2, unit: "m" },
  339. ]);
  340. expect(formatWorkDuration(102_000, format, "en-US")).toBe("1m 42s");
  341. expect(formatWorkDuration(102_000, format, "zh-CN")).toBe("1 m 42 s");
  342. expect(formatWorkDuration(undefined, format, "en-US")).toBeNull();
  343. });
  344. });