import { expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { generateReproductionReport } from "../src/report.ts";

test("report bundles actionable diagnostics while redacting secrets and user/internal data", () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-report-"));
  mkdirSync(join(root, "app/pages"), { recursive: true });
  writeFileSync(
    join(root, "package.json"),
    JSON.stringify({ dependencies: { "@wrnexus/core": "0.8.0" } }),
  );
  writeFileSync(
    join(root, "wrnexus.config.ts"),
    `export default { apiKey: "sk_secretsecretsecret", endpoint: "https://internal.police.local/api" }`,
  );
  writeFileSync(
    join(root, "app/pages/index.wrn"),
    `page Home { view { <p>officer@example.com</p> } }`,
  );
  const result = generateReproductionReport(root, {
    file: "app/pages/index.wrn",
    error: "token=wrn_supersecrettoken at 10.0.0.1",
    output: ".wrnexus/report-test",
  });
  const all = readFileSync(result.reportFile, "utf8") + readFileSync(result.sourceFile!, "utf8");
  expect(all).toContain("frameworkVersion");
  expect(all).toContain("diagnostics");
  expect(all).not.toContain("sk_secretsecretsecret");
  expect(all).not.toContain("officer@example.com");
  expect(all).not.toContain("internal.police.local");
  expect(all).not.toContain("10.0.0.1");
});
test("report rejects traversal inputs and outputs", () => {
  const root = mkdtempSync(join(tmpdir(), "wrnexus-report-"));
  writeFileSync(join(root, "package.json"), "{}");
  expect(() => generateReproductionReport(root, { file: "../secret" })).toThrow("WRN-REPORT-FILE");
  expect(() => generateReproductionReport(root, { output: "../outside" })).toThrow(
    "WRN-REPORT-OUTPUT",
  );
});
