Files
Pulse/scripts/tests/test_frontend_workflow_order.py
pulse-triage[bot] d3460e6128 Verify both steps of the required Frontend check
Keep the runtime and unit proof assertions and exercise the actual triage verdict before a no-runtime pass. Require exact triage dependencies without putting Frontend behind Go compilation. This reconciles the two smoke suites that passed at base and failed preflight on the CI routing change.

Contract-Neutral: Regression-test alignment only; workflow and subsystem contracts, checks, and permissions are unchanged.
Change-source: pulse-maintainer
2026-11-20 22:54:39 +00:00

287 lines
16 KiB
Python

#!/usr/bin/env python3
"""The complete unit suite and the real frontend build must both reach the required verdict."""
import os
from pathlib import Path
import subprocess
import unittest
import yaml
ROOT = Path(__file__).resolve().parents[2]
WORKFLOW = ROOT / ".github/workflows/build-and-test.yml"
TRIAGE_VERDICT_STEP = "Require applicable triage proof when runtime is not applicable"
RUNTIME_VERDICT_STEP = "Require complete frontend proof"
class FrontendWorkflowOrderTest(unittest.TestCase):
def setUp(self):
self.workflow = yaml.safe_load(WORKFLOW.read_text())
self.jobs = self.workflow["jobs"]
self.job = self.jobs["frontend-build"]
self.steps = self.job["steps"]
def step(self, name, *, job="frontend-build"):
matches = [step for step in self.jobs[job]["steps"] if step.get("name") == name]
self.assertEqual(len(matches), 1, f"expected one {name} step")
return matches[0]
def test_preinstall_serialization_step_runs_its_registered_graph_controls(self):
# Run the actual dependency-free pre-install recipe, not just the
# Python workflow assertions. Its Node controls also read this workflow
# and must follow the producer when build and runtime jobs are split.
# Otherwise a stale job selector can pass local proof and stop CI before
# npm installation, leaving all expensive proof inapplicable.
check = self.step("Check Solid serialization dependency edges")
self.assertEqual(check, {
"name": "Check Solid serialization dependency edges",
"run": "node --test scripts/tests/check-frontend-serialization.test.mjs\n"
"node scripts/check-frontend-serialization.mjs\n",
})
names = [step.get("name") for step in self.steps]
self.assertLess(names.index(check["name"]), names.index("Install frontend dependencies"))
result = subprocess.run(["bash", "-e", "-u", "-o", "pipefail", "-c", check["run"]],
cwd=ROOT, text=True, capture_output=True, timeout=15)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_type_build_and_size_admit_native_runtime_in_order(self):
names = [step.get("name") for step in self.steps]
ordered = [
"Install frontend dependencies",
"Type-check frontend",
"Build frontend bundle (with embed copy)",
"Check frontend bundle size budget",
"Bind the validated frontend embed",
"Transfer the validated frontend embed",
"Admit frontend runtime",
"Require frontend dependency audit",
]
for name in ordered:
self.step(name)
positions = [names.index(name) for name in ordered]
self.assertEqual(positions, sorted(positions))
def test_admission_keeps_real_embed_build_and_unchanged_commands(self):
commands = {
"Type-check frontend": "npm run type-check",
"Build frontend bundle (with embed copy)": "make frontend",
"Check frontend bundle size budget": "npm run check:bundlesize",
}
for name, command in commands.items():
with self.subTest(step=name):
step = self.step(name)
self.assertEqual(step["run"], command)
# GitHub's default success() condition must stop here on a
# failed prerequisite; neither warnings nor conditional skips
# may turn a broken build into a successful Frontend check.
self.assertNotIn("if", step)
self.assertNotIn("continue-on-error", step)
if name != "Build frontend bundle (with embed copy)":
self.assertEqual(step["working-directory"], "frontend-modern")
self.assertNotIn("continue-on-error", self.job)
def test_audit_still_reports_failure_after_an_earlier_failed_gate(self):
audit = self.step("Audit complete frontend dependency graph")
required = self.step("Require frontend dependency audit")
self.assertEqual(audit["id"], "audit-complete")
self.assertIs(audit["continue-on-error"], True)
self.assertEqual(required["if"], "${{ !cancelled() }}")
self.assertEqual(required["env"]["COMPLETE_AUDIT_RESULT"],
"${{ steps.audit-complete.outcome }}")
self.assertNotIn("continue-on-error", required)
self.assertIn('if [ "${COMPLETE_AUDIT_RESULT}" != success ]; then', required["run"])
self.assertIn("exit 1", required["run"])
def test_runtime_admission_is_after_bundle_transfer_but_before_the_audit_verdict(self):
admission = self.step("Admit frontend runtime")
self.assertEqual(admission["id"], "runtime-admission")
self.assertEqual(admission["env"], {
"EMBED_ARTIFACT_ID": "${{ steps.embed-upload.outputs.artifact-id }}",
})
self.assertIn('echo \'admitted=true\' >> "$GITHUB_OUTPUT"', admission["run"])
self.assertEqual(self.job["outputs"], {
"runtime_admitted": "${{ steps.runtime-admission.outputs.admitted }}",
"embed_sha256": "${{ steps.embed-packet.outputs.sha256 }}",
"embed_artifact_id": "${{ steps.embed-upload.outputs.artifact-id }}",
})
self.assertEqual(self.job["needs"], "changes")
self.assertEqual(self.job["if"], "needs.changes.outputs.code == 'true'")
self.assertEqual(self.job["timeout-minutes"], 30)
# GitHub's default success() is essential: a failed/hidden build or transfer
# phase must never produce the output admitting native runtime.
for step in self.steps:
if step["name"].startswith("Retain bounded "):
self.assertEqual(step["if"], "always()")
elif step["name"] != "Require frontend dependency audit":
self.assertNotIn("if", step)
if step["name"] != "Audit complete frontend dependency graph":
self.assertNotIn("continue-on-error", step)
self.assertEqual(self.steps[-1]["name"], "Require frontend dependency audit")
def test_one_unit_runner_starts_at_classification_and_runs_the_complete_inventory(self):
unit = self.jobs["frontend-unit"]
self.assertEqual(unit["needs"], "changes")
self.assertEqual(unit["if"], "needs.changes.outputs.code == 'true'")
self.assertNotIn("strategy", unit)
self.assertEqual(unit["runs-on"], "ubuntu-24.04")
self.assertEqual(unit["timeout-minutes"], 30)
self.assertNotIn("continue-on-error", unit)
self.assertNotIn("env", unit)
steps = unit["steps"]
self.assertEqual([step["name"] for step in steps], [
"Checkout repository", "Set up Node.js", "Install frontend dependencies", "Frontend unit tests",
"Retain frontend unit observations",
])
for step in steps[:-1]:
self.assertNotIn("if", step)
self.assertNotIn("continue-on-error", step)
if step["name"] != "Frontend unit tests":
self.assertEqual(step, self.step(step["name"]))
self.assertEqual(steps[-2], {
"name": "Frontend unit tests", "working-directory": "frontend-modern",
"env": {
"PULSE_UNIT_RECEIPT": "${{ runner.temp }}/frontend-unit-${{ github.run_attempt }}.json",
"PULSE_UNIT_SOURCE_SHA": "${{ github.sha }}",
},
"run": "npm run test -- "
"--reporter=default --reporter=./scripts/ci-unit-receipts.mjs",
})
self.assertEqual(steps[-1], {
"name": "Retain frontend unit observations", "if": "always()",
"uses": "actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9",
"with": {
"name": "frontend-unit-${{ github.run_attempt }}",
"path": "${{ runner.temp }}/frontend-unit-${{ github.run_attempt }}.json",
"if-no-files-found": "warn", "retention-days": 7,
},
})
self.assertEqual(self.workflow["permissions"], {"contents": "read"})
def test_unit_reporter_controls_are_registered_and_run_before_dependency_work(self):
step = self.step("Validate frontend unit evidence reporter")
self.assertEqual(step["run"], "node --test scripts/tests/frontend-unit-receipts.test.mjs")
names = [step["name"] for step in self.steps]
self.assertLess(names.index(step["name"]), names.index("Install frontend dependencies"))
result = subprocess.run(["node", "--test", "scripts/tests/frontend-unit-receipts.test.mjs"],
cwd=ROOT, text=True, capture_output=True, timeout=15)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_runtime_and_units_are_independent_required_siblings(self):
runtime = self.jobs["frontend-checks"]
unit = self.jobs["frontend-unit"]
self.assertEqual(runtime["needs"], ["changes", "frontend-build"])
self.assertEqual(unit["needs"], "changes")
self.assertEqual(runtime["if"], "${{ !cancelled() && needs.changes.outputs.code == 'true' && needs.frontend-build.outputs.runtime_admitted == 'true' }}")
self.assertEqual(runtime["runs-on"], "ubuntu-24.04")
self.assertEqual(runtime["timeout-minutes"], 30)
self.assertNotIn("continue-on-error", runtime)
names = [step["name"] for step in runtime["steps"]]
ordered = ["Check validated frontend embed identity",
"Download this run's validated frontend embed",
"Verify and restore the validated frontend embed",
"Set up Go for real-embed verification", "Verify backend with real frontend embed",
"Check current-source unattended recovery on native systemd",
"Check ordinary notification policy and restart"]
self.assertEqual([names.index(name) for name in ordered], sorted(names.index(name) for name in ordered))
for step in runtime["steps"]:
self.assertNotIn("continue-on-error", step)
if step["name"].startswith("Retain bounded "):
self.assertEqual(step["if"], "always()")
else:
self.assertNotIn("if", step)
def verdict(self, **values):
env = dict(os.environ, CHANGE_RESULT="success", CODE_CHANGED="true",
BUILD_RESULT="success", CHECKS_RESULT="success", UNIT_RESULT="success",
TRIAGE_REQUIRED="false", TRIAGE_RESULT="skipped")
env.update(values)
def run(name):
step = self.step(name, job="frontend")
return subprocess.run(["bash", "-e", "-u", "-o", "pipefail", "-c", step["run"]],
env=env, text=True, capture_output=True, timeout=5)
# The first step has its own GitHub condition; a failure prevents the
# following step (default success()) from reporting a runtime verdict.
if env["CODE_CHANGED"] == "false":
triage = run(TRIAGE_VERDICT_STEP)
if triage.returncode:
return triage
return run(RUNTIME_VERDICT_STEP)
def test_required_name_depends_on_classification_checks_and_unit_suite(self):
verdict = self.jobs["frontend"]
self.assertEqual(verdict["name"], "Frontend")
self.assertEqual(sum(job.get("name") == "Frontend" for job in self.jobs.values()), 1)
self.assertEqual(verdict["needs"],
["changes", "frontend-build", "frontend-checks", "frontend-unit", "triage-helper"])
self.assertEqual(verdict["if"], "always()")
self.assertNotIn("continue-on-error", verdict)
self.assertEqual([item.get("name") for item in verdict["steps"]],
[TRIAGE_VERDICT_STEP, RUNTIME_VERDICT_STEP])
triage = self.step(TRIAGE_VERDICT_STEP, job="frontend")
self.assertEqual(triage["if"], "always() && needs.changes.outputs.code == 'false'")
self.assertNotIn("continue-on-error", triage)
self.assertEqual(triage["env"], {
"TRIAGE_REQUIRED": "${{ needs.changes.outputs.triage_helper }}",
"TRIAGE_RESULT": "${{ needs.triage-helper.result }}",
})
step = self.step(RUNTIME_VERDICT_STEP, job="frontend")
self.assertNotIn("if", step)
self.assertNotIn("continue-on-error", step)
self.assertEqual(step["env"], {
"CHANGE_RESULT": "${{ needs.changes.result }}",
"CODE_CHANGED": "${{ needs.changes.outputs.code }}",
"BUILD_RESULT": "${{ needs.frontend-build.result }}",
"CHECKS_RESULT": "${{ needs.frontend-checks.result }}",
"UNIT_RESULT": "${{ needs.frontend-unit.result }}",
})
result = self.verdict()
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("the unit suite passed", result.stdout)
for name in ("CHANGE_RESULT", "BUILD_RESULT", "CHECKS_RESULT", "UNIT_RESULT"):
for state in ("failure", "cancelled", "skipped", "", "unknown", "pending", "SUCCESS"):
with self.subTest(name=name, state=state):
result = self.verdict(**{name: state})
self.assertNotEqual(result.returncode, 0)
self.assertIn("::error::Frontend proof", result.stdout)
for state in ("false", "", "unknown", "TRUE"):
with self.subTest(classification=state):
self.assertNotEqual(self.verdict(CODE_CHANGED=state).returncode, 0)
def test_only_consistent_inapplicable_proof_can_skip_the_unit_inventory(self):
values = dict(CODE_CHANGED="false", BUILD_RESULT="skipped", CHECKS_RESULT="skipped", UNIT_RESULT="skipped")
for required, state in (("false", "skipped"), ("true", "success")):
with self.subTest(required=required, triage=state):
result = self.verdict(**values, TRIAGE_REQUIRED=required, TRIAGE_RESULT=state)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("no frontend tests ran", result.stdout)
for required, state in (("true", "skipped"), ("true", "failure"), ("true", "cancelled"),
("true", ""), ("false", ""), ("", "skipped")):
with self.subTest(required=required, triage=state):
result = self.verdict(**values, TRIAGE_REQUIRED=required, TRIAGE_RESULT=state)
self.assertNotEqual(result.returncode, 0)
self.assertIn("::error::Issue-triage proof", result.stdout)
self.assertNotIn("no frontend tests ran", result.stdout)
for name in ("CHANGE_RESULT", "BUILD_RESULT", "CHECKS_RESULT", "UNIT_RESULT", "CODE_CHANGED"):
states = ("failure", "cancelled", "", "unknown")
if name in ("BUILD_RESULT", "CHECKS_RESULT", "UNIT_RESULT"):
states += ("success",)
for state in states:
with self.subTest(name=name, state=state):
self.assertNotEqual(self.verdict(**{**values, name: state}).returncode, 0)
def test_complete_audit_failure_still_fails_after_build_admits_runtime(self):
required = self.step("Require frontend dependency audit")
for state in ("success", "failure", "cancelled", "skipped", "", "unknown"):
with self.subTest(state=state):
result = subprocess.run(["bash", "-e", "-u", "-o", "pipefail", "-c", required["run"]],
env=dict(os.environ, COMPLETE_AUDIT_RESULT=state),
text=True, capture_output=True, timeout=5)
self.assertEqual(result.returncode == 0, state == "success")
if __name__ == "__main__":
unittest.main()