workflowCompletionLive.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // @vitest-environment jsdom
  2. // 完成卡的**活路径**(docs/dynamic-workflow/transcript-and-notifications.md「The card」「取数」):会话在场时,通知
  3. // 载荷(砍在 8 件)之上由活投影 / journal 补齐整份清单。
  4. //
  5. // Bug 原因(2026-09-14):产物多的 run 的完成卡写「还有 … 个」而不是数字。`artifactsTruncated` 是
  6. // **载荷**被砍的事实,卡却拿它决定「还有 N 个」的写法,而清单早已从 journal 补齐、数字明明可知。省略号
  7. // 只在载荷是唯一来源时才诚实:冷渲染、老 CLI、journal 还没答上来的那几帧。
  8. import { cleanup, fireEvent, render, waitFor } from "@testing-library/react";
  9. import { createElement, type ReactNode } from "react";
  10. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  11. import type { WorkflowRunArtifact } from "@zcode/shared/zcode-protocol-v4";
  12. vi.mock("@/logger.js", () => ({ logger: { debug: vi.fn(), warn: vi.fn() } }));
  13. vi.mock("@/ControlHintTooltip.js", () => ({
  14. ControlHintTooltip: ({ children }: { children?: ReactNode }) => children,
  15. }));
  16. const workflowRunArtifacts = vi.fn<() => Promise<{ artifacts: WorkflowRunArtifact[] }>>();
  17. const workflowRunArtifactData = vi.fn();
  18. const workflowRunArtifactRead = vi.fn();
  19. let hasConversation = true;
  20. vi.mock("@/v4/V4ConversationContext.js", () => ({
  21. useV4Conversation: () => ({ workflowRunArtifacts, workflowRunArtifactData, workflowRunArtifactRead }),
  22. useHasV4Conversation: () => hasConversation,
  23. }));
  24. // eslint-disable-next-line import/first -- 必须在 mock 之后再引入被测组件。
  25. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  26. // eslint-disable-next-line import/first
  27. import { ConversationWorkflowCompletion } from "@/v4/ConversationWorkflowCompletion.js";
  28. // eslint-disable-next-line import/first
  29. import type { ConversationRowRenderContext } from "@/v4/conversationRowContext.js";
  30. // eslint-disable-next-line import/first
  31. import type { WorkflowTurnCompletion } from "@/v4/workflowTurnCompletion.js";
  32. afterEach(() => {
  33. cleanup();
  34. });
  35. beforeEach(() => {
  36. hasConversation = true;
  37. // markdown 缩略的渲染器按 matchMedia 选代码块配色;jsdom 没有它。
  38. Object.defineProperty(window, "matchMedia", {
  39. configurable: true,
  40. writable: true,
  41. value: vi.fn(() => ({
  42. matches: false,
  43. addEventListener: vi.fn(),
  44. removeEventListener: vi.fn(),
  45. addListener: vi.fn(),
  46. removeListener: vi.fn(),
  47. })),
  48. });
  49. workflowRunArtifacts.mockReset();
  50. workflowRunArtifactData.mockReset();
  51. workflowRunArtifactData.mockResolvedValue({ items: [], hasMore: false });
  52. workflowRunArtifactRead.mockReset();
  53. workflowRunArtifactRead.mockResolvedValue({
  54. dataBase64: btoa("# note"),
  55. mediaType: "text/markdown",
  56. totalBytes: 6,
  57. nextOffset: null,
  58. });
  59. });
  60. /** 12 件产物:交付物 + 11 份笔记;载荷只带前 8 件(交付物在前)并置 artifactsTruncated。 */
  61. const IDS = Array.from({ length: 11 }, (_unused, index) => `note-${index + 1}`);
  62. const payloadArtifacts: WorkflowTurnCompletion["artifacts"] = [
  63. { id: "report", kind: "markdown", title: "审计报告", version: 1, primary: true, description: "结论" },
  64. ...IDS.slice(0, 7).map((id) => ({ id, kind: "markdown" as const, title: id, version: 1 })),
  65. ];
  66. const journalArtifacts: WorkflowRunArtifact[] = [
  67. {
  68. id: "report",
  69. kind: "markdown",
  70. title: "审计报告",
  71. description: "结论",
  72. version: 1,
  73. versions: [{ version: 1, publishedAt: 1, bytes: 6, primary: true }],
  74. itemCount: 0,
  75. primary: true,
  76. },
  77. ...IDS.map((id) => ({
  78. id,
  79. kind: "markdown" as const,
  80. title: id,
  81. version: 1,
  82. versions: [{ version: 1, publishedAt: 2, bytes: 6 }],
  83. itemCount: 0,
  84. })),
  85. ];
  86. function renderCompletion(
  87. truncated: boolean,
  88. options: {
  89. sessionId?: string | null;
  90. artifacts?: WorkflowTurnCompletion["artifacts"];
  91. onOpenWorkflowArtifact?: (request: unknown) => void;
  92. } = {},
  93. ) {
  94. const completion: WorkflowTurnCompletion = {
  95. runId: "dwfrun-1",
  96. name: "audit",
  97. durationMs: 1_000,
  98. artifacts: options.artifacts ?? payloadArtifacts,
  99. artifactsTruncated: truncated,
  100. summary: undefined,
  101. };
  102. const context = {
  103. theme: "system",
  104. sessionId: options.sessionId === undefined ? "session-1" : options.sessionId,
  105. onOpenWorkflowArtifact: options.onOpenWorkflowArtifact ?? (() => {}),
  106. } as unknown as ConversationRowRenderContext;
  107. return render(
  108. createElement(
  109. ZCodeIntlProvider,
  110. { initialLocale: "zh-CN" },
  111. createElement(ConversationWorkflowCompletion, { completion, context, turnKey: "t1" }),
  112. ),
  113. );
  114. }
  115. describe("ConversationWorkflowCompletion · 载荷砍过之后的 `+N`", () => {
  116. it("journal 答过之后写真实数字(12 件:行 + 5 行索引 + 「还有 6 个」),不再是省略号", async () => {
  117. workflowRunArtifacts.mockResolvedValue({ artifacts: journalArtifacts });
  118. const view = renderCompletion(true);
  119. await waitFor(() =>
  120. expect(view.getByTestId("workflow-completion-more").textContent).toBe("还有 6 个"),
  121. );
  122. expect(view.getAllByTestId("workflow-completion-line")).toHaveLength(5);
  123. expect(view.getByTestId("workflow-completion-card-t1").textContent).not.toContain("…");
  124. });
  125. it("老 CLI(查不到 journal):清单可能确实不全,省略号保留;journal 未答上来的首帧也是省略号", async () => {
  126. let resolve!: (value: { artifacts: WorkflowRunArtifact[] }) => void;
  127. workflowRunArtifacts.mockImplementation(
  128. () => new Promise<{ artifacts: WorkflowRunArtifact[] }>((r) => (resolve = r)),
  129. );
  130. const view = renderCompletion(true);
  131. expect(view.getByTestId("workflow-completion-more").textContent).toBe("还有 … 个");
  132. resolve({ artifacts: journalArtifacts });
  133. await waitFor(() =>
  134. expect(view.getByTestId("workflow-completion-more").textContent).toBe("还有 6 个"),
  135. );
  136. cleanup();
  137. workflowRunArtifacts.mockRejectedValue(new Error("fault.command.capabilityUnsupported"));
  138. const old = renderCompletion(true);
  139. await waitFor(() => expect(workflowRunArtifacts).toHaveBeenCalled());
  140. await new Promise((r) => setTimeout(r, 0));
  141. expect(old.getByTestId("workflow-completion-more").textContent).toBe("还有 … 个");
  142. });
  143. it("冷渲染(没有会话上下文)只有载荷:省略号是诚实的", () => {
  144. hasConversation = false;
  145. const view = renderCompletion(true);
  146. expect(view.getByTestId("workflow-completion-more").textContent).toBe("还有 … 个");
  147. expect(workflowRunArtifacts).not.toHaveBeenCalled();
  148. });
  149. it("没砍过的载荷:数字从一开始就对,journal 补齐后也不变", async () => {
  150. workflowRunArtifacts.mockResolvedValue({ artifacts: journalArtifacts.slice(0, 8) });
  151. const view = renderCompletion(false);
  152. expect(view.getByTestId("workflow-completion-more").textContent).toBe("还有 2 个");
  153. await waitFor(() => expect(workflowRunArtifacts).toHaveBeenCalled());
  154. expect(view.getByTestId("workflow-completion-more").textContent).toBe("还有 2 个");
  155. });
  156. });
  157. // 打开请求(docs/dynamic-workflow/authoring.md「How the user sees them」):宿主据 `contentType`
  158. // 把 html 产物直接开成浏览器 tab,据 `sourcePath` 定位工作区里的原文件。卡上的清单有两份——
  159. // 冷态只有通知载荷(没有出处),活路径下补齐过——请求必须跟着**当时画出来的那一份**走。
  160. describe("ConversationWorkflowCompletion · 交付物行交出去的打开请求", () => {
  161. const PAYLOAD: WorkflowTurnCompletion["artifacts"] = [
  162. { id: "report", kind: "file", title: "审计报告", version: 1, primary: true },
  163. ];
  164. it("journal 补齐后带上 contentType 与 sourcePath(载荷两个都没有)", async () => {
  165. workflowRunArtifacts.mockResolvedValue({
  166. artifacts: [
  167. {
  168. id: "report",
  169. kind: "file",
  170. title: "审计报告",
  171. version: 1,
  172. contentType: "text/html",
  173. sourcePath: "reports/audit.html",
  174. versions: [{ version: 1, publishedAt: 1, bytes: 6, primary: true }],
  175. itemCount: 0,
  176. primary: true,
  177. },
  178. ],
  179. });
  180. const onOpenWorkflowArtifact = vi.fn();
  181. const view = renderCompletion(false, { artifacts: PAYLOAD, onOpenWorkflowArtifact });
  182. await waitFor(() => expect(workflowRunArtifacts).toHaveBeenCalled());
  183. fireEvent.click(view.getByTestId("workflow-completion-row"));
  184. expect(onOpenWorkflowArtifact).toHaveBeenCalledWith({
  185. parentSessionId: "session-1",
  186. runId: "dwfrun-1",
  187. artifactId: "report",
  188. title: "审计报告",
  189. contentType: "text/html",
  190. sourcePath: "reports/audit.html",
  191. });
  192. });
  193. it("冷渲染只有载荷:contentType 照载荷交出,出处不编造", () => {
  194. hasConversation = false;
  195. const onOpenWorkflowArtifact = vi.fn();
  196. const view = renderCompletion(false, {
  197. artifacts: [{ ...PAYLOAD[0]!, contentType: "text/html" }],
  198. onOpenWorkflowArtifact,
  199. });
  200. fireEvent.click(view.getByTestId("workflow-completion-row"));
  201. expect(onOpenWorkflowArtifact).toHaveBeenCalledWith({
  202. parentSessionId: "session-1",
  203. runId: "dwfrun-1",
  204. artifactId: "report",
  205. title: "审计报告",
  206. contentType: "text/html",
  207. });
  208. });
  209. });