|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { formatExecApprovalContinuationOutput } from "./bash-tools.exec-approval-output.js"; |
| 3 | + |
| 4 | +// Pinned so a budget change is a deliberate edit rather than a silent drift. |
| 5 | +const MAX_UTF16_UNITS = 16_000; |
| 6 | +const MARKER = |
| 7 | + "[... truncated to fit the continuation budget; more output may have been dropped when it was captured ...]"; |
| 8 | + |
| 9 | +describe("formatExecApprovalContinuationOutput", () => { |
| 10 | + it("returns empty output when no stream carries content", () => { |
| 11 | + expect(formatExecApprovalContinuationOutput([])).toBe(""); |
| 12 | + expect( |
| 13 | + formatExecApprovalContinuationOutput([ |
| 14 | + { label: "stdout", value: "" }, |
| 15 | + { label: "stderr", value: undefined }, |
| 16 | + { label: "error", value: null }, |
| 17 | + ]), |
| 18 | + ).toBe(""); |
| 19 | + }); |
| 20 | + |
| 21 | + it("leaves a single stream verbatim and unlabelled", () => { |
| 22 | + const value = "line one\nline two\n"; |
| 23 | + expect( |
| 24 | + formatExecApprovalContinuationOutput([ |
| 25 | + { label: "stdout", value }, |
| 26 | + { label: "stderr", value: "" }, |
| 27 | + ]), |
| 28 | + ).toBe(value); |
| 29 | + }); |
| 30 | + |
| 31 | + it("preserves LF, CRLF, tabs, indentation, blank lines and trailing whitespace", () => { |
| 32 | + const value = "first\r\n\tindented\n\n spaced \ttrailing\t\n "; |
| 33 | + expect(formatExecApprovalContinuationOutput([{ label: "stdout", value }])).toBe(value); |
| 34 | + }); |
| 35 | + |
| 36 | + it("does not collapse whitespace the way the compact notify formatter does", () => { |
| 37 | + const value = "a\n\n\nb c"; |
| 38 | + const formatted = formatExecApprovalContinuationOutput([{ label: "stdout", value }]); |
| 39 | + expect(formatted).toBe(value); |
| 40 | + expect(formatted).not.toBe(value.replace(/\s+/g, " ").trim()); |
| 41 | + }); |
| 42 | + |
| 43 | + it("labels streams in deterministic order when several carry content", () => { |
| 44 | + expect( |
| 45 | + formatExecApprovalContinuationOutput([ |
| 46 | + { label: "stdout", value: "out\n" }, |
| 47 | + { label: "stderr", value: "err\n" }, |
| 48 | + { label: "error", value: "boom" }, |
| 49 | + ]), |
| 50 | + ).toBe("[stdout]\nout\n\n[stderr]\nerr\n\n[error]\nboom"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("keeps error-only output unlabelled", () => { |
| 54 | + expect( |
| 55 | + formatExecApprovalContinuationOutput([ |
| 56 | + { label: "stdout", value: "" }, |
| 57 | + { label: "stderr", value: "" }, |
| 58 | + { label: "error", value: "spawn failed" }, |
| 59 | + ]), |
| 60 | + ).toBe("spawn failed"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("is byte-identical at and below the cap", () => { |
| 64 | + const exact = "x".repeat(MAX_UTF16_UNITS); |
| 65 | + expect(formatExecApprovalContinuationOutput([{ label: "stdout", value: exact }])).toBe(exact); |
| 66 | + const under = "y".repeat(MAX_UTF16_UNITS - 1); |
| 67 | + expect(formatExecApprovalContinuationOutput([{ label: "stdout", value: under }])).toBe(under); |
| 68 | + }); |
| 69 | + |
| 70 | + it("keeps head and tail within the cap and marks the cut once", () => { |
| 71 | + const value = `${"a".repeat(30_000)}\n${"b".repeat(30_000)}`; |
| 72 | + const formatted = formatExecApprovalContinuationOutput([{ label: "stdout", value }]); |
| 73 | + expect(formatted.length).toBeLessThanOrEqual(MAX_UTF16_UNITS); |
| 74 | + expect(formatted.split(MARKER)).toHaveLength(2); |
| 75 | + expect(formatted.startsWith("a")).toBe(true); |
| 76 | + expect(formatted.endsWith("b")).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it("does not claim an exact omission count", () => { |
| 80 | + const formatted = formatExecApprovalContinuationOutput([ |
| 81 | + { label: "stdout", value: "z".repeat(50_000) }, |
| 82 | + ]); |
| 83 | + // Source-side capture can already have dropped output without a marker, so the |
| 84 | + // continuation must not imply this cut is the whole story. |
| 85 | + expect(formatted).toContain("may have been dropped when it was captured"); |
| 86 | + expect(formatted).not.toMatch(/\d+\s+(characters|units|chars)\s+omitted/); |
| 87 | + }); |
| 88 | + |
| 89 | + it("never splits a surrogate pair at either cut", () => { |
| 90 | + const value = "😀".repeat(20_000); |
| 91 | + const formatted = formatExecApprovalContinuationOutput([{ label: "stdout", value }]); |
| 92 | + expect(formatted.length).toBeLessThanOrEqual(MAX_UTF16_UNITS); |
| 93 | + for (const part of formatted.split(MARKER)) { |
| 94 | + expect(part).not.toMatch(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/); |
| 95 | + expect(part).not.toMatch(/(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + it("never emits a partial generated stream header", () => { |
| 100 | + // Sized so both the head cut and the tail cut land inside the "[stderr]" header. |
| 101 | + const headBudget = Math.floor((MAX_UTF16_UNITS - MARKER.length - 2) * 0.75); |
| 102 | + const formatted = formatExecApprovalContinuationOutput([ |
| 103 | + { label: "stdout", value: "s".repeat(headBudget - 3) }, |
| 104 | + { label: "stderr", value: "e".repeat(MAX_UTF16_UNITS) }, |
| 105 | + ]); |
| 106 | + expect(formatted.length).toBeLessThanOrEqual(MAX_UTF16_UNITS); |
| 107 | + for (const partial of ["[st\n", "[std\n", "[stde", "[stder", "[stderr\n"]) { |
| 108 | + expect(formatted).not.toContain(partial); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + it("keeps a source-side truncation marker visible when it fits", () => { |
| 113 | + const value = "head output\n... (truncated)\ntail output"; |
| 114 | + expect(formatExecApprovalContinuationOutput([{ label: "stdout", value }])).toContain( |
| 115 | + "... (truncated)", |
| 116 | + ); |
| 117 | + }); |
| 118 | +}); |
0 commit comments