from __future__ import annotations

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

Unit tests for mcp_proxy_adapter.transfer.compression.
"""

from pathlib import Path

import pytest

from mcp_proxy_adapter.transfer.compression import (
    compress_file,
    decompress_file,
    validate_compression_algorithm,
)
from mcp_proxy_adapter.transfer.errors import TransferCompressionError


def test_identity_compression_is_copy(tmp_path: Path) -> None:
    src = tmp_path / "a.bin"
    src.write_bytes(b"\x00\x01\x02\xff")
    dst = tmp_path / "b.bin"
    compress_file(str(src), str(dst), "identity")
    assert dst.read_bytes() == src.read_bytes()


def test_gzip_compress_and_decompress_round_trip(tmp_path: Path) -> None:
    src = tmp_path / "plain.txt"
    src.write_bytes(b"round trip payload")
    gz = tmp_path / "p.gz"
    out = tmp_path / "plain2.txt"
    compress_file(str(src), str(gz), "gzip")
    decompress_file(str(gz), str(out), "gzip")
    assert out.read_bytes() == src.read_bytes()


def test_unknown_algorithm_raises_transfer_compression_error(tmp_path: Path) -> None:
    with pytest.raises(TransferCompressionError):
        validate_compression_algorithm("bz2")


def test_identity_round_trip_empty_file(tmp_path: Path) -> None:
    src = tmp_path / "e"
    src.write_bytes(b"")
    dst = tmp_path / "e2"
    compress_file(str(src), str(dst), "identity")
    assert dst.read_bytes() == b""
