"""Focused coverage-helper tests for titan-iac quality modules.""" from __future__ import annotations import textwrap from pathlib import Path from testing.quality_coverage import compute_workspace_line_coverage, run_check def test_compute_workspace_line_coverage_handles_missing_xml(tmp_path: Path) -> None: """Missing coverage XML should produce a zero workspace coverage score.""" contract = {"coverage": {"tracked_files": ["managed.py"]}} assert ( compute_workspace_line_coverage(contract, tmp_path, tmp_path / "missing.xml") == 0.0 ) def test_compute_workspace_line_coverage_averages_present_tracked_files( tmp_path: Path, ) -> None: """Workspace coverage should average only tracked files that appear in the report.""" coverage_xml = tmp_path / "coverage.xml" coverage_xml.write_text( textwrap.dedent( """\ """ ), encoding="utf-8", ) contract = {"coverage": {"tracked_files": ["alpha.py", "beta.py", "missing.py"]}} assert compute_workspace_line_coverage(contract, tmp_path, coverage_xml) == 75.0 def test_run_check_keeps_relative_names_when_source_roots_do_not_match( tmp_path: Path, ) -> None: """Relative filenames should remain relative when no declared source root contains them.""" coverage_xml = tmp_path / "coverage.xml" unmatched_root = tmp_path / "other-root" unmatched_root.mkdir() coverage_xml.write_text( textwrap.dedent( f"""\ {unmatched_root} """ ), encoding="utf-8", ) issues = run_check( {"coverage": {"minimum_percent": 95.0, "tracked_files": ["relative.py"]}}, tmp_path, coverage_xml, ) assert issues == ["coverage below 95.0%: relative.py (80.0%)"] def test_run_check_enforces_branch_floor_and_requires_branch_data( tmp_path: Path, ) -> None: """Tracked modules must report and meet the configured branch floor.""" coverage_xml = tmp_path / "coverage.xml" coverage_xml.write_text( textwrap.dedent( """\ """ ), encoding="utf-8", ) issues = run_check( { "coverage": { "minimum_percent": 95.0, "minimum_branch_percent": 95.0, "tracked_files": ["low.py", "missing.py"], } }, tmp_path, coverage_xml, ) assert issues == [ "branch coverage below 95.0%: low.py (90.0%)", "branch coverage missing for tracked file: missing.py", ] def test_run_check_limits_branch_floor_to_explicit_branch_tracked_files( tmp_path: Path, ) -> None: """A branch rollout can cover selected security modules without hiding lines.""" coverage_xml = tmp_path / "coverage.xml" coverage_xml.write_text( """ """, encoding="utf-8", ) issues = run_check( { "coverage": { "minimum_percent": 95, "minimum_branch_percent": 95, "tracked_files": ["new.py", "legacy.py"], "branch_tracked_files": ["new.py"], } }, tmp_path, coverage_xml, ) assert issues == []