bash-output-preview.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, expect, it } from "vitest";
  2. import { buildBashOutputPreview } from "../src/exec/bash-output-preview.js";
  3. describe("bounded Bash output preview", () => {
  4. it("keeps the last 5 and 100 lines of one tail, including trailing newlines", () => {
  5. const text = Array.from({ length: 120 }, (_, i) => `line-${i + 1}`).join("\n");
  6. const bytes = Buffer.byteLength(text);
  7. expect(buildBashOutputPreview(text, bytes, bytes, 0)).toEqual({
  8. text: Array.from({ length: 5 }, (_, i) => `line-${i + 116}`).join("\n"),
  9. fullText: Array.from({ length: 100 }, (_, i) => `line-${i + 21}`).join("\n"),
  10. totalBytes: bytes,
  11. totalLines: 120,
  12. linesEstimated: false,
  13. });
  14. expect(buildBashOutputPreview("a\nb\n", 4, 4, 0).totalLines).toBe(3);
  15. });
  16. it("preserves CRLF and leading/trailing empty lines", () => {
  17. const text = "\r\none\r\ntwo\r\n";
  18. expect(buildBashOutputPreview(text, 12, 12, 0)).toMatchObject({
  19. text,
  20. fullText: text,
  21. totalLines: 4,
  22. linesEstimated: false,
  23. });
  24. expect(buildBashOutputPreview("", 0, 100, 12)).toMatchObject({
  25. totalLines: 12,
  26. linesEstimated: false,
  27. });
  28. });
  29. it("estimates from raw bytes and never decreases an estimate", () => {
  30. expect(buildBashOutputPreview("中文\n", 5, 100, 0)).toMatchObject({
  31. totalLines: 40,
  32. linesEstimated: true,
  33. });
  34. expect(buildBashOutputPreview("one long line", 13, 100, 40).totalLines).toBe(40);
  35. });
  36. it("handles empty output and a long line without division by zero", () => {
  37. expect(buildBashOutputPreview("", 0, 0, 0)).toEqual({
  38. text: "",
  39. fullText: "",
  40. totalBytes: 0,
  41. totalLines: 0,
  42. linesEstimated: false,
  43. });
  44. expect(buildBashOutputPreview("x".repeat(4096), 4096, 8192, 0)).toMatchObject({
  45. totalLines: 2,
  46. linesEstimated: true,
  47. });
  48. });
  49. });