submitResultToolCallBlock.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // submit_result 工具卡(docs/chat/submit-result-tool-card.md)。
  2. //
  3. // 文件名刻意是 `.test.ts` 而不是 `.test.tsx`:根 vitest 配置只收
  4. // `packages/*/test/**/*.test.ts`(vitest.config.ts:68),`.test.tsx` 一个用例都不会被采集
  5. // (仓库里已有几个这样的死文件)。所以这里和其余 renderer 测试一样用 createElement,不写 JSX。
  6. import { createElement, type ReactNode } from "react";
  7. import { renderToStaticMarkup } from "react-dom/server";
  8. import { describe, expect, it, vi } from "vitest";
  9. import { getZCodeToolFamilyForName, normalizeZCodeToolName } from "@zcode/shared";
  10. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  11. import { resolveToolCallIdentity } from "@/lib/toolIdentity.js";
  12. // CodeBlock 依赖 shiki / CodeViewer / Tooltip 根 provider,静态渲染跑不动;照
  13. // createWorkflowToolCallBlock.test.ts 的先例替换为可断言的桩,顺带把 language 暴露出来,
  14. // 让「JSON 分支确实是 language=json 的代码块」可以被钉住。
  15. vi.mock("@/components/ai-elements/code-block.js", () => ({
  16. CodeBlock: ({ code, language }: { code: string; language?: string; children?: ReactNode }) =>
  17. createElement("pre", { "data-testid": "code-block", "data-language": language }, code),
  18. }));
  19. // eslint-disable-next-line import/first -- 必须在 code-block mock 之后再引入被测 renderer。
  20. import { SubmitResultToolCallBlock } from "../src/ToolCallBlocks/renderers/submit-result.js";
  21. // eslint-disable-next-line import/first -- 同上;路由断言要拿到 workflow family 的另一个 renderer。
  22. import { CreateWorkflowToolCallBlock } from "../src/ToolCallBlocks/renderers/create-workflow.js";
  23. // eslint-disable-next-line import/first -- 同上。
  24. import { resolveToolCallRenderer } from "../src/ToolCallBlocks/resolveRenderer.js";
  25. // eslint-disable-next-line import/first -- 同上;整链路由断言要走真实入口组件。
  26. import { ToolCallBlock } from "@/ToolCallBlocks.js";
  27. interface ToolCallOverrides {
  28. input?: unknown;
  29. output?: unknown;
  30. raw?: unknown;
  31. status?: string;
  32. toolName?: string;
  33. snapshotRefs?: readonly { field: string; previewBytes: number; fullBytes: number }[];
  34. }
  35. interface ContextOverrides {
  36. isRunning?: boolean;
  37. errorText?: string;
  38. forceOpen?: boolean;
  39. onLoadFullToolCallFields?: (toolId: string) => void;
  40. }
  41. function buildContext(toolCall: ToolCallOverrides, contextOverrides: ContextOverrides = {}) {
  42. return {
  43. toolCallNode: {
  44. toolCall: {
  45. toolId: "tool-submit-result",
  46. toolName: toolCall.toolName ?? "submit_result",
  47. kind: toolCall.toolName ?? "submit_result",
  48. title: toolCall.toolName ?? "submit_result",
  49. input: toolCall.input,
  50. output: toolCall.output,
  51. status: toolCall.status ?? "completed",
  52. raw: toolCall.raw ?? {},
  53. snapshotRefs: toolCall.snapshotRefs,
  54. },
  55. childToolCalls: [],
  56. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 测试上下文只填 renderer 读取的字段。
  57. } as any,
  58. workspacePath: "/workspace",
  59. displayModel: {
  60. inlinePreview: { type: "none" },
  61. planResult: null,
  62. viewerSource: null,
  63. viewerLabelId: "codeViewer.viewCode",
  64. showSummaryFileLink: false,
  65. showInput: false,
  66. showOutput: true,
  67. showKind: false,
  68. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 同上。
  69. } as any,
  70. viewerSource: null,
  71. rawFileSummaries: [],
  72. isRunning: contextOverrides.isRunning ?? false,
  73. statusLabel: "已执行",
  74. errorText: contextOverrides.errorText,
  75. childToolList: null,
  76. showIcon: true,
  77. canToggle: true,
  78. forceOpen: contextOverrides.forceOpen ?? true,
  79. onLoadFullToolCallFields: contextOverrides.onLoadFullToolCallFields,
  80. };
  81. }
  82. function renderBlock(
  83. toolCall: ToolCallOverrides,
  84. contextOverrides: ContextOverrides = {},
  85. locale: "en-US" | "zh-CN" = "zh-CN",
  86. ): string {
  87. return renderToStaticMarkup(
  88. createElement(
  89. ZCodeIntlProvider,
  90. { initialLocale: locale },
  91. createElement(SubmitResultToolCallBlock, buildContext(toolCall, contextOverrides)),
  92. ),
  93. );
  94. }
  95. const REJECTION_TEXT = [
  96. "The submitted result does not match the required schema:",
  97. "verdict: expected one of proved | disproved, got string",
  98. "steps[0].tactic: expected string, got number",
  99. ].join("\n");
  100. describe("submit_result 路由", () => {
  101. it("submit_result 是已知工具名,归 workflow family", () => {
  102. // wire 名就是 snake_case 的 `submit_result`——下划线必须字面在场。
  103. expect(normalizeZCodeToolName("submit_result")).toBe("submit_result");
  104. expect(getZCodeToolFamilyForName("submit_result")).toBe("workflow");
  105. expect(resolveToolCallIdentity({ toolName: "submit_result" })).toMatchObject({
  106. toolName: "submit_result",
  107. family: "workflow",
  108. isLegacy: false,
  109. });
  110. });
  111. it("workflow family 内按工具名分派,不误伤 CreateWorkflow", () => {
  112. expect(resolveToolCallRenderer(buildContext({}) as never)).toBe(SubmitResultToolCallBlock);
  113. expect(resolveToolCallRenderer(buildContext({ toolName: "CreateWorkflow" }) as never)).toBe(
  114. CreateWorkflowToolCallBlock,
  115. );
  116. });
  117. it("从 ToolCallBlock 入口整链走下来就是新卡,不再是 fallback 的裸 JSON dump", () => {
  118. // 主会话与 actor 侧板共用这条链路(侧板挂的是嵌套只读 SessionPane),所以这一条
  119. // 同时钉住两个面。
  120. const html = renderToStaticMarkup(
  121. createElement(
  122. ZCodeIntlProvider,
  123. { initialLocale: "zh-CN" },
  124. createElement(ToolCallBlock, {
  125. toolCallNode: {
  126. toolCall: {
  127. toolId: "submit-e2e",
  128. toolName: "submit_result",
  129. kind: "submit_result",
  130. title: "submit_result",
  131. input: { result: { verdict: "proved" } },
  132. output: { status: "accepted" },
  133. status: "completed",
  134. raw: {},
  135. },
  136. childToolCalls: [],
  137. },
  138. workspacePath: "/workspace",
  139. }),
  140. ),
  141. );
  142. expect(html).toContain("已提交结果");
  143. // fallback 的首字母大写标签与整对象 dump 都不该再出现。
  144. expect(html).not.toContain("Submit_result");
  145. expect(html).not.toContain(""toolId"");
  146. });
  147. });
  148. describe("SubmitResultToolCallBlock 相位文案", () => {
  149. it.each([
  150. { name: "completed", status: "completed", isRunning: false, expected: "已提交结果" },
  151. { name: "pending", status: "pending", isRunning: true, expected: "正在提交结果" },
  152. { name: "in_progress", status: "in_progress", isRunning: true, expected: "正在提交结果" },
  153. { name: "failed", status: "failed", isRunning: false, expected: "提交被驳回" },
  154. { name: "stopped", status: "stopped", isRunning: false, expected: "提交已停止" },
  155. ])("$name 相写「$expected」", ({ status, isRunning, expected }) => {
  156. const html = renderBlock({ input: { result: { verdict: "proved" } }, status }, { isRunning });
  157. expect(html).toContain(expected);
  158. });
  159. it("en-US 下相位文案同样本地化", () => {
  160. const html = renderBlock({ input: { result: { verdict: "proved" } } }, {}, "en-US");
  161. expect(html).toContain("Result submitted");
  162. expect(html).not.toContain("已提交结果");
  163. });
  164. });
  165. describe("SubmitResultToolCallBlock body 智能分流", () => {
  166. it("对象 result 走 JSON 代码块,且是缩进后的 JSON", () => {
  167. const html = renderBlock({
  168. input: { result: { verdict: "proved", steps: [{ tactic: "simp" }] } },
  169. output: { status: "accepted" },
  170. });
  171. expect(html).toContain('data-testid="code-block"');
  172. expect(html).toContain('data-language="json"');
  173. expect(html).toContain(""verdict": "proved"");
  174. });
  175. it("数组 result 同样走 JSON 分支", () => {
  176. const html = renderBlock({ input: { result: ["alpha", "beta"] } });
  177. expect(html).toContain('data-testid="code-block"');
  178. expect(html).toContain(""alpha"");
  179. });
  180. it("纯字符串 result 显示正文且不进代码块", () => {
  181. const prose = "根因在 scheduler 的 may-set 展开:lane 副本没有 actor 路由。";
  182. const html = renderBlock({ input: { result: prose } });
  183. expect(html).toContain(prose);
  184. expect(html).not.toContain('data-testid="code-block"');
  185. });
  186. it("JSON 字符串 result 被解开成对象后走代码块分支", () => {
  187. // 引擎侧记档过的实盘事实:真实模型常把 result 序列化成 JSON 字符串。
  188. const html = renderBlock({ input: { result: '{"verdict":"disproved","steps":[]}' } });
  189. expect(html).toContain('data-testid="code-block"');
  190. expect(html).toContain(""verdict": "disproved"");
  191. });
  192. it("看着像 JSON 但解析不了的字符串保持 prose,不崩卡", () => {
  193. const broken = '{"verdict":"proved"';
  194. const html = renderBlock({ input: { result: broken } });
  195. expect(html).not.toContain('data-testid="code-block"');
  196. expect(html).toContain("{"verdict":"proved"");
  197. });
  198. it("数字 / 布尔 result 也有归宿(非字符串一律走 JSON 分支)", () => {
  199. expect(renderBlock({ input: { result: 42 } })).toContain('data-testid="code-block"');
  200. expect(renderBlock({ input: { result: false } })).toContain('data-testid="code-block"');
  201. });
  202. it("input 是 JSON 字符串时同样能读到 result", () => {
  203. const html = renderBlock({ input: JSON.stringify({ result: { verdict: "proved" } }) });
  204. expect(html).toContain('data-testid="code-block"');
  205. expect(html).toContain(""verdict"");
  206. });
  207. it("折叠头部给一行 result 概要", () => {
  208. const html = renderBlock(
  209. { input: { result: { verdict: "proved", steps: [{ tactic: "simp" }] } } },
  210. { forceOpen: false },
  211. );
  212. // 概要是单行压缩形态;折叠时 body 不渲染,缩进 JSON 不该出现。
  213. expect(html).toContain("{"verdict":"proved"");
  214. expect(html).not.toContain(""verdict": "proved"");
  215. expect(html).not.toContain('data-testid="code-block"');
  216. });
  217. });
  218. describe("SubmitResultToolCallBlock 流式门", () => {
  219. it("首帧还没有 result 键时不提供展开入口,也不渲染空面板", () => {
  220. const html = renderBlock(
  221. { input: {}, status: "pending", raw: { kind: "tool_input_start" } },
  222. { isRunning: true, forceOpen: true },
  223. );
  224. expect(html).not.toContain('role="button"');
  225. expect(html).not.toContain('data-testid="code-block"');
  226. expect(html).toContain("正在提交结果");
  227. });
  228. it("input 完全缺席时同样不提供展开入口", () => {
  229. const html = renderBlock({ status: "pending" }, { isRunning: true });
  230. expect(html).not.toContain('role="button"');
  231. expect(html).not.toContain('data-testid="code-block"');
  232. });
  233. });
  234. describe("SubmitResultToolCallBlock 驳回态", () => {
  235. it("驳回态是扁平行:不给展开入口、不渲染 body,也不复述逐字段违规", () => {
  236. const html = renderBlock(
  237. { input: { result: { verdict: "yes" } }, status: "failed", output: REJECTION_TEXT },
  238. { errorText: REJECTION_TEXT },
  239. );
  240. expect(html).toContain("提交被驳回");
  241. // 折叠头部的单行 result 概要仍在——扁平行本身是一条概要行。
  242. expect(html).toContain("{"verdict":"yes"}");
  243. // 不可展开:没有 toggle 按钮、没有展开后的 result 代码块。
  244. expect(html).not.toContain('role="button"');
  245. expect(html).not.toContain('data-testid="code-block"');
  246. // 逐字段违规面板整体撤除:违规原文与小标题都不再出现在卡里(走失败态 tooltip)。
  247. expect(html).not.toContain("expected one of proved | disproved, got string");
  248. expect(html).not.toContain("不符合要求的字段");
  249. });
  250. it("驳回态没有任何错误文本时同样是扁平行", () => {
  251. const html = renderBlock({ input: { result: { verdict: "yes" } }, status: "failed" });
  252. expect(html).toContain("提交被驳回");
  253. expect(html).not.toContain('role="button"');
  254. expect(html).not.toContain('data-testid="code-block"');
  255. });
  256. });
  257. describe("SubmitResultToolCallBlock 快照字段提示", () => {
  258. it("卡后仍挂着快照字段提示", () => {
  259. const html = renderBlock(
  260. {
  261. input: { result: { verdict: "proved" } },
  262. snapshotRefs: [{ field: "input", previewBytes: 100, fullBytes: 5_000 }],
  263. },
  264. { onLoadFullToolCallFields: () => {} },
  265. );
  266. expect(html).toContain("加载完整工具数据");
  267. });
  268. });