workflowNotificationArtifactChips.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // @vitest-environment jsdom
  2. // 终态通知行尾部的产物 chips 的**交互**(docs/dynamic-workflow/authoring.md「How the user sees them」)。
  3. //
  4. // chips 的在场性与「+N」在 v4ConversationTurnGroup.test.ts 里(那里是整条通知行的静态渲染面);
  5. // 点击与「不顺带把行展开」这两件事必须有真 DOM,所以单独一个 jsdom 文件。
  6. //
  7. // ⚠ 术语:chip 上的 artifact 是脚本经 `artifact.*` 交付给用户的产出,不是引擎内部那个
  8. // 「脚本顶层返回值」的同名词(spec 的「术语」表)。
  9. import { createElement } from "react";
  10. import { cleanup, fireEvent, render } from "@testing-library/react";
  11. import { beforeEach, describe, expect, it, vi } from "vitest";
  12. import { TID_CHAT_WORKFLOW_ARTIFACT_CHIP } from "@zcode/shared";
  13. import type { IPlatformService } from "@zcode/shared";
  14. import type { AssistantTextRow, TurnHeaderRow } from "@zcode/shared/zcode-protocol-v4";
  15. import type { WorkflowNotificationMeta } from "@zcode/shared/zcode-protocol-v4";
  16. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  17. useWorkspaceServices: () => ({}),
  18. }));
  19. // eslint-disable-next-line import/first -- 必须在 mock 之后再引入被测组件。
  20. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  21. // eslint-disable-next-line import/first
  22. import { TooltipProvider } from "@/components/ui/tooltip.js";
  23. // eslint-disable-next-line import/first
  24. import { PlatformProvider } from "@/hooks/usePlatform.js";
  25. // eslint-disable-next-line import/first
  26. import { WorkflowNotificationToolRow } from "@/v4/WorkflowNotificationToolRow.js";
  27. // eslint-disable-next-line import/first
  28. import { ConversationTurnGroup } from "@/v4/ConversationTurnGroup.js";
  29. // eslint-disable-next-line import/first
  30. import type { ConversationRowRenderContext } from "@/v4/conversationRowContext.js";
  31. // eslint-disable-next-line import/first
  32. import { buildConversationTurnRenderUnits } from "@/v4/conversationTurnRenderUnits.js";
  33. const onOpenArtifact = vi.fn();
  34. function terminal(
  35. artifacts: NonNullable<Extract<WorkflowNotificationMeta, { kind: "terminal" }>["artifacts"]>,
  36. ): WorkflowNotificationMeta {
  37. return {
  38. kind: "terminal",
  39. status: "completed",
  40. summary: "Workflow run completed.",
  41. durationMs: 1000,
  42. result: "done",
  43. resultForm: "prose",
  44. artifacts,
  45. };
  46. }
  47. // ToolLayout 的展开态按 `persistOpenKey` 记在模块级 Map 里,跨用例不清;每条用例给一个
  48. // 自己的 key,否则一条用例把行展开会让后面那条看不到折叠头部上的 chips。
  49. let rowSeq = 0;
  50. function renderRow(notification: WorkflowNotificationMeta, options: { openable?: boolean } = {}) {
  51. rowSeq += 1;
  52. return render(
  53. createElement(
  54. ZCodeIntlProvider,
  55. { initialLocale: "zh-CN" },
  56. createElement(
  57. TooltipProvider,
  58. null,
  59. createElement(WorkflowNotificationToolRow, {
  60. notification,
  61. runName: "flaky-test-triage",
  62. testIdKey: `unit-${rowSeq}`,
  63. theme: "system",
  64. ...(options.openable === false ? {} : { onOpenArtifact }),
  65. }),
  66. ),
  67. ),
  68. );
  69. }
  70. beforeEach(() => {
  71. cleanup();
  72. onOpenArtifact.mockClear();
  73. // 助手正文的代码块按 matchMedia 选配色;jsdom 没有它,整棵树会在渲染时炸掉。
  74. Object.defineProperty(window, "matchMedia", {
  75. configurable: true,
  76. writable: true,
  77. value: vi.fn(() => ({
  78. matches: false,
  79. addEventListener: vi.fn(),
  80. removeEventListener: vi.fn(),
  81. addListener: vi.fn(),
  82. removeListener: vi.fn(),
  83. })),
  84. });
  85. });
  86. describe("通知行的产物 chips", () => {
  87. it("点 chip 把产物 id 交给宿主", () => {
  88. const view = renderRow(
  89. terminal([
  90. { id: "book", kind: "file", title: "审计报告", version: 2 },
  91. { id: "perf", kind: "chart", title: "每轮耗时", version: 1 },
  92. ]),
  93. );
  94. const chips = view.getAllByTestId(TID_CHAT_WORKFLOW_ARTIFACT_CHIP);
  95. expect(chips).toHaveLength(2);
  96. fireEvent.click(chips[1]!);
  97. expect(onOpenArtifact).toHaveBeenCalledWith("perf");
  98. });
  99. it("点 chip 不顺带把通知行展开——头部整条是折叠开关,chip 的点击必须止步于自己", () => {
  100. const view = renderRow(terminal([{ id: "book", kind: "file", version: 1 }]));
  101. const trigger = view.container.querySelector("[aria-expanded]");
  102. expect(trigger?.getAttribute("aria-expanded")).toBe("false");
  103. fireEvent.click(view.getByTestId(TID_CHAT_WORKFLOW_ARTIFACT_CHIP));
  104. expect(onOpenArtifact).toHaveBeenCalledWith("book");
  105. expect(view.container.querySelector("[aria-expanded]")?.getAttribute("aria-expanded")).toBe(
  106. "false",
  107. );
  108. });
  109. it("title 缺席时 chip 退回 id(facade 的缺省 title 本来就是 id)", () => {
  110. const view = renderRow(terminal([{ id: "book", kind: "file", version: 1 }]));
  111. expect(view.getByTestId(TID_CHAT_WORKFLOW_ARTIFACT_CHIP).textContent).toContain("book");
  112. });
  113. it("长标题在 chip 上截断,hover 提示里仍是完整的 kind 词 + 标题", () => {
  114. const long = "这是一个非常非常非常长的产物标题,长到必须在 chip 上被截断掉一部分才摆得下";
  115. const view = renderRow(terminal([{ id: "book", kind: "file", title: long, version: 1 }]));
  116. const chip = view.getByTestId(TID_CHAT_WORKFLOW_ARTIFACT_CHIP);
  117. expect(chip.textContent?.endsWith("…")).toBe(true);
  118. expect(chip.getAttribute("title")).toBe(`文件 · ${long}`);
  119. });
  120. it("宿主没注入打开能力时 chip 不可点,点了也什么都不发生", () => {
  121. const view = renderRow(terminal([{ id: "book", kind: "file", version: 1 }]), {
  122. openable: false,
  123. });
  124. const chip = view.getByTestId(TID_CHAT_WORKFLOW_ARTIFACT_CHIP);
  125. expect(chip.hasAttribute("disabled")).toBe(true);
  126. fireEvent.click(chip);
  127. expect(onOpenArtifact).not.toHaveBeenCalled();
  128. });
  129. it("点**禁用的** chip 同样不展开行:禁用只是把打开变成空操作,不是把 chip 交还给开关", () => {
  130. // 回归:strip 之前只在 chip 自己的 onClick 里阻断冒泡,而禁用按钮上 React 不跑 onClick,
  131. // 于是那一下点击一路冒到折叠开关,把整行展开了。
  132. const view = renderRow(terminal([{ id: "book", kind: "file", version: 1 }]), {
  133. openable: false,
  134. });
  135. expect(view.container.querySelector("[aria-expanded]")?.getAttribute("aria-expanded")).toBe(
  136. "false",
  137. );
  138. fireEvent.click(view.getByTestId(TID_CHAT_WORKFLOW_ARTIFACT_CHIP));
  139. expect(view.container.querySelector("[aria-expanded]")?.getAttribute("aria-expanded")).toBe(
  140. "false",
  141. );
  142. expect(view.queryByTestId("workflow-notification-artifacts")).toBeTruthy();
  143. expect(onOpenArtifact).not.toHaveBeenCalled();
  144. });
  145. it("在 chip 上按 Enter 只打开产物,不顺带把行展开(键盘走的是开关的另一条通路)", () => {
  146. // 折叠开关是一个 role=button 的 div,它自己听 keydown 里的 Enter/Space;chip 上的按键
  147. // 会冒到它那里。strip 因此同时吞掉 click 与开合键。
  148. const view = renderRow(terminal([{ id: "book", kind: "file", version: 1 }]));
  149. fireEvent.keyDown(view.getByTestId(TID_CHAT_WORKFLOW_ARTIFACT_CHIP), { key: "Enter" });
  150. expect(view.container.querySelector("[aria-expanded]")?.getAttribute("aria-expanded")).toBe(
  151. "false",
  152. );
  153. });
  154. it("展开之后 chips 收起——展开体自己在说结果,chips 再挂着只是重复", () => {
  155. const view = renderRow(terminal([{ id: "book", kind: "file", version: 1 }]));
  156. expect(view.queryByTestId("workflow-notification-artifacts")).toBeTruthy();
  157. fireEvent.click(view.container.querySelector("[aria-expanded]")!);
  158. expect(view.queryByTestId("workflow-notification-artifacts")).toBeNull();
  159. });
  160. });
  161. // ── 接线(docs/dynamic-workflow/authoring.md「How the user sees them」)──
  162. // 上面钉的是 chip 把**产物 id** 交给上一层;这里钉上一层把 id 变成什么样的打开请求。
  163. // 载荷带最新版的 `contentType`,宿主据它把 html 产物直接开成浏览器 tab 而不是开产物 tab;
  164. // 载荷刻意不带 `sourcePath`(状态帧体积),所以请求里也不该凭空出现一个。
  165. describe("ConversationTurnGroup 把通知行的 chip 变成打开请求", () => {
  166. const noopPlatform = {} as IPlatformService;
  167. function notificationUnit(
  168. artifacts: NonNullable<Extract<WorkflowNotificationMeta, { kind: "terminal" }>["artifacts"]>,
  169. ) {
  170. const header: TurnHeaderRow = {
  171. rowId: 1,
  172. turnId: "turn-1",
  173. createdAt: 1_700_000_000_000,
  174. createdAtSeq: 1,
  175. kind: "turnHeader",
  176. origin: "backgroundResult",
  177. state: "completedSuccess",
  178. startedAt: 1_700_000_000_000,
  179. originMeta: {
  180. backgroundSource: "workflow",
  181. title: "flaky-test-triage",
  182. workId: "run-abc",
  183. workflowNotification: {
  184. kind: "terminal",
  185. status: "completed",
  186. summary: "Workflow run completed.",
  187. durationMs: 1000,
  188. artifacts,
  189. },
  190. },
  191. };
  192. const text: AssistantTextRow = {
  193. rowId: 2,
  194. entityId: "message-assistant-2",
  195. turnId: "turn-1",
  196. createdAt: 1_700_000_000_002,
  197. createdAtSeq: 2,
  198. kind: "assistantText",
  199. state: "complete",
  200. text: "后台总结最终段",
  201. };
  202. return buildConversationTurnRenderUnits([header, text])[0]!;
  203. }
  204. function renderTurn(
  205. artifacts: NonNullable<Extract<WorkflowNotificationMeta, { kind: "terminal" }>["artifacts"]>,
  206. onOpenWorkflowArtifact: (request: unknown) => void,
  207. ) {
  208. return render(
  209. createElement(
  210. ZCodeIntlProvider,
  211. { initialLocale: "zh-CN" },
  212. createElement(
  213. PlatformProvider,
  214. { platform: noopPlatform },
  215. createElement(
  216. TooltipProvider,
  217. null,
  218. createElement(ConversationTurnGroup, {
  219. unit: notificationUnit(artifacts),
  220. context: {
  221. workspacePath: "/workspace",
  222. theme: "system",
  223. sessionId: "parent-a",
  224. onOpenWorkflowArtifact,
  225. } as unknown as ConversationRowRenderContext,
  226. }),
  227. ),
  228. ),
  229. ),
  230. );
  231. }
  232. it("带上载荷里那一枚的 contentType(html 产物据它直接开浏览器 tab)", () => {
  233. const spy = vi.fn();
  234. const view = renderTurn(
  235. [
  236. { id: "book", kind: "file", title: "审计报告", version: 2, contentType: "text/html" },
  237. { id: "perf", kind: "chart", title: "每轮耗时", version: 1 },
  238. ],
  239. spy,
  240. );
  241. fireEvent.click(view.getAllByTestId(TID_CHAT_WORKFLOW_ARTIFACT_CHIP)[0]!);
  242. expect(spy).toHaveBeenCalledWith({
  243. parentSessionId: "parent-a",
  244. runId: "run-abc",
  245. artifactId: "book",
  246. contentType: "text/html",
  247. });
  248. });
  249. it("载荷上没有 contentType 的那一枚不编一个出来", () => {
  250. const spy = vi.fn();
  251. const view = renderTurn([{ id: "perf", kind: "chart", title: "每轮耗时", version: 1 }], spy);
  252. fireEvent.click(view.getByTestId(TID_CHAT_WORKFLOW_ARTIFACT_CHIP));
  253. expect(spy).toHaveBeenCalledWith({
  254. parentSessionId: "parent-a",
  255. runId: "run-abc",
  256. artifactId: "perf",
  257. });
  258. });
  259. });