|
1 | | -import { describe, expect, it } from "vitest"; |
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { resetProcessRegistryForTests } from "./bash-process-registry.test-support.js"; |
2 | 3 | import { |
3 | 4 | buildExecApprovalContinuationFallbackPrompt, |
4 | 5 | buildExecApprovalContinuationPrompt, |
5 | 6 | formatExecApprovalContinuationSourceOutput, |
6 | 7 | resizeExecApprovalContinuationPrompt, |
7 | 8 | } from "./bash-tools.exec-approval-output.js"; |
| 9 | +import { |
| 10 | + appendExecTimeoutRetryGuidance, |
| 11 | + renderExecOutputText, |
| 12 | + renderExecUpdateText, |
| 13 | +} from "./bash-tools.exec-output.js"; |
| 14 | +import { runExecProcess } from "./bash-tools.exec-runtime.js"; |
8 | 15 |
|
9 | 16 | const MAX_SOURCE_UTF16_UNITS = 256_000; |
10 | 17 | const MARKER = |
@@ -186,3 +193,98 @@ describe("resizeExecApprovalContinuationPrompt", () => { |
186 | 193 | expect(resized.split(OUTPUT_END)).toHaveLength(2); |
187 | 194 | }); |
188 | 195 | }); |
| 196 | + |
| 197 | +describe("exec output rendering", () => { |
| 198 | + it.each(["overall-timeout", "no-output-timeout"] as const)( |
| 199 | + "warns that %s may already have produced side effects", |
| 200 | + (exitReason) => { |
| 201 | + const text = appendExecTimeoutRetryGuidance("Command timed out.", exitReason); |
| 202 | + |
| 203 | + expect(text).toContain("external side effects may already have completed"); |
| 204 | + expect(text).toContain("Verify the resulting state before retrying"); |
| 205 | + expect(text).toContain("Do not automatically rerun non-idempotent commands"); |
| 206 | + expect(text).toContain("known to be safe to retry"); |
| 207 | + }, |
| 208 | + ); |
| 209 | + |
| 210 | + it("leaves non-timeout exits unchanged", () => { |
| 211 | + expect(appendExecTimeoutRetryGuidance("Command failed.", "signal")).toBe("Command failed."); |
| 212 | + }); |
| 213 | + |
| 214 | + it.each([ |
| 215 | + { name: "undefined input", input: undefined, expected: "(no output)" }, |
| 216 | + { name: "empty input", input: "", expected: "(no output)" }, |
| 217 | + { name: "non-empty input", input: "hello", expected: "hello" }, |
| 218 | + { name: "whitespace-only input", input: " ", expected: " " }, |
| 219 | + { name: "multiline input", input: "line1\nline2", expected: "line1\nline2" }, |
| 220 | + ])("renders $name", ({ input, expected }) => { |
| 221 | + expect(renderExecOutputText(input)).toBe(expected); |
| 222 | + }); |
| 223 | + |
| 224 | + it.each([ |
| 225 | + { name: "no output", input: { warnings: [] }, expected: "(no output)" }, |
| 226 | + { name: "tail output", input: { tailText: "hello", warnings: [] }, expected: "hello" }, |
| 227 | + { |
| 228 | + name: "warning without output", |
| 229 | + input: { warnings: ["warning1"] }, |
| 230 | + expected: "warning1\n\n(no output)", |
| 231 | + }, |
| 232 | + { |
| 233 | + name: "warning and output", |
| 234 | + input: { tailText: "hello", warnings: ["warning1"] }, |
| 235 | + expected: "warning1\n\nhello", |
| 236 | + }, |
| 237 | + { |
| 238 | + name: "multiple warnings", |
| 239 | + input: { tailText: "hello", warnings: ["warning1", "warning2"] }, |
| 240 | + expected: "warning1\nwarning2\n\nhello", |
| 241 | + }, |
| 242 | + { |
| 243 | + name: "explicit empty warnings", |
| 244 | + input: { tailText: "hello", warnings: [] }, |
| 245 | + expected: "hello", |
| 246 | + }, |
| 247 | + { |
| 248 | + name: "undefined tail with warnings", |
| 249 | + input: { tailText: undefined, warnings: ["warning1"] }, |
| 250 | + expected: "warning1\n\n(no output)", |
| 251 | + }, |
| 252 | + ])("renders updates with $name", ({ input, expected }) => { |
| 253 | + expect(renderExecUpdateText(input)).toBe(expected); |
| 254 | + }); |
| 255 | +}); |
| 256 | + |
| 257 | +describe("approved exec continuation producer", () => { |
| 258 | + afterEach(() => { |
| 259 | + resetProcessRegistryForTests(); |
| 260 | + }); |
| 261 | + |
| 262 | + it.runIf(process.platform !== "win32")( |
| 263 | + "preserves real multiline output beyond the legacy 16k boundary", |
| 264 | + async () => { |
| 265 | + const handle = await runExecProcess({ |
| 266 | + command: "/usr/bin/printf 'first line\\n\\tindented\\n\\n'; /usr/bin/printf '%017000d' 0", |
| 267 | + workdir: process.cwd(), |
| 268 | + env: { |
| 269 | + HOME: process.env.HOME ?? "/tmp", |
| 270 | + PATH: process.env.PATH ?? "/usr/bin:/bin", |
| 271 | + }, |
| 272 | + usePty: false, |
| 273 | + warnings: [], |
| 274 | + maxOutput: 200_000, |
| 275 | + pendingMaxOutput: 200_000, |
| 276 | + notifyOnExit: false, |
| 277 | + timeoutSec: 10, |
| 278 | + }); |
| 279 | + |
| 280 | + const outcome = await handle.promise; |
| 281 | + expect(outcome.status).toBe("completed"); |
| 282 | + const source = formatExecApprovalContinuationSourceOutput([ |
| 283 | + { label: "output", value: outcome.aggregated }, |
| 284 | + ]); |
| 285 | + expect(source).toContain("first line\n\tindented\n\n"); |
| 286 | + expect(source.length).toBeGreaterThan(16_000); |
| 287 | + expect(source).toBe(outcome.aggregated); |
| 288 | + }, |
| 289 | + ); |
| 290 | +}); |
0 commit comments