from __future__ import annotations

from pathlib import Path

from src.ctb_copy.read_only_guard import DEFAULT_READ_ONLY_GUARD, ReadOnlyGuard


def test_readonly_guard_defaults_are_safe() -> None:
    guard = DEFAULT_READ_ONLY_GUARD

    guard.assert_safe()
    assert guard.as_dict() == {
        "read_only": True,
        "live_orders": False,
        "vault_deposits": False,
        "wallet_execution": False,
        "signing_enabled": False,
    }


def test_ctb_copy_productive_code_contains_no_blocked_execution_terms() -> None:
    root = Path("src/ctb_copy")
    blocked = tuple(
        "".join(parts)
        for parts in [
            ("create", "_", "order"),
            ("market", "_", "open"),
            ("set", "_", "leverage"),
            ("vault", "Deposit"),
            ("exchange", ".", "order"),
            ("Account", ".", "from", "_", "key"),
            ("HL", "_", "AGENT", "_", "PRIVATE", "_", "KEY"),
            ("HL", "_", "API", "_", "PRIVATE", "_", "KEY"),
        ]
    )
    offenders: list[str] = []
    for path in root.rglob("*.py"):
        text = path.read_text(encoding="utf-8")
        for needle in blocked:
            if needle in text:
                offenders.append(f"{path}:{needle}")
    assert offenders == []


def test_unsafe_guard_raises() -> None:
    guard = ReadOnlyGuard(read_only=False)
    try:
        guard.assert_safe()
    except RuntimeError as exc:
        assert "not read-only safe" in str(exc)
    else:  # pragma: no cover
        raise AssertionError("unsafe guard did not raise")
