"""task-2693 regression: envelope owner_key vs actual cron channel owner_key
양방향 cross-check (PR #152 hardening #2 + 회장 verbatim 필수 4-요구 #1).

검증 대상: utils.callback_authority_4source_validator.validate
"""
from __future__ import annotations

import importlib.util
import sys
from pathlib import Path

_ROOT = Path(__file__).resolve().parents[3]
if str(_ROOT) not in sys.path:
    sys.path.insert(0, str(_ROOT))


def _load_real(modname: str, relpath: str):
    existing = sys.modules.get(modname)
    if existing is not None and getattr(existing, "__file__", "").endswith(relpath):
        return existing
    spec = importlib.util.spec_from_file_location(modname, _ROOT / relpath)
    assert spec is not None and spec.loader is not None
    mod = importlib.util.module_from_spec(spec)
    sys.modules[modname] = mod
    spec.loader.exec_module(mod)
    return mod


_load_real(
    "utils.callback_authority_4source_validator",
    "utils/callback_authority_4source_validator.py",
)

from utils.callback_authority_4source_validator import (  # noqa: E402
    ACTUAL_CRON_OWNER_NOT_ANU,
    AUTHORITATIVE_4SOURCE_OK,
    DEFAULT_ANU_KEYS,
    ENVELOPE_ACTUAL_CRON_OWNER_MISMATCH,
    ENVELOPE_OWNER_NOT_ANU,
    validate,
)

# exported surface sanity: ANU key 는 validator default set 에 포함되어야 한다.
assert "c119085addb0f8b7" in DEFAULT_ANU_KEYS

ANU = "c119085addb0f8b7"
SELF = "fedf78d1d09509f5"
SID = "53e89540-5bed-4692-a726-ed857820758a"


def _envelope(owner_key=ANU, sid=SID):
    return {
        "owner_key": owner_key,
        "chair_facing_session_id": sid,
        "collector_session_id": sid,
        "delivery_session_id": sid,
    }


def _sh(sid=SID):
    return {"status": "ok", "chair_facing_session_id": sid}


def test_envelope_self_key_rejected():
    v = validate(
        envelope=_envelope(owner_key=SELF),
        actual_cron_owner_key=ANU,
        schedule_history_record=_sh(),
    )
    assert v.classification == ENVELOPE_OWNER_NOT_ANU, v


def test_actual_cron_self_key_rejected():
    v = validate(
        envelope=_envelope(),
        actual_cron_owner_key=SELF,
        schedule_history_record=_sh(),
    )
    assert v.classification == ACTUAL_CRON_OWNER_NOT_ANU, v


def test_actual_cron_missing_rejected():
    v = validate(
        envelope=_envelope(),
        actual_cron_owner_key=None,
        schedule_history_record=_sh(),
    )
    assert v.classification == ACTUAL_CRON_OWNER_NOT_ANU, v


def test_envelope_actual_owner_mismatch_via_alt_anu():
    # 둘 다 ANU keys set 안에 있지만 서로 다른 key 일 때 cross-check 가 잡아야 한다.
    ALT_ANU = "deadbeefdeadbeef"
    anu_set = [ANU, ALT_ANU]
    v = validate(
        envelope=_envelope(owner_key=ANU),
        actual_cron_owner_key=ALT_ANU,
        schedule_history_record=_sh(),
        anu_keys=anu_set,
    )
    assert v.classification == ENVELOPE_ACTUAL_CRON_OWNER_MISMATCH, v


def test_both_anu_and_equal_passes():
    v = validate(
        envelope=_envelope(),
        actual_cron_owner_key=ANU,
        schedule_history_record=_sh(),
    )
    assert v.classification == AUTHORITATIVE_4SOURCE_OK, v
