|
| 1 | +// altimate_change - new file |
| 2 | +// `readRunStdin` on a real stdin: the other tests inject a fake source, so only these check that |
| 3 | +// `fstatSync(0)` sees a socket for a Node `spawn` and a FIFO for a shell pipeline. |
| 4 | +import { describe, expect, test } from "bun:test" |
| 5 | +import { spawn } from "node:child_process" |
| 6 | +import path from "node:path" |
| 7 | + |
| 8 | +const FIXTURE = path.join(import.meta.dir, "fixtures", "read-run-stdin.ts") |
| 9 | +const env = { ...process.env, ALTIMATE_RUN_STDIN_WAIT_MS: "200" } |
| 10 | + |
| 11 | +function collect(child: ReturnType<typeof spawn>): Promise<{ code: number | null; out: string; ms: number }> { |
| 12 | + const started = Date.now() |
| 13 | + let out = "" |
| 14 | + child.stdout!.on("data", (chunk) => (out += chunk)) |
| 15 | + return new Promise((resolve) => child.on("exit", (code) => resolve({ code, out, ms: Date.now() - started }))) |
| 16 | +} |
| 17 | + |
| 18 | +describe("readRunStdin on a real stdin", () => { |
| 19 | + test("a socket stdin that is never written or closed times out, and the process exits on its own", async () => { |
| 20 | + const child = spawn(process.execPath, [FIXTURE], { env }) |
| 21 | + const result = await collect(child) |
| 22 | + expect(result.code).toBe(0) |
| 23 | + expect(JSON.parse(result.out)).toEqual({ text: "", timedOut: true }) |
| 24 | + expect(result.ms).toBeLessThan(10_000) |
| 25 | + }) |
| 26 | + |
| 27 | + test("a socket stdin that is written and closed comes through", async () => { |
| 28 | + const child = spawn(process.execPath, [FIXTURE], { env }) |
| 29 | + child.stdin!.end("from the caller\n") |
| 30 | + expect(JSON.parse((await collect(child)).out)).toEqual({ text: "from the caller\n", timedOut: false }) |
| 31 | + }) |
| 32 | + |
| 33 | + test("with nothing else to run, a socket written after the wait still comes through", async () => { |
| 34 | + const child = spawn(process.execPath, [FIXTURE, "unbounded"], { env }) |
| 35 | + setTimeout(() => child.stdin!.end("the prompt\n"), 600) |
| 36 | + expect(JSON.parse((await collect(child)).out)).toEqual({ text: "the prompt\n", timedOut: false }) |
| 37 | + }) |
| 38 | + |
| 39 | + test("a shell pipeline is read to EOF even when its first byte comes after the wait", async () => { |
| 40 | + const child = spawn("sh", ["-c", `sleep 0.6; echo piped | "${process.execPath}" "${FIXTURE}"`], { env }) |
| 41 | + expect(JSON.parse((await collect(child)).out)).toEqual({ text: "piped\n", timedOut: false }) |
| 42 | + }) |
| 43 | +}) |
0 commit comments