|
| 1 | +"""Tests for image format detection.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from resolver_athena_client.client.image_format_detector import ( |
| 6 | + detect_image_format, |
| 7 | +) |
| 8 | +from resolver_athena_client.generated.athena.models_pb2 import ImageFormat |
| 9 | + |
| 10 | + |
| 11 | +def test_detect_png_format() -> None: |
| 12 | + """Test detection of PNG format.""" |
| 13 | + png_header = b"\x89PNG\r\n\x1a\n" + b"\x00" * 100 |
| 14 | + assert detect_image_format(png_header) == ImageFormat.IMAGE_FORMAT_PNG |
| 15 | + |
| 16 | + |
| 17 | +def test_detect_jpeg_format() -> None: |
| 18 | + """Test detection of JPEG format.""" |
| 19 | + jpeg_header = b"\xFF\xD8\xFF\xE0" + b"\x00" * 100 |
| 20 | + assert detect_image_format(jpeg_header) == ImageFormat.IMAGE_FORMAT_JPEG |
| 21 | + |
| 22 | + |
| 23 | +def test_detect_gif87a_format() -> None: |
| 24 | + """Test detection of GIF87a format.""" |
| 25 | + gif_header = b"GIF87a" + b"\x00" * 100 |
| 26 | + assert detect_image_format(gif_header) == ImageFormat.IMAGE_FORMAT_GIF |
| 27 | + |
| 28 | + |
| 29 | +def test_detect_gif89a_format() -> None: |
| 30 | + """Test detection of GIF89a format.""" |
| 31 | + gif_header = b"GIF89a" + b"\x00" * 100 |
| 32 | + assert detect_image_format(gif_header) == ImageFormat.IMAGE_FORMAT_GIF |
| 33 | + |
| 34 | + |
| 35 | +def test_detect_bmp_format() -> None: |
| 36 | + """Test detection of BMP format.""" |
| 37 | + bmp_header = b"BM" + b"\x00" * 100 |
| 38 | + assert detect_image_format(bmp_header) == ImageFormat.IMAGE_FORMAT_BMP |
| 39 | + |
| 40 | + |
| 41 | +def test_detect_webp_format() -> None: |
| 42 | + """Test detection of WebP format.""" |
| 43 | + webp_header = b"RIFF" + b"\x00\x00\x00\x00" + b"WEBP" + b"\x00" * 100 |
| 44 | + assert detect_image_format(webp_header) == ImageFormat.IMAGE_FORMAT_WEBP |
| 45 | + |
| 46 | + |
| 47 | +def test_detect_tiff_little_endian_format() -> None: |
| 48 | + """Test detection of TIFF format (little-endian).""" |
| 49 | + tiff_header = b"II*\x00" + b"\x00" * 100 |
| 50 | + assert detect_image_format(tiff_header) == ImageFormat.IMAGE_FORMAT_TIFF |
| 51 | + |
| 52 | + |
| 53 | +def test_detect_tiff_big_endian_format() -> None: |
| 54 | + """Test detection of TIFF format (big-endian).""" |
| 55 | + tiff_header = b"MM\x00*" + b"\x00" * 100 |
| 56 | + assert detect_image_format(tiff_header) == ImageFormat.IMAGE_FORMAT_TIFF |
| 57 | + |
| 58 | + |
| 59 | +def test_detect_unspecified_for_empty_data() -> None: |
| 60 | + """Test that empty data returns UNSPECIFIED.""" |
| 61 | + assert ( |
| 62 | + detect_image_format(b"") == ImageFormat.IMAGE_FORMAT_UNSPECIFIED |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def test_detect_unspecified_for_short_data() -> None: |
| 67 | + """Test that very short data returns UNSPECIFIED.""" |
| 68 | + assert ( |
| 69 | + detect_image_format(b"ab") == ImageFormat.IMAGE_FORMAT_UNSPECIFIED |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def test_detect_unspecified_for_unknown_format() -> None: |
| 74 | + """Test that unknown format returns UNSPECIFIED.""" |
| 75 | + unknown_data = b"UNKNOWN_FORMAT" + b"\x00" * 100 |
| 76 | + assert ( |
| 77 | + detect_image_format(unknown_data) |
| 78 | + == ImageFormat.IMAGE_FORMAT_UNSPECIFIED |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +def test_detect_format_with_real_png_data() -> None: |
| 83 | + """Test detection with minimal valid PNG data.""" |
| 84 | + # Minimal PNG: signature + IHDR chunk |
| 85 | + png_data = ( |
| 86 | + b"\x89PNG\r\n\x1a\n" # PNG signature |
| 87 | + b"\x00\x00\x00\x0dIHDR" # IHDR chunk |
| 88 | + b"\x00\x00\x00\x01" # width |
| 89 | + b"\x00\x00\x00\x01" # height |
| 90 | + b"\x08\x02\x00\x00\x00" # bit depth, color type, etc. |
| 91 | + ) |
| 92 | + assert detect_image_format(png_data) == ImageFormat.IMAGE_FORMAT_PNG |
| 93 | + |
| 94 | + |
| 95 | +def test_detect_format_with_real_jpeg_data() -> None: |
| 96 | + """Test detection with JPEG SOI marker.""" |
| 97 | + # JPEG Start of Image (SOI) marker |
| 98 | + jpeg_data = b"\xFF\xD8\xFF\xDB" + b"\x00" * 100 |
| 99 | + assert detect_image_format(jpeg_data) == ImageFormat.IMAGE_FORMAT_JPEG |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize( |
| 103 | + ("header", "expected"), |
| 104 | + [ |
| 105 | + (b"\x89PNG\r\n\x1a\n", ImageFormat.IMAGE_FORMAT_PNG), |
| 106 | + (b"\xFF\xD8\xFF", ImageFormat.IMAGE_FORMAT_JPEG), |
| 107 | + (b"GIF87a", ImageFormat.IMAGE_FORMAT_GIF), |
| 108 | + (b"GIF89a", ImageFormat.IMAGE_FORMAT_GIF), |
| 109 | + (b"BM", ImageFormat.IMAGE_FORMAT_BMP), |
| 110 | + (b"RIFF\x00\x00\x00\x00WEBP", ImageFormat.IMAGE_FORMAT_WEBP), |
| 111 | + (b"II*\x00", ImageFormat.IMAGE_FORMAT_TIFF), |
| 112 | + (b"MM\x00*", ImageFormat.IMAGE_FORMAT_TIFF), |
| 113 | + (b"", ImageFormat.IMAGE_FORMAT_UNSPECIFIED), |
| 114 | + (b"xyz", ImageFormat.IMAGE_FORMAT_UNSPECIFIED), |
| 115 | + ], |
| 116 | +) |
| 117 | +def test_detect_format_parametrized( |
| 118 | + header: bytes, expected: ImageFormat.ValueType |
| 119 | +) -> None: |
| 120 | + """Test format detection with various headers.""" |
| 121 | + assert detect_image_format(header) == expected |
0 commit comments