// Auto-generated by scripts/gen_parity_tests.py from docs/runtime-parity.md.
// Module: node:child_process
// This file is a byte-for-byte parity test against `node --experimental-strip-types`.
// Edit the TODO lines below to exercise each API; rerun
// `./run_parity_tests.sh --filter test_parity_child_process` to verify.

import * as child_process from "node:child_process";
import { promisify } from "node:util";


// ── Functions ──
// Avoid printing pid, raw exit codes, or stderr — those vary per run / per host.
// Use deterministic POSIX-only commands.

// child_process.spawn(command[, args][, options])
try {
  const sp = child_process.spawn("/bin/echo", ["spawn-hello"]);
  console.log("spawn typeof:", typeof sp);
  console.log("spawn typeof stdout:", typeof sp.stdout);
  console.log("spawn typeof kill:", typeof sp.kill);
  await new Promise<void>((resolve) => {
    let buf = "";
    sp.stdout.on("data", (chunk: any) => { buf += chunk.toString(); });
    sp.on("exit", () => {
      console.log("spawn stdout:", buf.trim());
      resolve();
    });
  });
} catch (e) {
  console.log("spawn threw");
}

// child_process.exec(command[, options][, callback])
try {
  const out = await new Promise<string>((resolve, reject) => {
    child_process.exec("/bin/echo exec-hello", (err: any, stdout: any) => {
      if (err) reject(err); else resolve(String(stdout).trim());
    });
  });
  console.log("exec stdout:", out);
} catch (e) {
  console.log("exec threw");
}

// child_process.execFile(file[, args][, options][, callback])
try {
  const out = await new Promise<string>((resolve, reject) => {
    child_process.execFile("/bin/echo", ["execFile-hello"], (err: any, stdout: any) => {
      if (err) reject(err); else resolve(String(stdout).trim());
    });
  });
  console.log("execFile stdout:", out);
} catch (e) {
  console.log("execFile threw");
}

// PERRY-SKIP: child_process.fork(modulePath[, args][, options]) — spawns a Node IPC child; not portable to compiled-Perry harness.

// child_process.spawnSync(command[, args][, options])
try {
  const r = child_process.spawnSync("/bin/echo", ["spawnSync-hello"]);
  console.log("spawnSync stdout:", r.stdout.toString().trim());
} catch (e) {
  console.log("spawnSync threw");
}

// child_process.execSync(command[, options])
try {
  console.log("execSync stdout:", child_process.execSync("/bin/echo execSync-hello").toString().trim());
} catch (e) {
  console.log("execSync threw");
}

// child_process.execFileSync(file[, args][, options])
try {
  console.log("execFileSync stdout:", child_process.execFileSync("/bin/echo", ["execFileSync-hello"]).toString().trim());
} catch (e) {
  console.log("execFileSync threw");
}

// util.promisify(exec)
try {
  const execP = promisify(child_process.exec);
  console.log("promisify(exec) typeof:", typeof execP);
  const r = await execP("/bin/echo promisify-exec");
  console.log("promisify(exec) stdout:", String(r.stdout).trim());
} catch (e) {
  console.log("promisify(exec) threw");
}

// util.promisify(execFile)
try {
  const execFileP = promisify(child_process.execFile);
  console.log("promisify(execFile) typeof:", typeof execFileP);
  const r = await execFileP("/bin/echo", ["promisify-execFile"]);
  console.log("promisify(execFile) stdout:", String(r.stdout).trim());
} catch (e) {
  console.log("promisify(execFile) threw");
}

// ── Class: ChildProcess ──
// Probe shape only — pid / exitCode / signalCode vary per run.
try {
  const sp = child_process.spawn("/bin/echo", ["shape-probe"]);
  console.log("subprocess typeof channel:", typeof sp.channel);
  console.log("subprocess typeof connected:", typeof sp.connected);
  console.log("subprocess typeof exitCode:", typeof sp.exitCode);
  console.log("subprocess typeof killed:", typeof sp.killed);
  console.log("subprocess typeof pid:", typeof sp.pid);
  console.log("subprocess typeof signalCode:", typeof sp.signalCode);
  console.log("subprocess typeof spawnargs:", typeof sp.spawnargs);
  console.log("subprocess spawnargs isArray:", Array.isArray(sp.spawnargs));
  console.log("subprocess typeof spawnfile:", typeof sp.spawnfile);
  console.log("subprocess typeof stdin:", typeof sp.stdin);
  console.log("subprocess typeof stdout:", typeof sp.stdout);
  console.log("subprocess typeof stderr:", typeof sp.stderr);
  console.log("subprocess typeof stdio:", typeof sp.stdio);
  console.log("subprocess typeof uid:", typeof sp.uid);
  console.log("subprocess typeof gid:", typeof sp.gid);
  console.log("subprocess typeof kill:", typeof sp.kill);
  console.log("subprocess typeof send:", typeof sp.send);
  console.log("subprocess typeof disconnect:", typeof sp.disconnect);
  console.log("subprocess typeof ref:", typeof sp.ref);
  console.log("subprocess typeof unref:", typeof sp.unref);
  // PERRY-SKIP: subprocess[Symbol.dispose]() — symbol-key dispatch via well-known Symbol.dispose not yet uniform across Perry runtime + Node.
  await new Promise<void>((resolve) => { sp.on("exit", () => resolve()); });
  console.log("subprocess exit observed:", true);
} catch (e) {
  console.log("ChildProcess shape probe threw");
}

// ── Events on ChildProcess ──
// Probe handler registration only (handler-fired booleans are deterministic; pid / exit-code values are not).
try {
  const sp = child_process.spawn("/bin/echo", ["events"]);
  let sawSpawn = false;
  let sawExit = false;
  let sawClose = false;
  sp.on("spawn", () => { sawSpawn = true; });
  sp.on("error", () => { /* not expected for /bin/echo */ });
  sp.on("exit", () => { sawExit = true; });
  sp.on("close", () => { sawClose = true; });
  sp.on("disconnect", () => { /* only fires on IPC child */ });
  sp.on("message", () => { /* only fires on IPC child */ });
  await new Promise<void>((resolve) => { sp.on("close", () => resolve()); });
  console.log("event spawn fired:", sawSpawn);
  console.log("event exit fired:", sawExit);
  console.log("event close fired:", sawClose);
} catch (e) {
  console.log("event probe threw");
}

// ── Exports ──
console.log("child_process.ChildProcess:", child_process.ChildProcess);
console.log("child_process.Stream:", child_process.Stream);

// Coverage: 2 auto-emitted, 35 TODO, 0 skip-listed.
