railyard/packages/cli/test/fixtures/fake-wrangler.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

66 lines
3.0 KiB
JavaScript
Executable File

#!/usr/bin/env node
// A stand-in for wrangler in tests: records its arguments and answers like
// wrangler 4 does for whoami, login, deploy and rollback.
import { appendFileSync, existsSync, readFileSync, writeFileSync } from "node:fs";
const [cmd, ...rest] = process.argv.slice(2);
const log = process.env.FAKE_WRANGLER_LOG;
if (log) appendFileSync(log, `${JSON.stringify({ cmd, rest, cwd: process.cwd() })}\n`);
const state = process.env.FAKE_WRANGLER_STATE;
const loggedIn = () => state && existsSync(state) && JSON.parse(readFileSync(state, "utf8")).loggedIn;
if (cmd === "whoami") {
if (loggedIn() || process.env.FAKE_WRANGLER_LOGGED_IN === "1") {
console.log("You are logged in with an OAuth Token, associated with the email founder@example.com.");
process.exit(0);
}
console.log("You are not authenticated. Please run `wrangler login`.");
process.exit(1);
}
if (cmd === "login") {
writeFileSync(state, JSON.stringify({ loggedIn: true }));
console.log("Successfully logged in.");
process.exit(0);
}
if (cmd === "deploy") {
if (process.env.FAKE_WRANGLER_FAIL === "1") {
console.error("X [ERROR] A request to the Cloudflare API failed.");
process.exit(1);
}
const name = rest[rest.indexOf("--name") + 1];
const n = Number(process.env.FAKE_WRANGLER_N ?? "1");
console.log(`Uploaded ${name} (1.02 sec)`);
console.log(`Deployed ${name} triggers (0.31 sec)`);
console.log(` https://${name}.founder.workers.dev`);
console.log(`Current Version ID: 0000000${n}-aaaa-4bbb-8ccc-dddddddddddd`);
process.exit(0);
}
if (cmd === "rollback") {
console.log(`Successfully rolled back to Worker Version: ${rest[0]}`);
console.log(`\nCurrent Version ID: ${rest[0]}`);
process.exit(0);
}
if (cmd === "tail") {
// Stands in for `wrangler tail`'s real behaviour: it streams until something
// kills it, it never exits on its own. Used to load-test `railyard logs`
// against a long-running stream (many lines, one carrying a secret to prove
// redaction still runs per-chunk) and against a Ctrl+C-style signal stop
// (FAKE_WRANGLER_TAIL_SELF_SIGTERM simulates the terminal sending SIGTERM to
// this child, the same way Ctrl+C would, without a second process to send it).
const n = Number(process.env.FAKE_WRANGLER_TAIL_LINES ?? "0");
const secret = process.env.FAKE_WRANGLER_TAIL_SECRET ?? "";
for (let i = 0; i < n; i += 1) {
// The secret rides on the very first line (whole in the first small write,
// nowhere near a pipe chunk boundary) so the redaction assertion below can't
// flake on the token happening to straddle two separate `data` events.
console.log(JSON.stringify({ line: i, message: i === 0 && secret ? `token ${secret} seen` : `log line ${i}` }));
}
if (process.env.FAKE_WRANGLER_TAIL_SELF_SIGTERM === "1") process.kill(process.pid, "SIGTERM");
else if (n > 0) process.exit(0);
else setInterval(() => {}, 1000); // hang like a real tail until the parent kills us
process.exitCode = 0;
} else {
console.error(`fake wrangler: unknown command ${cmd}`);
process.exit(1);
}