bashOutputPreview.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. if (expanded) expect(html).toContain("whitespace-pre-wrap");
  65. },
  66. );
  67. });
  68. describe("Bash final output content", () => {
  69. function renderResult(display: unknown, expanded = true, truncated?: ToolCallRow["output"]) {
  70. const row = {
  71. kind: "toolCall",
  72. rowId: 1,
  73. turnId: "turn",
  74. createdAt: 1,
  75. createdAtSeq: 1,
  76. toolCallId: "result-bash",
  77. toolName: "Bash",
  78. status: "success",
  79. inputText: "",
  80. input: { command: "build" },
  81. output: { text: "provider summary", display, ...truncated },
  82. } as ToolCallRow;
  83. return renderToStaticMarkup(
  84. createElement(
  85. ZCodeIntlProvider,
  86. { initialLocale: "zh-CN" },
  87. createElement(ExecuteToolCallBlock, {
  88. toolCallNode: toolCallRowToLegacyNode(row),
  89. workspacePath: "/workspace",
  90. displayModel: {
  91. inlinePreview: { type: "none" },
  92. planResult: null,
  93. viewerSource: null,
  94. viewerLabelId: "codeViewer.viewCode",
  95. showSummaryFileLink: true,
  96. showInput: false,
  97. showOutput: true,
  98. showKind: false,
  99. },
  100. viewerSource: null,
  101. rawFileSummaries: [],
  102. isRunning: false,
  103. statusLabel: "Completed",
  104. childToolList: null,
  105. forceOpen: expanded,
  106. }),
  107. ),
  108. );
  109. }
  110. it.each([false, true])(
  111. "shows only the output when expanded, without notices or a file link (%s)",
  112. (expanded) => {
  113. const html = renderResult(
  114. {
  115. kind: "bash_output",
  116. output: "head of output",
  117. truncated: true,
  118. outputPath: "/中文 空格/output.log",
  119. },
  120. expanded,
  121. );
  122. expect(html).not.toContain("仅显示部分输出");
  123. expect(html).not.toContain("/中文 空格/output.log");
  124. expect(html).not.toContain("bash-output-notice");
  125. expect(html).not.toContain("bash-output-file");
  126. expect(html).not.toContain("provider summary");
  127. expect(html.includes("head of output")).toBe(expanded);
  128. },
  129. );
  130. it("does not infer truncation from exactly 30,000 characters", () => {
  131. const html = renderResult({ kind: "bash_output", output: "x".repeat(30000), truncated: false });
  132. expect(html).not.toContain("bash-output-notice");
  133. expect(html).toContain("x".repeat(30000));
  134. });
  135. it("keeps only the output when a truncated file was cleaned up", () => {
  136. const html = renderResult({ kind: "bash_output", output: "head", truncated: true });
  137. expect(html).toContain("head");
  138. expect(html).not.toContain("仅显示部分输出");
  139. expect(html).not.toContain("完整输出");
  140. });
  141. it("keeps old output text without adding a notice or exposing the protocol truncation ref", () => {
  142. const html = renderResult(undefined, true, {
  143. text: "legacy head and tail",
  144. truncated: { totalBytes: 100000, ref: "tool-output/result-bash" },
  145. });
  146. expect(html).toContain("legacy head and tail");
  147. expect(html).not.toContain("仅显示部分输出");
  148. expect(html).not.toContain("tool-output/result-bash");
  149. });
  150. });