bash-output-display.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { describe, expect, it } from "vitest";
  2. import { toolResultDisplayPayloadSchema } from "@zcode/contracts";
  3. import { createToolResultDisplay } from "../src/tool/executor/result-display.js";
  4. const base = {
  5. stdout: "head",
  6. stderr: "",
  7. interrupted: false,
  8. isImage: false,
  9. status: "completed",
  10. };
  11. describe("Bash result display", () => {
  12. it("preserves the bounded head, actual truncation and canonical file path", () => {
  13. const display = createToolResultDisplay("Bash", {
  14. ...base,
  15. stdout: "x".repeat(30000),
  16. stdoutTruncated: true,
  17. persistedOutputPath: "/中文 空格/output.log",
  18. });
  19. expect(display).toEqual({
  20. kind: "bash_output",
  21. output: "x".repeat(30000),
  22. truncated: true,
  23. outputPath: "/中文 空格/output.log",
  24. });
  25. expect(toolResultDisplayPayloadSchema.parse(display)).toEqual(display);
  26. });
  27. it("retains small-output and background/image rendering without introducing a display", () => {
  28. for (const output of [
  29. base,
  30. { ...base, stdout: "x".repeat(30000), stdoutTruncated: false },
  31. { ...base, status: "backgrounded", persistedOutputPath: "/file" },
  32. { ...base, isImage: true, persistedOutputPath: "/file" },
  33. ]) {
  34. expect(createToolResultDisplay("Bash", output)).toBeUndefined();
  35. }
  36. });
  37. it("shows truncation without promising a file after output-limit cleanup", () => {
  38. expect(
  39. createToolResultDisplay("Bash", { ...base, stdoutTruncated: true, stderr: "killed" }),
  40. ).toEqual({ kind: "bash_output", output: "head\nkilled", truncated: true });
  41. });
  42. it("bounds display independently of an oversized or multibyte handler result", () => {
  43. const display = createToolResultDisplay("Bash", {
  44. ...base,
  45. stdout: "中".repeat(60000),
  46. persistedOutputPath: "/file",
  47. });
  48. expect(display?.kind).toBe("bash_output");
  49. if (display?.kind !== "bash_output") throw new Error("missing display");
  50. expect(display.truncated).toBe(true);
  51. expect(Buffer.byteLength(display.output)).toBeLessThanOrEqual(150000);
  52. expect(display.output).not.toContain("�");
  53. });
  54. });