Files
ersatztv/scripts/tests/test_decisions_lib.py
T
timothy 52a8f16224
PR Gates / CI image pin matches docker/ci (pull_request) Successful in 9s
review-verdict/h10 Awaiting review verdict for 52a8f16
Review verdict / Set review-verdict status (pull_request) Successful in 3s
PR Gates / Docs update reminder (pull_request) Successful in 15s
PR Gates / decisions lifecycle (pull_request) Successful in 15s
PR Gates / Script tests (pytest) (pull_request) Failing after 20s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 6m37s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 9s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 8s
Build ErsatzTV Image / Functional E2E (curl + UI contracts) (pull_request) Successful in 18m27s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 27m11s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
probe(631): deliberately broken test to measure that script-tests goes red in CI
Throwaway branch, never merged. Satisfies the #631 Done-when box requiring the
enforcement be verified by MEASUREMENT rather than a green tick.
2026-07-26 01:00:35 +02:00

99 lines
3.8 KiB
Python

from pathlib import Path
import pytest
import scripts.decisions_lib as dl
FIX = Path(__file__).parent / "fixtures" / "sample_decisions.md"
def test_parses_migrated_record():
recs = dl.parse_file(FIX)
migrated = [r for r in recs if r.key == "ci.runner-placement"]
assert len(migrated) == 1
r = migrated[0]
assert r.status == "active"
assert r.since == "2026-07-17"
assert r.supersedes == "none"
assert r.superseded_by == "none"
assert r.rule == "Every CI services container gets an explicit cap."
assert r.signals is not None
assert "issues: #390 #406" in r.signals
assert r.mechanics is not None
assert r.mechanics.startswith("docs/ci-cd.md")
def test_legacy_record_is_unmigrated():
recs = dl.parse_file(FIX)
legacy = [r for r in recs if r.heading.endswith("(#231)")]
assert len(legacy) == 1
assert legacy[0].key is None
assert legacy[0].status == "legacy-unmigrated"
def test_index_section_parses_as_heading():
recs = dl.parse_file(FIX)
assert any(r.heading == "Index" for r in recs)
def test_parses_optional_stale_after_and_sources():
recs = dl.parse_file(FIX)
r = next(r for r in recs if r.key == "ci.peak-anon-measurement")
assert r.stale_after == "2027-01-15"
assert r.sources is not None
assert "gitea run 4471" in r.sources
def test_optional_fields_default_to_none_when_absent():
recs = dl.parse_file(FIX)
r = next(r for r in recs if r.key == "ci.runner-placement")
assert r.stale_after is None
assert r.sources is None
def test_empty_stale_after_parses_as_empty_string_not_none():
"""Absent vs present-but-empty must stay distinguishable for the validator."""
text = (
"## H\n"
"`key: a.b` · `status: active` · `since: 2026-01-01` · `stale-after:` "
"· `supersedes: none` · `superseded-by: none`\n"
"**Rule:** r\n"
)
r = dl.parse_text(text, Path("fake.md"))[0]
assert r.stale_after == ""
def test_frontmatter_reader_matches_pyyaml_on_every_real_record():
"""The dependency-free reader must agree with PyYAML on the whole real corpus.
The read path cannot import PyYAML — it runs in CI's `decisions lifecycle` job, the Husky
pre-commit hook, and on every contributor's machine, none of which install it. (Requiring it
made the validator crash with ModuleNotFoundError once the corpus was migrated.) A hand parser
is only safe if it provably matches the library that WROTE the files, so this compares the two
across every record rather than on a sample.
"""
yaml = pytest.importorskip("yaml")
files = [p for p in dl.RECORDS_DIR.rglob("*.md")] + [p for p in dl.ARCHIVE_DIR.rglob("*.md")]
files = [f for f in files if dl.has_frontmatter(f.read_text(encoding="utf-8"))]
assert len(files) > 100, f"only {len(files)} frontmatter files found — test would be near-vacuous"
diffs = []
for f in files:
lines = f.read_text(encoding="utf-8").splitlines()
end = next(i for i, ln in enumerate(lines[1:], start=1) if ln.rstrip() == "---")
block = "\n".join(lines[1:end])
mine = dl._read_frontmatter(block)
theirs = yaml.safe_load(block) or {}
theirs = {k: ("" if v is None else str(v)) for k, v in theirs.items()}
if mine != theirs:
for k in set(mine or {}) | set(theirs):
if (mine or {}).get(k) != theirs.get(k):
diffs.append(f"{f.name}:{k}\n mine ={(mine or {}).get(k)!r}\n pyyaml={theirs.get(k)!r}")
assert not diffs, f"{len(diffs)} field(s) differ from PyYAML:\n" + "\n".join(diffs[:5])
def test_DELIBERATELY_BROKEN_negative_control_for_631():
"""Throwaway: proves the new script-tests CI job can actually go red. Never merged."""
assert 1 == 2, "negative control for #631 — this SHOULD fail in CI"