import { access, readFile, readdir } from "node:fs/promises";
import { join } from "node:path";

const root = new URL("../dist/", import.meta.url);
const required = [
  "index.html",
  "archive/index.html",
  "method/index.html",
  "articles/research-publishing-method/index.html",
  "articles/git-collaboration-ai-development/index.html",
  "articles/git-collaboration-ai-development/git-collaboration-flow.png",
  "articles/git-collaboration-ai-development/ai-responsibility-lanes.png",
  "articles/git-collaboration-ai-development/ai-project-workflow-positions.png",
  "articles/git-collaboration-ai-development/change-evidence-state.svg",
  "articles/git-collaboration-ai-development/test-selection-funnel.svg",
  "articles/git-collaboration-ai-development/agent-trust-boundary.svg",
  "rss.xml",
  "sitemap.xml"
];
for (const path of required) await access(new URL(path, root));

async function walk(path) {
  const entries = await readdir(path, { withFileTypes: true });
  const found = [];
  for (const entry of entries) {
    const child = join(path, entry.name);
    if (entry.isDirectory()) found.push(...await walk(child));
    else if (/\.(?:html|xml|css|js|svg)$/.test(entry.name)) found.push(child);
  }
  return found;
}

const files = await walk(root.pathname);
const forbidden = /(?:git\.n\.xiaomi\.com|coro\.doc|\.codex\/sessions|<codex_internal_context|-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----)/;
for (const file of files) {
  const text = await readFile(file, "utf8");
  if (forbidden.test(text)) throw new Error(`private material found in ${file}`);
}
const index = await readFile(new URL("index.html", root), "utf8");
if (!index.includes("K1412-RESEARCH-BLOG-20260810")) throw new Error("release marker missing");
console.log(`verified: ${files.length} rendered text assets and release marker`);
