/**
 * 门禁检查模块 — Phase 14: 依赖漏洞扫描。
 */
import path from "node:path";
import fs from "node:fs";
import { safeJsonParse } from "../../core/helpers.js";
export async function check(ctx, hardFail, info) {
    const rootDir = ctx.rootDir;
    const _info = info ?? (() => { });
    const reportPath = path.join(rootDir, ".soloforge", "advisory-report.json");
    if (!fs.existsSync(reportPath)) {
        hardFail("DEPENDENCY_AUDIT", "未找到依赖漏洞扫描报告 (.soloforge/advisory-report.json)", ["package.json"], "依赖健康", "旧 gate 不检查依赖漏洞", "运行 npm run audit-deps");
        return;
    }
    try {
        const report = safeJsonParse(fs.readFileSync(reportPath, "utf-8"));
        if (!report) {
            hardFail("DEPENDENCY_AUDIT", "漏洞扫描报告格式无效", ["package.json"], "依赖健康", "旧 gate 不验证报告格式", "删除 .soloforge/advisory-report.json 并重新运行 npm run audit-deps");
            return;
        }
        if (!report.timestamp) {
            hardFail("DEPENDENCY_AUDIT", "漏洞扫描报告缺少时间戳", ["package.json"], "依赖健康", "旧 gate 不验证报告完整性", "重新运行 npm run audit-deps");
            return;
        }
        // 报告过期检查: 超过 24 小时视为过期
        const reportAge = Date.now() - new Date(report.timestamp).getTime();
        const maxAge = 24 * 60 * 60 * 1000;
        if (reportAge > maxAge) {
            hardFail("DEPENDENCY_AUDIT", `漏洞扫描报告已过期 (生成于 ${report.timestamp})`, ["package.json"], "依赖健康", "旧 gate 不检查报告时效", "重新运行 npm run audit-deps");
            return;
        }
        const blocked = report.blocked ?? [];
        if (blocked.length > 0) {
            const details = blocked.map((b) => `[${b.severity}] ${b.package}: ${b.title}`).join("; ");
            hardFail("DEPENDENCY_AUDIT", `发现 ${blocked.length} 个阻断级漏洞: ${details}`, ["package.json"], "依赖健康", "旧 gate 不扫描依赖漏洞", "npm audit fix 或升级依赖");
            return;
        }
        const total = report.vulnerability_summary?.total ?? 0;
        _info(`  依赖漏洞扫描通过: ${total} 个已知漏洞（无阻断级）`);
    }
    catch {
        hardFail("DEPENDENCY_AUDIT", "漏洞扫描报告格式无效", ["package.json"], "依赖健康", "旧 gate 不验证报告格式", "删除 .soloforge/advisory-report.json 并重新运行 npm run audit-deps");
    }
}
//# sourceMappingURL=checkDependencyAudit.js.map