submitResultToolCallBlock.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // eslint-disable-next-line import/first -- 同上;词条只用于双语言存在性断言。
  28. import enUS from "../src/i18n/locales/en-US.js";
  29. // eslint-disable-next-line import/first -- 同上。
  30. import zhCN from "../src/i18n/locales/zh-CN.js";
  31. interface ToolCallOverrides {
  32. input?: unknown;
  33. output?: unknown;
  34. raw?: unknown;
  35. status?: string;
  36. toolName?: string;
  37. snapshotRefs?: readonly { field: string; previewBytes: number; fullBytes: number }[];
  38. }
  39. interface ContextOverrides {
  40. isRunning?: boolean;
  41. errorText?: string;
  42. forceOpen?: boolean;
  43. onLoadFullToolCallFields?: (toolId: string) => void;
  44. }
  45. function buildContext(toolCall: ToolCallOverrides, contextOverrides: ContextOverrides = {}) {
  46. return {
  47. toolCallNode: {
  48. toolCall: {
  49. toolId: "tool-submit-result",
  50. toolName: toolCall.toolName ?? "submit_result",
  51. kind: toolCall.toolName ?? "submit_result",
  52. title: toolCall.toolName ?? "submit_result",
  53. input: toolCall.input,
  54. output: toolCall.output,
  55. status: toolCall.status ?? "completed",
  56. raw: toolCall.raw ?? {},
  57. snapshotRefs: toolCall.snapshotRefs,
  58. },
  59. childToolCalls: [],
  60. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 测试上下文只填 renderer 读取的字段。
  61. } as any,
  62. workspacePath: "/workspace",
  63. displayModel: {
  64. inlinePreview: { type: "none" },
  65. planResult: null,
  66. viewerSource: null,
  67. viewerLabelId: "codeViewer.viewCode",
  68. showSummaryFileLink: false,
  69. showInput: false,
  70. showOutput: true,
  71. showKind: false,
  72. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 同上。
  73. } as any,
  74. viewerSource: null,
  75. rawFileSummaries: [],
  76. isRunning: contextOverrides.isRunning ?? false,
  77. statusLabel: "已执行",
  78. errorText: contextOverrides.errorText,
  79. childToolList: null,
  80. showIcon: true,
  81. canToggle: true,
  82. forceOpen: contextOverrides.forceOpen ?? true,
  83. onLoadFullToolCallFields: contextOverrides.onLoadFullToolCallFields,
  84. };
  85. }
  86. function renderBlock(
  87. toolCall: ToolCallOverrides,
  88. contextOverrides: ContextOverrides = {},
  89. locale: "en-US" | "zh-CN" = "zh-CN",
  90. ): string {
  91. return renderToStaticMarkup(
  92. createElement(
  93. ZCodeIntlProvider,
  94. { initialLocale: locale },
  95. createElement(SubmitResultToolCallBlock, buildContext(toolCall, contextOverrides)),
  96. ),
  97. );
  98. }
  99. const REJECTION_TEXT = [
  100. "The submitted result does not match the required schema:",
  101. "verdict: expected one of proved | disproved, got string",
  102. "steps[0].tactic: expected string, got number",
  103. ].join("\n");
  104. describe("submit_result 路由", () => {
  105. it("submit_result 是已知工具名,归 workflow family", () => {
  106. // wire 名就是 snake_case 的 `submit_result`——下划线必须字面在场。
  107. expect(normalizeZCodeToolName("submit_result")).toBe("submit_result");
  108. expect(getZCodeToolFamilyForName("submit_result")).toBe("workflow");
  109. expect(resolveToolCallIdentity({ toolName: "submit_result" })).toMatchObject({
  110. toolName: "submit_result",
  111. family: "workflow",
  112. isLegacy: false,
  113. });
  114. });
  115. it("workflow family 内按工具名分派,不误伤 CreateWorkflow", () => {
  116. expect(resolveToolCallRenderer(buildContext({}) as never)).toBe(SubmitResultToolCallBlock);
  117. expect(resolveToolCallRenderer(buildContext({ toolName: "CreateWorkflow" }) as never)).toBe(
  118. CreateWorkflowToolCallBlock,
  119. );
  120. });
  121. it("从 ToolCallBlock 入口整链走下来就是新卡,不再是 fallback 的裸 JSON dump", () => {
  122. // 主会话与 actor 侧板共用这条链路(侧板挂的是嵌套只读 SessionPane),所以这一条
  123. // 同时钉住两个面。
  124. const html = renderToStaticMarkup(
  125. createElement(
  126. ZCodeIntlProvider,
  127. { initialLocale: "zh-CN" },
  128. createElement(ToolCallBlock, {
  129. toolCallNode: {
  130. toolCall: {
  131. toolId: "submit-e2e",
  132. toolName: "submit_result",
  133. kind: "submit_result",
  134. title: "submit_result",
  135. input: { result: { verdict: "proved" } },
  136. output: { status: "accepted" },
  137. status: "completed",
  138. raw: {},
  139. },
  140. childToolCalls: [],
  141. },
  142. workspacePath: "/workspace",
  143. }),
  144. ),
  145. );
  146. expect(html).toContain("已提交结果");
  147. // fallback 的首字母大写标签与整对象 dump 都不该再出现。
  148. expect(html).not.toContain("Submit_result");
  149. expect(html).not.toContain(""toolId"");
  150. });
  151. });
  152. describe("SubmitResultToolCallBlock 相位文案", () => {
  153. it.each([
  154. { name: "completed", status: "completed", isRunning: false, expected: "已提交结果" },
  155. { name: "pending", status: "pending", isRunning: true, expected: "正在提交结果" },
  156. { name: "in_progress", status: "in_progress", isRunning: true, expected: "正在提交结果" },
  157. { name: "failed", status: "failed", isRunning: false, expected: "提交被驳回" },
  158. { name: "stopped", status: "stopped", isRunning: false, expected: "提交已停止" },
  159. ])("$name 相写「$expected」", ({ status, isRunning, expected }) => {
  160. const html = renderBlock({ input: { result: { verdict: "proved" } }, status }, { isRunning });
  161. expect(html).toContain(expected);
  162. });
  163. it("en-US 下相位文案同样本地化", () => {
  164. const html = renderBlock({ input: { result: { verdict: "proved" } } }, {}, "en-US");
  165. expect(html).toContain("Result submitted");
  166. expect(html).not.toContain("已提交结果");
  167. });
  168. });
  169. describe("SubmitResultToolCallBlock body 智能分流", () => {
  170. it("对象 result 走 JSON 代码块,且是缩进后的 JSON", () => {
  171. const html = renderBlock({
  172. input: { result: { verdict: "proved", steps: [{ tactic: "simp" }] } },
  173. output: { status: "accepted" },
  174. });
  175. expect(html).toContain('data-testid="code-block"');
  176. expect(html).toContain('data-language="json"');
  177. expect(html).toContain(""verdict": "proved"");
  178. });
  179. it("数组 result 同样走 JSON 分支", () => {
  180. const html = renderBlock({ input: { result: ["alpha", "beta"] } });
  181. expect(html).toContain('data-testid="code-block"');
  182. expect(html).toContain(""alpha"");
  183. });
  184. it("纯字符串 result 走正文 prose,不进代码块、不用 mono", () => {
  185. const prose = "根因在 scheduler 的 may-set 展开:lane 副本没有 actor 路由。";
  186. const html = renderBlock({ input: { result: prose } });
  187. expect(html).toContain(prose);
  188. expect(html).not.toContain('data-testid="code-block"');
  189. // DESIGN.md 把 mono 留给路径/命令/代码/标识符;人话是 prose。
  190. expect(html).not.toMatch(/font-mono[^>]*>根因在/u);
  191. expect(html).toContain("whitespace-pre-wrap");
  192. });
  193. it("JSON 字符串 result 被解开成对象后走代码块分支", () => {
  194. // 引擎侧记档过的实盘事实:真实模型常把 result 序列化成 JSON 字符串。
  195. const html = renderBlock({ input: { result: '{"verdict":"disproved","steps":[]}' } });
  196. expect(html).toContain('data-testid="code-block"');
  197. expect(html).toContain(""verdict": "disproved"");
  198. // 折叠头部的单行概要仍是压缩形态,但 body 里不能再多出一块 prose 面板。
  199. expect(html).not.toContain("rounded-lg border border-border bg-panel");
  200. });
  201. it("看着像 JSON 但解析不了的字符串保持 prose,不崩卡", () => {
  202. const broken = '{"verdict":"proved"';
  203. const html = renderBlock({ input: { result: broken } });
  204. expect(html).not.toContain('data-testid="code-block"');
  205. expect(html).toContain("{"verdict":"proved"");
  206. });
  207. it("数字 / 布尔 result 也有归宿(非字符串一律走 JSON 分支)", () => {
  208. expect(renderBlock({ input: { result: 42 } })).toContain('data-testid="code-block"');
  209. expect(renderBlock({ input: { result: false } })).toContain('data-testid="code-block"');
  210. });
  211. it("input 是 JSON 字符串时同样能读到 result", () => {
  212. const html = renderBlock({ input: JSON.stringify({ result: { verdict: "proved" } }) });
  213. expect(html).toContain('data-testid="code-block"');
  214. expect(html).toContain(""verdict"");
  215. });
  216. it("折叠头部给一行 result 概要", () => {
  217. const html = renderBlock(
  218. { input: { result: { verdict: "proved", steps: [{ tactic: "simp" }] } } },
  219. { forceOpen: false },
  220. );
  221. // 概要是单行压缩形态;折叠时 body 不渲染,缩进 JSON 不该出现。
  222. expect(html).toContain("{"verdict":"proved"");
  223. expect(html).not.toContain(""verdict": "proved"");
  224. expect(html).not.toContain('data-testid="code-block"');
  225. });
  226. });
  227. describe("SubmitResultToolCallBlock 流式门", () => {
  228. it("首帧还没有 result 键时不提供展开入口,也不渲染空面板", () => {
  229. const html = renderBlock(
  230. { input: {}, status: "pending", raw: { kind: "tool_input_start" } },
  231. { isRunning: true, forceOpen: true },
  232. );
  233. expect(html).not.toContain('role="button"');
  234. expect(html).not.toContain('data-testid="code-block"');
  235. expect(html).not.toContain("rounded-lg border border-border bg-panel");
  236. expect(html).toContain("正在提交结果");
  237. });
  238. it("input 完全缺席时同样不提供展开入口", () => {
  239. const html = renderBlock({ status: "pending" }, { isRunning: true });
  240. expect(html).not.toContain('role="button"');
  241. expect(html).not.toContain('data-testid="code-block"');
  242. });
  243. });
  244. describe("SubmitResultToolCallBlock 驳回态", () => {
  245. it("驳回态是扁平行:不给展开入口、不渲染 body,也不复述逐字段违规", () => {
  246. const html = renderBlock(
  247. { input: { result: { verdict: "yes" } }, status: "failed", output: REJECTION_TEXT },
  248. { errorText: REJECTION_TEXT },
  249. );
  250. expect(html).toContain("提交被驳回");
  251. // 折叠头部的单行 result 概要仍在——扁平行本身是一条概要行。
  252. expect(html).toContain("{"verdict":"yes"}");
  253. // 不可展开:没有 toggle 按钮、没有展开后的 result 代码块。
  254. expect(html).not.toContain('role="button"');
  255. expect(html).not.toContain('data-testid="code-block"');
  256. // 逐字段违规面板整体撤除:违规原文与小标题都不再出现在卡里(走失败态 tooltip)。
  257. expect(html).not.toContain("expected one of proved | disproved, got string");
  258. expect(html).not.toContain("不符合要求的字段");
  259. });
  260. it("驳回态没有任何错误文本时同样是扁平行", () => {
  261. const html = renderBlock({ input: { result: { verdict: "yes" } }, status: "failed" });
  262. expect(html).toContain("提交被驳回");
  263. expect(html).not.toContain('role="button"');
  264. expect(html).not.toContain('data-testid="code-block"');
  265. });
  266. });
  267. describe("SubmitResultToolCallBlock 快照字段提示", () => {
  268. it("卡后仍挂着快照字段提示", () => {
  269. const html = renderBlock(
  270. {
  271. input: { result: { verdict: "proved" } },
  272. snapshotRefs: [{ field: "input", previewBytes: 100, fullBytes: 5_000 }],
  273. },
  274. { onLoadFullToolCallFields: () => {} },
  275. );
  276. expect(html).toContain("加载完整工具数据");
  277. });
  278. });
  279. describe("submitResult 词条双语齐备", () => {
  280. it("四个相位 key 加一个小标题 key 在两种语言里都在场,且不是英文占位", () => {
  281. // 缺 key 会静默渲染裸 key(IntlProvider.tsx 的 messages[id] ?? id)。
  282. for (const key of [
  283. "chat.toolCall.submitResult.submitting",
  284. "chat.toolCall.submitResult.submitted",
  285. "chat.toolCall.submitResult.rejected",
  286. "chat.toolCall.submitResult.stopped",
  287. "chat.toolCall.submitResult.resultHeading",
  288. ]) {
  289. expect(enUS[key]).toBeTruthy();
  290. expect(zhCN[key]).toBeTruthy();
  291. expect(zhCN[key]).not.toBe(enUS[key]);
  292. }
  293. });
  294. });