import shutil import subprocess import sys from pathlib import Path from PIL import Image PROJECT_ROOT = Path(__file__).resolve().parents[2] ASSET_DIRECTORY = PROJECT_ROOT / "assets" / "images" ALTER_EGO_ASSETS = ( "alter-ego-code.png", "alter-ego-literature.png", "alter-ego-research.png", "alter-ego-podcasts.png", "alter-ego-life.png", "alter-ego-gaming.png", ) CONTACT_SHEET_SCRIPT = PROJECT_ROOT / "scripts" / "build-alter-ego-contact-sheet.py" def test_alter_ego_asset_inventory_and_image_contract() -> None: expected = set(ALTER_EGO_ASSETS) actual = {path.name for path in ASSET_DIRECTORY.glob("alter-ego-*.png")} missing = [name for name in ALTER_EGO_ASSETS if name not in actual] unexpected = sorted(actual - expected) inventory_violations = [ *(f"missing assets/images/{name}" for name in missing), *(f"unexpected assets/images/{name}" for name in unexpected), ] assert not inventory_violations, "Alter ego asset inventory mismatch:\n" + "\n".join( f"- {violation}" for violation in inventory_violations ) violations: list[str] = [] for name in ALTER_EGO_ASSETS: path = ASSET_DIRECTORY / name with Image.open(path) as image: if image.format != "PNG": violations.append(f"{name}: expected PNG, got {image.format!r}") if image.mode != "RGBA": violations.append(f"{name}: expected RGBA, got {image.mode!r}") width, height = image.size if width < 1000 or height < 1200: violations.append( f"{name}: expected at least 1000x1200, got {width}x{height}" ) if image.mode != "RGBA": continue with image.getchannel("A") as alpha: alpha_extrema = alpha.getextrema() if alpha_extrema != (0, 255): violations.append( f"{name}: expected alpha extrema (0, 255), got {alpha_extrema}" ) corners = ( alpha.getpixel((0, 0)), alpha.getpixel((width - 1, 0)), alpha.getpixel((0, height - 1)), alpha.getpixel((width - 1, height - 1)), ) if corners != (0, 0, 0, 0): violations.append( f"{name}: expected four transparent corners, got {corners}" ) opaque_pixels = alpha.histogram()[255] opaque_coverage = opaque_pixels / (width * height) if not 0.18 <= opaque_coverage <= 0.80: violations.append( f"{name}: opaque coverage {opaque_coverage:.4f} outside 0.18..0.80" ) assert not violations, "Alter ego asset contract violations:\n" + "\n".join( f"- {violation}" for violation in violations ) def _install_contact_sheet_script(project_root: Path) -> Path: assert CONTACT_SHEET_SCRIPT.is_file(), ( f"Missing contact-sheet script: {CONTACT_SHEET_SCRIPT}" ) script = project_root / "scripts" / CONTACT_SHEET_SCRIPT.name script.parent.mkdir(parents=True) shutil.copyfile(CONTACT_SHEET_SCRIPT, script) return script def test_contact_sheet_cli_reports_every_missing_input(tmp_path: Path) -> None: script = _install_contact_sheet_script(tmp_path) result = subprocess.run( [sys.executable, str(script)], cwd=tmp_path, capture_output=True, text=True, check=False, ) assert result.returncode != 0 for name in ALTER_EGO_ASSETS: assert f"assets/images/{name}" in result.stderr assert not (tmp_path / "docs/visual-reviews/alter-ego-v1.png").exists() def test_contact_sheet_cli_uses_fixed_order_and_deterministic_layout( tmp_path: Path, ) -> None: script = _install_contact_sheet_script(tmp_path) asset_directory = tmp_path / "assets" / "images" asset_directory.mkdir(parents=True) colors = ( (210, 45, 45, 255), (45, 180, 75, 255), (55, 95, 220, 255), (220, 150, 40, 255), (155, 65, 205, 255), (40, 185, 195, 255), ) for name, color in zip(ALTER_EGO_ASSETS, colors, strict=True): image = Image.new("RGBA", (1000, 1200), (0, 0, 0, 0)) try: image.paste(color, (200, 200, 800, 1000)) image.save(asset_directory / name) finally: image.close() first = subprocess.run( [sys.executable, str(script)], cwd=tmp_path, capture_output=True, text=True, check=False, ) assert first.returncode == 0, first.stderr output = tmp_path / "docs" / "visual-reviews" / "alter-ego-v1.png" first_bytes = output.read_bytes() second = subprocess.run( [sys.executable, str(script)], cwd=tmp_path, capture_output=True, text=True, check=False, ) assert second.returncode == 0, second.stderr assert output.read_bytes() == first_bytes with Image.open(output) as sheet: assert sheet.mode == "RGB" assert sheet.size == (704, 2664) for row, color in enumerate(colors): center_y = 248 + (row * 440) assert sheet.getpixel((184, center_y)) == color[:3] assert sheet.getpixel((520, center_y)) == color[:3] assert sheet.getpixel((40, 72)) == (13, 17, 23) assert sheet.getpixel((376, 72)) == (246, 242, 233)