from __future__ import annotations

from pathlib import Path

from sync_state_machine.engine import StateMachineConfig, StateMachineRuntime


CFG_PATH = Path(__file__).resolve().parents[2] / "sync_state_machine" / "config" / "node_state_machine.yaml"


def test_precondition_failed_requires_update_action() -> None:
    cfg = StateMachineConfig.load(CFG_PATH)
    runtime = StateMachineRuntime(cfg)

    bad_snapshot = {
        "binding_status": "NORMAL",
        "action": "NONE",
        "status": "PRECONDITION_FAILED",
        "data_id_exist": False,
    }
    violations = runtime.check_invariants(bad_snapshot)
    assert any(v.invariant_id == "INV-3_PRECOND_IMPLIES_UPDATE" for v in violations)

    good_snapshot = {
        "binding_status": "NORMAL",
        "action": "UPDATE",
        "status": "PRECONDITION_FAILED",
        "data_id_exist": True,
    }
    violations2 = runtime.check_invariants(good_snapshot)
    assert not any(v.invariant_id == "INV-3_PRECOND_IMPLIES_UPDATE" for v in violations2)
