// Auto-generated by scripts/gen_parity_tests.py from docs/runtime-parity.md.
// Module: node:stream
// 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_stream` to verify.

import * as stream from "node:stream";


// ── Functions ──
// Most module-level probes use `typeof` to keep output deterministic across
// implementations that may or may not have a given API. Behavioral probes are
// guarded by try/catch and report the error message on mismatch.

try {
  console.log("typeof stream.pipeline:", typeof stream.pipeline);
} catch (e) {
  console.log("typeof stream.pipeline ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.finished:", typeof stream.finished);
} catch (e) {
  console.log("typeof stream.finished ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.compose:", typeof stream.compose);
} catch (e) {
  console.log("typeof stream.compose ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.isReadable:", typeof stream.isReadable);
} catch (e) {
  console.log("typeof stream.isReadable ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.isWritable:", typeof stream.isWritable);
} catch (e) {
  console.log("typeof stream.isWritable ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.isErrored:", typeof stream.isErrored);
} catch (e) {
  console.log("typeof stream.isErrored ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.getDefaultHighWaterMark:", typeof stream.getDefaultHighWaterMark);
} catch (e) {
  console.log("typeof stream.getDefaultHighWaterMark ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.setDefaultHighWaterMark:", typeof stream.setDefaultHighWaterMark);
} catch (e) {
  console.log("typeof stream.setDefaultHighWaterMark ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.addAbortSignal:", typeof stream.addAbortSignal);
} catch (e) {
  console.log("typeof stream.addAbortSignal ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.duplexPair:", typeof stream.duplexPair);
} catch (e) {
  console.log("typeof stream.duplexPair ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Readable:", typeof stream.Readable);
} catch (e) {
  console.log("typeof stream.Readable ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Readable.from:", typeof (stream.Readable as any)?.from);
} catch (e) {
  console.log("typeof stream.Readable.from ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Readable.fromWeb:", typeof (stream.Readable as any)?.fromWeb);
} catch (e) {
  console.log("typeof stream.Readable.fromWeb ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Readable.toWeb:", typeof (stream.Readable as any)?.toWeb);
} catch (e) {
  console.log("typeof stream.Readable.toWeb ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Readable.isDisturbed:", typeof (stream.Readable as any)?.isDisturbed);
} catch (e) {
  console.log("typeof stream.Readable.isDisturbed ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Writable:", typeof stream.Writable);
} catch (e) {
  console.log("typeof stream.Writable ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Writable.fromWeb:", typeof (stream.Writable as any)?.fromWeb);
} catch (e) {
  console.log("typeof stream.Writable.fromWeb ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Writable.toWeb:", typeof (stream.Writable as any)?.toWeb);
} catch (e) {
  console.log("typeof stream.Writable.toWeb ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Duplex:", typeof stream.Duplex);
} catch (e) {
  console.log("typeof stream.Duplex ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Duplex.from:", typeof (stream.Duplex as any)?.from);
} catch (e) {
  console.log("typeof stream.Duplex.from ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Duplex.fromWeb:", typeof (stream.Duplex as any)?.fromWeb);
} catch (e) {
  console.log("typeof stream.Duplex.fromWeb ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Duplex.toWeb:", typeof (stream.Duplex as any)?.toWeb);
} catch (e) {
  console.log("typeof stream.Duplex.toWeb ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.Transform:", typeof stream.Transform);
} catch (e) {
  console.log("typeof stream.Transform ERROR:", (e as Error).message);
}
try {
  console.log("typeof stream.PassThrough:", typeof stream.PassThrough);
} catch (e) {
  console.log("typeof stream.PassThrough ERROR:", (e as Error).message);
}

// ── Behavioral probes — stable, deterministic outputs only ──

// getDefaultHighWaterMark — pure-data probe, deterministic.
try {
  console.log("getDefaultHighWaterMark(false):", stream.getDefaultHighWaterMark(false));
} catch (e) {
  console.log("getDefaultHighWaterMark(false) ERROR:", (e as Error).message);
}
try {
  console.log("getDefaultHighWaterMark(true):", stream.getDefaultHighWaterMark(true));
} catch (e) {
  console.log("getDefaultHighWaterMark(true) ERROR:", (e as Error).message);
}

// isReadable / isWritable / isErrored on a non-stream — should return null.
try {
  console.log("isReadable(non-stream):", stream.isReadable({} as any));
} catch (e) {
  console.log("isReadable(non-stream) ERROR:", (e as Error).message);
}
try {
  console.log("isWritable(non-stream):", stream.isWritable({} as any));
} catch (e) {
  console.log("isWritable(non-stream) ERROR:", (e as Error).message);
}
try {
  console.log("isErrored(non-stream):", stream.isErrored({} as any));
} catch (e) {
  console.log("isErrored(non-stream) ERROR:", (e as Error).message);
}

// ── Classes — stream.Readable ──
try {
  const r = stream.Readable.from(["a", "b", "c"]);
  console.log("Readable.from typeof:", typeof r);
  console.log("Readable.from readable:", r.readable);
  console.log("Readable.from destroyed:", r.destroyed);
  const arr = await r.toArray();
  console.log("Readable.from values:", arr);
} catch (e) {
  console.log("Readable.from ERROR:", (e as Error).message);
}

// readable.read — Readable.from is in object mode, so .read() returns items.
try {
  const r = stream.Readable.from([10, 20, 30]);
  // Give it a tick to be in flowing/paused state consistently.
  await new Promise<void>(res => setImmediate(res));
  const v = r.read();
  console.log("readable.read typeof:", typeof v);
  r.destroy();
} catch (e) {
  console.log("readable.read ERROR:", (e as Error).message);
}

// readable.pause / resume / isPaused
try {
  const r = stream.Readable.from(["x"]);
  r.pause();
  console.log("readable.isPaused after pause:", r.isPaused());
  r.resume();
  console.log("readable.isPaused after resume:", r.isPaused());
  r.destroy();
} catch (e) {
  console.log("readable.pause/resume ERROR:", (e as Error).message);
}

// readable.pipe / unpipe
try {
  const r = stream.Readable.from(["one", "two"]);
  const chunks: string[] = [];
  const w = new stream.Writable({
    write(c: any, _e: any, cb: any) { chunks.push(String(c)); cb(); }
  });
  r.pipe(w);
  await new Promise<void>(res => w.on("finish", res));
  console.log("readable.pipe chunks:", chunks.join(","));
} catch (e) {
  console.log("readable.pipe ERROR:", (e as Error).message);
}

try {
  const r = stream.Readable.from(["k"]);
  const w = new stream.Writable({ write(_c: any, _e: any, cb: any) { cb(); } });
  r.pipe(w);
  r.unpipe(w);
  console.log("readable.unpipe: ok");
  r.destroy();
  w.destroy();
} catch (e) {
  console.log("readable.unpipe ERROR:", (e as Error).message);
}

// readable.unshift
try {
  const r = stream.Readable.from(["a"]);
  r.pause();
  r.unshift("z");
  console.log("readable.unshift: ok");
  r.destroy();
} catch (e) {
  console.log("readable.unshift ERROR:", (e as Error).message);
}

// readable.setEncoding
try {
  const r = stream.Readable.from(["a"]);
  r.setEncoding("utf8");
  console.log("readable.setEncoding: ok");
  r.destroy();
} catch (e) {
  console.log("readable.setEncoding ERROR:", (e as Error).message);
}

// readable.destroy
try {
  const r = stream.Readable.from(["a"]);
  r.destroy();
  console.log("readable.destroy destroyed:", r.destroyed);
} catch (e) {
  console.log("readable.destroy ERROR:", (e as Error).message);
}

// readable.wrap — accepts an old-style stream; probe type.
try {
  const r = stream.Readable.from(["a"]);
  console.log("typeof readable.wrap:", typeof r.wrap);
  r.destroy();
} catch (e) {
  console.log("readable.wrap ERROR:", (e as Error).message);
}

// readable.compose
try {
  const r = stream.Readable.from(["a"]);
  console.log("typeof readable.compose:", typeof r.compose);
  r.destroy();
} catch (e) {
  console.log("readable.compose ERROR:", (e as Error).message);
}

// readable.iterator — returns an async iterator.
try {
  const r = stream.Readable.from(["a", "b"]);
  const it = r.iterator();
  console.log("typeof readable.iterator():", typeof it);
  r.destroy();
} catch (e) {
  console.log("readable.iterator ERROR:", (e as Error).message);
}

// readable.map
try {
  const r = stream.Readable.from([1, 2, 3]);
  const out: any[] = [];
  for await (const v of r.map((x: any) => x * 10)) out.push(v);
  console.log("readable.map values:", out);
} catch (e) {
  console.log("readable.map ERROR:", (e as Error).message);
}

// readable.filter
try {
  const r = stream.Readable.from([1, 2, 3, 4]);
  const out: any[] = [];
  for await (const v of r.filter((x: any) => x % 2 === 0)) out.push(v);
  console.log("readable.filter values:", out);
} catch (e) {
  console.log("readable.filter ERROR:", (e as Error).message);
}

// readable.forEach
try {
  const r = stream.Readable.from(["a", "b"]);
  const out: any[] = [];
  await r.forEach((v: any) => { out.push(v); });
  console.log("readable.forEach values:", out);
} catch (e) {
  console.log("readable.forEach ERROR:", (e as Error).message);
}

// readable.toArray
try {
  const r = stream.Readable.from([1, 2, 3]);
  const arr = await r.toArray();
  console.log("readable.toArray values:", arr);
} catch (e) {
  console.log("readable.toArray ERROR:", (e as Error).message);
}

// readable.some
try {
  const r = stream.Readable.from([1, 2, 3]);
  const v = await r.some((x: any) => x === 2);
  console.log("readable.some(==2):", v);
} catch (e) {
  console.log("readable.some ERROR:", (e as Error).message);
}

// readable.find
try {
  const r = stream.Readable.from([1, 2, 3]);
  const v = await r.find((x: any) => x === 3);
  console.log("readable.find(==3):", v);
} catch (e) {
  console.log("readable.find ERROR:", (e as Error).message);
}

// readable.every
try {
  const r = stream.Readable.from([2, 4, 6]);
  const v = await r.every((x: any) => x % 2 === 0);
  console.log("readable.every(even):", v);
} catch (e) {
  console.log("readable.every ERROR:", (e as Error).message);
}

// readable.flatMap
try {
  const r = stream.Readable.from([1, 2]);
  const out: any[] = [];
  for await (const v of r.flatMap((x: any) => [x, x * 10])) out.push(v);
  console.log("readable.flatMap values:", out);
} catch (e) {
  console.log("readable.flatMap ERROR:", (e as Error).message);
}

// readable.drop
try {
  const r = stream.Readable.from([1, 2, 3, 4]);
  const out: any[] = [];
  for await (const v of r.drop(2)) out.push(v);
  console.log("readable.drop(2) values:", out);
} catch (e) {
  console.log("readable.drop ERROR:", (e as Error).message);
}

// readable.take
try {
  const r = stream.Readable.from([1, 2, 3, 4]);
  const out: any[] = [];
  for await (const v of r.take(2)) out.push(v);
  console.log("readable.take(2) values:", out);
} catch (e) {
  console.log("readable.take ERROR:", (e as Error).message);
}

// readable.reduce
try {
  const r = stream.Readable.from([1, 2, 3, 4]);
  const sum = await r.reduce((acc: any, v: any) => acc + v, 0);
  console.log("readable.reduce sum:", sum);
} catch (e) {
  console.log("readable.reduce ERROR:", (e as Error).message);
}

// readable.push — exercised via a custom Readable subclass would normally
// be required; here we just probe that the method exists on instances.
try {
  const r = stream.Readable.from(["a"]);
  console.log("typeof readable.push:", typeof r.push);
  r.destroy();
} catch (e) {
  console.log("readable.push ERROR:", (e as Error).message);
}

// ── Classes — stream.Writable ──
try {
  console.log("class stream.Writable typeof:", typeof stream.Writable);
} catch (e) {
  console.log("class stream.Writable ERROR:", (e as Error).message);
}

// writable.write / writable.end
try {
  const collected: string[] = [];
  const w = new stream.Writable({
    write(chunk: any, _enc: any, cb: any) { collected.push(String(chunk)); cb(); }
  });
  w.write("foo");
  w.write("bar");
  w.end("baz");
  await new Promise<void>(res => w.on("finish", res));
  console.log("writable.write/end collected:", collected.join(","));
} catch (e) {
  console.log("writable.write/end ERROR:", (e as Error).message);
}

// writable.cork / uncork — silent ops; just probe instance methods exist.
try {
  const w = new stream.Writable({ write(_c: any, _e: any, cb: any) { cb(); } });
  w.cork();
  w.uncork();
  console.log("writable.cork/uncork: ok");
  w.destroy();
} catch (e) {
  console.log("writable.cork/uncork ERROR:", (e as Error).message);
}

// writable.destroy
try {
  const w = new stream.Writable({ write(_c: any, _e: any, cb: any) { cb(); } });
  w.destroy();
  console.log("writable.destroy destroyed:", w.destroyed);
} catch (e) {
  console.log("writable.destroy ERROR:", (e as Error).message);
}

// writable.setDefaultEncoding
try {
  const w = new stream.Writable({ write(_c: any, _e: any, cb: any) { cb(); } });
  w.setDefaultEncoding("utf8");
  console.log("writable.setDefaultEncoding: ok");
  w.destroy();
} catch (e) {
  console.log("writable.setDefaultEncoding ERROR:", (e as Error).message);
}

// ── Classes — stream.Duplex / Transform / PassThrough ──
try {
  console.log("class stream.Duplex typeof:", typeof stream.Duplex);
} catch (e) {
  console.log("class stream.Duplex ERROR:", (e as Error).message);
}

// new stream.Duplex - direct construction requires read/write impls.
try {
  const d = new stream.Duplex({
    read(_s: any) { this.push(null); },
    write(_c: any, _e: any, cb: any) { cb(); }
  });
  console.log("new stream.Duplex typeof:", typeof d);
  console.log("duplex.allowHalfOpen typeof:", typeof d.allowHalfOpen);
  d.destroy();
} catch (e) {
  console.log("new stream.Duplex ERROR:", (e as Error).message);
}

try {
  console.log("class stream.Transform typeof:", typeof stream.Transform);
} catch (e) {
  console.log("class stream.Transform ERROR:", (e as Error).message);
}

// new stream.Transform
try {
  const t = new stream.Transform({
    transform(chunk: any, _enc: any, cb: any) {
      cb(null, String(chunk).toUpperCase());
    }
  });
  console.log("new stream.Transform typeof:", typeof t);
  console.log("transform.readable:", t.readable);
  console.log("transform.writable:", t.writable);
  t.destroy();
  console.log("transform.destroy destroyed:", t.destroyed);
} catch (e) {
  console.log("new stream.Transform ERROR:", (e as Error).message);
}

// PassThrough — bonus, mentioned in conventions.
try {
  const p = new stream.PassThrough();
  console.log("new stream.PassThrough typeof:", typeof p);
  console.log("passthrough.readable:", p.readable);
  console.log("passthrough.writable:", p.writable);
  p.destroy();
} catch (e) {
  console.log("new stream.PassThrough ERROR:", (e as Error).message);
}

// ── stream.pipeline (callback) ──
try {
  const r = stream.Readable.from(["pipe-a", "pipe-b"]);
  const chunks: string[] = [];
  const w = new stream.Writable({
    write(c: any, _e: any, cb: any) { chunks.push(String(c)); cb(); }
  });
  await new Promise<void>((resolve) => {
    stream.pipeline(r, w, (err: any) => {
      if (err) console.log("pipeline(cb) err:", err.message);
      else console.log("pipeline(cb) chunks:", chunks.join(","));
      resolve();
    });
  });
} catch (e) {
  console.log("pipeline(cb) ERROR:", (e as Error).message);
}

// ── stream.finished ──
try {
  const r = stream.Readable.from(["fin-1", "fin-2"]);
  r.resume();
  await new Promise<void>((resolve) => {
    stream.finished(r, (err: any) => {
      if (err) console.log("finished(cb) err:", err.message);
      else console.log("finished(cb): ok");
      resolve();
    });
  });
} catch (e) {
  console.log("finished(cb) ERROR:", (e as Error).message);
}

// ── stream.compose ──
try {
  console.log("typeof stream.compose result:", typeof stream.compose);
} catch (e) {
  console.log("stream.compose ERROR:", (e as Error).message);
}

// ── stream.addAbortSignal ──
try {
  const ac = new AbortController();
  const r = stream.Readable.from(["a", "b"]);
  const out = stream.addAbortSignal(ac.signal, r);
  console.log("addAbortSignal typeof:", typeof out);
  r.destroy();
} catch (e) {
  console.log("addAbortSignal ERROR:", (e as Error).message);
}

// ── Static helpers ──
try {
  const r = stream.Readable.from(["a"]);
  const web = stream.Readable.toWeb(r);
  console.log("Readable.toWeb typeof:", typeof web);
} catch (e) {
  console.log("Readable.toWeb ERROR:", (e as Error).message);
}

try {
  const r = stream.Readable.from(["a"]);
  const v = (stream.Readable as any).isDisturbed(r);
  console.log("Readable.isDisturbed typeof:", typeof v);
  r.destroy();
} catch (e) {
  console.log("Readable.isDisturbed ERROR:", (e as Error).message);
}

try {
  const w = new stream.Writable({ write(_c: any, _e: any, cb: any) { cb(); } });
  const wweb = stream.Writable.toWeb(w);
  console.log("Writable.toWeb typeof:", typeof wweb);
} catch (e) {
  console.log("Writable.toWeb ERROR:", (e as Error).message);
}

try {
  const d = stream.Duplex.from(stream.Readable.from(["a"]));
  console.log("Duplex.from typeof:", typeof d);
} catch (e) {
  console.log("Duplex.from ERROR:", (e as Error).message);
}

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