"""Presentation contracts: escaped content, measured signals and honest states."""
from html.parser import HTMLParser
import math
import re
from types import SimpleNamespace
import pytest
from obliteratus.interventions import NeuronEdit, NeuronRecipe
from obliteratus.neuron_lab_visuals import (
LAB_CSS, render_comparison, render_feedback, render_hero, render_inspector, render_stack,
)
pytestmark = pytest.mark.cpu
class Markup(HTMLParser):
def __init__(self, source):
super().__init__()
self.elements, self.words = [], []
self.feed(source)
def handle_starttag(self, tag, attrs):
self.elements.append((tag, dict(attrs)))
def handle_data(self, data):
self.words.append(data)
@property
def text(self):
return " ".join(self.words)
def tagged(self, tag):
return [attrs for kind, attrs in self.elements if kind == tag]
def classes(self, name):
return [attrs for _, attrs in self.elements if name in attrs.get("class", "").split()]
def capture(values, *, tokens=None):
tokens = tokens or [f"token-{i}" for i in range(len(values))]
finite = [value for value in values if isinstance(value, (int, float)) and math.isfinite(value)]
return {
"layer": 3, "neuron": 17,
"tokens": [[i, token, value] for i, (token, value) in enumerate(zip(tokens, values))],
"statistics": {"mean": sum(finite) / len(finite) if finite else None,
"min": min(finite, default=None), "max": max(finite, default=None),
"positive_fraction": sum(value > 0 for value in finite) / len(finite) if finite else None},
}
def test_initial_state_contains_no_invented_measurements():
hero = Markup(render_hero())
assert "No model connected" in hero.text and "OBLITERATUS / NEURON LAB" in hero.text
assert "NEURON" in hero.text and "LAB" in hero.text
inspector = Markup(render_inspector())
assert "No activations captured yet" in inspector.text
assert not inspector.tagged("svg") and not inspector.classes("nl-stat")
stack = Markup(render_stack())
assert "No intervention yet" in stack.text
comparison = Markup(render_comparison())
assert len(comparison.classes("nl-condition")) == 2
assert not comparison.classes("nl-trial-metrics")
assert "Trait and capability accuracy are untested" in comparison.text
def test_hero_uses_actual_model_geometry_and_enabled_count():
recipe = NeuronRecipe("model", (NeuronEdit(0, 1, "zero"), NeuronEdit(1, 2, "flip", enabled=False)))
session = SimpleNamespace(handle=SimpleNamespace(model_name="local/Qwen"),
layers=[{}, {}, {}], recipe=recipe)
hero = Markup(render_hero(session))
assert "local/Qwen" in hero.text and "3 layers" in hero.text
assert "1 enabled edit" in hero.text
assert "Runtime only" in hero.text and "Raw activation units" in hero.text
def test_ambient_hero_is_labelled_decorative_and_has_no_measurement_claim():
hero = Markup(render_hero())
field = hero.tagged("svg")[0]
assert field["aria-hidden"] == "true" and field["focusable"] == "false"
assert field["data-kind"] == "decorative"
assert "Ambient field · schematic" in hero.text
assert not hero.classes("nl-stat") and not hero.classes("nl-live")
assert "layers" not in hero.text and "enabled edits" not in hero.text
# Decorative SVG references stay inside the document: no remote images or scripts.
ids = {attrs["id"] for _, attrs in hero.elements if "id" in attrs}
for _, attrs in hero.elements:
for value in attrs.values():
for reference in re.findall(r"url\(([^)]+)\)", value):
assert reference.startswith("#") and reference[1:] in ids
assert not hero.tagged("image") and not hero.tagged("script")
@pytest.mark.parametrize("name,visible", [
("/Users/researcher/checkpoints/Qwen3-4B/", "Qwen3-4B"),
(r"C:\Research\Models\Qwen3-4B", "Qwen3-4B"),
(r"\\server\checkpoints\Qwen3-4B", "Qwen3-4B"),
("~/models/Qwen3-4B", "Qwen3-4B"),
("./cache/Qwen3-4B", "Qwen3-4B"),
("Qwen/Qwen3-4B", "Qwen/Qwen3-4B"),
("tiny-model", "tiny-model"),
("/", "/"),
])
def test_hero_shortens_explicit_local_paths_but_keeps_full_accessible_identity(name, visible):
session = SimpleNamespace(handle=SimpleNamespace(model_name=name), layers=[], recipe=None)
markup = Markup(render_hero(session))
model = markup.classes("nl-model-name")[0]
assert model["title"] == name and model["aria-label"] == name
assert visible in markup.text
if visible != name:
assert name not in markup.text
def test_local_model_basename_still_escapes_hostile_markup():
name = '/tmp/checkpoints/'
session = SimpleNamespace(handle=SimpleNamespace(model_name=name), layers=[], recipe=None)
source = render_hero(session)
markup = Markup(source)
assert not markup.tagged("img") and "<img" in source
assert markup.classes("nl-model-name")[0]["aria-label"] == name
@pytest.mark.parametrize("value", ["", '\">',
"a & b ' c