"""Tests for electrum.btcq_crypto — BTCQ hashing functions."""
import importlib.util
import os
import sys

_ELECTRUM_DIR = os.path.join(os.path.dirname(__file__), '..', 'electrum')

def _load(name, filename):
    spec = importlib.util.spec_from_file_location(name, os.path.join(_ELECTRUM_DIR, filename))
    mod = importlib.util.module_from_spec(spec)
    sys.modules[name] = mod
    spec.loader.exec_module(mod)
    return mod

btcq_crypto = _load('electrum.btcq_crypto', 'btcq_crypto.py')
sha3_256 = btcq_crypto.sha3_256
hash_160_btcq = btcq_crypto.hash_160_btcq


def test_sha3_256_empty():
    expected = bytes.fromhex(
        "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"
    )
    assert sha3_256(b"") == expected


def test_sha3_256_length():
    assert len(sha3_256(b"hello")) == 32


def test_sha3_256_deterministic():
    assert sha3_256(b"test") == sha3_256(b"test")


def test_sha3_256_different_inputs():
    assert sha3_256(b"a") != sha3_256(b"b")


def test_hash_160_btcq_length():
    assert len(hash_160_btcq(b"test")) == 20


def test_hash_160_btcq_is_sha3_256_prefix():
    data = b"some data to hash"
    full = sha3_256(data)
    truncated = hash_160_btcq(data)
    assert truncated == full[:20]


if __name__ == "__main__":
    tests = [f for f in dir() if f.startswith('test_')]
    for t in sorted(tests):
        print(f"  {t} ... ", end="", flush=True)
        globals()[t]()
        print("OK")
    print(f"\nAll {len(tests)} btcq_crypto tests passed.")
