from __future__ import annotations

"""Author: Vasiliy Zdanovskiy
email: vasilyvz@gmail.com

Unit tests for mcp_proxy_adapter.transfer.checksums.
"""

from pathlib import Path

import pytest

from mcp_proxy_adapter.transfer.checksums import (
    compute_file_checksum,
    verify_file_checksum,
)
from mcp_proxy_adapter.transfer.errors import (
    TransferChecksumMismatchError,
    TransferError,
)

FIXTURE_BYTES = b"hello-transfer\n"
EXPECTED_SHA256 = "79b604d5347225ff00a34347bcb100a2a731e4db2a461820621d504ea63527ce"


def test_compute_file_checksum_sha256(tmp_path: Path) -> None:
    fpath = tmp_path / "f.bin"
    fpath.write_bytes(FIXTURE_BYTES)
    assert compute_file_checksum(str(fpath)) == EXPECTED_SHA256


def test_verify_file_checksum_success(tmp_path: Path) -> None:
    fpath = tmp_path / "f.bin"
    fpath.write_bytes(FIXTURE_BYTES)
    verify_file_checksum(str(fpath), EXPECTED_SHA256)


def test_verify_file_checksum_mismatch_raises_typed_error(tmp_path: Path) -> None:
    fpath = tmp_path / "f.bin"
    fpath.write_bytes(FIXTURE_BYTES)
    wrong = "0" * 64
    with pytest.raises(TransferChecksumMismatchError) as ctx:
        verify_file_checksum(str(fpath), wrong, transfer_id="tid", phase="verify")
    assert ctx.value.checksum_expected == wrong
    assert ctx.value.checksum_actual == EXPECTED_SHA256
    assert ctx.value.transfer_id == "tid"


def test_compute_unsupported_algorithm_raises_transfer_error(tmp_path: Path) -> None:
    fpath = tmp_path / "f.bin"
    fpath.write_bytes(FIXTURE_BYTES)
    with pytest.raises(TransferError) as ctx:
        compute_file_checksum(str(fpath), algorithm="md5")
    assert "md5" in str(ctx.value).lower() or ctx.value.phase == "checksum"
