from eth_account import Account

from src.tools.hl_testnet_legacy_credential_probe import build_legacy_probe_plan


def test_legacy_probe_plan_does_not_require_trading_flags(tmp_path, monkeypatch):
    signer = Account.create()
    wallet = Account.create().address
    (tmp_path / "Testnet.env").write_text(
        f"HL_WALLET_ADDRESS={wallet}\n"
        f"HL_API_PRIVATE_KEY={signer.key.hex()}\n"
        f"API Key={signer.address}\n",
        encoding="utf-8",
    )
    monkeypatch.delenv("CTB_HL_ENV", raising=False)
    monkeypatch.delenv("CTB_TESTNET_TRADING_ALLOWED", raising=False)

    plan = build_legacy_probe_plan(search_dirs=(tmp_path,))

    assert plan["status"] == "validated"
    assert plan["signed_actions_blocked_until_credentials_green"] is True
    assert "CTB_HL_ENV_not_testnet" in plan["warnings"]
    assert plan["mainnet_blocked"] is True


def test_legacy_probe_plan_uses_wallet_as_account_and_derived_signer(tmp_path, monkeypatch):
    signer = Account.create()
    wallet = Account.create().address
    (tmp_path / "Testnet.env").write_text(
        f"HL_WALLET_ADDRESS={wallet}\n"
        f"HL_API_PRIVATE_KEY={signer.key.hex()}\n"
        f"API Key={Account.create().address}\n",
        encoding="utf-8",
    )
    monkeypatch.setenv("CTB_HL_ENV", "testnet")
    monkeypatch.setenv("CTB_TESTNET_TRADING_ALLOWED", "true")

    plan = build_legacy_probe_plan(search_dirs=(tmp_path,))

    assert plan["status"] == "blocked"
    assert plan["account_address_masked"].startswith(wallet[:6])
    assert plan["agent_wallet_address_derived_masked"].startswith(signer.address[:6])
    assert plan["signed_actions_blocked_until_credentials_green"] is True
    assert plan["mainnet_blocked"] is True
    assert signer.key.hex() not in str(plan)
