"""Viewer wiring gate: atlas.html must load the render JSON and the JSON must
carry exactly what the viewer's honesty guardrails depend on."""
from __future__ import annotations

import json
from pathlib import Path

_MONO = Path(__file__).resolve().parents[4]
_HTML = _MONO / "recoil" / "docs" / "atlas.html"
_JSON = _MONO / "recoil" / "architecture" / "topology" / "generated" / "atlas.render.json"


def test_html_exists_and_wires_to_render_json():
    """No headless browser is available in the build env, so this asserts the
    data-bound wiring tokens exist in the HTML/JS source — the load-bearing code
    paths a broken viewer would be missing. Runtime visual correctness is checked
    in the post-build human-verify step (see the Validation note)."""
    assert _HTML.exists(), "recoil/docs/atlas.html missing"
    html = _HTML.read_text()
    required = {
        "render-JSON fetch path": "../architecture/topology/generated/atlas.render.json",
        "node DOM hook": "data-node-id",
        "swimlane DOM hook": "data-swimlane",
        "edge DOM hook": "data-edge",
        "detail panel id": "atlas-detail-panel",
        "human-review branch": "is_human_review",
        "human node hook": "data-human",
        "substrate branch": "is_substrate",
        "substrate node hook": "data-substrate",
        "substrate ghost class": "ghost",
        "deprecated branch": "is_deprecated",
        "deprecated node hook": "data-deprecated",
        "deprecated style class": "deprecated",
    }
    missing = [name for name, tok in required.items() if tok not in html]
    assert not missing, f"viewer missing wiring: {missing}"
    assert ("mouseenter" in html) or ("mouseover" in html), "viewer lacks a hover handler"
    assert "click" in html, "viewer lacks a click handler"
    # Handlers must be BOUND TO node elements (not merely present somewhere): the source
    # must select node elements by their stable hook and attach a listener to them.
    assert "[data-node-id]" in html, "handlers not bound via the data-node-id selector"
    assert "addEventListener" in html, "viewer attaches no event listeners"


def test_render_json_guardrails_present():
    g = json.loads(_JSON.read_text())
    humans = [n["id"] for n in g["nodes"] if n["is_human_review"]]
    subs = sorted(n["id"] for n in g["nodes"] if n["is_substrate"])
    deps = sorted(n["id"] for n in g["nodes"] if n["is_deprecated"])
    assert len(humans) == 6, f"expected 6 human gates, got {humans}"
    assert subs == ["eval_panel", "modality_eval", "strategy_score_bridge"]
    assert deps == ["previz", "previz_review"], f"deprecated set drift: {deps}"
