"""All shard execution durations count toward the unchanged serial CPU budget."""
import json
from pathlib import Path
from xml.etree import ElementTree as ET
import pytest
from scripts.check_quality_policy import validate_duration_evidence
from scripts.write_test_evidence import junit_snapshot
@pytest.mark.parametrize("times", [(1, 1200), (1200, 1)])
@pytest.mark.parametrize("version", ["3.11", "3.12"])
def test_later_or_earlier_slow_shard_exceeds_existing_suite_budget(tmp_path, times, version):
root = ET.Element("testsuites")
for index, seconds in enumerate(times):
suite = ET.SubElement(root, "testsuite", {"time": str(seconds)})
case = ET.SubElement(suite, "testcase", {"classname": "tests.example", "name": f"test_{index}", "time": "0.01"})
props = ET.SubElement(case, "properties")
ET.SubElement(props, "property", {"name": "duration_markers", "value": "cpu"})
path = tmp_path / "junit.xml"
ET.ElementTree(root).write(path)
snapshot = junit_snapshot(path)
assert snapshot["suite_duration_seconds"] == 1201.0
assert snapshot["duration_seconds"] == 0.02 # Case budgets remain a separate measure.
policy = json.loads((Path(__file__).parents[1] / "ci/test-quality-policy.json").read_text())
assert policy["test_evidence"]["duration_budgets"]["mandatory_cpu"]["max_suite_seconds_by_python"][version] == 1050.0
failures = validate_duration_evidence({"python": version, "tests": snapshot}, policy)
assert f"test trend Python {version} suite duration 1201.000s exceeds 1050.000s" in failures
@pytest.mark.parametrize("bad", ["nan", "inf", "-1", "invalid"])
def test_invalid_later_suite_cannot_hide_behind_first_suite(tmp_path, bad):
path = tmp_path / "junit.xml"
path.write_text(f'')
with pytest.raises(ValueError, match="testsuite has invalid time"):
junit_snapshot(path)
def test_missing_time_falls_back_per_suite_without_dropping_other_suites(tmp_path):
path = tmp_path / "junit.xml"
path.write_text('')
assert junit_snapshot(path)["suite_duration_seconds"] == 5.0