workflowRunArtifactsSection.test.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // @vitest-environment jsdom
  2. // run 侧板的 Artifacts 区:交付物行 + 索引,或瓦片画廊(docs/dynamic-workflow/authoring.md「How the user sees them」;
  3. // 形态见 docs/dynamic-workflow/presentation.md「The run pane」)。
  4. //
  5. // ⚠ 术语:这一区的 artifact 是脚本经 `artifact.*` 交付给**用户**的产出,与「结果」(脚本顶层
  6. // 返回值,给模型的)是两件事——spec 的不变式要求两者措辞可区分。(`report` 条目那一节已于
  7. // 2026-09-04 按用户指令撤走,见 docs/dynamic-workflow/presentation.md 的同日修订。)
  8. //
  9. // 「这一区是面板的最后一节」那条位置断言在 workflowRunSidePane.test.ts 里(那里才有
  10. // 完整的面板);本文件只钉这一区自己的形态与交互。
  11. import { createElement } from "react";
  12. import { act, cleanup, fireEvent, render, waitFor, within } from "@testing-library/react";
  13. import { beforeEach, describe, expect, it, vi } from "vitest";
  14. import { TID_WORKFLOW_ARTIFACTS_TOGGLE, TID_WORKFLOW_ARTIFACT_CARD } from "@zcode/shared";
  15. import type { V4ConversationWorkflowRunArtifactDataResult } from "@zcode/shared/zcode-protocol-v4";
  16. vi.mock("@/logger.js", () => ({
  17. logger: { debug: vi.fn(), warn: vi.fn() },
  18. }));
  19. /** 看板取数的传输面:用例直接观察它收到的 `afterSequence`(增量续拉的唯一可观察点)。 */
  20. const workflowRunArtifactData =
  21. vi.fn<
  22. (params: {
  23. sessionId: string;
  24. runId: string;
  25. artifactId: string;
  26. afterSequence?: number;
  27. limit?: number;
  28. }) => Promise<V4ConversationWorkflowRunArtifactDataResult>
  29. >();
  30. /** 字节读取面:文档 / CSV / 图片的缩略走它;PDF 与看板不该碰它。 */
  31. const workflowRunArtifactRead = vi.fn();
  32. vi.mock("@/v4/V4ConversationContext.js", () => ({
  33. useV4Conversation: () => ({ workflowRunArtifactData, workflowRunArtifactRead }),
  34. useHasV4Conversation: () => true,
  35. }));
  36. // eslint-disable-next-line import/first -- 必须在 mock 之后再引入被测组件。
  37. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  38. // eslint-disable-next-line import/first
  39. import { WorkflowRunArtifactsSection } from "@/app-shell/WorkflowRunArtifactsSection.js";
  40. // eslint-disable-next-line import/first
  41. import type { WorkflowRunArtifactView } from "@/hooks/useWorkflowRunArtifacts.js";
  42. const onOpenArtifact = vi.fn();
  43. function artifact(overrides: Partial<WorkflowRunArtifactView> = {}): WorkflowRunArtifactView {
  44. return {
  45. id: "book",
  46. kind: "file",
  47. title: "审计报告",
  48. contentType: "application/pdf",
  49. bytes: 4096,
  50. sourcePath: "out/book.pdf",
  51. version: 2,
  52. itemCount: 0,
  53. ...overrides,
  54. };
  55. }
  56. function renderSection(
  57. artifacts: readonly WorkflowRunArtifactView[],
  58. options: { locale?: "en-US" | "zh-CN"; openable?: boolean } = {},
  59. ) {
  60. return render(
  61. createElement(
  62. ZCodeIntlProvider,
  63. { initialLocale: options.locale ?? "zh-CN" },
  64. createElement(WorkflowRunArtifactsSection, {
  65. artifacts,
  66. runId: "dwfrun-1",
  67. sessionId: "parent-a",
  68. ...(options.openable === false ? {} : { onOpenArtifact }),
  69. }),
  70. ),
  71. );
  72. }
  73. beforeEach(() => {
  74. cleanup();
  75. // MessageResponse 按 matchMedia 选代码块配色;jsdom 没有它。
  76. Object.defineProperty(window, "matchMedia", {
  77. configurable: true,
  78. writable: true,
  79. value: vi.fn(() => ({
  80. matches: false,
  81. addEventListener: vi.fn(),
  82. removeEventListener: vi.fn(),
  83. addListener: vi.fn(),
  84. removeListener: vi.fn(),
  85. })),
  86. });
  87. onOpenArtifact.mockClear();
  88. workflowRunArtifactData.mockReset();
  89. workflowRunArtifactData.mockResolvedValue({ items: [], hasMore: false });
  90. workflowRunArtifactRead.mockReset();
  91. });
  92. describe("WorkflowRunArtifactsSection", () => {
  93. it("默认展开,卡直接在场(产物是交付物,不该藏在一次点击后面);一件产物即交付物行", () => {
  94. const view = renderSection([artifact()]);
  95. expect(view.getByTestId(TID_WORKFLOW_ARTIFACTS_TOGGLE).getAttribute("aria-expanded")).toBe(
  96. "true",
  97. );
  98. const cards = view.getAllByTestId(TID_WORKFLOW_ARTIFACT_CARD);
  99. expect(cards).toHaveLength(1);
  100. // 单件规则:没有旗子、只有一件 ⇒ 交付物行(同一个 testid,形态记在 data-variant 上)。
  101. expect(cards[0]!.getAttribute("data-variant")).toBe("row");
  102. expect(view.queryByTestId("workflow-run-artifact-gallery")).toBeNull();
  103. });
  104. // 交付物(docs/dynamic-workflow/presentation.md「The run pane」第 4 项):行 + 细线 + 单列索引;收起时节头带标题。
  105. it("有交付物:交付物行领头(容器查询下窄面板收成 136 × 85),其余是细线之下的单列索引行;收起时节头在件数后带交付物标题", () => {
  106. const view = renderSection([
  107. artifact({
  108. id: "perf",
  109. kind: "chart",
  110. bytes: undefined,
  111. sourcePath: undefined,
  112. version: 1,
  113. itemCount: 4,
  114. }),
  115. artifact({
  116. id: "notes",
  117. kind: "markdown",
  118. contentType: "text/markdown",
  119. bytes: 2048,
  120. sourcePath: undefined,
  121. }),
  122. artifact({ title: "审计报告", description: "本轮审计的结论与修复建议。", primary: true }),
  123. ]);
  124. const body = view.getByTestId("workflow-run-artifact-primary-body");
  125. expect(body.className).toContain("@container/wf-artifacts");
  126. const cards = within(body).getAllByTestId(TID_WORKFLOW_ARTIFACT_CARD);
  127. // 清单已由 hook 排成交付物在前;这里按旗子选行,不按位置。
  128. expect(cards[0]!.getAttribute("data-variant")).toBe("row");
  129. expect(cards[0]!.getAttribute("data-artifact-id")).toBe("book");
  130. expect(within(cards[0]!).getByTestId("workflow-artifact-row-description").textContent).toBe(
  131. "本轮审计的结论与修复建议。",
  132. );
  133. expect(within(cards[0]!).getByTestId("workflow-artifact-row-detail").textContent).toBe(
  134. "文件·PDF·4.0 KB",
  135. );
  136. // 预览框是按钮的兄弟(缩略里的控件不能嵌进按钮),所以从瓦片容器里找。
  137. const frame = within(cards[0]!.parentElement!).getByTestId("workflow-artifact-row-frame");
  138. expect(frame.className).toContain("@max-[380px]/wf-artifacts:w-[136px]");
  139. expect(frame.className).toContain("@max-[380px]/wf-artifacts:h-[85px]");
  140. const index = view.getByTestId("workflow-run-artifact-index");
  141. expect(index.className).toContain("grid-cols-1");
  142. expect(index.className).toContain("border-t");
  143. const lines = within(index).getAllByTestId(TID_WORKFLOW_ARTIFACT_CARD);
  144. expect(lines.map((card) => card.getAttribute("data-artifact-id"))).toEqual(["perf", "notes"]);
  145. expect(lines.every((card) => card.getAttribute("data-variant") === "line")).toBe(true);
  146. // 索引行不画框;细节就在行上(看板是条数、文档是大小),tooltip 只有种类词 · 标题(+ 出处)。
  147. expect(within(index).queryByTestId("workflow-artifact-tile-frame")).toBeNull();
  148. expect(within(lines[0]!).getByTestId("workflow-run-artifact-items").textContent).toBe("4 条");
  149. expect(within(lines[1]!).getByTestId("workflow-run-artifact-bytes").textContent).toBe("2.0 KB");
  150. expect(lines[0]!.getAttribute("title")).toBe("图表 · 审计报告");
  151. // 节拍:行 30 ms,索引行接着。
  152. expect((cards[0] as HTMLElement).style.animationDelay).toBe("30ms");
  153. expect((lines[0] as HTMLElement).style.animationDelay).toBe("60ms");
  154. // 展开时节头不重复标题;收起后带上。
  155. expect(view.queryByTestId("workflow-run-artifacts-primary-title")).toBeNull();
  156. fireEvent.click(view.getByTestId(TID_WORKFLOW_ARTIFACTS_TOGGLE));
  157. expect(view.getByTestId("workflow-run-artifacts-count").textContent).toBe("3");
  158. expect(view.getByTestId("workflow-run-artifacts-primary-title").textContent).toBe("· 审计报告");
  159. // 「primary」这个词不上屏。
  160. expect(view.container.textContent).not.toMatch(/primary/iu);
  161. });
  162. it("节身是一片画廊:一件产物一张瓦片(预览框 + 说明行),依次落地;不再有药丸行 + 挂卡", () => {
  163. const view = renderSection([
  164. artifact(),
  165. artifact({ id: "notes", kind: "markdown", contentType: "text/markdown", bytes: 2048 }),
  166. artifact({ id: "perf", kind: "chart", bytes: undefined, sourcePath: undefined, version: 1 }),
  167. ]);
  168. const gallery = view.getByTestId("workflow-run-artifact-gallery");
  169. const tiles = within(gallery).getAllByTestId(TID_WORKFLOW_ARTIFACT_CARD);
  170. expect(tiles).toHaveLength(3);
  171. // 每张瓦片自带预览框(按钮的兄弟节点、inert);PDF 画纸页字形 + 徽字。
  172. const tile = tiles[0]!.parentElement!;
  173. const frame = within(tile).getByTestId("workflow-artifact-tile-frame");
  174. expect(frame.hasAttribute("inert")).toBe(true);
  175. expect(within(tile).getByTestId("workflow-artifact-sheet-badge").textContent).toBe("PDF");
  176. expect(within(tiles[0]!).queryByTestId("workflow-artifact-tile-frame")).toBeNull();
  177. // 30 ms 一枚。
  178. expect((tiles[0] as HTMLElement).style.animationDelay).toBe("30ms");
  179. expect((tiles[2] as HTMLElement).style.animationDelay).toBe("90ms");
  180. expect(view.queryByTestId("workflow-run-artifact-row")).toBeNull();
  181. expect(view.queryByTestId("workflow-run-artifact-preview")).toBeNull();
  182. });
  183. it("可以手动收起,收起时表头仍报件数", () => {
  184. const view = renderSection([artifact(), artifact({ id: "notes", kind: "markdown" })]);
  185. expect(view.getByTestId("workflow-run-artifacts-count").textContent).toBe("2");
  186. fireEvent.click(view.getByTestId(TID_WORKFLOW_ARTIFACTS_TOGGLE));
  187. expect(view.queryAllByTestId(TID_WORKFLOW_ARTIFACT_CARD)).toHaveLength(0);
  188. // 折叠不能让「这个 run 到底交付了什么」不可见。
  189. expect(view.getByTestId("workflow-run-artifacts-count").textContent).toBe("2");
  190. });
  191. it("说明行带类型徽字、字节数与版本尾槽;kind 词与工作区出处进 tooltip;title 缺席时退回 id", () => {
  192. const view = renderSection([artifact({ title: undefined })]);
  193. const card = view.getByTestId(TID_WORKFLOW_ARTIFACT_CARD);
  194. // 尾槽里是紧凑的 v2(两种语言一样),长写在 title 里。
  195. const version = within(card).getByTestId("workflow-artifact-tile-version");
  196. expect(version.textContent).toBe("v2");
  197. expect(version.getAttribute("title")).toBe("第 2 版");
  198. expect(within(card).getByTestId("workflow-run-artifact-badge").textContent).toBe("PDF");
  199. expect(within(card).getByTestId("workflow-run-artifact-bytes").textContent).toBe("4.0 KB");
  200. // 出处是一条路径,行上放不下:与 kind 词一起进 tooltip。
  201. expect(card.getAttribute("title")).toBe("文件 · book\nout/book.pdf");
  202. expect(view.queryByTestId("workflow-run-artifact-source-path")).toBeNull();
  203. // facade 的缺省 title 本来就是 id,所以说明行退回 id 而不是留空。
  204. expect(within(card).getByText("book")).toBeTruthy();
  205. });
  206. it("英文语境下版本尾槽同样是 v{n},tooltip 用英文", () => {
  207. const view = renderSection([artifact()], { locale: "en-US" });
  208. const version = view.getByTestId("workflow-artifact-tile-version");
  209. expect(version.textContent).toBe("v2");
  210. expect(version.getAttribute("title")).toBe("v2");
  211. });
  212. it("第 1 版不写版本号(v1 是常态,写出来是噪音)", () => {
  213. const view = renderSection([artifact({ version: 1 })]);
  214. expect(view.queryByTestId("workflow-artifact-tile-version")).toBeNull();
  215. });
  216. it("看板瓦片没有字节与出处(它的「大小」是有多少条数据)", () => {
  217. const view = renderSection([
  218. artifact({
  219. id: "perf",
  220. kind: "chart",
  221. title: "每轮耗时",
  222. bytes: undefined,
  223. contentType: undefined,
  224. sourcePath: undefined,
  225. version: 1,
  226. }),
  227. ]);
  228. expect(view.queryByTestId("workflow-run-artifact-bytes")).toBeNull();
  229. expect(view.queryByTestId("workflow-run-artifact-badge")).toBeNull();
  230. expect(view.queryByTestId("workflow-run-artifact-source-path")).toBeNull();
  231. });
  232. it("点瓦片把产物 id 交给宿主(瓦片只发意图,scope 由上层补齐)", () => {
  233. const view = renderSection([artifact()]);
  234. fireEvent.click(view.getByTestId(TID_WORKFLOW_ARTIFACT_CARD));
  235. expect(onOpenArtifact).toHaveBeenCalledWith("book");
  236. });
  237. it("瓦片是一颗真按钮(键盘激活交给浏览器,不用自己接 keydown)", () => {
  238. const view = renderSection([artifact()]);
  239. const card = view.getByTestId(TID_WORKFLOW_ARTIFACT_CARD) as HTMLButtonElement;
  240. expect(card.tagName).toBe("BUTTON");
  241. expect(card.disabled).toBe(false);
  242. expect(card.getAttribute("data-artifact-open")).toBe("true");
  243. expect(card.getAttribute("aria-label")).toBe("打开产物: 审计报告");
  244. });
  245. it("宿主没注入打开能力时瓦片禁用,但内容照旧在场", () => {
  246. const view = renderSection([artifact()], { openable: false });
  247. const card = view.getByTestId(TID_WORKFLOW_ARTIFACT_CARD) as HTMLButtonElement;
  248. expect(card.disabled).toBe(true);
  249. expect(card.getAttribute("data-artifact-open")).toBeNull();
  250. expect(card.getAttribute("aria-label")).toBeNull();
  251. expect(within(card).getByText("审计报告")).toBeTruthy();
  252. fireEvent.click(card);
  253. expect(onOpenArtifact).not.toHaveBeenCalled();
  254. });
  255. it("内容产物的瓦片不去翻 journal 的 report 行;PDF 也不读字节(画不了就不拉)", async () => {
  256. renderSection([artifact()]);
  257. await act(async () => {});
  258. expect(workflowRunArtifactData).not.toHaveBeenCalled();
  259. expect(workflowRunArtifactRead).not.toHaveBeenCalled();
  260. });
  261. it("markdown 产物的预览框里是文档开头的缩放渲染,字节走产物 tab 同一条读取", async () => {
  262. workflowRunArtifactRead.mockResolvedValue({
  263. dataBase64: btoa("# Hello\n\nBody."),
  264. mediaType: "text/markdown",
  265. totalBytes: 15,
  266. nextOffset: null,
  267. });
  268. const view = renderSection([
  269. artifact({
  270. id: "notes",
  271. kind: "markdown",
  272. title: "说明",
  273. contentType: "text/markdown",
  274. bytes: 15,
  275. sourcePath: undefined,
  276. version: 1,
  277. }),
  278. ]);
  279. await waitFor(() => expect(view.getByTestId("workflow-artifact-preview-body")).toBeTruthy());
  280. const body = view.getByTestId("workflow-artifact-preview-body");
  281. expect(body.getAttribute("data-preview-mode")).toBe("markdown");
  282. expect(body.textContent).toContain("Hello");
  283. expect(workflowRunArtifactRead).toHaveBeenCalledWith(
  284. expect.objectContaining({ artifactId: "notes", version: 1, runId: "dwfrun-1" }),
  285. );
  286. });
  287. it("预置看板在预览框里实时渲染,并随 itemCount 变化带 afterSequence 增量续拉", async () => {
  288. workflowRunArtifactData
  289. .mockResolvedValueOnce({
  290. items: [
  291. { sequence: 1, siteId: "report#1", ordinal: 1, item: { round: 1, ms: 120 } },
  292. { sequence: 2, siteId: "report#1", ordinal: 2, item: { round: 2, ms: 90 } },
  293. ],
  294. hasMore: false,
  295. })
  296. .mockResolvedValueOnce({
  297. items: [{ sequence: 3, siteId: "report#1", ordinal: 3, item: { round: 3, ms: 80 } }],
  298. hasMore: false,
  299. });
  300. const board = (itemCount: number): WorkflowRunArtifactView =>
  301. artifact({
  302. id: "rounds",
  303. kind: "table",
  304. title: "每轮",
  305. bytes: undefined,
  306. sourcePath: undefined,
  307. contentType: undefined,
  308. version: 1,
  309. itemCount,
  310. spec: { columns: [{ field: "round" }, { field: "ms", unit: "ms" }] },
  311. });
  312. const view = renderSection([board(2)]);
  313. await waitFor(() => expect(view.getAllByTestId("artifact-table-row")).toHaveLength(2));
  314. // 只让 CLI 取 spec 点名的两列,而不是整条 item。
  315. expect(workflowRunArtifactData).toHaveBeenCalledWith({
  316. sessionId: "parent-a",
  317. runId: "dwfrun-1",
  318. artifactId: "rounds",
  319. limit: 200,
  320. fields: ["round", "ms"],
  321. });
  322. view.rerender(
  323. createElement(
  324. ZCodeIntlProvider,
  325. { initialLocale: "zh-CN" },
  326. createElement(WorkflowRunArtifactsSection, {
  327. artifacts: [board(3)],
  328. runId: "dwfrun-1",
  329. sessionId: "parent-a",
  330. onOpenArtifact,
  331. }),
  332. ),
  333. );
  334. await waitFor(() => expect(view.getAllByTestId("artifact-table-row")).toHaveLength(3));
  335. expect(workflowRunArtifactData).toHaveBeenLastCalledWith({
  336. sessionId: "parent-a",
  337. runId: "dwfrun-1",
  338. artifactId: "rounds",
  339. afterSequence: 2,
  340. limit: 200,
  341. fields: ["round", "ms"],
  342. });
  343. });
  344. it("spec 还没读回来时看板瓦片保持安静,不闪一句「无法渲染」", async () => {
  345. // 回归:spec 只有 journal 查询带得回来,把「还没到」与「坏了」合并成一句错误文案,
  346. // 结果是每个看板打开时都先闪一次红字。
  347. const view = renderSection([
  348. artifact({
  349. id: "perf",
  350. kind: "chart",
  351. bytes: undefined,
  352. sourcePath: undefined,
  353. contentType: undefined,
  354. version: 1,
  355. spec: undefined,
  356. }),
  357. ]);
  358. await act(async () => {});
  359. expect(view.queryByTestId("workflow-run-artifact-preset-invalid")).toBeNull();
  360. expect(view.queryByTestId("workflow-run-artifact-preset-missing")).toBeNull();
  361. // 说明行仍然是真的;预览框留白。
  362. expect(view.getByTestId(TID_WORKFLOW_ARTIFACT_CARD)).toBeTruthy();
  363. expect(view.getByTestId("workflow-artifact-preview-pending")).toBeTruthy();
  364. });
  365. it("spec 在场但解析不出来时,只有预览框降级成一句话,说明行照旧", async () => {
  366. const view = renderSection([
  367. artifact({
  368. id: "perf",
  369. kind: "chart",
  370. title: "坏看板",
  371. bytes: undefined,
  372. sourcePath: undefined,
  373. contentType: undefined,
  374. version: 1,
  375. spec: { nonsense: true },
  376. }),
  377. ]);
  378. await waitFor(() =>
  379. expect(view.getByTestId("workflow-run-artifact-preset-invalid")).toBeTruthy(),
  380. );
  381. expect(view.getByText("坏看板")).toBeTruthy();
  382. });
  383. });