generalModeToolRendering.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  5. import { ExecuteToolCallBlock } from "@/ToolCallBlocks/renderers/execute.js";
  6. import { ExecuteGroupToolCallBlock } from "@/ToolCallBlocks/renderers/execute-group.js";
  7. import { EditToolCallBlock } from "@/ToolCallBlocks/renderers/edit.js";
  8. import type { ToolCallBlockRenderContext } from "@/ToolCallBlocks/fileSummaryTypes.js";
  9. function context(general: boolean, failed = false): ToolCallBlockRenderContext {
  10. return {
  11. isOfficeMode: general,
  12. toolCallNode: {
  13. toolCall: {
  14. toolId: "mode-execute",
  15. kind: "execute",
  16. title: "MODE_PRIVATE_COMMAND",
  17. input: { command: "echo MODE_PRIVATE_COMMAND" },
  18. output: { stdout: "MODE_PRIVATE_OUTPUT", exitCode: failed ? 1 : 0 },
  19. status: failed ? "failed" : "completed",
  20. },
  21. childToolCalls: [],
  22. },
  23. workspacePath: "/workspace",
  24. displayModel: {
  25. inlinePreview: { type: "none" },
  26. planResult: null,
  27. viewerSource: null,
  28. viewerLabelId: "codeViewer.viewCode",
  29. showSummaryFileLink: true,
  30. showInput: false,
  31. showOutput: true,
  32. showKind: false,
  33. },
  34. viewerSource: null,
  35. rawFileSummaries: [],
  36. isRunning: false,
  37. statusLabel: failed ? "失败" : "已完成",
  38. errorText: failed ? "MODE_PRIVATE_ERROR" : undefined,
  39. childToolList: null,
  40. forceOpen: true,
  41. canToggle: true,
  42. };
  43. }
  44. function renderTool(component: typeof ExecuteToolCallBlock, props: ToolCallBlockRenderContext) {
  45. return renderToStaticMarkup(
  46. createElement(ZCodeIntlProvider, { initialLocale: "zh-CN" }, createElement(component, props)),
  47. );
  48. }
  49. describe("通用模式工具展示", () => {
  50. it.each([false, true])("执行详情隐藏但终态保留(failed=%s)", (failed) => {
  51. const props = context(true, failed);
  52. const original = JSON.stringify(props.toolCallNode);
  53. const general = renderTool(ExecuteToolCallBlock, props);
  54. expect(general).not.toContain("MODE_PRIVATE_COMMAND");
  55. expect(general).not.toContain("MODE_PRIVATE_OUTPUT");
  56. expect(general).not.toContain("MODE_PRIVATE_ERROR");
  57. expect(general).toContain(failed ? "失败" : "已运行命令");
  58. expect(general).not.toContain('aria-expanded="true"');
  59. const coding = renderTool(ExecuteToolCallBlock, { ...props, isOfficeMode: false });
  60. expect(coding).toContain("MODE_PRIVATE_COMMAND");
  61. expect(coding).toContain(failed ? "MODE_PRIVATE_ERROR" : "MODE_PRIVATE_OUTPUT");
  62. expect(JSON.stringify(props.toolCallNode)).toBe(original);
  63. });
  64. it("编辑保留文件入口但不渲染 Diff 正文", () => {
  65. const props = context(true);
  66. props.toolCallNode.toolCall.kind = "edit";
  67. props.rawFileSummaries = [
  68. {
  69. path: "/workspace/demo.py",
  70. filePath: "/workspace/",
  71. fileName: "demo.py",
  72. fileIconSrc: "/python.svg",
  73. actionLabel: "Edited",
  74. operationKind: "update",
  75. changeStat: { added: 1, removed: 1 },
  76. patch: "--- a/demo.py\n+++ b/demo.py\n@@ -1 +1 @@\n-old_value\n+MODE_NEW_VALUE",
  77. },
  78. ];
  79. props.onOpenCodeViewer = () => {};
  80. const html = renderTool(EditToolCallBlock, props);
  81. expect(html).toContain("demo.py");
  82. expect(html).not.toContain("MODE_NEW_VALUE");
  83. expect(html).not.toContain('aria-expanded="true"');
  84. expect(props.rawFileSummaries[0]?.patch).toContain("MODE_NEW_VALUE");
  85. });
  86. });
  87. it("运行中的执行组只展示状态,不泄露子命令或强制展开详情", () => {
  88. const props = context(true);
  89. props.isRunning = true;
  90. props.toolCallNode.childToolCalls = [
  91. {
  92. ...context(true).toolCallNode,
  93. toolCall: { ...context(true).toolCallNode.toolCall, status: "in_progress" },
  94. },
  95. ];
  96. const general = renderTool(ExecuteGroupToolCallBlock, props);
  97. expect(general).not.toContain("MODE_PRIVATE_COMMAND");
  98. expect(general).not.toContain("MODE_PRIVATE_OUTPUT");
  99. expect(general).not.toContain('aria-expanded="true"');
  100. });