Editorial diagram design for Claude Code, Codex, GitHub Copilot, Cursor, Factory Droid, Qoder, and Pi. 45 diagram types. Self-contained HTML + SVG. No shadows. No Mermaid slop.
#!/usr/bin/env python3
"""Adversarial tests for verify-heatmap.py — both polarities.
Per ADR 0005, a geometric contract is a checker plus fixtures proving it fires
when it should and stays quiet when it shouldn't. Each mutation below renders
visually identical to the good example (or plausibly so) and no other gate
would catch it.
Positive cases (must PASS):
P1 — the shipped light example passes clean.
P2 — the shipped dark example passes clean.
P3 — the shipped full example passes clean.
Negative cases (must FAIL):
N1 — two focal cells (data-focal on two different cells).
N2 — missing cell (one (row,col) pair removed from the grid).
N3 — duplicate cell (one (row,col) pair appears twice, different values).
N4 — non-monotone fill (higher value gets lower opacity).
N5 — inconsistent fill (same value, two different opacities in the same figure).
N6 — no parseable cells (data-row attribute removed from all cells).
N7 — non-focal cell with no rgba fill.
N8 — CSS transform present in <style> block.
N9 — missing axis declarations entirely.
N10 — undeclared row/column values in the cells.
N11 — focal value text fails AA contrast against the focal accent fill.
N12 — duplicate axis declarations silently shrink the grid vocabulary.
N13 — dark focal text is checked against the wrong underlay color.
Usage: python3 scripts/test-verify-heatmap.py
Exit: 0 all pass, 1 a case failed.
"""
from __future__ import annotations
import os
import re
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
CHECKER = ROOT / "scripts/verify-heatmap.py"
GOOD = ROOT / "skills/diagram-design/assets/example-heatmap.html"
GOOD_DARK = ROOT / "skills/diagram-design/assets/example-heatmap-dark.html"
GOOD_FULL = ROOT / "skills/diagram-design/assets/example-heatmap-full.html"
CHILD_ENV = {**os.environ, "PYTHONIOENCODING": "utf-8"}
# Anchors — literal excerpts of the shipped light example.
FOCAL_CELL = (
'data-row="payments" data-col="S4" data-value="47" data-focal="true" '
'x="520" y="124" width="116" height="56" fill="rgba(191,69,32,0.85)"'
)
FIRST_NONFOCAL = (
'data-row="auth" data-col="S1" data-value="4" '
'x="160" y="64" width="116" height="56" fill="rgba(45,49,66,0.29)"'
)
HIGH_VALUE_CELL = (
'data-row="billing" data-col="S3" data-value="9" '
'x="400" y="244" width="116" height="56" fill="rgba(45,49,66,0.65)"'
)
LOW_VALUE_CELL = (
'data-row="notifications" data-col="S1" data-value="1" '
'x="160" y="304" width="116" height="56" fill="rgba(45,49,66,0.07)"'
)
# A non-focal cell used as the "missing" anchor in N2
MISSING_TARGET = (
'data-row="billing" data-col="S6" data-value="3" '
'x="760" y="244" width="116" height="56" fill="rgba(45,49,66,0.22)"'
)
# A cell with value=5 used as the inconsistent-opacity anchor in N5
FIVE_VALUE_CELL = (
'data-row="auth" data-col="S2" data-value="5" '
'x="280" y="64" width="116" height="56" fill="rgba(45,49,66,0.36)"'
)
def invoke(argv: list[str]) -> tuple[int, str]:
result = subprocess.run(
argv,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
env=CHILD_ENV,
)
return result.returncode, (result.stdout or "") + (result.stderr or "")
def run(*paths: Path) -> tuple[int, str]:
return invoke([sys.executable, str(CHECKER), *(str(p) for p in paths)])
def write(directory: Path, name: str, source: str) -> Path:
path = directory / name
path.write_text(source, encoding="utf-8")
return path
def case(
failures: list[str],
directory: Path,
name: str,
source: str,
original: str,
expect_pass: bool,
describe: str,
) -> None:
if source == original:
failures.append(
f"could not build the {name!r} fixture (anchor not found in source)"
)
return
path = write(directory, name, source)
code, output = run(path)
passed = code == 0
if passed != expect_pass:
direction = "pass" if expect_pass else "fail"
actual = "passed" if passed else f"failed:\n {output.strip()}"
failures.append(f"{name}: expected {direction} but {actual}")
def main() -> int:
original = GOOD.read_text(encoding="utf-8")
failures: list[str] = []
with tempfile.TemporaryDirectory() as tmp:
d = Path(tmp)
# ── Positive: shipped examples must pass ──────────────────────────────
for label, path in [
("P1-light", GOOD),
("P2-dark", GOOD_DARK),
("P3-full", GOOD_FULL),
]:
code, output = run(path)
if code != 0:
failures.append(f"{label}: shipped example failed:\n {output.strip()}")
single_quoted = re.sub(
r'="([^"<>]*)"',
lambda match: "='" + match.group(1).replace("'", "'") + "'",
original,
)
case(failures, d, "P-single-quotes.html", single_quoted, original, True,
"single-quoted attributes retain the same heatmap semantics")
mixed_quotes = original.replace(
FIRST_NONFOCAL,
FIRST_NONFOCAL.replace('fill="', 'data-note="fill=\'red\'" fill="', 1),
)
case(failures, d, "P-mixed-quotes.html", mixed_quotes, original, True,
"attribute-like metadata does not override the real fill")
mixed_single = single_quoted.replace(
"fill='rgba(45,49,66,0.29)'",
"data-note='fill=\"red\"' fill='rgba(45,49,66,0.29)'",
1,
)
case(failures, d, "P-mixed-single-quotes.html", mixed_single, single_quoted, True,
"double-quoted text inside metadata does not override a single-quoted fill")
quoted_gt = original.replace(
"<rect " + FIRST_NONFOCAL,
'<rect data-note="p95>budget" ' + FIRST_NONFOCAL,
1,
)
case(failures, d, "P-quoted-greater-than.html", quoted_gt, original, True,
"a greater-than inside a double-quoted attribute does not drop the cell")
single_gt = original.replace(
"<rect " + FIRST_NONFOCAL,
"<rect data-note='p95>budget' " + FIRST_NONFOCAL,
1,
)
case(failures, d, "P-single-quoted-greater-than.html", single_gt, original, True,
"a greater-than inside a single-quoted attribute does not drop the cell")
label_gt = original.replace(
'<text data-row-label="auth"',
'<text data-note="a>b" data-row-label="auth"',
1,
)
case(failures, d, "P-quoted-greater-than-axis.html", label_gt, original, True,
"a greater-than inside axis metadata does not drop the row label")
commented_transform = original.replace('</style>', '/* archived experiment\n transform: translateX(12px);\n */\n</style>', 1)
case(failures, d, "P-commented-transform.html", commented_transform, original, True,
"a CSS comment cannot move verified geometry")
archived_style = original.replace('</style>', '</style><!-- <style>rect { transform: translateX(12px); }</style> -->', 1)
case(failures, d, "P-archived-style.html", archived_style, original, True,
"an HTML comment cannot activate archived CSS")
live_transform = original.replace('</style>', 'rect {/**/transform: translateX(12px); }\n</style>', 1)
case(failures, d, "N-comment-prefixed-transform.html", live_transform, original, False,
"a CSS comment before a property cannot hide an active transform")
for name, css in (
("legacy-html-markers", "<!-- rect { transform: translateX(12px); } -->"),
("quoted-comment-opener", 'rect { --note: "/*"; transform: translateX(12px); /* note */ }'),
("escaped-quote-comment-opener", r'rect { --note: "\"/*"; transform: translateX(12px); /* note */ }'),
):
live_css = original.replace('</style>', css + '\n</style>', 1)
case(failures, d, f"N-{name}.html", live_css, original, False,
"quoted comment markers and HTML legacy markers cannot hide active CSS")
# ── Negative: mutations that must be rejected ─────────────────────────
# N1: two focal cells
second_focal = FIRST_NONFOCAL.replace(
'fill="rgba(45,49,66,0.29)"',
'data-focal="true" fill="rgba(191,69,32,0.85)"',
)
case(
failures,
d,
"N1-two-focal.html",
original.replace(FIRST_NONFOCAL, second_focal),
original,
False,
"two data-focal cells",
)
# N2: missing cell (remove one complete <rect> pair — underlay + data)
# We remove just the data rect for billing·S6; the grid becomes 29 cells.
missing_rect = f"<rect {MISSING_TARGET}/>"
case(
failures,
d,
"N2-missing-cell.html",
original.replace(missing_rect, ""),
original,
False,
"missing (billing, S6) cell",
)
# N3: duplicate cell — replace the billing·S6 cell's col with S5
# so we have two (billing, S5) cells.
duplicate = MISSING_TARGET.replace('data-col="S6"', 'data-col="S5"')
case(
failures,
d,
"N3-duplicate-cell.html",
original.replace(MISSING_TARGET, duplicate),
original,
False,
"duplicate (billing, S5) cell",
)
# N4: non-monotone fill — swap opacities of high-value (9, 0.65) and
# low-value (1, 0.07) cells so a lower value gets higher opacity.
inverted = original.replace(
HIGH_VALUE_CELL,
HIGH_VALUE_CELL.replace("0.65", "0.07"),
).replace(
LOW_VALUE_CELL,
LOW_VALUE_CELL.replace("0.07", "0.65"),
)
case(
failures,
d,
"N4-non-monotone.html",
inverted,
original,
False,
"non-monotone opacity (value=9 gets opacity=0.07, value=1 gets 0.65)",
)
# N5: inconsistent fill — give one of the value=5 cells a different opacity.
inconsistent = original.replace(
FIVE_VALUE_CELL,
FIVE_VALUE_CELL.replace("0.36", "0.55"),
)
case(
failures,
d,
"N5-inconsistent-opacity.html",
inconsistent,
original,
False,
"same value=5 with two different opacities (0.36 vs 0.55)",
)
# N6: no parseable cells — strip data-row from every rect.
no_cells = re.sub(r'\s+data-row="[^"]*"', "", original)
case(
failures,
d,
"N6-no-cells.html",
no_cells,
original,
False,
"no parseable heatmap cells",
)
# N7: non-focal cell with no rgba fill — replace with a named color.
no_rgba = original.replace(
'fill="rgba(45,49,66,0.07)"',
'fill="#f0eeec"',
1, # only the first occurrence
)
case(
failures,
d,
"N7-no-rgba-fill.html",
no_rgba,
original,
False,
"non-focal cell with no rgba fill",
)
# N8: CSS transform in <style> block.
css_transform = original.replace(
"svg { display: block; width: 100%; height: auto; max-width: 100%; }",
"svg { display: block; width: 100%; height: auto; max-width: 100%; transform: scale(1); }",
)
case(
failures,
d,
"N8-css-transform.html",
css_transform,
original,
False,
"CSS transform property in <style>",
)
# N9: missing axis declarations.
no_axis_labels = re.sub(r'\s+data-row-label="[^"]+"', "", original)
no_axis_labels = re.sub(r'\s+data-col="[^"]+"', "", no_axis_labels)
case(
failures,
d,
"N9-missing-axis-labels.html",
no_axis_labels,
original,
False,
"missing axis declarations",
)
# N10: undeclared row/column value in a cell.
undeclared_cell = original.replace(
'data-row="payments" data-col="S4" data-value="47" data-focal="true"',
'data-row="unknown" data-col="S4" data-value="47" data-focal="true"',
1,
)
case(
failures,
d,
"N10-undeclared-row-col.html",
undeclared_cell,
original,
False,
"undeclared row/column values",
)
# N11: focal value text fails AA contrast against the focal accent fill.
bad_focal_text = original.replace(
'fill="#111111" font-size="10" font-weight="600"',
'fill="#f5f5f5" font-size="10" font-weight="600"',
1,
)
case(
failures,
d,
"N11-focal-text-contrast.html",
bad_focal_text,
original,
False,
"focal value text fails 4.5:1 contrast on accent fill",
)
# N12: duplicate axis declarations must not quietly shrink the declared grid.
duplicate_axis = original.replace(
'data-row-label="auth" x="148" y="96" fill="#2d3142" font-size="9" font-family="\'Geist Mono\', monospace" text-anchor="end">auth</text>',
'data-row-label="auth" x="148" y="96" fill="#2d3142" font-size="9" font-family="\'Geist Mono\', monospace" text-anchor="end">auth</text>\n'
' <text data-row-label="auth" x="148" y="110" fill="#2d3142" font-size="9" font-family="\'Geist Mono\', monospace" text-anchor="end">auth</text>',
1,
)
case(
failures,
d,
"N12-duplicate-axis-label.html",
duplicate_axis,
original,
False,
"duplicate axis label declaration silently shrinks the grid",
)
# N13: dark focal text must be evaluated against the dark background, not paper.
dark_bad_focal_text = GOOD_DARK.read_text(encoding="utf-8").replace(
'fill="#111111" font-size="10" font-weight="600" font-family="\'Geist\', sans-serif" text-anchor="middle">47%</text>',
'fill="#f5f5f5" font-size="10" font-weight="600" font-family="\'Geist\', sans-serif" text-anchor="middle">47%</text>',
1,
)
case(
failures,
d,
"N13-dark-focal-text-contrast.html",
dark_bad_focal_text,
GOOD_DARK.read_text(encoding="utf-8"),
False,
"dark focal value text fails 4.5:1 contrast against the actual dark underlay",
)
with tempfile.TemporaryDirectory() as tmp:
d = Path(tmp)
for factor in (250_000_000, 1_000_000_000, 10_000_000_000):
scaled = re.sub(r'data-value="(\d+)"',
lambda m: 'data-value="%d"' % (int(m.group(1)) * factor),
original)
code, output = run(write(d, f"billion-counts-{factor}.html", scaled))
if code != 0:
failures.append(f"finite counts scaled by {factor} were rejected: {output.strip()}")
for invalid in ("nan", "inf", "-1", "1e400"):
changed = original.replace('data-value="4"', f'data-value="{invalid}"', 1)
code, output = run(write(d, "invalid-value.html", changed))
if code == 0 or "expected 30 cells" not in output or "Traceback" in output:
failures.append(f"invalid {invalid} did not retain grid completeness finding: {output.strip()}")
print("OK: billion-scale counts preserve the grid; non-finite/negative values remain findings")
with tempfile.TemporaryDirectory() as tmp:
d = Path(tmp)
for original_source, label in ((original, "light"),
(GOOD_DARK.read_text(encoding="utf-8"), "dark")):
pixel_lengths = re.sub(r'\b(x|y|width|height)="([-+0-9.eE]+)"',
lambda m: f'{m.group(1)}="{m.group(2)}px"', original_source)
code, output = run(write(d, f"pixel-lengths-{label}.html", pixel_lengths))
if code != 0:
failures.append(f"equivalent pixel lengths {label} were rejected: {output.strip()}")
# Supported coordinate syntax must not bypass the contrast comparison.
for template, theme in ((original, "light"),
(GOOD_DARK.read_text(encoding="utf-8"), "dark")):
bad_contrast = template.replace('x="578" y="149" fill="#111111"',
'x="578" y="149" fill="#ffffff"', 1)
for notation in ("px", "exponent-px"):
def length(match: re.Match[str]) -> str:
number = match.group(2)
if notation == "exponent-px":
number = f"{float(number):.4e}"
return f'{match.group(1)}="{number}px"'
changed = re.sub(r'\b(x|y|width|height)="([-+0-9.eE]+)"', length, bad_contrast)
code, output = run(write(d, f"bad-contrast-{theme}-{notation}.html", changed))
if code == 0 or "below WCAG AA" not in output or "Traceback" in output:
failures.append(f"{theme}/{notation} bypassed contrast failure: {output.strip()}")
for length in ("520%", "unknown", "nan", "1e400px"):
changed = original.replace(FOCAL_CELL, FOCAL_CELL.replace('x="520"', f'x="{length}"'), 1)
code, output = run(write(d, "unsupported-length.html", changed))
if code == 0 or "focal-text contrast measurement" not in output or "Traceback" in output:
failures.append(f"unsupported length {length!r} lacked a named finding: {output.strip()}")
print("OK: equivalent pixel lengths pass; unsupported/non-finite lengths are named findings")
# Translucent foregrounds compose against the cell beneath them, not paper.
with tempfile.TemporaryDirectory() as tmp:
d = Path(tmp)
# The light focal cell (accent #bf4520 at 0.85) leaves solid #111111 at
# 4.64:1, so only a nearly opaque translucent ink still clears 4.5:1 there.
for template, theme, passing_alpha in ((original, "light", "0.98"),
(GOOD_DARK.read_text(encoding="utf-8"), "dark", "0.8")):
for alpha, expected_pass in ((passing_alpha, True), ("0.2", False)):
changed = template.replace('x="578" y="149" fill="#111111"',
f'x="578" y="149" fill="rgba(17,17,17,{alpha})"', 1)
code, output = run(write(d, f"alpha-{theme}-{alpha}.html", changed))
if (code == 0) != expected_pass or (not expected_pass and "below WCAG AA" not in output):
failures.append(f"alpha foreground {theme}/{alpha} unexpected result: {output.strip()}")
print("OK: translucent AA foregrounds pass; weak-alpha text still fails in both themes")
with tempfile.TemporaryDirectory() as tmp:
encoded = re.sub(r'(\b(?:x|y|width|height|data-value|data-share)="[^"<>]*)[0-9]',
lambda match: match.group(0)[:-1] + f"&#{ord(match.group(0)[-1])};",
GOOD.read_text(encoding="utf-8"))
code, output = run(write(Path(tmp), "encoded-attributes.html", encoded))
if code != 0:
failures.append(f"XML-encoded numeric attributes rejected: {output.strip()}")
if failures:
for f in failures:
print("FAIL:", f, file=sys.stderr)
return 1
print("OK — every heatmap verifier case passed.")
return 0
if __name__ == "__main__":
sys.exit(main())