#!/usr/bin/env python3 """Adversarial tests for verify-treemap.py — both polarities. Per ADR 0005, a geometric contract in this repo is a checker plus fixtures that prove it fires when it should and stays quiet when it shouldn't. The mutations below are the two defects that actually shipped in review: a sliver cell drawn a third too small, and a label longer than the cell holding it. Usage: python3 scripts/test-verify-treemap.py Exit: 0 all pass, 1 a case failed. """ from __future__ import annotations import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent.parent CHECKER = ROOT / "scripts/verify-treemap.py" GOOD = ROOT / "skills/diagram-design/assets/example-treemap.html" def run(path: Path) -> tuple[int, str]: result = subprocess.run( [sys.executable, str(CHECKER), str(path)], capture_output=True, text=True, encoding="utf-8", errors="replace", ) return result.returncode, (result.stdout or "") + (result.stderr or "") def write(directory: Path, name: str, source: str) -> Path: path = directory / name path.write_text(source, encoding="utf-8") return path def main() -> int: source = GOOD.read_text(encoding="utf-8") failures: list[str] = [] with tempfile.TemporaryDirectory() as raw: directory = Path(raw) # 1. The shipped example must pass untouched. code, output = run(write(directory, "clean.html", source)) if code != 0: failures.append(f"clean example was rejected: {output.strip()}") else: print("OK: the shipped treemap passes") # 2. Shrink a labelled cell below the share it prints. This is the # defect that shipped in review — a cell drawn smaller than it claims # — caught here on Africa, which carries a label to contradict. shrunk = source.replace( 'South America", ">South America and the Caribbean Basin", ) if overflowing == source: failures.append("could not build the overflowing-label fixture (anchor moved)") else: code, output = run(write(directory, "overflow.html", overflowing)) if code == 0: failures.append("label overflowing its cell was accepted") elif "overflows" not in output: failures.append(f"overflow reported without a fit finding: {output.strip()}") else: print("OK: a label wider than its cell is rejected") # 4b. Overflow off the LEFT edge, via an end-anchored label. Checking # only the right and bottom edges would pass this silently, and the # text would read as belonging to the cell next door. left_over = source.replace( '