import { readFile } from "node:fs/promises"; import { constants } from "node:fs"; import { spawnSync } from "node:child_process"; import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { CI_ACTION_REGISTRY, indexCiGateContract, loadCiGateContract, parseCiActionRegistry, parseCiGateContract, resolveCiActionUses, withCiGatePreflight, } from "../../scripts/contracts/ci-gates.ts"; import { createCiWorkflowGenerator, generateCiWorkflow, renderCiWorkflow, workflowGateExecutionFailures, type CiWorkflowFileSystem, } from "../../scripts/generate-ci-workflow.ts"; import { isNpmScopeEnvironmentName, validateInstallScriptPolicy, validatePackageScriptGraph, } from "../../scripts/lib/package-script-graph.ts"; import { VISUAL_TEST_FONT_PACKAGES } from "../../scripts/lib/visual-test-fonts.ts"; import { childProcessTimeout } from "../helpers/child-process-timeout.ts"; const temporaryRoots: string[] = []; const npmPostScriptForeignScopeCommands = [ "npm run nested --workspace fixture", "npm run nested --workspace=fixture", "npm run nested -w fixture", "npm run nested --prefix fixture", "npm run nested --prefix=fixture", "npm test --workspace fixture", "npm start --prefix fixture", ] as const; const npmLifecycleHookDispatches = [ ["npm run nested", "nested"], ["npm run-script nested", "nested"], ["npm start", "start"], ["npm stop", "stop"], ["npm test", "test"], ["npm restart", "restart"], ] as const; const npmDispatchExpansionCommands = [ "SCOPE=--workspace; npm run nested $SCOPE fixture", "SCOPE=--workspace; npm run nested ${SCOPE} fixture", "npm run nested $SCOPE fixture", "npm test ${SCOPE}", "npm run nested --if-present=${FLAG}", ] as const; const npmDispatchScopeEnvironmentCommands = [ "npm_config_workspace=fixture npm run nested", "NPM_CONFIG_WORKSPACES=true npm test", "env npm_config_prefix=fixture npm run nested", "/usr/bin/env npm_config_workspace=fixture npm run nested", "export NpM_CoNfIg_PrEfIx=fixture && npm run nested", "/bin/env npm_config_prefix=fixture npm run nested", "command env npm_config_workspace=fixture npm run nested", "command /opt/reviewer/bin/env NPM_CONFIG_WORKSPACES=true npm test", "npm_config_prefix=fixture; export npm_config_prefix; npm run nested", "npm_config_workspace=fixture && export npm_config_workspace && npm run nested", "export npm_config_workspaces; npm_config_workspaces=true; npm test", "set -a; npm_config_prefix=fixture; npm run nested", "DYNAMIC_SCOPE=workspace; env npm_config_${DYNAMIC_SCOPE}=fixture npm run nested", "DYNAMIC_SCOPE=prefix; npm_config_${DYNAMIC_SCOPE}=fixture npm run nested", "DYNAMIC_SCOPE=workspaces; export npm_config_${DYNAMIC_SCOPE}=true; npm test", "ENV_WRAPPER=env; $ENV_WRAPPER npm_config_prefix=fixture npm run nested", "set +a; npm run nested", "unset npm_config_prefix; npm run nested", "export -n npm_config_workspace; npm run nested", "eval 'export npm_config_prefix=fixture'; npm run nested", ". ./scope-env.sh; npm run nested", "source ./scope-env.sh; npm run nested", ] as const; const npmShellPrefixScopeEnvironmentCommands = [ "exec env npm_config_workspace=fixture npm run nested", "exec /bin/env npm_config_prefix=fixture npm run nested", "command exec env npm_config_workspaces=true npm test", "command -- env npm_config_prefix=fixture npm run nested", "command -p env npm_config_workspace=fixture npm run nested", "SAFE=1 export npm_config_prefix=fixture; npm run nested", "SAFE=1 set -a; npm_config_prefix=fixture; npm run nested", "command --unknown env npm_config_prefix=fixture npm run nested", "exec -a reviewer env npm_config_workspace=fixture npm run nested", ] as const; const npmIndirectConfigAuthorityCommands = [ "npm --userconfig fixture run nested", "npm --globalconfig=fixture test", "npm --userconfig fixture ci --ignore-scripts", "npm --globalconfig fixture audit", "NPM_CONFIG_USERCONFIG=fixture npm run nested", "npm_config_globalconfig=fixture npm test", "env NPM_CONFIG_USERCONFIG=fixture npm ci --ignore-scripts", "/usr/bin/env npm_config_globalconfig=fixture npm audit", ] as const; const npmPreDelimiterGlobCommands = [ "npm run nested *", "npm test ?", "npm run nested [a-z]*", ] as const; const unmodeledManagerPrefixCommands = [ "nice env npm_config_workspace=fixture npm run nested", "/usr/bin/nice env npm_config_prefix=fixture npm run nested", "nohup env npm_config_workspaces=true npm test", "SAFE=1 nice env npm_config_workspace=fixture npm run nested", "nice npm run nested", "time pnpm nested", ] as const; afterEach(async () => { await Promise.all( temporaryRoots.splice(0).map((root) => rm(root, { recursive: true, force: true })), ); }); async function createUnsafeCiGateFixture( command: string, additionalScripts: Readonly> = {}, ): Promise { const root = await mkdtemp(path.join(tmpdir(), "ci-contract-unsafe-command-")); temporaryRoots.push(root); await mkdir(path.join(root, "config/ci"), { recursive: true }); const contract = JSON.parse( JSON.stringify(await loadCiGateContract(process.cwd())), ) as Record; const packageDocument = JSON.parse(await readFile("package.json", "utf8")) as { scripts: Record; }; contract.commands[0].script = "unsafe:preflight"; packageDocument.scripts["unsafe:preflight"] = command; Object.assign(packageDocument.scripts, additionalScripts); await writeFile(path.join(root, "config/ci/gates.json"), `${JSON.stringify(contract)}\n`); await writeFile(path.join(root, "package.json"), `${JSON.stringify(packageDocument)}\n`); return root; } describe("CI gate contract", () => { it.each([ ["unknown-field.json", /unrecognized|unknown/i], ["duplicate-gate-id.json", /duplicate gate id/i], ["missing-artifact-schema.json", /unknown artifact schema/i], ])("rejects %s before projection", async (fixture, diagnostic) => { const candidate = JSON.parse( await readFile(`tests/fixtures/ci-contract/${fixture}`, "utf8"), ); expect(() => parseCiGateContract(candidate)).toThrow(diagnostic); }); it.each([ ["unknown-job-dependency.json", /unknown job dependency/i], ["job-cycle.json", /job dependency cycle/i], ["unowned-gate.json", /unowned gate: FE-GATE-001/i], ["multiply-owned-gate.json", /multiply owned gate: FE-GATE-001/i], ])("applies and rejects physical semantic fixture %s", async (fixture, diagnostic) => { const mutation = JSON.parse( await readFile(`tests/fixtures/ci-contract/${fixture}`, "utf8"), ) as Readonly<{ jobId: string; needs?: string[]; removeGateId?: string; addGateId?: string; }>; const candidate = JSON.parse( JSON.stringify(await loadCiGateContract(process.cwd())), ) as Record; const job = candidate.jobs.find( (entry: Record) => entry.id === mutation.jobId, ); if (!job) throw new Error(`fixture job does not exist: ${mutation.jobId}`); if (mutation.needs) job.needs = mutation.needs; if (mutation.removeGateId) { job.gateIds = job.gateIds.filter((id: string) => id !== mutation.removeGateId); } if (mutation.addGateId) job.gateIds.push(mutation.addGateId); expect(() => parseCiGateContract(candidate)).toThrow(diagnostic); }); // The command and artifact counts are not pinned here: the count and shape // baseline was deleted on 2026-11-08 (design-docs repo-structure/README.md // 3.3), and the contract's semantic checks hold gate, job and stage shape. it("loads the strict v2 registry", async () => { const contract = await loadCiGateContract(process.cwd()); const index = indexCiGateContract(contract); expect(contract.schemaVersion).toBe(2); expect(contract.gates.map(({ id }) => id)).toEqual( Array.from({ length: 30 }, (_, index) => `FE-GATE-${String(index + 1).padStart(3, "0")}`, ).filter((gateId) => gateId !== "FE-GATE-016"), ); expect(contract.jobs).toHaveLength(13); expect(contract.stages).toHaveLength(6); expect(contract.retention.classes).toHaveLength(5); expect(index.gates.get("FE-GATE-015")?.commandIds).toHaveLength(2); expect(index.gates.get("FE-GATE-020")?.name).toBe("removability"); }); it("runs the optional runtime removal drills in the main stage, the feature drills in merge", async () => { // Owner decision 2026-11-12 p: the three optional runtime removal drills // left the pull request's merge stage; the sample and scaffold drills stay. const contract = await loadCiGateContract(process.cwd()); const index = indexCiGateContract(contract); const scriptsOf = (gateId: string) => (index.gates.get(gateId)?.commandIds ?? []).map((id) => index.commands.get(id)?.script); const stageOf = (gateId: string) => contract.stages.find(({ gateIds }) => gateIds.includes(gateId))?.id; const jobOf = (gateId: string) => contract.jobs.find(({ gateIds }) => gateIds.includes(gateId))?.id; expect(scriptsOf("FE-GATE-020")).toEqual(["test:sample-removal", "test:feature-scaffold"]); expect([stageOf("FE-GATE-020"), jobOf("FE-GATE-020")]).toEqual(["merge", "merge_test"]); expect(scriptsOf("FE-GATE-030")).toEqual([ "test:optional-recipe-removal", "test:browser-file-storage-removal", "test:realtime-removal", ]); expect([stageOf("FE-GATE-030"), jobOf("FE-GATE-030")]).toEqual(["main", "main_gate"]); }); it("runs the signed manual accessibility review at release, not merge (08-06)", async () => { const contract = await loadCiGateContract(process.cwd()); const index = indexCiGateContract(contract); const scriptsOf = (gateId: string) => (index.gates.get(gateId)?.commandIds ?? []).map((id) => { const command = index.commands.get(id); return [command?.script, ...(command?.args ?? [])].join(" "); }); const stageOf = (gateId: string) => contract.stages.find(({ gateIds }) => gateIds.includes(gateId))?.id; expect(scriptsOf("FE-GATE-009")).toEqual(["test:a11y", "review:a11y-manual -- --format-only"]); expect(scriptsOf("FE-GATE-028")).toEqual(["review:a11y-manual"]); expect(stageOf("FE-GATE-009")).toBe("merge"); expect(stageOf("FE-GATE-028")).toBe("release"); }); it("runs the CPU-independent scale PR tier in a merge gate (11-03)", async () => { const contract = await loadCiGateContract(process.cwd()); const index = indexCiGateContract(contract); const scripts = index.gates.get("FE-GATE-005")?.commandIds.map((id) => index.commands.get(id)?.script); expect(scripts).toContain("check:scale:pr"); expect(contract.stages.find(({ id }) => id === "merge")?.gateIds).toContain("FE-GATE-005"); }); it("probes live hosting headers after promotion, not before the release is built (13-05)", async () => { const contract = await loadCiGateContract(process.cwd()); expect(contract.stages.find(({ id }) => id === "production")?.gateIds).toContain("FE-GATE-019"); expect(contract.stages.find(({ id }) => id === "release")?.gateIds).not.toContain("FE-GATE-019"); const productionGate = contract.jobs.find(({ id }) => id === "production_gate"); expect(productionGate?.gateIds).toContain("FE-GATE-019"); expect(productionGate?.environment).toContainEqual({ name: "HOSTING_BASE_URL", value: "${{ vars.HOSTING_BASE_URL }}", }); }); it("drills the downloaded promoted archive in the production stage without rebuilding (13-03)", async () => { const contract = await loadCiGateContract(process.cwd()); const index = indexCiGateContract(contract); const productionGate = contract.jobs.find(({ id }) => id === "production_gate"); const kinds = productionGate?.steps.map(({ kind }) => kind) ?? []; const download = productionGate?.steps.find((step) => step.kind === "download"); const extract = productionGate?.steps.find((step) => step.kind === "extract"); expect(download).toEqual({ kind: "download", transferId: "promoted-release", path: ".release/promoted", }); expect(extract).toEqual({ kind: "extract", archivePath: ".release/promoted/release-candidate.tar.gz", targetRoot: ".release/promoted-release", }); expect(kinds.indexOf("download")).toBeLessThan(kinds.indexOf("extract")); expect(kinds.indexOf("extract")).toBeLessThan(kinds.indexOf("run-gate")); // The extract step checks the archive against the digest immutable_build // recorded, so the drilled bytes are the promoted candidate. expect(productionGate?.needs).toContain("immutable_build"); expect(productionGate?.environment).toContainEqual({ name: "CANDIDATE_ARCHIVE_SHA256", value: "${{ needs.immutable_build.outputs.archive_sha256 }}", }); for (const gateId of ["FE-GATE-021", "FE-GATE-022", "FE-GATE-023", "FE-GATE-024", "FE-GATE-025"]) { const commands = index.gates.get(gateId)?.commandIds.map((id) => index.commands.get(id)!) ?? []; expect(commands.map(({ script }) => script), gateId).toEqual(["drill:runbook"]); const args = commands[0]?.args ?? []; expect(args[args.indexOf("--release-root") + 1], gateId).toBe(".release/promoted-release"); } const workflow = renderCiWorkflow(contract); const productionJob = workflow.slice( workflow.indexOf(" production_gate:\n"), workflow.indexOf(" field_gate:\n"), ); const downloadAt = productionJob.indexOf("- name: Download promoted release"); const extractAt = productionJob.indexOf( '--archive ".release/promoted/release-candidate.tar.gz" --extract-to ".release/promoted-release"', ); expect(downloadAt).toBeGreaterThan(0); expect(extractAt).toBeGreaterThan(downloadAt); expect(productionJob.indexOf("Run blocking gate")).toBeGreaterThan(extractAt); }); it.each([ [ "a drill without --release-root", (contract: Record) => { const drill = contract.commands.find((command: Record) => command.id === "drill-runbook"); drill.args = ["--", "FE-RB-005"]; }, ], [ "a drill gate that rebuilds from source", (contract: Record) => { const gate = contract.gates.find((candidate: Record) => candidate.id === "FE-GATE-025"); gate.commandIds = ["build", ...gate.commandIds]; }, ], [ "a production stage without the promoted archive", (contract: Record) => { const job = contract.jobs.find((candidate: Record) => candidate.id === "production_gate"); job.steps = job.steps.filter((step: Record) => !["download", "extract"].includes(step.kind)); }, ], ])("rejects %s in the production stage (13-03)", async (_name, mutate) => { const contract = JSON.parse( JSON.stringify(await loadCiGateContract(process.cwd())), ) as Record; mutate(contract); expect(() => parseCiGateContract(contract)).toThrow(/promoted/iu); }); // 1101/13-01. FE-GATE-019 rebuilt with the local profile and checked that // dist; the live probe compared nothing with the promoted candidate. it("checks the promoted candidate in FE-GATE-019 without rebuilding (1101/13-01)", async () => { const contract = await loadCiGateContract(process.cwd()); const index = indexCiGateContract(contract); const commands = (index.gates.get("FE-GATE-019")?.commandIds ?? []).map((id) => { const command = index.commands.get(id); return [command?.script, ...(command?.args ?? [])].join(" "); }); expect(commands).toEqual(["verify:hosting-headers -- --release-root .release/promoted-release"]); }); it("declares an empty deployment slot between promotion and production_gate (1101/13-01)", async () => { const contract = await loadCiGateContract(process.cwd()); const ids = contract.jobs.map(({ id }) => id); expect(ids.indexOf("deployment")).toBe(ids.indexOf("promotion") + 1); expect(ids.indexOf("production_gate")).toBe(ids.indexOf("deployment") + 1); const slot = contract.jobs.find(({ id }) => id === "deployment"); expect(slot).toMatchObject({ kind: "deployment", needs: ["immutable_build", "promotion"], condition: "production", gateIds: [], environment: [], steps: [{ kind: "deploy" }], }); const production = contract.jobs.find(({ id }) => id === "production_gate"); expect(production?.needs).toContain("deployment"); expect(production?.environment).toContainEqual({ name: "RELEASE_DEPLOYED", value: "${{ needs.deployment.outputs.deployed }}", }); const workflow = renderCiWorkflow(contract); const job = workflow.slice(workflow.indexOf(" deployment:\n"), workflow.indexOf(" production_gate:\n")); expect(job).toContain(" outputs:\n deployed: ${{ steps.deploy.outputs.deployed }}"); expect(job).toContain('run: echo "deployed=false" >> "$GITHUB_OUTPUT"'); }); const fillDeploymentSlot = (contract: Record) => { const slot = contract.jobs.find((job: Record) => job.id === "deployment"); slot.environment = [ { name: "CANDIDATE_ARCHIVE_SHA256", value: "${{ needs.immutable_build.outputs.archive_sha256 }}" }, { name: "DEPLOY_HOST", value: "${{ vars.DEPLOY_HOST }}" }, { name: "DEPLOY_SSH_KEY", value: "${{ secrets.DEPLOY_SSH_KEY }}" }, ]; slot.steps = [ { kind: "checkout" }, { kind: "setup-node" }, { kind: "frozen-install" }, { kind: "download", transferId: "promoted-release", path: ".release/promoted" }, { kind: "extract", archivePath: ".release/promoted/release-candidate.tar.gz", targetRoot: ".release/promoted-release", }, { kind: "deploy", script: "deploy:release" }, ]; return contract; }; it("accepts a deployment job the slot allows (1101/13-01)", async () => { const contract = parseCiGateContract( fillDeploymentSlot(JSON.parse(JSON.stringify(await loadCiGateContract(process.cwd())))), ); const workflow = renderCiWorkflow(contract); const job = workflow.slice(workflow.indexOf(" deployment:\n"), workflow.indexOf(" production_gate:\n")); // 1103/13-05. Job env reaches every step, checkout and the third-party // download action included; a credential belongs to the deploy step only. const jobEnvironment = job.slice(job.indexOf(" env:\n"), job.indexOf(" steps:\n")); const deployStep = job.slice(job.indexOf(" - name: Deploy the promoted release")); expect(jobEnvironment).toContain('DEPLOY_HOST: "${{ vars.DEPLOY_HOST }}"'); expect(jobEnvironment).not.toContain("secrets."); expect(deployStep).toContain(' env:\n DEPLOY_SSH_KEY: "${{ secrets.DEPLOY_SSH_KEY }}"\n'); expect(job.match(/secrets\./gu)).toHaveLength(1); expect(job.indexOf("--extract-to")).toBeLessThan(job.indexOf("corepack pnpm deploy:release")); expect(job.indexOf("corepack pnpm deploy:release")).toBeLessThan( job.indexOf('echo "deployed=true" >> "$GITHUB_OUTPUT"'), ); expect(workflowGateExecutionFailures(contract, workflow)).toEqual([]); }); it.each([ [ "a slot that runs a gate", (slot: Record) => slot.steps.splice(5, 0, { kind: "run-gate" }), /forbidden for deployment/u, ], [ "a slot that binds another job's output", (slot: Record) => slot.environment.push({ name: "NONCE", value: "${{ needs.promotion.outputs.staging_root }}" }), /deployment slot binding/u, ], [ "a slot whose deploy step is not last", (slot: Record) => slot.steps.push({ kind: "checkout" }), /deploy step/u, ], // 1103/13-01. A slot that rebuilt (or deployed anything but the promoted // archive) passed the contract under the name deploy-promoted-release. [ "a slot that deploys without the promoted candidate", (slot: Record) => slot.steps.splice(3, 2), /promoted-release/u, ], [ "a slot that extracts the candidate without its digest", (slot: Record) => slot.environment.shift(), /CANDIDATE_ARCHIVE_SHA256/u, ], ])("rejects %s (1101/13-01)", async (_name, mutate, diagnostic) => { const contract = fillDeploymentSlot( JSON.parse(JSON.stringify(await loadCiGateContract(process.cwd()))), ); mutate(contract.jobs.find((job: Record) => job.id === "deployment")); expect(() => parseCiGateContract(contract)).toThrow(diagnostic); }); it("rejects a hosting gate that rebuilds instead of reading the promoted archive (1101/13-01)", async () => { const contract = JSON.parse( JSON.stringify(await loadCiGateContract(process.cwd())), ) as Record; const gate = contract.gates.find((candidate: Record) => candidate.id === "FE-GATE-019"); gate.commandIds = ["build", ...gate.commandIds]; expect(() => parseCiGateContract(contract)).toThrow(/promoted/iu); }); it("keeps the action registry recursively immutable and resolves only known actions", () => { expect(Object.isFrozen(CI_ACTION_REGISTRY)).toBe(true); expect(Object.values(CI_ACTION_REGISTRY).every((action) => Object.isFrozen(action))).toBe(true); expect(resolveCiActionUses("checkout")).toBe( "https://github.com/actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5", ); expect(() => resolveCiActionUses("actions/checkout@v4" as never)).toThrow(/unknown CI action/i); }); it.each([ ["unknown action", (registry: Record) => { registry.unknown = registry.checkout; }, /unrecognized|unknown/i], ["movable branch", (registry: Record) => { registry.checkout.revision = "v4"; }, /full 40-hex commit SHA/i], ["short SHA", (registry: Record) => { registry.checkout.revision = "34e114876b0b"; }, /full 40-hex commit SHA/i], ["relative repository", (registry: Record) => { registry.checkout.repository = "actions/checkout"; }, /absolute upstream GitHub URL/i], ])("rejects an unsafe CI action registry mutation: %s", (_name, mutate, diagnostic) => { const candidate = JSON.parse(JSON.stringify(CI_ACTION_REGISTRY)) as Record; mutate(candidate); expect(() => parseCiActionRegistry(candidate)).toThrow(diagnostic); }); it.each([ ["unknown nested field", (value: Record) => (value.commands[0].unknown = true), /unrecognized|unknown/i], ["duplicate command id", (value: Record) => value.commands.push({ ...value.commands[0] }), /duplicate command id/i], ["duplicate command tuple", (value: Record) => value.commands.push({ ...value.commands[0], id: "duplicate-tuple" }), /duplicate command tuple/i], ["duplicate command reference", (value: Record) => value.gates[0].commandIds.push(value.gates[0].commandIds[0]), /duplicate command reference within gate/i], ["duplicate artifact id", (value: Record) => value.artifacts.push({ ...value.artifacts[0] }), /duplicate artifact id/i], ["duplicate artifact schema id", (value: Record) => value.artifactSchemas.push({ ...value.artifactSchemas[0] }), /duplicate artifact schema id/i], ["duplicate stage id", (value: Record) => value.stages.push({ ...value.stages[0] }), /duplicate stage id/i], ["duplicate job id", (value: Record) => value.jobs.push({ ...value.jobs[0] }), /duplicate job id/i], ["duplicate artifact path", (value: Record) => value.artifacts.push({ ...value.artifacts[0], id: "duplicate-path" }), /duplicate artifact path/i], ["missing artifact production classification", (value: Record) => delete value.artifacts[0].production, /production/i], ["unknown command reference", (value: Record) => value.gates[0].commandIds.push("missing-command"), /unknown command missing-command/i], ["unknown artifact reference", (value: Record) => (value.gates[0].logArtifactId = "missing-artifact"), /unknown artifact missing-artifact/i], ["unknown schema reference", (value: Record) => (value.artifacts[0].schemaId = "missing-schema"), /unknown artifact schema missing-schema/i], ["unknown retention reference", (value: Record) => (value.gates[0].retentionClassId = "missing-retention"), /unknown retention class missing-retention/i], ["unsafe artifact path", (value: Record) => (value.artifacts[0].path = "../escape"), /unsafe repository path/i], ["artifact path controls", (value: Record) => (value.artifacts[0].path = "artifacts/bad\n.json"), /control and Unicode line-break characters/i], ["empty commands", (value: Record) => (value.commands = []), /too small|at least 1/i], ["empty artifacts", (value: Record) => (value.artifacts = []), /too small|at least 1/i], ["empty gates", (value: Record) => (value.gates = []), /too small|at least 1/i], ["empty gate command refs", (value: Record) => (value.gates[0].commandIds = []), /too small|at least 1/i], ["empty gate evidence refs", (value: Record) => (value.gates[0].evidenceArtifactIds = []), /too small|at least 1/i], ["unowned gate", (value: Record) => (value.jobs[0].gateIds = value.jobs[0].gateIds.filter((id: string) => id !== "FE-GATE-001")), /unowned gate: FE-GATE-001/i], ["multiply owned gate", (value: Record) => value.jobs[1].gateIds.push("FE-GATE-001"), /multiply owned gate: FE-GATE-001/i], ["duplicated immutable gate", (value: Record) => value.jobs.find((job: any) => job.id === "release_gate").gateIds.push("FE-GATE-015"), /release matrix duplicates FE-GATE-015/i], ["unreachable producer", (value: Record) => (value.jobs.find((job: any) => job.id === "promotion").needs = ["immutable_build", "provenance_provider"]), /download producer vulnerability_provider is unreachable/i], ["missing producer", (value: Record) => { const producer = value.jobs.find((job: any) => job.id === "immutable_build"); producer.steps = producer.steps.filter((step: any) => step.kind !== "upload"); }, /unknown download transfer release-candidate|job step sequence drift/i], ["promotion rebuild authority", (value: Record) => value.jobs.find((job: any) => job.id === "promotion").steps.splice(3, 0, { kind: "archive-candidate", stepId: "bad", archivePath: ".release/bad.tar.gz", members: ["dist"], archiveOutputName: "bad", distOutputName: "bad-dist" }), /promotion job must not build|step kind archive-candidate is forbidden/i], ["browser gate drift", (value: Record) => (value.jobs[0].browserGateIds = ["FE-GATE-008"]), /browser gate set drift/i], ["missing gate execution step", (value: Record) => (value.jobs[0].steps = value.jobs[0].steps.filter((step: any) => step.kind !== "run-gate")), /canonical job step sequence drift/i], ["browser install drift", (value: Record) => { const job = value.jobs.find((candidate: any) => candidate.id === "merge_browser"); job.steps = job.steps.filter((step: any) => step.kind !== "browser-install"); }, /browser install step drift|job step sequence drift/i], ["job environment drift", (value: Record) => value.jobs[0].environment.push({ name: "UNOWNED", value: "x" }), /job environment binding drift/i], ["job kind drift", (value: Record) => (value.jobs[0].kind = "gate-single"), /job graph drift/i], ["job needs drift", (value: Record) => (value.jobs.find((job: any) => job.id === "release_gate").needs = []), /job graph drift/i], ["job condition drift", (value: Record) => (value.jobs[1].condition = "always"), /job graph drift/i], ["promotion success condition drift", (value: Record) => (value.jobs.find((job: any) => job.id === "promotion").condition = "always"), /job graph drift|promotion.*condition/i], ["candidate output identity drift", (value: Record) => { const job = value.jobs.find((candidate: any) => candidate.id === "immutable_build"); job.steps.find((step: any) => step.kind === "archive-candidate").archiveOutputName = "renamed"; }, /candidate output identity drift/i], ["stage cycle", (value: Record) => (value.stages[0].needs = ["release"]), /stage dependency cycle/i], ["provider adapter target drift", (value: Record) => (value.providerAdapter = "package.json"), /canonical generated workflow/i], ["promotion standalone extraction", (value: Record) => value.jobs.find((candidate: any) => candidate.id === "promotion").steps.splice(6, 0, { kind: "extract", archivePath: ".release/candidate/candidate.tar.gz", targetRoot: ".release/verified-candidate" }), /step kind extract is forbidden|job step sequence drift/i], ["normalized upload root", (value: Record) => (value.jobs[0].steps.find((step: any) => step.kind === "upload").paths = ["foo/.."]), /unsafe workflow path/i], ["immutable archive field drift", (value: Record) => (value.jobs.find((candidate: any) => candidate.id === "immutable_build").steps.find((step: any) => step.kind === "archive-candidate").archivePath = ".release/other.tar.gz"), /candidate output identity drift|archive and upload fields must remain linked/i], ["provider role drift", (value: Record) => (value.jobs.find((candidate: any) => candidate.id === "vulnerability_provider").steps.find((step: any) => step.kind === "run-provider").provider = "provenance"), /provider archive, extraction, evidence, and upload fields must remain linked/i], ["provider archive SHA environment drift", (value: Record) => (value.jobs.find((candidate: any) => candidate.id === "vulnerability_provider").environment.find((entry: any) => entry.name === "CANDIDATE_ARCHIVE_SHA256").value = "0".repeat(64)), /job environment binding drift/i], ["promotion transfer swap", (value: Record) => (value.jobs.find((candidate: any) => candidate.id === "promotion").steps.find((step: any) => step.kind === "download").transferId = "vulnerability-provider-evidence"), /promotion download fields.*remain linked|duplicate.*download/i], ["raw provider upload", (value: Record) => (value.jobs.find((candidate: any) => candidate.id === "vulnerability_provider").steps.find((step: any) => step.kind === "upload").paths = ["provider-evidence/untrusted/vulnerability-report.json"]), /provider archive, extraction, evidence, and upload fields must remain linked/i], ["intervening promotion step", (value: Record) => { const steps = value.jobs.find((candidate: any) => candidate.id === "promotion").steps; steps.splice(steps.findIndex((step: any) => step.kind === "upload"), 0, { kind: "frozen-install" }); }, /promotion verification and upload must be immediately adjacent/i], ["promotion upload path drift", (value: Record) => (value.jobs.find((candidate: any) => candidate.id === "promotion").steps.find((step: any) => step.kind === "upload").paths[0] = ".release/promoted-staging/replaced.tar.gz"), /exact five typed paths/i], ["always promotion upload", (value: Record) => (value.jobs.find((candidate: any) => candidate.id === "promotion").steps.find((step: any) => step.kind === "upload").always = true), /promotion upload must not use always/i], ["provider nonce output step drift", (value: Record) => (value.jobs.find((candidate: any) => candidate.id === "vulnerability_provider").steps.find((step: any) => step.kind === "run-provider").stepId = "renamed"), /provider.*linked|step identity/i], ["promotion nonce binding drift", (value: Record) => (value.jobs.find((candidate: any) => candidate.id === "promotion").environment.find((entry: any) => entry.name === "VULNERABILITY_INVOCATION_NONCE").value = "5".repeat(64)), /job environment binding drift/i], ["missing promotion cleanup", (value: Record) => { const job = value.jobs.find((candidate: any) => candidate.id === "promotion"); job.steps = job.steps.filter((step: any) => step.kind !== "cleanup-promotion"); }, /job step sequence drift|cleanup/i], ])("rejects semantic mutation: %s", async (_name, mutate, diagnostic) => { const contract = await loadCiGateContract(process.cwd()); const candidate = JSON.parse(JSON.stringify(contract)) as Record; mutate(candidate); expect(() => parseCiGateContract(candidate)).toThrow(diagnostic); }); it("rejects package scripts missing from the shared command registry", async () => { const root = await mkdtemp(path.join(tmpdir(), "ci-contract-package-")); temporaryRoots.push(root); await mkdir(path.join(root, "config/ci"), { recursive: true }); const contract = await loadCiGateContract(process.cwd()); await writeFile(path.join(root, "config/ci/gates.json"), `${JSON.stringify(contract)}\n`); await writeFile(path.join(root, "package.json"), '{"scripts":{}}\n'); await expect(loadCiGateContract(root)).rejects.toThrow(/missing package scripts/i); }); it("rejects swapped canonical gate command ownership", async () => { const root = await mkdtemp(path.join(tmpdir(), "ci-contract-gate-shape-")); temporaryRoots.push(root); await mkdir(path.join(root, "config/ci"), { recursive: true }); const contract = JSON.parse( JSON.stringify(await loadCiGateContract(process.cwd())), ) as Record; const security = contract.gates.find((gate: Record) => gate.id === "FE-GATE-013"); const documentation = contract.gates.find((gate: Record) => gate.id === "FE-GATE-017"); [security.commandIds, documentation.commandIds] = [ documentation.commandIds, security.commandIds, ]; await writeFile(path.join(root, "config/ci/gates.json"), `${JSON.stringify(contract)}\n`); await writeFile(path.join(root, "package.json"), await readFile("package.json")); await expect(loadCiGateContract(root)).rejects.toThrow( /lacks a bound producer command/i, ); }); it.each(["check:artifact-schemas", "check:ci-workflow"])( "rejects a missing nested check:ci dependency: %s", async (removedScript) => { const root = await mkdtemp(path.join(tmpdir(), "ci-contract-script-graph-")); temporaryRoots.push(root); await mkdir(path.join(root, "config/ci"), { recursive: true }); const contract = await loadCiGateContract(process.cwd()); const packageDocument = JSON.parse(await readFile("package.json", "utf8")) as { scripts: Record; }; delete packageDocument.scripts[removedScript]; await writeFile(path.join(root, "config/ci/gates.json"), `${JSON.stringify(contract)}\n`); await writeFile(path.join(root, "package.json"), `${JSON.stringify(packageDocument)}\n`); await expect(loadCiGateContract(root)).rejects.toThrow( /missing package scripts|package script graph invalid|canonical check:ci dependency/i, ); }, ); it.each(["check:artifact-schemas", "check:ci-workflow"])( "rejects a no-op nested check:ci dependency: %s", async (bypassedScript) => { const root = await mkdtemp(path.join(tmpdir(), "ci-contract-script-meaning-")); temporaryRoots.push(root); await mkdir(path.join(root, "config/ci"), { recursive: true }); const contract = await loadCiGateContract(process.cwd()); const packageDocument = JSON.parse(await readFile("package.json", "utf8")) as { scripts: Record; }; packageDocument.scripts[bypassedScript] = "true"; await writeFile(path.join(root, "config/ci/gates.json"), `${JSON.stringify(contract)}\n`); await writeFile(path.join(root, "package.json"), `${JSON.stringify(packageDocument)}\n`); await expect(loadCiGateContract(root)).rejects.toThrow(/canonical check:ci dependency/i); }, ); it.each([ ["true bypass", "true"], ["direct self recursion", "corepack pnpm check:ci"], ["alias cycle", "corepack pnpm check:ci-alias"], ["option-form gate alias", "corepack pnpm --silent ci:gate"], ])("rejects non-canonical check:ci orchestration: %s", async (_name, command) => { const root = await mkdtemp(path.join(tmpdir(), "ci-contract-closed-script-")); temporaryRoots.push(root); await mkdir(path.join(root, "config/ci"), { recursive: true }); const contract = await loadCiGateContract(process.cwd()); const packageDocument = JSON.parse(await readFile("package.json", "utf8")) as { scripts: Record; }; packageDocument.scripts["check:ci"] = command; packageDocument.scripts["check:ci-alias"] = "corepack pnpm check:ci"; await writeFile(path.join(root, "config/ci/gates.json"), `${JSON.stringify(contract)}\n`); await writeFile(path.join(root, "package.json"), `${JSON.stringify(packageDocument)}\n`); await expect(loadCiGateContract(root)).rejects.toThrow(/exact canonical non-recursive orchestration/i); }); it.each([ "npm ci --ignore-scripts --no-ignore-scripts", "pnpm install --ignore-scripts --config.ignore-scripts=false", "pnpm ln fixture", ])("rejects an unsafe lifecycle in every contract command entry: %s", async (command) => { const root = await createUnsafeCiGateFixture(command); await expect(loadCiGateContract(root)).rejects.toThrow( /install policy|install-bearing|ignore-scripts/i, ); }); it("does not enter the production runner callback when contract preflight fails", async () => { const root = await createUnsafeCiGateFixture( "npm ci --ignore-scripts --no-ignore-scripts", ); const contracts = await import("../../scripts/contracts/ci-gates.ts"); const preflight = (contracts as typeof contracts & { withCiGatePreflight?: ( root: string, gateId: string | undefined, execute: (context: unknown) => Promise, ) => Promise; }).withCiGatePreflight; expect(preflight).toBeTypeOf("function"); if (!preflight) return; let executed = false; await expect( preflight(root, "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/install policy|install-bearing|ignore-scripts/i); expect(executed).toBe(false); }); it.each(npmPostScriptForeignScopeCommands)( "rejects post-script foreign npm scope before entering the runner callback: %s", async (command) => { const root = await createUnsafeCiGateFixture(command, { nested: "echo root-only safe", start: "echo root-only safe", test: "echo root-only safe", }); await expect(loadCiGateContract(root)).rejects.toThrow( /install policy|install-bearing|not safely parseable/i, ); let executed = false; await expect( withCiGatePreflight(root, "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/install policy|install-bearing|not safely parseable/i); expect(executed).toBe(false); }, ); it.each([ ...npmDispatchExpansionCommands, ...npmDispatchScopeEnvironmentCommands, ])( "rejects dynamic npm dispatch scope before entering the runner callback: %s", async (command) => { const root = await createUnsafeCiGateFixture(command, { nested: "echo root-only safe", test: "echo root-only safe", }); await expect(loadCiGateContract(root)).rejects.toThrow( /install policy|install-bearing|not safely parseable/i, ); let executed = false; await expect( withCiGatePreflight(root, "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/install policy|install-bearing|not safely parseable/i); expect(executed).toBe(false); }, ); it.each(npmShellPrefixScopeEnvironmentCommands)( "rejects unsafe npm shell prefix before entering the runner callback: %s", async (command) => { const root = await createUnsafeCiGateFixture(command, { nested: "echo root-only safe", test: "echo root-only safe", }); await expect(loadCiGateContract(root)).rejects.toThrow( /install policy|install-bearing|not safely parseable/i, ); let executed = false; await expect( withCiGatePreflight(root, "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/install policy|install-bearing|not safely parseable/i); expect(executed).toBe(false); }, ); it.each(unmodeledManagerPrefixCommands)( "rejects an unmodeled manager prefix before entering the runner callback: %s", async (command) => { const root = await createUnsafeCiGateFixture(command, { nested: "echo root-only safe", test: "echo root-only safe", }); await expect(loadCiGateContract(root)).rejects.toThrow( /install policy|install-bearing|not safely parseable/i, ); let executed = false; await expect( withCiGatePreflight(root, "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/install policy|install-bearing|not safely parseable/i); expect(executed).toBe(false); }, ); it.each(npmIndirectConfigAuthorityCommands)( "rejects indirect npm config authority before entering the runner callback: %s", async (command) => { const root = await createUnsafeCiGateFixture(command, { nested: "echo root-only safe", test: "echo root-only safe", }); await expect(loadCiGateContract(root)).rejects.toThrow( /install policy|install-bearing|not safely parseable/i, ); let executed = false; await expect( withCiGatePreflight(root, "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/install policy|install-bearing|not safely parseable/i); expect(executed).toBe(false); }, ); it.each(npmPreDelimiterGlobCommands)( "rejects pre-delimiter npm glob expansion before entering the runner callback: %s", async (command) => { const root = await createUnsafeCiGateFixture(command, { nested: "echo root-only safe", test: "echo root-only safe", }); await expect(loadCiGateContract(root)).rejects.toThrow( /install policy|install-bearing|not safely parseable/i, ); let executed = false; await expect( withCiGatePreflight(root, "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/install policy|install-bearing|not safely parseable/i); expect(executed).toBe(false); }, ); it("rejects scoped npm lifecycle before entering the runner callback", async () => { const root = await createUnsafeCiGateFixture( "npm_config_prefix=fixture npm ci --ignore-scripts", ); let executed = false; await expect( withCiGatePreflight(root, "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/install policy|install-bearing|not safely parseable/i); expect(executed).toBe(false); }); it.each(npmLifecycleHookDispatches)( "rejects unsafe npm hooks before entering the runner callback: %s", async (command, scriptName) => { const root = await createUnsafeCiGateFixture(command, { [`pre${scriptName}`]: "npm ci", [scriptName]: "echo root main safe", [`post${scriptName}`]: "npm ci", }); await expect(loadCiGateContract(root)).rejects.toThrow( /install policy|install-bearing|ignore-scripts/i, ); let executed = false; await expect( withCiGatePreflight(root, "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/install policy|install-bearing|ignore-scripts/i); expect(executed).toBe(false); }, ); it("rejects inherited npm scope configuration before entering the runner callback", async () => { const variable = "NpM_CoNfIg_WoRkSpAcE"; const previous = process.env[variable]; process.env[variable] = "fixture"; let executed = false; try { await expect( withCiGatePreflight(process.cwd(), "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/npm.*scope.*environment|npm_config_workspace/i); expect(executed).toBe(false); } finally { if (previous === undefined) delete process.env[variable]; else process.env[variable] = previous; } }); it.each(["NPM_CONFIG_USERCONFIG", "npm_config_globalconfig"])( "rejects inherited indirect npm config authority before callback: %s", async (variable) => { const previous = process.env[variable]; process.env[variable] = "fixture"; let executed = false; try { await expect( withCiGatePreflight(process.cwd(), "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/npm.*scope.*environment|npm_config/i); expect(executed).toBe(false); } finally { if (previous === undefined) delete process.env[variable]; else process.env[variable] = previous; } }, ); // 1101/15-05. `npx vitest` and npm-launched IDE runners hand the test // process npm_config_prefix/userconfig/globalconfig. The preflight above // rightly refuses them, so 79 tests here failed for the caller's // environment, not for the code. vitest.config.ts drops them. it("starts without the npm scope variables an npm-launched runner sets", () => { expect(Object.keys(process.env).filter(isNpmScopeEnvironmentName)).toEqual([]); }); it.each([ ["npm run nested --ignore-scripts=false", "nested"], ["npm test -- --ignore-scripts=true", "test"], ])( "keeps unsafe npm hooks inside contract preflight without effective suppression: %s", async (command, scriptName) => { const root = await createUnsafeCiGateFixture(command, { [`pre${scriptName}`]: "npm ci", [scriptName]: "echo root main safe", [`post${scriptName}`]: "npm ci", }); let executed = false; await expect( withCiGatePreflight(root, "FE-GATE-001", async () => { executed = true; }), ).rejects.toThrow(/install policy|install-bearing|ignore-scripts/i); expect(executed).toBe(false); }, ); it.each([ ["npm --ignore-scripts=true run nested", "nested"], ["npm test --ignore-scripts=true", "test"], ["npm restart --ignore-scripts true", "restart"], ["npm --ignore-scripts run nested", "nested"], ["npm run nested --ignore-scripts", "nested"], ["npm run-script nested --ignore-scripts", "nested"], ["npm start --ignore-scripts", "start"], ["npm stop --ignore-scripts", "stop"], ["npm restart --ignore-scripts", "restart"], ])( "allows contract execution when effective true suppresses npm hooks: %s", async (command, scriptName) => { const root = await createUnsafeCiGateFixture(command, { [`pre${scriptName}`]: "npm ci", [scriptName]: "echo root main safe", [`post${scriptName}`]: "npm ci", }); let executed = false; await withCiGatePreflight(root, "FE-GATE-001", async () => { executed = true; }); expect(executed).toBe(true); }, ); it("detects reachable alias cycles and option-form ci:gate invocations", () => { expect(validatePackageScriptGraph({ "check:ci": "pnpm alias", alias: "pnpm check:ci" }, "check:ci")) .toEqual(expect.arrayContaining([expect.stringMatching(/cycle/i)])); expect(validatePackageScriptGraph({ "check:ci": "pnpm alias", alias: "pnpm --silent ci:gate", "ci:gate": "node scripts/run-ci-gate.ts" }, "check:ci")) .toEqual(expect.arrayContaining([expect.stringMatching(/must not invoke ci:gate/i)])); }); it.each(npmLifecycleHookDispatches)( "traverses existing npm pre/main/post scripts in lifecycle order: %s", (gate, scriptName) => { const preScript = `pre${scriptName}`; const postScript = `post${scriptName}`; const scripts = { gate, [preScript]: "npm run missing:pre", [scriptName]: "npm run missing:main", [postScript]: "npm run missing:post", }; expect(validatePackageScriptGraph(scripts, "gate")).toEqual([ `package script missing: ${preScript} -> missing:pre`, `package script missing: ${scriptName} -> missing:main`, `package script missing: ${postScript} -> missing:post`, ]); }, ); it.each(npmLifecycleHookDispatches)( "applies install policy to existing npm pre/main/post scripts: %s", (gate, scriptName) => { const preScript = `pre${scriptName}`; const postScript = `post${scriptName}`; const scripts = { gate, [preScript]: "npm ci", [scriptName]: "npm ci", [postScript]: "npm ci", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toEqual([ `install-bearing package script must use --ignore-scripts: ${preScript}`, `install-bearing package script must use --ignore-scripts: ${scriptName}`, `install-bearing package script must use --ignore-scripts: ${postScript}`, ]); }, ); it.each([ ["npm --ignore-scripts=true run nested", "nested"], ["npm --ignore-scripts true run nested", "nested"], ["npm run nested --ignore-scripts=true", "nested"], ["npm test --ignore-scripts=true", "test"], ["npm restart --ignore-scripts true", "restart"], ["npm --ignore-scripts run nested", "nested"], ["npm run nested --ignore-scripts", "nested"], ["npm run-script nested --ignore-scripts", "nested"], ["npm start --ignore-scripts", "start"], ["npm stop --ignore-scripts", "stop"], ["npm restart --ignore-scripts", "restart"], ])("omits npm hooks for effective suppression: %s", (gate, scriptName) => { const scripts = { gate, [`pre${scriptName}`]: "npm ci", [scriptName]: "echo root main safe", [`post${scriptName}`]: "npm run missing:post", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toEqual([]); expect(validatePackageScriptGraph(scripts, "gate")).toEqual([]); }); it.each([ ["npm --ignore-scripts=false run nested", "nested"], ["npm test --ignore-scripts=false", "test"], ["npm --ignore-scripts=true run nested --no-ignore-scripts", "nested"], ["npm restart --ignore-scripts=true --ignore-scripts=false", "restart"], ["npm run nested -- --ignore-scripts=true", "nested"], ["npm test -- --ignore-scripts=true", "test"], ])("keeps npm hooks for non-effective or post-delimiter suppression: %s", (gate, scriptName) => { const preScript = `pre${scriptName}`; const postScript = `post${scriptName}`; const scripts = { gate, [preScript]: "npm ci", [scriptName]: "echo root main safe", [postScript]: "npm run missing:post", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( `install-bearing package script must use --ignore-scripts: ${preScript}`, ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( `package script missing: ${postScript} -> missing:post`, ); }); it("rejects reachable nested package installs without lifecycle suppression", () => { const unsafe = { gate: "corepack pnpm nested", nested: "corepack pnpm install --frozen-lockfile", }; const safe = { ...unsafe, nested: "corepack pnpm install --frozen-lockfile --ignore-scripts", }; expect(validateInstallScriptPolicy(unsafe, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: nested", ); expect(validateInstallScriptPolicy(safe, ["gate"])).toEqual([]); }); it.each([ "corepack pnpm install --frozen-lockfile && echo --ignore-scripts", "corepack pnpm install && corepack pnpm install --ignore-scripts", ])("rejects an unsafe install invocation masked by another token: %s", (nested) => { const scripts = { gate: "corepack pnpm nested", nested, }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: nested", ); }); it.each([ "corepack pnpm --dir=fixture install", "corepack pnpm --dir fixture install", "pnpm -C fixture install", "pnpm --dir . install", "pnpm i", "npm --prefix fixture install", "npm ci", "npm i", "yarn --cwd fixture install", ])("rejects install aliases and manager-global-option forms: %s", (nested) => { expect(validateInstallScriptPolicy({ gate: "pnpm nested", nested }, ["gate"])) .toContain("install-bearing package script must use --ignore-scripts: nested"); }); it.each([ "corepack pnpm --dir", "pnpm --unknown-manager-option install --ignore-scripts", ])("fails closed for an unparsed reachable manager invocation: %s", (nested) => { expect(validateInstallScriptPolicy({ gate: "pnpm nested", nested }, ["gate"])) .toContain("install-bearing package script must use --ignore-scripts: nested"); }); it("treats a single ampersand as a command boundary for install policy", () => { const scripts = { gate: "pnpm install & pnpm install --ignore-scripts", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); }); it.each([ "pnpm install --ignore-scripts=false", "pnpm install --ignore-scripts --ignore-scripts=false", "npm ci --ignore-scripts=false", "npm install --ignore-scripts=true --ignore-scripts=false", "yarn install --ignore-scripts=false", "yarn install --ignore-scripts --ignore-scripts=false", "pnpm install --ignore-scripts=", "npm ci --ignore-scripts=maybe", "yarn install --ignore-scripts false", "pnpm install --ignore-scripts=false --ignore-scripts=true", ])("rejects ineffective or contradictory lifecycle suppression: %s", (gate) => { expect(validateInstallScriptPolicy({ gate }, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); }); it.each([ "pnpm install --ignore-scripts=true", "npm ci --ignore-scripts", "yarn install --ignore-scripts=true --ignore-scripts=true", ])("accepts an unambiguous effective ignore-scripts true: %s", (gate) => { expect(validateInstallScriptPolicy({ gate }, ["gate"])).toEqual([]); }); it.each([ "npm --silent run nested", "yarn nested", "yarn run nested", "corepack yarn nested", ])("follows manager-option and yarn script invocations into unsafe installs: %s", (gate) => { const scripts = { gate, nested: "npm ci" }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: nested", ); }); it.each([ "npm --silent run missing", "corepack yarn run missing", ])("uses the manager parser for package graph dependencies: %s", (gate) => { expect(validatePackageScriptGraph({ gate }, "gate")).toContain( "package script missing: gate -> missing", ); }); it.each([ "yarn workspace fixture run nested", "yarn workspace fixture nested", "yarn workspaces foreach -A run nested", ])("fails closed for an unmodeled Yarn workspace dispatcher: %s", (gate) => { const scripts = { gate, nested: "npm ci" }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }); it.each([ "pnpm install # --ignore-scripts", "npm ci # --ignore-scripts=true", ])("fails closed when an unquoted shell comment masks lifecycle suppression: %s", (gate) => { const scripts = { gate }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }); it("keeps a quoted hash as ordinary lifecycle argument content", () => { const scripts = { gate: "pnpm install '#' --ignore-scripts" }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toEqual([]); expect(validatePackageScriptGraph(scripts, "gate")).toEqual([]); }); it.each([ "npm ci --ignore-scripts --no-ignore-scripts", "pnpm install --ignore-scripts --config.ignore-scripts=false", "pnpm install --ignore-scripts --unknown-lifecycle-setting", "pnpm install --config.ignore-scripts=maybe --ignore-scripts", ])("rejects contradictory, malformed, or unknown lifecycle settings: %s", (gate) => { expect(validateInstallScriptPolicy({ gate }, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); }); it.each([ "npm ci --ignore-scripts true", "pnpm install --config.ignore-scripts", "pnpm install --config.ignore-scripts=true", "pnpm install --config.ignore-scripts true --frozen-lockfile", ])("accepts a supported canonical lifecycle suppression form: %s", (gate) => { expect(validateInstallScriptPolicy({ gate }, ["gate"])).toEqual([]); }); it("canonicalizes a lifecycle builtin alias before package-script lookup", () => { const scripts = { gate: "pnpm ln fixture", ln: "npm ci", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toEqual([ "install-bearing package script must use --ignore-scripts: gate", ]); expect(validatePackageScriptGraph(scripts, "gate")).toEqual([]); }); it.each([ "pnpm frobnicate", "yarn frobnicate", ])("fails closed for an unknown manager subcommand absent from root scripts: %s", (gate) => { const scripts = { gate }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }); it.each([ "pnpm --filter fixture run nested", "pnpm --dir fixture run nested", "pnpm -C fixture nested", "npm --workspace fixture run nested", "npm --prefix fixture run nested", "yarn --cwd fixture run nested", ])("fails closed when package-script dispatch changes authoritative manifest scope: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe" }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }); it.each(npmPostScriptForeignScopeCommands)( "fails closed when post-script npm options change package-script scope: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", start: "echo root-only safe", test: "echo root-only safe", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }, ); it.each([ ...npmDispatchExpansionCommands, ...npmDispatchScopeEnvironmentCommands, ])("fails closed for dynamic npm dispatch scope: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", test: "echo root-only safe", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }); it.each(npmShellPrefixScopeEnvironmentCommands)( "fails closed for unsafe npm shell prefix: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", test: "echo root-only safe", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }, ); it.each(unmodeledManagerPrefixCommands)( "fails closed for an unmodeled manager prefix: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", test: "echo root-only safe", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }, ); it.each(npmIndirectConfigAuthorityCommands)( "fails closed for indirect npm config authority: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", test: "echo root-only safe", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }, ); it.each(npmPreDelimiterGlobCommands)( "fails closed for unquoted glob expansion before npm's literal delimiter: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", test: "echo root-only safe", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }, ); it.each([ "npm_config_prefix=fixture npm ci --ignore-scripts", "env npm_config_workspace=fixture npm audit", ])("rejects npm scope environment for every npm command class: %s", (gate) => { expect(validateInstallScriptPolicy({ gate }, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph({ gate }, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }); it.each([ "npm run nested --workspaces", "npm run nested -wfixture", "npm run nested -w=fixture", "npm run nested --unknown-manager-option", ])("fails closed for additional or unknown pre-delimiter npm options: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe" }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }); it.each([ "pnpm run nested --filter foreign", "pnpm nested --dir foreign", "yarn run nested --cwd foreign", "yarn nested --cwd foreign", ])("treats other-manager tokens after a script name as script arguments: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", test: "echo root-only safe", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toEqual([]); expect(validatePackageScriptGraph(scripts, "gate")).toEqual([]); }); it.each([ "npm run nested -- --workspace foreign", "npm run-script nested -- --prefix foreign", "npm test -- --workspace foreign", "npm start -- --unknown-manager-option", "SCOPE=--workspace; npm run nested -- $SCOPE fixture", "SCOPE=--prefix; npm test -- ${SCOPE} fixture", ])("treats npm tokens after the script-argument delimiter as script arguments: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", start: "echo root-only safe", test: "echo root-only safe", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toEqual([]); expect(validatePackageScriptGraph(scripts, "gate")).toEqual([]); }); it.each([ "SAFE=value npm run nested", "npm_config_loglevel=warn npm run nested", "/bin/env npm_config_loglevel=warn npm run nested", "command env SAFE=value npm run nested", "command -- env SAFE=value npm run nested", "command -p env SAFE=value npm run nested", "command /opt/reviewer/bin/env SAFE=value npm run nested", "exec env SAFE=value npm run nested", "exec /bin/env SAFE=value npm run nested", "command exec env SAFE=value npm run nested", "SAFE=1 command -- npm run nested", "command exec env SAFE=value corepack npm run nested", "SAFE=1 corepack pnpm nested", "exec corepack pnpm nested", "MESSAGE='npm_config_workspace=fixture' npm run nested", "MESSAGE=\"npm_config_${SAFE_NAME}=fixture\" npm run nested", "echo 'npm_config_workspace=fixture' && npm run nested", "echo 'set -a; npm_config_prefix=fixture' && npm run nested", "export npm_config_loglevel=warn && npm run nested", "SAFE=value; export SAFE; npm run nested", "SAFE=1 export npm_config_loglevel=warn; npm run nested", "SAFE=1 set -a; SAFE_TWO=2; npm run nested", "command npm run nested", "exec npm run nested", "env -i SAFE=value npm run nested", "/usr/bin/env --ignore-environment npm test", "env -u npm_config_workspace npm run nested", "env --unset=NPM_CONFIG_PREFIX npm test", "env -- npm run nested", "env '$NAME=fixture' npm run nested", "SCOPE=--workspace; pnpm run nested $SCOPE fixture", "SCOPE=--workspace; yarn run nested ${SCOPE} fixture", ])("preserves unrelated environment and other-manager script arguments: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", test: "echo root-only safe", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toEqual([]); expect(validatePackageScriptGraph(scripts, "gate")).toEqual([]); }); it.each([ "npm run nested '*'", 'npm test "?"', "npm run nested \\*", "npm run nested -- *", "npm test -- [a-z]*", ])("preserves quoted, escaped, or post-delimiter glob arguments: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", test: "echo root-only safe" }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toEqual([]); expect(validatePackageScriptGraph(scripts, "gate")).toEqual([]); }); it.each([ "npm run nested", "npm run nested --if-present", "npm run nested --ignore-scripts=true", "npm test --foreground-scripts", "npm start", ])("keeps current-tree npm dispatch and exact harmless options supported: %s", (gate) => { const scripts = { gate, nested: "echo root-only safe", start: "echo root-only safe", test: "echo root-only safe", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toEqual([]); expect(validatePackageScriptGraph(scripts, "gate")).toEqual([]); }); it.each([ "pnpm --dir fixture install --ignore-scripts", "npm --prefix fixture ci --ignore-scripts", "yarn --cwd fixture install --ignore-scripts", ])("still classifies externally scoped lifecycle commands by suppression: %s", (gate) => { expect(validateInstallScriptPolicy({ gate }, ["gate"])).toEqual([]); expect(validatePackageScriptGraph({ gate }, "gate")).toEqual([]); }); it.each([ "npm init unreviewed-tool", "npm explore fixture -- npm ci", "npm audit fix", "yarn npm publish", ])("fails closed for an argument-sensitive or mutating builtin dispatcher: %s", (gate) => { expect(validateInstallScriptPolicy({ gate }, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph({ gate }, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }); it.each(["npm audit", "pnpm audit"])( "accepts only an exact bare read-only audit builtin: %s", (gate) => { expect(validateInstallScriptPolicy({ gate }, ["gate"])).toEqual([]); expect(validatePackageScriptGraph({ gate }, "gate")).toEqual([]); }, ); it.each([ "npm --silent audit", "npm --prefix fixture audit", "npm -- audit", "npm audit --silent", "npm audit --", "pnpm --silent audit", "pnpm --dir fixture audit", "pnpm -- audit", "pnpm audit --silent", "pnpm audit --", ])("rejects a read-only builtin with manager syntax or arguments: %s", (gate) => { expect(validateInstallScriptPolicy({ gate }, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph({ gate }, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }); it.each([ "pnpm exec npm ci", "corepack pnpm exec npm install", "pnpm dlx npm ci", "npm exec pnpm install", "pnpm exec unreviewed-tool", ])("fails closed for nested manager and unknown executable dispatchers: %s", (gate) => { expect(validateInstallScriptPolicy({ gate }, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph({ gate }, "gate")).toContain( "package script manager invocation is not safely parseable: gate", ); }); it.each([ "pnpm add fixture", "npm update fixture", "yarn rebuild fixture", "corepack pnpm rebuild fixture", ])("treats lifecycle mutation builtins as unsafe even when a script has the same name: %s", (gate) => { const scripts = { gate, add: "echo script", update: "echo script", rebuild: "echo script", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: gate", ); expect(validatePackageScriptGraph(scripts, "gate")).not.toEqual( expect.arrayContaining([expect.stringMatching(/package script missing|cycle/u)]), ); }); it("allows an explicit run of a script whose name collides with a manager builtin", () => { const scripts = { gate: "pnpm run add", add: "npm ci", }; expect(validateInstallScriptPolicy(scripts, ["gate"])).toContain( "install-bearing package script must use --ignore-scripts: add", ); }); it("rejects an invalid package graph before the gate runner can spawn it", async () => { const root = await mkdtemp(path.join(tmpdir(), "ci-contract-runner-preflight-")); temporaryRoots.push(root); await mkdir(path.join(root, "config/ci"), { recursive: true }); const marker = path.join(root, "spawned"); const contract = await loadCiGateContract(process.cwd()); const packageDocument = JSON.parse(await readFile("package.json", "utf8")) as { scripts: Record }; packageDocument.scripts["check:ci"] = `node -e 'require("node:fs").writeFileSync(${JSON.stringify(marker)}, "spawned")'`; await writeFile(path.join(root, "config/ci/gates.json"), `${JSON.stringify(contract)}\n`); await writeFile(path.join(root, "package.json"), `${JSON.stringify(packageDocument)}\n`); const result = spawnSync(process.execPath, [path.resolve("scripts/run-ci-gate.ts"), "FE-GATE-010"], { cwd: root, encoding: "utf8" }); expect(result.status).not.toBe(0); await expect(readFile(marker)).rejects.toMatchObject({ code: "ENOENT" }); }, childProcessTimeout(1_298)); it("records checked-in workflow drift as a failing typed report", async () => { const root = await mkdtemp(path.join(tmpdir(), "ci-contract-workflow-drift-")); temporaryRoots.push(root); await mkdir(path.join(root, "config/ci"), { recursive: true }); await mkdir(path.join(root, ".gitea/workflows"), { recursive: true }); await mkdir(path.join(root, "artifacts/quality"), { recursive: true }); const contract = await loadCiGateContract(process.cwd()); await writeFile(path.join(root, "config/ci/gates.json"), `${JSON.stringify(contract)}\n`); await writeFile(path.join(root, "package.json"), await readFile("package.json")); await writeFile(path.join(root, ".nvmrc"), await readFile(".nvmrc")); const drift = Buffer.from("drifted workflow\n", "utf8"); await writeFile(path.join(root, ".gitea/workflows/quality-gates.yml"), drift); const result = spawnSync(process.execPath, [path.resolve("scripts/check-ci-contract.ts")], { cwd: root, encoding: "utf8", }); expect(result.status).toBe(1); const report = JSON.parse( await readFile(path.join(root, "artifacts/quality/ci-contract.json"), "utf8"), ) as { passed: boolean; failures: string[]; workflowSha256: string }; expect(report.passed).toBe(false); expect(report.failures).toEqual(expect.arrayContaining([expect.stringMatching(/workflow drift/i)])); expect(report.workflowSha256).toBe( await import("node:crypto").then(({ createHash }) => createHash("sha256").update(drift).digest("hex")), ); }, childProcessTimeout(1_376)); }); describe("CI workflow generation", () => { it("renders the complete workflow deterministically with one final LF", async () => { const contract = await loadCiGateContract(process.cwd()); const first = renderCiWorkflow(contract); const second = renderCiWorkflow(contract); expect(second).toBe(first); expect(first).toMatchSnapshot(); expect(first).toMatch(/^# GENERATED FILE/u); expect(first.endsWith("\n")).toBe(true); expect(first.endsWith("\n\n")).toBe(false); expect(first).not.toContain("\r"); expect(first).not.toMatch(/\\\$\{\{/u); expect(first).toContain( 'node scripts/verify-ci-candidate-archive.ts --archive ".release/release-candidate-${{ gitea.run_id }}-${{ gitea.run_attempt }}.tar.gz" --github-output "$GITHUB_OUTPUT"', ); expect(first).not.toContain("process_dist_sha256"); expect(first).toContain("persist-credentials: false"); // A newer push to the same pull request or branch cancels the older run; // a manual run never cancels a running one. expect(first).toContain( [ "concurrency:", " group: frontend-quality-gates-${{ gitea.event_name }}-${{ gitea.ref }}-${{ inputs.stage }}", " cancel-in-progress: ${{ gitea.event_name != 'workflow_dispatch' }}", ].join("\n"), ); expect( first.match(/corepack pnpm install --frozen-lockfile --ignore-scripts/gu), ).toHaveLength(12); expect(first).not.toMatch(/corepack pnpm install --frozen-lockfile$/mu); expect(first).toContain("verify-ci-candidate-archive.ts --archive"); // Providers and promotion read the archive without extracting it; only the // production drills extract the promoted archive (13-03). expect(first.match(/--extract-to/gu)).toHaveLength(1); expect(first.indexOf("--extract-to")).toBeGreaterThan(first.indexOf(" production_gate:\n")); expect(first).not.toMatch(/\btar\s+[^\n]*--extract/u); expect(first).toContain("node scripts/stage-verified-promotion.ts"); expect(first).toContain("outputs:\n invocation_nonce: ${{ steps.supervise_vulnerability.outputs.invocation_nonce }}"); expect(first).toContain("outputs:\n invocation_nonce: ${{ steps.supervise_provenance.outputs.invocation_nonce }}"); expect(first).toContain('VULNERABILITY_INVOCATION_NONCE: "${{ needs.vulnerability_provider.outputs.invocation_nonce }}"'); expect(first).toContain('PROVENANCE_INVOCATION_NONCE: "${{ needs.provenance_provider.outputs.invocation_nonce }}"'); expect(first).toContain("${{ steps.finalize.outputs.staging_root }}/release-candidate.tar.gz"); expect(first).not.toContain(".release/promoted-staging"); const promotionJobStart = first.indexOf(" promotion:\n"); const productionJobStart = first.indexOf(" production_gate:\n"); expect(promotionJobStart).toBeGreaterThan(0); expect(productionJobStart).toBeGreaterThan(promotionJobStart); const promotionJob = first.slice(promotionJobStart, productionJobStart); expect(promotionJob.slice(0, promotionJob.indexOf(" steps:\n"))).not.toMatch(/^\s+if:/mu); expect(promotionJob).not.toContain("cancelled()"); const finalizerIndex = first.indexOf("node scripts/stage-verified-promotion.ts"); const promotedUploadIndex = first.indexOf("Upload promoted release"); const cleanupStepIndex = first.indexOf("- name: Always remove private promotion staging"); const cleanupIndex = first.indexOf("node scripts/cleanup-verified-promotion.ts"); expect(finalizerIndex).toBeGreaterThan(0); expect(promotedUploadIndex).toBeGreaterThan(finalizerIndex); expect(cleanupStepIndex).toBeGreaterThan(promotedUploadIndex); expect(cleanupIndex).toBeGreaterThan(cleanupStepIndex); expect(first.slice(promotedUploadIndex, cleanupStepIndex)).not.toContain("if: always()"); expect(first.slice(cleanupStepIndex, cleanupIndex)).toContain("if: always()"); expect(first.slice(cleanupStepIndex, cleanupIndex)).toContain( 'if [ -n "$PROMOTION_STAGING_ROOT" ] && [ -n "$PROMOTION_CLEANUP_TOKEN" ] && [ -n "$PROMOTION_RUNNER_TEMP_DEV" ] && [ -n "$PROMOTION_RUNNER_TEMP_INO" ] && [ -n "$PROMOTION_STAGING_DEV" ] && [ -n "$PROMOTION_STAGING_INO" ]; then', ); const actionUses = [...first.matchAll(/^\s+-?\s*uses: (.+)$/gmu)].map((match) => match[1]); expect(actionUses).toHaveLength(43); expect(new Set(actionUses)).toEqual( new Set([ "https://github.com/actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5", "https://github.com/actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020", "https://github.com/ChristopherHX/gitea-upload-artifact@81f940d004763f986ba3582c007fd842dd5cb0d7", "https://github.com/ChristopherHX/gitea-download-artifact@75635f32b4c1c41c4b3d64e8f85210112ed4c9c7", ]), ); expect(actionUses.every((uses) => /^https:\/\/github\.com\/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+@[0-9a-f]{40}$/u.test(uses!))).toBe(true); }); // 1103/11-02. tests/visual/fonts.conf reads the goldens' faces from OS // packages that Playwright's own dependencies do not include, so a clean // runner failed FE-GATE-008 at `visual-fonts`. Every browser step installs // them, after `--with-deps` has refreshed the package lists. it("installs the visual-test font packages in every browser step", async () => { const workflow = renderCiWorkflow(await loadCiGateContract(process.cwd())); const steps = workflow.split(" - name: "); const fontsInstall = `apt-get install -y ${VISUAL_TEST_FONT_PACKAGES.join(" ")}`; // A job without its own image downloads the browsers with their OS // libraries; `--with-deps` refreshes the package lists first. const downloading = steps.filter((step) => step.startsWith("Install Playwright browsers")); // merge_browser runs in Playwright's image, which has the browsers but no // package lists (tests/unit/ci-browser-image.test.ts). const imaged = steps.filter((step) => step.startsWith("Install visual-test fonts")); expect([downloading.length, imaged.length]).toEqual([1, 1]); for (const step of downloading) { const playwright = step.indexOf("playwright install --with-deps"); expect(playwright).toBeGreaterThan(0); expect(step.indexOf(fontsInstall)).toBeGreaterThan(playwright); } for (const step of imaged) { expect(step).not.toContain("playwright install"); const update = step.indexOf("apt-get update"); expect(update).toBeGreaterThan(0); expect(step.indexOf(fontsInstall)).toBeGreaterThan(update); } expect(VISUAL_TEST_FONT_PACKAGES).toEqual([ "fonts-noto-core", "fonts-noto-cjk", "fonts-noto-cjk-extra", "fonts-liberation", ]); }); // 12-01: main is the integration branch; a push there must run the gates. it("runs on pushes to main", async () => { const workflow = renderCiWorkflow(await loadCiGateContract(process.cwd())); expect(workflow).toMatch(/^on:\n {2}push:\n {4}branches: \[main, develop\]$/mu); }); // 1105/12-01. The provider sandbox in FE-GATE-013 needs unprivileged user // and network namespaces. No runner requirement said so, and a host that // refuses them failed 18 provider tests after the prerequisite check passed. it("states FE-GATE-013's runner requirements beside the runner label of the job that runs it", async () => { const contract = await loadCiGateContract(process.cwd()); const requirements = indexCiGateContract(contract).gates.get("FE-GATE-013")?.hostRequirements ?? []; expect(requirements.join("\n")).toMatch(/unprivileged user and network namespaces/u); expect(requirements.join("\n")).toContain("--unshare-net"); expect(requirements.join("\n")).toContain("kernel.apparmor_restrict_unprivileged_userns"); expect(requirements.join("\n")).toContain("scripts/check-system-test-prerequisites.ts"); const workflow = renderCiWorkflow(contract); const merge = workflow.slice(workflow.indexOf(" merge_test:\n"), workflow.indexOf(" merge_browser:\n")); const header = merge.slice(0, merge.indexOf(" runs-on: ubuntu-latest\n")); expect(header.split("\n").filter((line) => line.startsWith(" # "))).toEqual( requirements.map((requirement) => ` # FE-GATE-013 runner requirement: ${requirement}`), ); expect(workflow.match(/ runner requirement: /gu)).toHaveLength(requirements.length); }); // The job container has neither systemd nor bubblewrap, so FE-GATE-013 runs // in the system container its contract names (hostEnvironment). it("runs a gate with a host environment through it, and counts only that run", async () => { const contract = await loadCiGateContract(process.cwd()); expect(contract.gates.filter((gate) => gate.hostEnvironment).map(({ id }) => id)).toEqual(["FE-GATE-013"]); const workflow = renderCiWorkflow(contract); const wrapped = " run: scripts/run-in-system-container.sh corepack pnpm ci:gate -- FE-GATE-013"; expect(workflow.split("\n").filter((line) => line.includes("ci:gate -- FE-GATE-013"))).toEqual([wrapped]); expect(workflowGateExecutionFailures(contract, workflow)).toEqual([]); expect(workflowGateExecutionFailures( contract, workflow.replace(wrapped, " run: corepack pnpm ci:gate -- FE-GATE-013"), )).toEqual(["gate is not executed by any workflow run step: FE-GATE-013"]); expect(workflowGateExecutionFailures( contract, workflow.replace( " run: corepack pnpm ci:gate -- FE-GATE-005", " run: scripts/run-in-system-container.sh corepack pnpm ci:gate -- FE-GATE-005", ), )).toEqual(["gate is not executed by any workflow run step: FE-GATE-005"]); }); it("rejects a runner requirement that would leave its workflow comment (1105/12-01)", async () => { const contract = JSON.parse( JSON.stringify(await loadCiGateContract(process.cwd())), ) as Record; contract.gates.find((gate: Record) => gate.id === "FE-GATE-013").hostRequirements = [ "namespaces\n runs-on: self-hosted", ]; expect(() => parseCiGateContract(contract)).toThrow(/line-break characters are forbidden/u); }); // 12-02 / 13-01: immutable_build owns FE-GATE-015 and FE-GATE-027, but only // the first gate of a non-matrix job was ever rendered, so release admission // never ran in CI. it("runs every gate a job owns, in order", async () => { const contract = await loadCiGateContract(process.cwd()); const workflow = renderCiWorkflow(contract); const immutable = workflow.slice( workflow.indexOf(" immutable_build:\n"), workflow.indexOf(" vulnerability_provider:\n"), ); expect([...immutable.matchAll(/ci:gate -- (FE-GATE-\d{3})$/gmu)].map((match) => match[1])) .toEqual(["FE-GATE-015", "FE-GATE-027"]); expect(workflowGateExecutionFailures(contract, workflow)).toEqual([]); }); // 1105/13-01. ci:gate writes each command's output only to the gate log and // prints one PASS/FAIL line. immutable_build uploaded only the candidate, on // success, so why FE-GATE-015 or FE-GATE-027 failed, and the advisory record // FE-GATE-027 writes (1103/13-03), went with the runner's workspace. const IMMUTABLE_GATE_EVIDENCE_PATHS = [ "artifacts/quality/gates/FE-GATE-015.txt", "artifacts/release/release-candidate.json", "artifacts/release/verification.json", "artifacts/security/supply-chain-coherence.json", "artifacts/quality/gates/FE-GATE-027.txt", "artifacts/release/deployment-admission.json", "artifacts/security/dependency-advisories.json", ]; it("uploads immutable_build's gate logs, evidence and advisory record on every outcome (1105/13-01)", async () => { const contract = await loadCiGateContract(process.cwd()); const index = indexCiGateContract(contract); const gatePaths = ["FE-GATE-015", "FE-GATE-027"].flatMap((gateId) => { const gate = index.gates.get(gateId)!; return [gate.logArtifactId, ...gate.evidenceArtifactIds].map((artifactId) => index.artifacts.get(artifactId)!.path); }); expect(IMMUTABLE_GATE_EVIDENCE_PATHS).toEqual([...gatePaths, "artifacts/security/dependency-advisories.json"]); const workflow = renderCiWorkflow(contract); const immutable = workflow.slice( workflow.indexOf(" immutable_build:\n"), workflow.indexOf(" vulnerability_provider:\n"), ); const candidateUpload = immutable.indexOf(" - name: Upload release candidate\n"); const evidenceUpload = immutable.indexOf(" - name: Upload immutable gate evidence\n"); expect(candidateUpload).toBeGreaterThan(immutable.indexOf("ci:gate -- FE-GATE-027")); expect(evidenceUpload).toBeGreaterThan(candidateUpload); expect(immutable.slice(evidenceUpload)).toBe( [ " - name: Upload immutable gate evidence", " if: always()", ` uses: ${resolveCiActionUses("uploadArtifact")}`, " with:", ' name: "immutable-gate-evidence-${{ gitea.run_id }}-${{ gitea.run_attempt }}"', " path: |", ...IMMUTABLE_GATE_EVIDENCE_PATHS.map((target) => ` ${target}`), " if-no-files-found: error", "", "", ].join("\n"), ); // The candidate still goes up only after its archive was validated, and // the archive's member set is not widened by the evidence. expect(immutable.slice(candidateUpload, evidenceUpload)).not.toContain("if: always()"); const archive = contract.jobs .find(({ id }) => id === "immutable_build") ?.steps.find((step) => step.kind === "archive-candidate"); expect(archive?.kind === "archive-candidate" ? archive.members : []).not.toContain( "artifacts/quality/gates/FE-GATE-027.txt", ); }); it.each([ [ "is missing", (steps: Record[]) => steps.splice(steps.findIndex((step) => step.transferId === "immutable-gate-evidence"), 1), ], [ "runs only on success", (steps: Record[]) => delete steps.find((step) => step.transferId === "immutable-gate-evidence")!.always, ], [ "leaves out the advisory record", (steps: Record[]) => { const upload = steps.find((step) => step.transferId === "immutable-gate-evidence")!; upload.paths = upload.paths.filter((target: string) => target !== "artifacts/security/dependency-advisories.json"); }, ], [ "leaves out the FE-GATE-027 log", (steps: Record[]) => { const upload = steps.find((step) => step.transferId === "immutable-gate-evidence")!; upload.paths = upload.paths.filter((target: string) => target !== "artifacts/quality/gates/FE-GATE-027.txt"); }, ], [ "comes before the candidate upload", (steps: Record[]) => { const evidence = steps.splice(steps.findIndex((step) => step.transferId === "immutable-gate-evidence"), 1)[0]!; steps.splice(steps.findIndex((step) => step.transferId === "release-candidate"), 0, evidence); }, ], ])("rejects an immutable_build whose gate evidence upload %s (1105/13-01)", async (_name, mutate) => { const contract = JSON.parse( JSON.stringify(await loadCiGateContract(process.cwd())), ) as Record; const steps = contract.jobs.find((job: Record) => job.id === "immutable_build").steps; expect(steps.filter((step: Record) => step.transferId === "immutable-gate-evidence")).toHaveLength(1); mutate(steps); expect(() => parseCiGateContract(contract)).toThrow( /immutable_build must upload its gates' logs and evidence and the dependency advisory record/u, ); }); it.each([ ["a non-matrix gate step", " run: corepack pnpm ci:gate -- FE-GATE-027\n", "FE-GATE-027"], ["a matrix entry", " - { gate: FE-GATE-028, name: manual-accessibility-review, browser: false }\n", "FE-GATE-028"], ["a gate step of a merge job", " run: corepack pnpm ci:gate -- FE-GATE-020\n", "FE-GATE-020"], ])("rejects a workflow that drops %s", async (_name, line, gateId) => { const contract = await loadCiGateContract(process.cwd()); const workflow = renderCiWorkflow(contract); expect(workflow).toContain(line); expect(workflowGateExecutionFailures(contract, workflow.replace(line, ""))).toEqual([ `gate is not executed by any workflow run step: ${gateId}`, ]); }); it("derives download artifact names from the typed upload producer", async () => { const contract = await loadCiGateContract(process.cwd()); const candidate = JSON.parse(JSON.stringify(contract)) as Record; const producer = candidate.jobs.find( (job: Record) => job.id === "immutable_build", ); const upload = producer.steps.find( (step: Record) => step.kind === "upload", ); upload.name = "renamed-candidate-${{ gitea.run_id }}"; const rendered = renderCiWorkflow( candidate as unknown as Awaited>, ); expect(rendered.match(/name: "renamed-candidate-\$\{\{ gitea\.run_id \}\}"/gu)).toHaveLength(4); }); it("check mode reports missing and byte-level drift without writing", async () => { const root = await mkdtemp(path.join(tmpdir(), "ci-workflow-check-")); temporaryRoots.push(root); await writeFile(path.join(root, "package.json"), '{"scripts":{}}\n'); const contract = await loadCiGateContract(process.cwd()); const missing = await generateCiWorkflow({ root, contract, check: true }); expect(missing).toEqual( expect.objectContaining({ written: false, matches: false, firstDifferenceLine: 1 }), ); const target = path.join(root, ".gitea/workflows/quality-gates.yml"); await mkdir(path.dirname(target), { recursive: true }); await writeFile(target, renderCiWorkflow(contract).replace("permissions:", "permissions: ")); const drift = await generateCiWorkflow({ root, contract, check: true }); expect(drift.written).toBe(false); expect(drift.matches).toBe(false); expect(drift.firstDifferenceByte).toBeGreaterThan(0); expect(await readFile(target, "utf8")).toContain("permissions: "); }); it("check mode rejects CRLF and extra final newlines as byte drift", async () => { const root = await mkdtemp(path.join(tmpdir(), "ci-workflow-newline-")); temporaryRoots.push(root); const contract = await loadCiGateContract(process.cwd()); const target = path.join(root, ".gitea/workflows/quality-gates.yml"); await mkdir(path.dirname(target), { recursive: true }); await writeFile(target, `${renderCiWorkflow(contract).replaceAll("\n", "\r\n")}\r\n`); const result = await generateCiWorkflow({ root, contract, check: true }); expect(result.matches).toBe(false); expect(result.firstDifferenceLine).toBeGreaterThan(0); }); it("writes a missing workflow and then passes byte-for-byte check mode", async () => { const root = await mkdtemp(path.join(tmpdir(), "ci-workflow-write-")); temporaryRoots.push(root); const contract = await loadCiGateContract(process.cwd()); const written = await generateCiWorkflow({ root, contract, check: false }); expect(written).toEqual(expect.objectContaining({ written: true, matches: true })); const checked = await generateCiWorkflow({ root, contract, check: true }); expect(checked).toEqual(expect.objectContaining({ written: false, matches: true })); }); it.each(["ancestor", "leaf"])("rejects a %s symlink in check and write modes without touching its canary", async (kind) => { const root = await mkdtemp(path.join(tmpdir(), "ci-workflow-symlink-root-")); const outside = await mkdtemp(path.join(tmpdir(), "ci-workflow-symlink-outside-")); temporaryRoots.push(root, outside); const contract = await loadCiGateContract(process.cwd()); const canary = path.join(outside, "canary"); await writeFile(canary, "unchanged\n"); if (kind === "ancestor") { await mkdir(path.join(outside, "workflows"), { recursive: true }); await writeFile(path.join(outside, "workflows/quality-gates.yml"), renderCiWorkflow(contract)); await symlink(outside, path.join(root, ".gitea")); } else { await mkdir(path.join(root, ".gitea/workflows"), { recursive: true }); await writeFile(path.join(outside, "quality-gates.yml"), renderCiWorkflow(contract)); await symlink(path.join(outside, "quality-gates.yml"), path.join(root, ".gitea/workflows/quality-gates.yml")); } await expect(generateCiWorkflow({ root, contract, check: true })).rejects.toThrow(/unsafe/i); await expect(generateCiWorkflow({ root, contract, check: false })).rejects.toThrow(/unsafe/i); await expect(readFile(canary, "utf8")).resolves.toBe("unchanged\n"); }); it("preserves the destination and cleans only its owned temp when atomic write fails", async () => { const contract = await loadCiGateContract(process.cwd()); const removed: string[] = []; const flags: Array<{ flags: number; mode: number }> = []; let renamed = false; const fileSystem: CiWorkflowFileSystem = { mkdir: async () => undefined, readFile: async () => Buffer.from("existing workflow\n"), open: async (_target, openFlags, mode) => { flags.push({ flags: openFlags, mode }); return { writeFile: async () => { throw new Error("injected write failure"); }, chmod: async () => undefined, sync: async () => undefined, close: async () => undefined, }; }, openDirectory: async () => ({ sync: async () => undefined, close: async () => undefined, }), rename: async () => { renamed = true; }, rm: async (target) => { removed.push(target); }, }; const generate = createCiWorkflowGenerator({ fileSystem, createNonce: () => "owned", }); await expect( generate({ root: "/tmp/ci-workflow-atomic", contract, check: false }), ).rejects.toThrow(/injected write failure/u); expect(renamed).toBe(false); expect(removed).toEqual([ "/tmp/ci-workflow-atomic/.gitea/workflows/.quality-gates.yml.owned.tmp", ]); expect(flags).toEqual([ { flags: constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, mode: 0o644, }, ]); }); });