from __future__ import annotations import hashlib from io import BytesIO import random from types import SimpleNamespace import pytest from PIL import Image from app.jobs.pipeline import PermanentImageError, process_image from app.jobs.worker import Worker, _run_isolated def encoded(width: int, height: int, mode: str = 'RGBA') -> bytes: image = Image.new(mode, (width, height), (20, 80, 160, 128) if mode == 'RGBA' else 'navy') output = BytesIO() image.save(output, 'PNG', pnginfo=None) return output.getvalue() def run(source: bytes): return process_image( source, 'image/png', hashlib.sha256(source).hexdigest(), 40_000_000, 85, 85, 82, ) def test_pipeline_creates_all_grade4_variants() -> None: """Create every grade-4 variant with the required format, size and checksum.""" variants = {item.name: item for item in run(encoded(320, 200))} assert set(variants) == {'thumbnail', 'crop', 'optimized'} assert (variants['thumbnail'].width, variants['thumbnail'].height) == (256, 160) assert variants['thumbnail'].content_type == 'image/jpeg' assert (variants['crop'].width, variants['crop'].height) == (128, 128) assert variants['crop'].content_type == 'image/jpeg' assert (variants['optimized'].width, variants['optimized'].height) == (320, 200) assert variants['optimized'].content_type == 'image/webp' for item in variants.values(): assert hashlib.sha256(item.content).hexdigest() == item.sha256 def test_thumbnail_and_optimized_do_not_upscale_small_image() -> None: """Keep small images at source size except for the required 128x128 crop.""" variants = {item.name: item for item in run(encoded(40, 20, 'RGB'))} assert (variants['thumbnail'].width, variants['thumbnail'].height) == (40, 20) assert (variants['optimized'].width, variants['optimized'].height) == (40, 20) assert (variants['crop'].width, variants['crop'].height) == (128, 128) def test_jpeg_variants_composite_alpha_on_white() -> None: """Flatten transparent input before encoding JPEG variants without an alpha channel.""" variants = {item.name: item for item in run(encoded(2, 2))} with Image.open(BytesIO(variants['thumbnail'].content)) as image: assert image.mode == 'RGB' def test_checksum_mismatch_is_permanent() -> None: """Reject changed staging bytes as a permanent checksum error.""" source = encoded(20, 20) with pytest.raises(PermanentImageError, match='checksum_mismatch'): process_image(source, 'image/png', '0' * 64, 40_000_000, 85, 85, 82) def test_exif_orientation_is_applied_and_removed() -> None: """Apply EXIF orientation before resizing and omit metadata from generated output.""" image = Image.new('RGB', (320, 200), 'red') exif = Image.Exif() exif[274] = 6 output = BytesIO() image.save(output, 'JPEG', exif=exif) source = output.getvalue() variants = { item.name: item for item in process_image( source, 'image/jpeg', hashlib.sha256(source).hexdigest(), 40_000_000, 85, 85, 82, ) } assert (variants['thumbnail'].width, variants['thumbnail'].height) == (160, 256) with Image.open(BytesIO(variants['thumbnail'].content)) as transformed: assert not transformed.getexif() def test_isolated_pipeline_returns_results_larger_than_pipe_buffer() -> None: """Drain child output before join so a large valid result cannot deadlock the pipe.""" pixels = random.Random(0).randbytes(512 * 512 * 3) image = Image.frombytes('RGB', (512, 512), pixels) output = BytesIO() image.save(output, 'PNG') source = output.getvalue() variants = _run_isolated( (source, 'image/png', hashlib.sha256(source).hexdigest(), 40_000_000, 85, 85, 82), timeout=10, memory_bytes=512 * 1024 * 1024, ) assert {item.name for item in variants} == {'thumbnail', 'crop', 'optimized'} assert sum(len(item.content) for item in variants) > 64 * 1024 def test_retry_delay_uses_capped_exponential_backoff_and_jitter(monkeypatch) -> None: """Calculate capped exponential backoff and add the configured random jitter.""" settings = SimpleNamespace(worker_concurrency=1, retry_base_seconds=2, retry_max_seconds=8) worker = Worker(settings, jobs=None, storage=None) monkeypatch.setattr('app.jobs.worker.random.uniform', lambda start, end: end / 2) assert worker._delay(1) == 3 assert worker._delay(4) == 12