workflowArtifactRow.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // @vitest-environment jsdom
  2. // 交付物行(docs/dynamic-workflow/transcript-and-notifications.md「Artifact tiles」)与它的两条呈现规则
  3. // (`resolvePrimaryArtifact` / `orderArtifactsPrimaryFirst`,docs/dynamic-workflow/authoring.md「Ids, tags and versions」)。
  4. // 行在完成卡与 run 侧板上的**落位**分别在 workflowCompletionCard.test.ts 与 workflowRunArtifactsSection.test.ts;
  5. // 这里只钉行自己的形态,与两条纯函数。
  6. import { cleanup, fireEvent, render } from "@testing-library/react";
  7. import { createElement } from "react";
  8. import { afterEach, describe, expect, it, vi } from "vitest";
  9. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  10. import {
  11. buildPresetLabels,
  12. orderArtifactsPrimaryFirst,
  13. resolvePrimaryArtifact,
  14. } from "@/app-shell/workflow-artifacts/artifactPresentation.js";
  15. import { WorkflowArtifactRow } from "@/components/workflow-timeline/WorkflowArtifactRow.js";
  16. import type { WorkflowCompletionArtifact } from "@/components/workflow-timeline/WorkflowArtifactTile.js";
  17. afterEach(() => {
  18. cleanup();
  19. });
  20. const REPORT: WorkflowCompletionArtifact = {
  21. id: "report",
  22. kind: "file",
  23. title: "季度基准报告",
  24. description: "对比十二个服务的 p50 / p99 延迟与上季度基线。",
  25. contentType: "application/pdf",
  26. sourcePath: "out/report.pdf",
  27. bytes: 4_194_304,
  28. version: 2,
  29. primary: true,
  30. };
  31. function renderRow(
  32. artifact: WorkflowCompletionArtifact,
  33. options: { locale?: "en-US" | "zh-CN"; onOpen?: (id: string) => void; title?: string } = {},
  34. ) {
  35. const labels = buildPresetLabels((descriptor) => descriptor.id.split(".").at(-1)!);
  36. return render(
  37. createElement(
  38. ZCodeIntlProvider,
  39. { initialLocale: options.locale ?? "en-US" },
  40. createElement(WorkflowArtifactRow, {
  41. artifact,
  42. labels,
  43. ...(options.onOpen === undefined ? {} : { onOpen: options.onOpen }),
  44. ...(options.title === undefined ? {} : { title: options.title }),
  45. }),
  46. ),
  47. );
  48. }
  49. describe("WorkflowArtifactRow", () => {
  50. it("一颗按钮:框 160 × 100 在左,右侧标题一级大、说明三行截断、`kind · badge · size` 行、尾槽 v2 与 ↗", () => {
  51. const onOpen = vi.fn();
  52. const view = renderRow(REPORT, { onOpen });
  53. const row = view.getByTestId("workflow-artifact-row") as HTMLButtonElement;
  54. expect(row.tagName).toBe("BUTTON");
  55. // 框与按钮是兄弟:markdown 缩略里的控件(表格的复制按钮)不能嵌进按钮;点击面由 ::after 铺满。
  56. const shell = row.parentElement!;
  57. expect(shell.className).toContain("grid-cols-[160px_minmax(0,1fr)]");
  58. expect(shell.className).toContain("@max-[380px]/wf-artifacts:grid-cols-[136px_minmax(0,1fr)]");
  59. expect(row.className).toContain("wf-tile-hit");
  60. const frame = view.getByTestId("workflow-artifact-row-frame");
  61. expect(frame.parentElement).toBe(shell);
  62. expect(frame.hasAttribute("inert")).toBe(true);
  63. expect(row.contains(frame)).toBe(false);
  64. expect(row.getAttribute("data-variant")).toBe("row");
  65. expect(row.getAttribute("title")).toBe("File · 季度基准报告");
  66. expect(view.getByTestId("workflow-artifact-row-title").className).toContain("font-medium");
  67. expect(view.getByTestId("workflow-artifact-row-description").className).toContain(
  68. "line-clamp-3",
  69. );
  70. const detail = view.getByTestId("workflow-artifact-row-detail");
  71. expect(detail.textContent).toBe("File·PDF·4.00 MB");
  72. expect(view.getByTestId("workflow-run-artifact-badge").textContent).toBe("PDF");
  73. expect(view.getByTestId("workflow-run-artifact-bytes").textContent).toBe("4.00 MB");
  74. expect(view.getByTestId("workflow-artifact-tile-version").textContent).toBe("v2");
  75. expect(view.getByTestId("workflow-artifact-tile-open")).toBeTruthy();
  76. fireEvent.click(row);
  77. expect(onOpen).toHaveBeenCalledWith("report");
  78. // 没有「primary」字样。
  79. expect(row.textContent).not.toMatch(/primary/iu);
  80. });
  81. it("没有回调:禁用、无 aria-label、无 ↗;说明与版本仍在;tooltip 可由宿主覆盖", () => {
  82. const view = renderRow(REPORT, { title: "File · 季度基准报告\nout/report.pdf" });
  83. const row = view.getByTestId("workflow-artifact-row") as HTMLButtonElement;
  84. expect(row.disabled).toBe(true);
  85. expect(row.getAttribute("aria-label")).toBeNull();
  86. expect(view.queryByTestId("workflow-artifact-tile-open")).toBeNull();
  87. expect(view.getByTestId("workflow-artifact-tile-version").textContent).toBe("v2");
  88. expect(row.getAttribute("title")).toBe("File · 季度基准报告\nout/report.pdf");
  89. });
  90. it("看板的细节是条数;说明缺席时那一行不画;v1 不写版本;zh-CN 的 kind 词", () => {
  91. const view = renderRow(
  92. { id: "perf", kind: "chart", title: "每轮耗时", version: 1, itemCount: 12 },
  93. { locale: "zh-CN" },
  94. );
  95. expect(view.queryByTestId("workflow-artifact-row-description")).toBeNull();
  96. expect(view.queryByTestId("workflow-artifact-tile-version")).toBeNull();
  97. expect(view.getByTestId("workflow-artifact-row-detail").textContent).toBe("图表·items");
  98. cleanup();
  99. // 字节未知、无条数:只剩 kind 词,没有点。
  100. const bare = renderRow({ id: "notes", kind: "markdown", version: 1 });
  101. expect(bare.getByTestId("workflow-artifact-row-detail").textContent).toBe("Document");
  102. });
  103. });
  104. describe("resolvePrimaryArtifact / orderArtifactsPrimaryFirst", () => {
  105. const a = { id: "a" };
  106. const b = { id: "b", primary: true as const };
  107. const c = { id: "c" };
  108. it("旗子优先,位置无关;没有旗子时只有单件才算交付物;两件以上无旗子 ⇒ 没有", () => {
  109. expect(resolvePrimaryArtifact([a, b, c])).toBe(b);
  110. expect(resolvePrimaryArtifact([a])).toBe(a);
  111. expect(resolvePrimaryArtifact([a, c])).toBeUndefined();
  112. expect(resolvePrimaryArtifact([])).toBeUndefined();
  113. });
  114. it("交付物带头,其余保持原顺序;没有旗子时原样复制(不按单件规则重排——只有一件也没什么可排)", () => {
  115. expect(orderArtifactsPrimaryFirst([a, b, c]).map((x) => x.id)).toEqual(["b", "a", "c"]);
  116. expect(orderArtifactsPrimaryFirst([b, a, c]).map((x) => x.id)).toEqual(["b", "a", "c"]);
  117. const plain = [a, c];
  118. const copy = orderArtifactsPrimaryFirst(plain);
  119. expect(copy).toEqual(plain);
  120. expect(copy).not.toBe(plain);
  121. });
  122. });