Skip to content

Local-first 🧠 memory for Hermes Agent that lives in your Obsidian vault and remembers project context. Nightly 😴 dream passes turn repeat corrections into confirmed preferences with measurable confidence. Adapters ship for Claude Code, Codex, and OpenClaw, with an MCP server for anything else.

TypeScript
3.9 KB
Raw Blame
/**
 * `readStdinText` drains piped stdin completely before the process exits.
 *
 * The bug it replaced was an exit, not a wrong value: on Windows Bun,
 * `await Bun.stdin.text()` did not keep the event loop alive, so a CLI
 * entry awaiting it exited 0 with nothing read. Only a real child process
 * with a real pipe reproduces that, so this spawns one, on every platform.
 */

import { describe, expect, setDefaultTimeout, test } from "bun:test";
import { spawnSync } from "node:child_process";
import { join, resolve } from "node:path";
import { CLI_SPAWN_BUDGET_MS } from "../helpers/cli-timeout.ts";

// Tests here spawn processes; see tests/helpers/cli-timeout.ts.
setDefaultTimeout(CLI_SPAWN_BUDGET_MS);

const STDIN_MODULE = resolve(join(import.meta.dir, "..", "..", "src", "cli", "stdin.ts"));

/** A child that awaits `readStdinText` through a promise chain, like the CLI entry. */
const CHILD = `
import { readStdinText } from ${JSON.stringify(STDIN_MODULE)};
async function main() {
  const text = await readStdinText();
  process.stdout.write(JSON.stringify({ length: text.length, head: text.slice(0, 16), tail: text.slice(-16) }));
}
main().then(() => process.exit(0));
`;

/**
 * A child that reads once and then checks the stream is free for the next
 * reader in the SAME process - the lock-release regression the last test
 * pins. Its stdin is the closed, empty pipe the runner hands it, so the
 * read completes immediately and deterministically.
 */
const CHILD_LOCK = `
import { readStdinText } from ${JSON.stringify(STDIN_MODULE)};
async function main() {
  await readStdinText();
  let released = true;
  try {
    process.stdin.resume();
    process.stdin.pause();
  } catch {
    released = false;
  }
  process.stdout.write(JSON.stringify({ released }));
}
main().then(() => process.exit(0));
`;

function run(input: string | Buffer, child: string = CHILD) {
  return spawnSync(process.execPath, ["-e", child], {
    input,
    encoding: "utf8",
    timeout: 30_000,
    windowsHide: true,
  });
}

describe("readStdinText", () => {
  test("returns the whole piped body, not an empty string", () => {
    const proc = run("hello from a pipe\n");
    expect(proc.status).toBe(0);
    expect(JSON.parse(proc.stdout)).toEqual({
      length: 18,
      head: "hello from a pip",
      tail: "hello from a pipe\n".slice(-16),
    });
  });

  test("a body larger than one pipe buffer arrives complete, as UTF-8", () => {
    const body = "é".repeat(200_000) + "END";
    const proc = run(body);
    expect(proc.status).toBe(0);
    const out = JSON.parse(proc.stdout) as { length: number; head: string; tail: string };
    expect(out.length).toBe(body.length);
    expect(out.tail.endsWith("END")).toBe(true);
    expect(out.head).toBe("é".repeat(16));
  });

  test("an empty pipe is an empty string", () => {
    const proc = run("");
    expect(proc.status).toBe(0);
    expect(JSON.parse(proc.stdout)).toEqual({ length: 0, head: "", tail: "" });
  });

  test("a completed read releases stdin for the next reader in the same process", () => {
    // The CLI reads stdin once per process, but a long-lived embedder
    // (the test runner among them) shares one stdin across files: an
    // earlier file's in-process readStdinText left Bun's stdin body
    // locked forever, so a later file's in-process y/N prompt
    // (readSingleLine's event-listener read) threw `ReadableStream is
    // locked` - and the suite's verdict depended on bun's file discovery
    // order. A completed read must release the lock it took; only the
    // lock is under test. The read runs in a child whose stdin is a
    // closed, empty pipe: reading the RUNNER's own stdin here would hang
    // a terminal `scripts/test` on the drain until EOF, the exact stall
    // this file exists to rule out.
    const proc = run("", CHILD_LOCK);
    expect(proc.status).toBe(0);
    expect(JSON.parse(proc.stdout)).toEqual({ released: true });
  });
});