bashOutputPreview.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 { toolCallRowToLegacyNode } from "../src/v4/toolCallRowAdapter.js";
  6. import type { ToolCallRow } from "@zcode/shared/zcode-protocol-v4";
  7. import { ExecuteToolCallBlock } from "../src/ToolCallBlocks/renderers/execute.js";
  8. describe("Bash bounded output preview", () => {
  9. it.each([false, true])(
  10. "renders V4 preview only inside expanded details (expanded=%s)",
  11. (expanded) => {
  12. const toolCallNode = toolCallRowToLegacyNode({
  13. kind: "toolCall",
  14. rowId: 1,
  15. turnId: "turn",
  16. createdAt: 1,
  17. createdAtSeq: 1,
  18. toolCallId: "preview-bash",
  19. toolName: "Bash",
  20. status: "running",
  21. inputText: "",
  22. input: { command: "build" },
  23. outputPreview: {
  24. text: "latest-line",
  25. fullText: "earlier-line\nlatest-line",
  26. totalLines: 1234,
  27. totalBytes: 40960,
  28. linesEstimated: true,
  29. },
  30. } as ToolCallRow);
  31. const html = renderToStaticMarkup(
  32. createElement(
  33. ZCodeIntlProvider,
  34. { initialLocale: "zh-CN" },
  35. createElement(ExecuteToolCallBlock, {
  36. toolCallNode,
  37. workspacePath: "/workspace",
  38. displayModel: {
  39. inlinePreview: { type: "none" },
  40. planResult: null,
  41. viewerSource: null,
  42. viewerLabelId: "codeViewer.viewCode",
  43. showSummaryFileLink: true,
  44. showInput: false,
  45. showOutput: true,
  46. showKind: false,
  47. },
  48. viewerSource: null,
  49. rawFileSummaries: [],
  50. isRunning: true,
  51. statusLabel: "Running",
  52. childToolList: null,
  53. forceOpen: expanded,
  54. }),
  55. ),
  56. );
  57. expect(html.includes("latest-line")).toBe(expanded);
  58. expect(html.includes("earlier-line")).toBe(expanded);
  59. expect(html.includes("bash-output-preview-full")).toBe(expanded);
  60. expect(html).not.toContain("bash-output-preview-short");
  61. expect(html).not.toContain("1,234");
  62. expect(html).not.toContain("40,960");
  63. expect(html).not.toContain("约");
  64. },
  65. );
  66. });
  67. describe("Bash final output content", () => {
  68. function renderResult(display: unknown, expanded = true, truncated?: ToolCallRow["output"]) {
  69. const row = {
  70. kind: "toolCall",
  71. rowId: 1,
  72. turnId: "turn",
  73. createdAt: 1,
  74. createdAtSeq: 1,
  75. toolCallId: "result-bash",
  76. toolName: "Bash",
  77. status: "success",
  78. inputText: "",
  79. input: { command: "build" },
  80. output: { text: "provider summary", display, ...truncated },
  81. } as ToolCallRow;
  82. return renderToStaticMarkup(
  83. createElement(
  84. ZCodeIntlProvider,
  85. { initialLocale: "zh-CN" },
  86. createElement(ExecuteToolCallBlock, {
  87. toolCallNode: toolCallRowToLegacyNode(row),
  88. workspacePath: "/workspace",
  89. displayModel: {
  90. inlinePreview: { type: "none" },
  91. planResult: null,
  92. viewerSource: null,
  93. viewerLabelId: "codeViewer.viewCode",
  94. showSummaryFileLink: true,
  95. showInput: false,
  96. showOutput: true,
  97. showKind: false,
  98. },
  99. viewerSource: null,
  100. rawFileSummaries: [],
  101. isRunning: false,
  102. statusLabel: "Completed",
  103. childToolList: null,
  104. forceOpen: expanded,
  105. }),
  106. ),
  107. );
  108. }
  109. it.each([false, true])(
  110. "shows only the output when expanded, without notices or a file link (%s)",
  111. (expanded) => {
  112. const html = renderResult(
  113. {
  114. kind: "bash_output",
  115. output: "head of output",
  116. truncated: true,
  117. outputPath: "/中文 空格/output.log",
  118. },
  119. expanded,
  120. );
  121. expect(html).not.toContain("仅显示部分输出");
  122. expect(html).not.toContain("/中文 空格/output.log");
  123. expect(html).not.toContain("bash-output-notice");
  124. expect(html).not.toContain("bash-output-file");
  125. expect(html).not.toContain("provider summary");
  126. expect(html.includes("head of output")).toBe(expanded);
  127. },
  128. );
  129. it("does not infer truncation from exactly 30,000 characters", () => {
  130. const html = renderResult({ kind: "bash_output", output: "x".repeat(30000), truncated: false });
  131. expect(html).not.toContain("bash-output-notice");
  132. expect(html).toContain("x".repeat(30000));
  133. });
  134. it("keeps only the output when a truncated file was cleaned up", () => {
  135. const html = renderResult({ kind: "bash_output", output: "head", truncated: true });
  136. expect(html).toContain("head");
  137. expect(html).not.toContain("仅显示部分输出");
  138. expect(html).not.toContain("完整输出");
  139. });
  140. it("keeps old output text without adding a notice or exposing the protocol truncation ref", () => {
  141. const html = renderResult(undefined, true, {
  142. text: "legacy head and tail",
  143. truncated: { totalBytes: 100000, ref: "tool-output/result-bash" },
  144. });
  145. expect(html).toContain("legacy head and tail");
  146. expect(html).not.toContain("仅显示部分输出");
  147. expect(html).not.toContain("tool-output/result-bash");
  148. });
  149. });