File size: 7,899 Bytes
3144483 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | import { describe, expect, it } from "vitest";
import { createCommandError, formatCommandOutput, formatCommandResult } from "./command-error.js";
import type { SpawnResult } from "./exec-result.js";
const failure: SpawnResult = {
stdout: "",
stderr: "",
code: 124,
signal: null,
killed: true,
termination: "timeout",
};
describe("createCommandError", () => {
it.each([
{ termination: "timeout", expected: "timed out after 3 seconds" },
{ termination: "no-output-timeout", expected: "timed out waiting for output" },
{ termination: "signal", expected: "terminated" },
] as const)("reports $termination without inventing a signal or Git advice", (entry) => {
const error = createCommandError(
"setup",
{ ...failure, termination: entry.termination, code: null },
{ timeoutMs: 3_000 },
);
expect(error.message).toBe(`setup failed (${entry.expected})`);
});
it("bounds labels and diagnostic tails without splitting surrogate pairs", () => {
const command = `\u001b[31m${"x".repeat(254)}\r\n🦞${"y".repeat(300)}\u001b[0m`;
const stderr = `${"x".repeat(100)}🦞${"y".repeat(1999)}`;
const error = createCommandError(command, { ...failure, stderr }, { timeoutMs: 120_000 });
const [label, detail] = error.message.split(" failed (timed out after 120 seconds):\n");
expect(label).toBe(`${"x".repeat(254)} `);
expect(detail).toBe(`…\n${"y".repeat(1999)}`);
});
});
it.each([false, true])("retains both diagnostics when outputs are buffers: %s", (buffered) => {
const text: SpawnResult = {
...failure,
code: 23,
killed: false,
termination: "exit",
stdout: "Setup cannot continue: create local-fixture-input.txt and retry.\n",
stderr: "Warning: optional fixture hint is unset.\n",
};
const result = buffered
? {
...text,
termination: "exit" as const,
stdout: Buffer.from(text.stdout),
stderr: Buffer.from(text.stderr),
}
: text;
const error = createCommandError("worktree setup", result, { timeoutMs: 120_000 });
expect(error.message).toContain("exit code 23");
expect(error.message).toContain("create local-fixture-input.txt and retry");
expect(error.message).toContain("optional fixture hint is unset");
expect(error.message).toContain("stderr:");
expect(error.message).toContain("stdout:");
expect(error.message).not.toContain("timed out");
});
it("retains complete uneven streams when they fit within the shared budget", () => {
const stdout = `recovery instruction: ${"x".repeat(1_200)}`;
const error = createCommandError(
"worktree setup",
{
...failure,
code: 23,
killed: false,
termination: "exit",
stdout,
stderr: "warning",
},
{ timeoutMs: 120_000 },
);
const detail = error.message.slice(error.message.indexOf(":\n") + 2);
expect(detail).toBe(`stderr: warning\nstdout: ${stdout}`);
expect(detail).not.toContain("…");
});
it("sanitizes terminal controls without flattening logical lines", () => {
const error = createCommandError(
"worktree setup",
{
...failure,
code: 23,
killed: false,
termination: "exit",
stdout: "recovery",
stderr: "\u001b[31mstale\rfinal\tfield\u0007\u007f\u0085\nnext\u001b[0m",
},
{ timeoutMs: 120_000 },
);
const detail = error.message.slice(error.message.indexOf(":\n") + 2);
expect(detail).toBe("stderr: final\\tfield\nnext\nstdout: recovery");
});
it("keeps independent recent stream tails within the existing error budget", () => {
const error = createCommandError(
"worktree setup",
{
...failure,
code: 23,
killed: false,
termination: "exit",
stdout: `${"old output\n".repeat(30)}${"x".repeat(3000)}\nstdout recovery 🦞`,
stderr: `${"old warning\n".repeat(30)}${"y".repeat(3000)}\nwarning detail 🦞`,
},
{ timeoutMs: 120_000 },
);
expect(error.message).toContain("stdout recovery 🦞");
expect(error.message).toContain("warning detail 🦞");
expect(error.message).not.toContain("old output");
expect(error.message).not.toContain("old warning");
expect(error.message).toContain(":\n");
const detail = error.message.slice(error.message.indexOf(":\n") + 2);
expect(detail).toMatch(/^stderr: …\n/u);
expect(detail).toContain("\nstdout: …\n");
expect(detail.length).toBeLessThanOrEqual(2_000);
expect(detail).not.toMatch(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/u);
expect(detail).not.toMatch(/(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/u);
expect(error.message.length).toBeLessThan(2300);
});
it.each([
{ stdout: "stdout recovery", stderr: "", present: "stdout recovery" },
{ stdout: "", stderr: "stderr recovery", present: "stderr recovery" },
{ stdout: "stdout recovery", stderr: "\u001b[31m\u001b[0m\r\n", present: "stdout recovery" },
])("keeps single visible output %#: $present", ({ stdout, stderr, present }) => {
const error = createCommandError(
"setup",
{
...failure,
code: 23,
killed: false,
termination: "exit",
stdout,
stderr,
},
{ timeoutMs: 120_000 },
);
const detail = error.message.slice(error.message.indexOf(":\n") + 2);
expect(detail).toBe(present);
expect(error.message).toContain("exit code 23");
});
// Buffered capture/launch failures carry an error termination, sometimes with an exit code.
it.each([23, null])("keeps buffered error exit metadata %#", (code) => {
const error = createCommandError(
"setup",
{
...failure,
code,
killed: false,
stdout: Buffer.alloc(0),
stderr: Buffer.alloc(0),
termination: "error",
},
{ timeoutMs: 3_000 },
);
expect(error.message).toBe(code === null ? "setup failed" : "setup failed (exit code 23)");
});
it.each([0, 1])("keeps zero and tiny output caps surrogate-safe %#", (maxChars) => {
expect(formatCommandOutput("🦞", maxChars)).toBe("…\n");
});
it.each([
{ stderr: " \tstale\r\tfinal\tfield\t \n", expected: "stderr: final\\tfield\nstdout: recovery" },
{ stderr: "\u0007\u007f\u0085", expected: "recovery" },
])("keeps trim and control-only normalization %#", ({ stderr, expected }) => {
const error = createCommandError(
"setup",
{
...failure,
code: 23,
killed: false,
termination: "exit",
stderr,
stdout: "recovery",
},
{ timeoutMs: 3_000 },
);
expect(error.message).toBe(`setup failed (exit code 23):\n${expected}`);
});
it("retains a short already-omitted tail alongside the other stream", () => {
const error = createCommandError(
"setup",
{
...failure,
code: 23,
killed: false,
termination: "exit",
stderr: Array.from({ length: 13 }, (_, index) => `note ${index}`).join("\n"),
stdout: "recovery",
},
{ timeoutMs: 3_000 },
);
expect(error.message).toContain("stderr: …\nnote 1\n");
expect(error.message).not.toContain("note 0\n");
expect(error.message).toContain("note 12\nstdout: recovery");
expect(error.message.match(/…/g)).toHaveLength(1);
});
it.each(["stdout", "stderr"] as const)(
"does not widen the result %s cap for a single stream",
(stream) => {
const result = formatCommandResult("command", {
...failure,
code: 23,
killed: false,
termination: "exit",
stdout: "",
stderr: "",
[stream]: "x".repeat(1_600),
});
expect(result).toContain(`${stream}:`);
expect(result).not.toContain("x".repeat(801));
expect(result).toContain("…");
},
);
it("keeps timeout ahead of an output-limit flag", () => {
const error = createCommandError(
"setup",
{
...failure,
code: null,
termination: "timeout",
outputLimitExceeded: true,
},
{ timeoutMs: 3_000 },
);
expect(error.message).toBe("setup failed (timed out after 3 seconds)");
});
|