"""The gate: no stored spec may lack the component contract.

Reads the real database. The gate is about what is stored, not what the code
intends.
"""

import pytest

from orchestrator import bot_factory as bf
from orchestrator.contract import _canonical, attach_contract, contract_gaps


def test_no_stored_spec_lacks_the_contract():
    offenders = []
    for row in bf.list_specs(limit=1000):
        gaps = contract_gaps(row.get("spec") or {})
        if gaps:
            offenders.append((row.get("bot_name"), gaps))
    assert offenders == [], "specs stored without the contract: %r" % (offenders,)


def test_contract_is_deterministic():
    rows = bf.list_specs(limit=1)
    if not rows:
        pytest.skip("no specs stored")
    spec = rows[0].get("spec") or {}
    assert _canonical(attach_contract(spec)) == _canonical(attach_contract(spec))
