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

import * as fs_promises from "node:fs/promises";

// All paths anchored under /tmp/perry_parity_fs_promises so test runs are
// idempotent. Created at section entry, removed at section exit.
const ROOT = "/tmp/perry_parity_fs_promises";

// ── Setup: ensure clean root ──
try {
  await fs_promises.rm(ROOT, { recursive: true, force: true });
  await fs_promises.mkdir(ROOT, { recursive: true });
  console.log("setup OK");
} catch (e: any) { console.log("setup ERR:", e && e.message); }

// ── writeFile / readFile ──
try {
  const p = ROOT + "/hello.txt";
  await fs_promises.writeFile(p, "hello world");
  const data: any = await fs_promises.readFile(p, "utf-8");
  console.log("writeFile+readFile content:", data);
  console.log("readFile typeof:", typeof data);
} catch (e: any) { console.log("writeFile/readFile ERR:", e && e.message); }

// ── appendFile ──
try {
  const p = ROOT + "/append.txt";
  await fs_promises.writeFile(p, "A");
  await fs_promises.appendFile(p, "B");
  const data: any = await fs_promises.readFile(p, "utf-8");
  console.log("appendFile content:", data);
} catch (e: any) { console.log("appendFile ERR:", e && e.message); }

// ── stat ──
try {
  const p = ROOT + "/hello.txt";
  const st: any = await fs_promises.stat(p);
  console.log("stat isFile:", st.isFile());
  console.log("stat isDirectory:", st.isDirectory());
  console.log("stat size:", st.size);
} catch (e: any) { console.log("stat ERR:", e && e.message); }

// ── lstat ──
try {
  const p = ROOT + "/hello.txt";
  const st: any = await fs_promises.lstat(p);
  console.log("lstat isFile:", st.isFile());
} catch (e: any) { console.log("lstat ERR:", e && e.message); }

// ── mkdir ──
try {
  const d = ROOT + "/sub/nested";
  await fs_promises.mkdir(d, { recursive: true });
  const st: any = await fs_promises.stat(d);
  console.log("mkdir recursive isDirectory:", st.isDirectory());
} catch (e: any) { console.log("mkdir ERR:", e && e.message); }

// ── readdir ──
try {
  const d = ROOT + "/listme";
  await fs_promises.mkdir(d, { recursive: true });
  await fs_promises.writeFile(d + "/a.txt", "1");
  await fs_promises.writeFile(d + "/b.txt", "2");
  const names: any = await fs_promises.readdir(d);
  // Sort for determinism: readdir order is not guaranteed.
  const sorted = (names as string[]).slice().sort();
  console.log("readdir entries:", sorted);
} catch (e: any) { console.log("readdir ERR:", e && e.message); }

// ── rename ──
try {
  const a = ROOT + "/rename_a.txt";
  const b = ROOT + "/rename_b.txt";
  await fs_promises.writeFile(a, "X");
  await fs_promises.rename(a, b);
  const data: any = await fs_promises.readFile(b, "utf-8");
  console.log("rename moved content:", data);
} catch (e: any) { console.log("rename ERR:", e && e.message); }

// ── copyFile ──
try {
  const a = ROOT + "/copy_src.txt";
  const b = ROOT + "/copy_dst.txt";
  await fs_promises.writeFile(a, "Y");
  await fs_promises.copyFile(a, b);
  const data: any = await fs_promises.readFile(b, "utf-8");
  console.log("copyFile content:", data);
} catch (e: any) { console.log("copyFile ERR:", e && e.message); }

// ── unlink + access ──
try {
  const p = ROOT + "/unlinkme.txt";
  await fs_promises.writeFile(p, "Z");
  await fs_promises.access(p);
  console.log("access OK before unlink");
  await fs_promises.unlink(p);
  let accessed = true;
  try {
    await fs_promises.access(p);
  } catch (_e) {
    accessed = false;
  }
  console.log("access after unlink:", accessed);
} catch (e: any) { console.log("unlink/access ERR:", e && e.message); }

// ── realpath ──
try {
  const p = ROOT + "/hello.txt";
  const r: any = await fs_promises.realpath(p);
  console.log("realpath typeof:", typeof r);
  console.log("realpath endsWith hello.txt:", String(r).endsWith("hello.txt"));
} catch (e: any) { console.log("realpath ERR:", e && e.message); }

// ── chmod ──
try {
  const p = ROOT + "/chmod_target.txt";
  await fs_promises.writeFile(p, "perm");
  await fs_promises.chmod(p, 0o644);
  console.log("chmod 0o644 OK");
} catch (e: any) { console.log("chmod ERR:", e && e.message); }

// ── truncate ──
try {
  const p = ROOT + "/truncate.txt";
  await fs_promises.writeFile(p, "1234567890");
  await fs_promises.truncate(p, 4);
  const data: any = await fs_promises.readFile(p, "utf-8");
  console.log("truncate content:", data);
} catch (e: any) { console.log("truncate ERR:", e && e.message); }

// ── FileHandle: open / read / close ──
try {
  const p = ROOT + "/fh.txt";
  await fs_promises.writeFile(p, "filehandle data");
  const fh: any = await fs_promises.open(p, "r");
  console.log("FileHandle typeof:", typeof fh);
  console.log("FileHandle.fd typeof:", typeof fh.fd);
  const content: any = await fh.readFile("utf-8");
  console.log("FileHandle.readFile content:", content);
  const stat: any = await fh.stat();
  console.log("FileHandle.stat size:", stat.size);
  await fh.close();
  console.log("FileHandle.close OK");
} catch (e: any) { console.log("FileHandle ERR:", e && e.message); }

// ── FileHandle: write through handle ──
try {
  const p = ROOT + "/fh_write.txt";
  const fh: any = await fs_promises.open(p, "w");
  await fh.writeFile("written via handle");
  await fh.close();
  const data: any = await fs_promises.readFile(p, "utf-8");
  console.log("FileHandle.writeFile content:", data);
} catch (e: any) { console.log("FileHandle.write ERR:", e && e.message); }

// ── Constants ──
try {
  const c: any = (fs_promises as any).constants;
  console.log("constants typeof:", typeof c);
  if (c) {
    console.log("constants.F_OK typeof:", typeof c.F_OK);
    console.log("constants.R_OK typeof:", typeof c.R_OK);
    console.log("constants.W_OK typeof:", typeof c.W_OK);
  }
} catch (e: any) { console.log("constants ERR:", e && e.message); }

// PERRY-SKIP: chown / lchmod / lchown / link / symlink / readlink — host-permission / setup dependent.
// PERRY-SKIP: lutimes / utimes — timestamp values are non-deterministic.
// PERRY-SKIP: mkdtemp / mkdtempDisposable — generated paths are non-deterministic.
// PERRY-SKIP: opendir / Dir iterator — entry-order varies.
// PERRY-SKIP: cp / glob / statfs / watch — newer / host-dependent surface.
// PERRY-SKIP: filehandle.createReadStream / createWriteStream / readableWebStream / readLines / readv / writev / sync / datasync / pull / pullSync / writer / [Symbol.asyncDispose] — newer / streaming surface, omitted for determinism.

// ── Cleanup ──
try {
  await fs_promises.rm(ROOT, { recursive: true, force: true });
  console.log("cleanup OK");
} catch (e: any) { console.log("cleanup ERR:", e && e.message); }

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