|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +import concore |
| 7 | + |
| 8 | + |
| 9 | +FIXTURE_DIR = Path(__file__).parent / "protocol_fixtures" |
| 10 | +SCHEMA_PATH = FIXTURE_DIR / "schema.phase1.json" |
| 11 | +CASES_PATH = FIXTURE_DIR / "python_phase1_cases.json" |
| 12 | +SUPPORTED_TARGETS = {"parse_params", "initval", "write_zmq"} |
| 13 | + |
| 14 | + |
| 15 | +def _load_json(path): |
| 16 | + with path.open("r", encoding="utf-8") as f: |
| 17 | + return json.load(f) |
| 18 | + |
| 19 | + |
| 20 | +def _validate_fixture_document_shape(doc): |
| 21 | + required_top = {"schema_version", "runtime", "mode", "cases"} |
| 22 | + missing = required_top - set(doc.keys()) |
| 23 | + if missing: |
| 24 | + raise AssertionError(f"Fixture document missing required top-level keys: {sorted(missing)}") |
| 25 | + if doc["runtime"] != "python": |
| 26 | + raise AssertionError(f"Phase-1 fixture runtime must be 'python', found: {doc['runtime']}") |
| 27 | + if doc["mode"] != "report_only": |
| 28 | + raise AssertionError(f"Phase-1 fixture mode must be 'report_only', found: {doc['mode']}") |
| 29 | + if not isinstance(doc["cases"], list) or not doc["cases"]: |
| 30 | + raise AssertionError("Fixture document must contain a non-empty 'cases' list") |
| 31 | + |
| 32 | + for idx, case in enumerate(doc["cases"]): |
| 33 | + for key in ("id", "target", "input", "expected"): |
| 34 | + if key not in case: |
| 35 | + raise AssertionError(f"Case index {idx} missing required key '{key}'") |
| 36 | + if case["target"] not in SUPPORTED_TARGETS: |
| 37 | + raise AssertionError( |
| 38 | + f"Case '{case['id']}' has unsupported target '{case['target']}'" |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def _run_parse_params_case(case): |
| 43 | + result = concore.parse_params(case["input"]["sparams"]) |
| 44 | + assert result == case["expected"]["result"] |
| 45 | + |
| 46 | + |
| 47 | +def _run_initval_case(case): |
| 48 | + old_simtime = concore.simtime |
| 49 | + try: |
| 50 | + concore.simtime = case["input"]["initial_simtime"] |
| 51 | + result = concore.initval(case["input"]["simtime_val_str"]) |
| 52 | + assert result == case["expected"]["result"] |
| 53 | + assert concore.simtime == case["expected"]["simtime_after"] |
| 54 | + finally: |
| 55 | + concore.simtime = old_simtime |
| 56 | + |
| 57 | + |
| 58 | +def _run_write_zmq_case(case): |
| 59 | + class DummyPort: |
| 60 | + def __init__(self): |
| 61 | + self.sent_payload = None |
| 62 | + |
| 63 | + def send_json_with_retry(self, message): |
| 64 | + self.sent_payload = message |
| 65 | + |
| 66 | + old_simtime = concore.simtime |
| 67 | + port_name = f"fixture_{case['id'].replace('/', '_')}" |
| 68 | + existing_port = concore.zmq_ports.get(port_name) |
| 69 | + dummy_port = DummyPort() |
| 70 | + |
| 71 | + try: |
| 72 | + concore.simtime = case["input"]["initial_simtime"] |
| 73 | + concore.zmq_ports[port_name] = dummy_port |
| 74 | + concore.write( |
| 75 | + port_name, |
| 76 | + case["input"]["name"], |
| 77 | + case["input"]["value"], |
| 78 | + delta=case["input"]["delta"], |
| 79 | + ) |
| 80 | + assert dummy_port.sent_payload == case["expected"]["sent_payload"] |
| 81 | + assert concore.simtime == case["expected"]["simtime_after"] |
| 82 | + finally: |
| 83 | + concore.simtime = old_simtime |
| 84 | + if existing_port is None: |
| 85 | + concore.zmq_ports.pop(port_name, None) |
| 86 | + else: |
| 87 | + concore.zmq_ports[port_name] = existing_port |
| 88 | + |
| 89 | + |
| 90 | +def _run_case(case): |
| 91 | + if case["target"] == "parse_params": |
| 92 | + _run_parse_params_case(case) |
| 93 | + elif case["target"] == "initval": |
| 94 | + _run_initval_case(case) |
| 95 | + elif case["target"] == "write_zmq": |
| 96 | + _run_write_zmq_case(case) |
| 97 | + else: |
| 98 | + raise AssertionError(f"Unsupported target: {case['target']}") |
| 99 | + |
| 100 | + |
| 101 | +def _load_cases(): |
| 102 | + doc = _load_json(CASES_PATH) |
| 103 | + _validate_fixture_document_shape(doc) |
| 104 | + return doc["cases"] |
| 105 | + |
| 106 | + |
| 107 | +def test_phase1_schema_file_present_and_basic_shape(): |
| 108 | + schema = _load_json(SCHEMA_PATH) |
| 109 | + assert schema["title"] == "Concore Protocol Conformance Fixtures (Phase 1)" |
| 110 | + assert "cases" in schema["properties"] |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.parametrize("case", _load_cases(), ids=lambda case: case["id"]) |
| 114 | +def test_phase1_python_protocol_conformance(case): |
| 115 | + _run_case(case) |
0 commit comments