/** Human and CI views of the same evidence; no independently computed success. */
import { writeFileSync } from "node:fs";
import { join } from "node:path";
const xml = value => String(value).replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f]/g, "").replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll('"', """).replaceAll("'", "'");
export function renderJUnit(report) {
const optionalNA=test=>test.status==='notApplicable'&&!(report.requiredTestIds||[]).includes(test.testId);
const unsuccessful = report.tests.filter(test => test.status !== "passed"&&!optionalNA(test));
const cases = report.tests.map(test => `${test.status === "passed" ? "" : optionalNA(test)?``:`${xml(test.reason || test.status)}`}`);
for(const suite of report.failedSuites||[])cases.push(`${xml(suite.reason)}`);
// A required blocked case is deliberately a failure, never a skipped green job.
if (report.status !== "passed" && !unsuccessful.length && !report.failedSuites?.length) cases.push(`${xml(report.error || report.cleanupError || "Suite, discovery or cleanup gate failed")}`);
return `\n${cases.join("\n")}\n`;
}
export function writeReportFiles(report, output) {
writeFileSync(join(output, "junit.xml"), renderJUnit(report));
const counts = Object.fromEntries(["passed", "failed", "blocked", "notApplicable"].map(status => [status, report.tests.filter(test => test.status === status).length]));
writeFileSync(join(output, "summary.txt"), `${report.suiteId}: ${report.status}\nCleanup: ${report.cleanup}\n${JSON.stringify(counts)}\n` + report.tests.filter(test => test.status !== "passed").map(test => `${test.testId}: ${test.status}\n${test.reason || "No reason supplied"}`).join("\n") + "\n");
}