workflowCompletionCard.test.ts 20 KB

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