import { spawnSync } from "node:child_process";
import { describe, expect, test } from "bun:test";

describe("gh Markdown stdin", () => {
  test("preserves backticks, dollar expressions, and heredoc markers byte-for-byte", () => {
    const body = [
      "目标合并分支: 不适用",
      "",
      "`G14` 与 $HOME 保持原样。",
      "",
      "<<'INNER'",
      "markdown `code` ${literal}",
      "INNER",
      "",
    ].join("\n");
    const script = [
      'import { readMarkdownBodyFileOrStdin } from "./scripts/src/gh/body-input.ts";',
      'process.stdout.write(JSON.stringify(readMarkdownBodyFileOrStdin("-")));',
    ].join(" ");
    const result = spawnSync(process.execPath, ["-e", script], {
      cwd: process.cwd(),
      input: body,
      encoding: "utf8",
    });

    expect(result.status).toBe(0);
    expect(result.stderr).toBe("");
    expect(JSON.parse(result.stdout)).toEqual({
      body,
      bodySource: { kind: "stdin", path: "-" },
    });
  });
});
