from fxhnt.application.gate_policy import POLICY, POLICY_VERSION, GatePolicy, policy_hash

_EXPECTED_HASH = "6b1be01449f69daca99f274fd7f292c9ddc91eb3437c185bc27a5441dd2d2c65"


def test_policy_defaults_match_todays_constants():
    # these values must equal the pre-B1 reconciliation_gate / gate.py constants (byte-identical verdicts)
    assert (POLICY.min_forward_days, POLICY.recon_tolerance_frac, POLICY.recon_min_band,
            POLICY.recon_min_corr, POLICY.max_gap_days) == (14, 0.5, 0.05, 0.0, 1)
    assert (POLICY.min_days, POLICY.min_total_return, POLICY.min_sharpe) == (20, 0.0, 0.0)
    assert (POLICY.defund_window, POLICY.refund_window) == (10, 15)
    assert POLICY_VERSION == 2


def test_policy_hash_is_stable_and_content_derived():
    assert policy_hash(POLICY, POLICY_VERSION) == policy_hash(POLICY, POLICY_VERSION)  # stable
    changed = GatePolicy(min_forward_days=21)  # field changed
    # → different hash when policy changes
    assert policy_hash(changed, POLICY_VERSION) != policy_hash(POLICY, POLICY_VERSION)
    # version bump → different hash
    assert policy_hash(POLICY, 2) != policy_hash(POLICY, 1)


def test_policy_hash_selfcheck_forces_version_bump():
    # PIN the current hash. If a policy field changes without this literal being updated (and the version
    # bumped), this test fails — forcing a deliberate, audited change. Update BOTH on any real policy change.
    assert policy_hash(POLICY, POLICY_VERSION) == _EXPECTED_HASH  # noqa: F821 — defined below in the test file
