test: define alter ego asset contract

This commit is contained in:
chrnx committed 2026-08-11 17:32:49 -05:00
1 parent f983cec4f9
commit 34808696a6
3 files changed
+290

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
#!/usr/bin/env python3
"""Build the fixed alter-ego portrait review contact sheet."""
import sys
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
ASSETS = (
("Code", "alter-ego-code.png"),
("Literature", "alter-ego-literature.png"),
("Research", "alter-ego-research.png"),
("Podcasts", "alter-ego-podcasts.png"),
("Life", "alter-ego-life.png"),
("Gaming", "alter-ego-gaming.png"),
)
PROJECT_ROOT = Path(__file__).resolve().parents[1]
ASSET_DIRECTORY = PROJECT_ROOT / "assets" / "images"
OUTPUT_PATH = PROJECT_ROOT / "docs" / "visual-reviews" / "alter-ego-v1.png"
DARK_BACKGROUND = "#0D1117"
LIGHT_BACKGROUND = "#F6F2E9"
SHEET_BACKGROUND = "#D8D4CC"
TEXT_COLOR = "#17191C"
MARGIN = 24
COLUMN_GAP = 16
ROW_GAP = 24
LABEL_HEIGHT = 32
PREVIEW_WIDTH = 320
PREVIEW_HEIGHT = 384
SHEET_WIDTH = (MARGIN * 2) + (PREVIEW_WIDTH * 2) + COLUMN_GAP
SHEET_HEIGHT = (
(MARGIN * 2)
+ (len(ASSETS) * (LABEL_HEIGHT + PREVIEW_HEIGHT))
+ ((len(ASSETS) - 1) * ROW_GAP)
)
def _composite_preview(source: Image.Image, background: str) -> Image.Image:
portrait = source.convert("RGBA")
try:
portrait.thumbnail(
(PREVIEW_WIDTH, PREVIEW_HEIGHT),
Image.Resampling.LANCZOS,
)
preview = Image.new("RGB", (PREVIEW_WIDTH, PREVIEW_HEIGHT), background)
offset = (
(PREVIEW_WIDTH - portrait.width) // 2,
(PREVIEW_HEIGHT - portrait.height) // 2,
)
preview.paste(portrait, offset, portrait)
return preview
finally:
portrait.close()
def build_contact_sheet() -> Path:
missing = [
ASSET_DIRECTORY / filename
for _, filename in ASSETS
if not (ASSET_DIRECTORY / filename).is_file()
]
if missing:
relative_paths = "\n".join(
f"- {path.relative_to(PROJECT_ROOT)}" for path in missing
)
raise FileNotFoundError(f"Missing alter ego inputs:\n{relative_paths}")
sheet = Image.new("RGB", (SHEET_WIDTH, SHEET_HEIGHT), SHEET_BACKGROUND)
draw = ImageDraw.Draw(sheet)
font = ImageFont.load_default()
try:
for row, (topic, filename) in enumerate(ASSETS):
row_y = MARGIN + row * (LABEL_HEIGHT + PREVIEW_HEIGHT + ROW_GAP)
draw.text((MARGIN, row_y + 8), topic, fill=TEXT_COLOR, font=font)
draw.text(
(MARGIN + PREVIEW_WIDTH - 27, row_y + 8),
"dark",
anchor="ra",
fill=TEXT_COLOR,
font=font,
)
draw.text(
(
MARGIN + (PREVIEW_WIDTH * 2) + COLUMN_GAP - 27,
row_y + 8,
),
"light",
anchor="ra",
fill=TEXT_COLOR,
font=font,
)
with Image.open(ASSET_DIRECTORY / filename) as source:
dark_preview = _composite_preview(source, DARK_BACKGROUND)
light_preview = _composite_preview(source, LIGHT_BACKGROUND)
try:
preview_y = row_y + LABEL_HEIGHT
sheet.paste(dark_preview, (MARGIN, preview_y))
sheet.paste(
light_preview,
(MARGIN + PREVIEW_WIDTH + COLUMN_GAP, preview_y),
)
finally:
dark_preview.close()
light_preview.close()
OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True)
sheet.save(OUTPUT_PATH, format="PNG", optimize=False)
finally:
sheet.close()
return OUTPUT_PATH
def main() -> int:
try:
output = build_contact_sheet()
except (FileNotFoundError, OSError) as error:
print(f"error: {error}", file=sys.stderr)
return 1
print(output.relative_to(PROJECT_ROOT))
return 0
if __name__ == "__main__":
raise SystemExit(main())
+161
View File
@@ -0,0 +1,161 @@
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)
+1
View File
@@ -0,0 +1 @@