dynamicWorkflowGrayResume.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // @vitest-environment jsdom
  2. // 灰度未命中时不留任何一条起引擎的路(docs/dynamic-workflow/launch.md「Gray release」DWG-07)。
  3. //
  4. // 供给点只有一个:SessionPane 把 Resume 回调放进会话上下文。工具卡页脚与轮尾摘要卡都以
  5. // 「回调在不在场」作为按钮门控,所以这里按真实的门(resolveWorkflowResumeHandler)算出回调,
  6. // 再把两处真的渲染出来看按钮在不在——而不是各自复述一遍规则。
  7. import { cleanup, render } from "@testing-library/react";
  8. import { createElement, type ReactNode } from "react";
  9. import { renderToStaticMarkup } from "react-dom/server";
  10. import { afterEach, describe, expect, it, vi } from "vitest";
  11. import type { WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  12. import type { WorkflowRunCardSummary } from "@/ToolCallBlocks/shared.js";
  13. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  14. import { resolveWorkflowResumeHandler } from "@/v4/workflowResumeGate.js";
  15. vi.mock("@/components/ai-elements/code-block.js", () => ({
  16. CodeBlockHeader: () => null,
  17. CodeBlock: ({ code }: { code: string; children?: ReactNode }) =>
  18. createElement("pre", { "data-testid": "code-block" }, code),
  19. }));
  20. // ControlHintTooltip 依赖应用根部的 TooltipProvider;测试里按既有惯例替换为透传桩。
  21. vi.mock("@/ControlHintTooltip.js", () => ({
  22. ControlHintTooltip: ({ children }: { children?: ReactNode }) => children,
  23. }));
  24. // eslint-disable-next-line import/first -- 必须在上面的 mock 之后再引入被测组件。
  25. import { CreateWorkflowToolCallBlock } from "@/ToolCallBlocks/renderers/create-workflow.js";
  26. // eslint-disable-next-line import/first -- 同上。
  27. import {
  28. WorkflowRunDigest,
  29. type WorkflowRunDigestProps,
  30. } from "@/components/workflow-timeline/WorkflowRunDigest.js";
  31. /** 会话上下文里那个回调:由真实的门算出来,`undefined` 就是「这条路没有了」。 */
  32. function resumeHandlerFor(dynamicWorkflowEnabled: boolean) {
  33. return resolveWorkflowResumeHandler({
  34. readOnly: false,
  35. dynamicWorkflowEnabled,
  36. handler: vi.fn(),
  37. });
  38. }
  39. const GRAPH = {
  40. steps: [{ id: "ask#1", kind: "ask", label: "plan", line: 2, column: 21, lane: "actor#1" }],
  41. lanes: [{ id: "actor#1", name: "planner", line: 1, column: 11 }],
  42. participants: [{ id: "unphased:actor#1", phase: "unphased", lane: "actor#1", steps: ["ask#1"] }],
  43. handoffs: [],
  44. } as const;
  45. /** 停下且可恢复:这是三处 Resume 都会出现的那一种 run。 */
  46. function stoppedResumableRun(): WorkflowRunState {
  47. return {
  48. runId: "dwfrun-42",
  49. toolCallId: "tool-create-workflow",
  50. status: "stopped",
  51. stopReason: "interrupted",
  52. usage: { spentTokens: 1_234, nodesUsed: 1 },
  53. actors: [],
  54. nodes: [],
  55. lastEventSequence: 7,
  56. } as unknown as WorkflowRunState;
  57. }
  58. function resumableSummary(): WorkflowRunCardSummary {
  59. const run = stoppedResumableRun();
  60. return {
  61. runId: run.runId,
  62. toolCallId: run.toolCallId,
  63. status: run.status,
  64. nodesSettled: 0,
  65. nodesTotal: 1,
  66. resumable: true,
  67. run,
  68. } as WorkflowRunCardSummary;
  69. }
  70. function renderCard(
  71. onResumeWorkflowRun: ((request: { workflowName?: string }) => void) | undefined,
  72. ) {
  73. return renderToStaticMarkup(
  74. createElement(
  75. ZCodeIntlProvider,
  76. { initialLocale: "zh-CN" },
  77. createElement(CreateWorkflowToolCallBlock, {
  78. toolCallNode: {
  79. toolCall: {
  80. toolId: "tool-create-workflow",
  81. kind: "CreateWorkflow",
  82. title: "CreateWorkflow",
  83. output: "",
  84. status: "completed",
  85. },
  86. childToolCalls: [],
  87. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 测试上下文只填 renderer 读取的字段。
  88. } as any,
  89. workspacePath: "/workspace",
  90. displayModel: {
  91. inlinePreview: { type: "none" },
  92. planResult: null,
  93. viewerSource: null,
  94. viewerLabelId: "codeViewer.viewCode",
  95. showSummaryFileLink: false,
  96. showInput: false,
  97. showOutput: true,
  98. showKind: false,
  99. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 同上。
  100. } as any,
  101. viewerSource: null,
  102. rawFileSummaries: [],
  103. isRunning: false,
  104. statusLabel: "已停止",
  105. childToolList: null,
  106. forceOpen: true,
  107. canToggle: true,
  108. showIcon: true,
  109. onResumeWorkflowRun,
  110. workflowRun: resumableSummary(),
  111. }),
  112. ),
  113. );
  114. }
  115. function renderDigest(onResume: (() => void) | undefined) {
  116. return render(
  117. createElement(
  118. ZCodeIntlProvider,
  119. { initialLocale: "zh-CN" },
  120. createElement(WorkflowRunDigest, {
  121. graph: GRAPH as unknown as WorkflowRunDigestProps["graph"],
  122. name: "implement-verify",
  123. runId: "dwfrun-42",
  124. testIdKey: "t",
  125. summary: resumableSummary(),
  126. ...(onResume === undefined ? {} : { onResume }),
  127. }),
  128. ),
  129. );
  130. }
  131. afterEach(() => {
  132. cleanup();
  133. });
  134. describe("resolveWorkflowResumeHandler", () => {
  135. it("灰度命中且可写 → 回调照常供给", () => {
  136. const handler = vi.fn();
  137. expect(
  138. resolveWorkflowResumeHandler({ readOnly: false, dynamicWorkflowEnabled: true, handler }),
  139. ).toBe(handler);
  140. // readOnly 缺省(undefined)等同于可写。
  141. expect(
  142. resolveWorkflowResumeHandler({ readOnly: undefined, dynamicWorkflowEnabled: true, handler }),
  143. ).toBe(handler);
  144. });
  145. it("灰度未命中 → 不供给;只读会话照旧不供给;两者都成立时也不供给", () => {
  146. const handler = vi.fn();
  147. expect(
  148. resolveWorkflowResumeHandler({ readOnly: false, dynamicWorkflowEnabled: false, handler }),
  149. ).toBeUndefined();
  150. expect(
  151. resolveWorkflowResumeHandler({ readOnly: true, dynamicWorkflowEnabled: true, handler }),
  152. ).toBeUndefined();
  153. expect(
  154. resolveWorkflowResumeHandler({ readOnly: true, dynamicWorkflowEnabled: false, handler }),
  155. ).toBeUndefined();
  156. });
  157. });
  158. describe("灰度关掉后工具卡页脚与摘要卡的 Resume", () => {
  159. it("工具卡页脚:灰度关 → 没有 Resume;灰度开 → 有", () => {
  160. expect(renderCard(resumeHandlerFor(false))).not.toContain("workflow-card-resume");
  161. expect(renderCard(resumeHandlerFor(true))).toContain("workflow-card-resume");
  162. });
  163. it("轮尾摘要卡:灰度关 → 没有 Resume;灰度开 → 有", () => {
  164. expect(
  165. renderDigest(resumeHandlerFor(false)).queryByTestId("workflow-digest-resume"),
  166. ).toBeNull();
  167. cleanup();
  168. expect(
  169. renderDigest(resumeHandlerFor(true)).queryByTestId("workflow-digest-resume"),
  170. ).not.toBeNull();
  171. });
  172. it("灰度关只收走 Resume:卡与摘要本身照常渲染", () => {
  173. // 「已有的 run 照常渲染」——种类词、状态这些都还在,只是按不动。
  174. expect(renderCard(resumeHandlerFor(false))).toContain("workflow-card-kind");
  175. expect(
  176. renderDigest(resumeHandlerFor(false)).queryByTestId("workflow-card-kind"),
  177. ).not.toBeNull();
  178. });
  179. });