"""Alignment contract: mgf.test_supervisor.records ↔ mgf.common.testing.

Per TS-23 the supervisor (this library) MUST be stdlib-only — so
it cannot import from `mgf.common.testing` at runtime. mgf-common
is one of the consumers under test; the supervisor must run even
when mgf-common is broken. The two surfaces therefore carry
parallel copies of the JSONL schema:

  - `mgf.common.testing.TestRecord` / `SCHEMA_VERSION` /
    `MARKER_TIER` — the importable library shape consumers use to
    read `run.jsonl`.
  - `mgf.test_supervisor.records.TestRecord` / `SCHEMA_VERSION` and
    `mgf.test_supervisor.tiers.MARKER_TIER` — the supervisor's
    own stdlib-only copies.

These tests assert the two stay aligned. If a field is renamed in
one without the other, this file's tests fail and the mismatch
surfaces immediately — where it can be fixed in one edit.

(The alternative — having the supervisor import from
`mgf.common.testing` — would violate TS-23: a broken mgf-common
runtime would break the test runner that's trying to diagnose
why mgf-common is broken. The duplication is the lesser cost; the
contract test is the alignment guarantee.)

`mgf-common` is declared as a DEV dependency (not a runtime dep)
so the alignment can be checked in CI without coupling the
runtime install footprint.
"""

from __future__ import annotations

import pytest

from mgf.test_supervisor import records as sup_records
from mgf.test_supervisor import tiers as sup_tiers

try:
    from mgf.common import testing as public
except ImportError:  # pragma: no cover
    pytest.skip("mgf-common not installed; alignment tests skipped", allow_module_level=True)


class TestSchemaVersionAligned:
    def test_schema_versions_match(self) -> None:
        assert public.SCHEMA_VERSION == sup_records.SCHEMA_VERSION, (
            f"Schema-version drift: public {public.SCHEMA_VERSION!r} "
            f"vs supervisor {sup_records.SCHEMA_VERSION!r}. Bump both "
            f"in the same commit."
        )


class TestMarkerTierAligned:
    def test_marker_tier_mappings_match(self) -> None:
        public_mapping = dict(public.MARKER_TIER)
        sup_mapping = dict(sup_tiers.MARKER_TIER)
        assert public_mapping == sup_mapping, (
            f"MARKER_TIER drift:\n"
            f"  public:     {public_mapping}\n"
            f"  supervisor: {sup_mapping}\n"
            f"  diff (public - supervisor): "
            f"{set(public_mapping.items()) - set(sup_mapping.items())}\n"
            f"  diff (supervisor - public): "
            f"{set(sup_mapping.items()) - set(public_mapping.items())}"
        )


class TestRecordFieldsAligned:
    def test_field_names_match(self) -> None:
        import dataclasses

        public_fields = {f.name for f in dataclasses.fields(public.TestRecord)}
        sup_fields = {f.name for f in dataclasses.fields(sup_records.TestRecord)}
        assert public_fields == sup_fields, (
            f"TestRecord field drift:\n"
            f"  public-only:    {public_fields - sup_fields}\n"
            f"  supervisor-only: {sup_fields - public_fields}"
        )

    def test_jsonl_round_trip_through_public_reader(self) -> None:
        sup_rec = sup_records.TestRecord(
            test_id="tests/unit/foo.py::test_bar",
            outcome="passed",
            severity="OK",
            duration_ms=12,
            tier=1,
            markers=("contract",),
            run_id="run-id",
            ts_iso="2026-05-26T01:11:37.000Z",
        )
        line = sup_rec.to_json()
        public_rec = public.TestRecord.from_jsonl_line(line)
        assert public_rec.test_id == sup_rec.test_id
        assert public_rec.outcome is public.Outcome.PASSED
        assert public_rec.tier == sup_rec.tier
        assert public_rec.markers == list(sup_rec.markers)
