import { readFile } from "node:fs/promises"; import { describe, expect, it } from "vitest"; import { summarizeDependencyAdvisories } from "../../scripts/lib/dependency-advisories.ts"; /** * 1103/13-03. The lockfile broke the template's own vulnerability policy * (high and above, no exception) and nothing said so before the production * promotion provider, which a template copy may not have. The summary applies * that policy to `pnpm audit --json`. */ const POLICY = { blockAtSeverity: "high" }; const NO_EXCEPTIONS = { schemaVersion: 1, exceptions: [] }; const LOCKFILE = "a".repeat(64); const advisory = ( id: string, module: string, severity: string, findings: readonly Readonly<{ version: string; dev: boolean }>[], ) => ({ github_advisory_id: id, module_name: module, severity, patched_versions: ">=9.9.9", findings: findings.map((finding) => ({ ...finding, paths: [`.>${module}`] })), }); const audit = (...advisories: ReturnType[]) => ({ advisories: Object.fromEntries(advisories.map((entry, index) => [String(index), entry])), metadata: { vulnerabilities: {} }, }); describe("dependency advisory summary (1103/13-03)", () => { it("names what the policy would block at promotion, in production and development", () => { const summary = summarizeDependencyAdvisories( audit( advisory("GHSA-prod-high", "zod", "high", [{ version: "4.4.3", dev: false }]), advisory("GHSA-dev-critical", "storybook", "critical", [{ version: "10.5.4", dev: true }]), advisory("GHSA-dev-moderate", "postcss", "moderate", [{ version: "8.5.22", dev: true }]), ), POLICY, NO_EXCEPTIONS, LOCKFILE, ); expect(summary.status).toBe("POLICY_VIOLATION"); expect(summary.blocking).toEqual([ "GHSA-prod-high:zod@4.4.3:high", "GHSA-dev-critical:storybook@10.5.4:critical", ]); expect(summary.counts.production).toMatchObject({ high: 1, critical: 0 }); expect(summary.counts.development).toMatchObject({ critical: 1, moderate: 1 }); }); it("passes when nothing reaches the blocking severity", () => { const summary = summarizeDependencyAdvisories( audit(advisory("GHSA-dev-moderate", "postcss", "moderate", [{ version: "8.5.22", dev: true }])), POLICY, NO_EXCEPTIONS, LOCKFILE, ); expect(summary).toMatchObject({ status: "PASS", blocking: [] }); }); it("honours a valid exception the way the promotion verdict does", () => { const exceptions = { schemaVersion: 1, exceptions: [ { vulnerabilityId: "GHSA-prod-high", packageName: "zod", owner: "frontend-platform", reviewer: "security-review", reason: "not reachable", expiresAt: "2027-01-01T00:00:00.000Z", }, ], }; expect( summarizeDependencyAdvisories( audit(advisory("GHSA-prod-high", "zod", "high", [{ version: "4.4.3", dev: false }])), POLICY, exceptions, LOCKFILE, new Date("2026-11-04T00:00:00.000Z"), ), ).toMatchObject({ status: "PASS", blocking: [] }); }); it("is UNVERIFIED, not PASS, when the audit returned no report", () => { for (const output of [null, {}, { error: { code: "ENOTFOUND" } }]) { expect(summarizeDependencyAdvisories(output, POLICY, NO_EXCEPTIONS, LOCKFILE)).toMatchObject({ status: "UNVERIFIED", reason: expect.stringContaining("pnpm audit"), }); } }); // 1106/13-01. ci:gate stops at the first command that fails. With the scan // after the admission, a refused run had no advisory record, while the // operations guide says the release evidence carries it. The scan never // fails, so running it first leaves the admission verdict as it was. it("runs before the release admission (FE-GATE-027) and never fails the gate by itself", async () => { const contract = JSON.parse(await readFile("config/ci/gates.json", "utf8")) as { gates: { id: string; commandIds: string[] }[]; commands: { id: string; script: string }[]; }; const scripts = (contract.gates.find(({ id }) => id === "FE-GATE-027")?.commandIds ?? []).map( (commandId) => contract.commands.find(({ id }) => id === commandId)?.script, ); expect(scripts).toEqual(["scan:dependency-advisories", "check:release-admission"]); const source = await readFile("scripts/scan-dependency-advisories.ts", "utf8"); expect(source).not.toMatch(/process\.exit(?:Code)?\s*(?:=|\()\s*[1-9]/u); }); });