railyard/packages/cli/test/run.test.mjs
caelum0x 2e9ca56c36 fix(cli): stop railyard logs from buffering an unbounded tail in memory
`run()` always appended every stdout/stderr chunk from a child process onto
accumulator strings, even for `railyard logs` (wraps `wrangler tail`), which
only ever reads the exit code and can stream for hours -- so a long session
held every log line it had ever printed in memory for no reason. Added a
`capture` option (default true, unchanged for deploy/rollback which do need
the text back) and set `capture: false` for logs.

Load-tested the fix for real: a new fake-wrangler `tail` fixture that
behaves like the real thing (streams until killed, never exits on its own),
a 4,000-line end-to-end `railyard logs` test (every line still streamed
live, saved token redacted, --format/--status forwarded correctly), a
Ctrl+C-style signal-stop test, and a direct run() test proving capture:false
keeps result.stdout/stderr empty across 20,000 printed lines while
capture:true is unchanged. 28/28 CLI tests passing (23 pre-existing + 5 new).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-24 04:53:23 +03:00

38 lines
1.8 KiB
JavaScript

// run()'s `capture` option: a long-running stream (railyard logs wraps `wrangler
// tail`, which can run for hours) must never buffer the text it prints into
// memory just because something eventually checks the exit code.
import { test } from "node:test";
import assert from "node:assert/strict";
import { run } from "../src/run.mjs";
const LINES = 20_000;
const printLines = `for (let i = 0; i < ${LINES}; i++) { process.stdout.write("line " + i + "\\n"); }`;
test("capture: true (the default) still returns the full output, as deploy/rollback need", async () => {
const seen = [];
const r = await run(process.execPath, ["-e", printLines], { env: process.env, echo: (s) => seen.push(s) });
assert.equal(r.code, 0);
assert.equal(r.stdout.split("\n").filter(Boolean).length, LINES);
assert.ok(seen.length > 0, "echo still fires with capture on");
});
test("capture: false streams the same output through `echo` but never buffers it", async () => {
const seen = [];
const r = await run(process.execPath, ["-e", printLines], { env: process.env, echo: (s) => seen.push(s), capture: false });
assert.equal(r.code, 0);
// The whole point: unlike the capture:true run above, stdout/stderr stay empty
// no matter how much the child prints, so a day-long `railyard logs` tail
// never grows these strings.
assert.equal(r.stdout, "");
assert.equal(r.stderr, "");
// But every line still reached the caller live -- nothing was silently dropped.
assert.equal(seen.join("").split("\n").filter(Boolean).length, LINES);
});
test("capture: false still reports a spawn error (node:child_process ENOENT), just without buffered text", async () => {
const r = await run("railyard-definitely-not-a-real-binary", [], { env: process.env, capture: false });
assert.equal(r.code, 127);
assert.match(r.stderr, /ENOENT/);
});