"""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 "]) def test_untrusted_model_token_prompt_output_and_feedback_are_text_only(value): session = SimpleNamespace(handle=SimpleNamespace(model_name=value), layers=[], recipe=None) sources = [render_hero(session), render_inspector(capture([1.], tokens=[value])), render_comparison({"prompt": value, "trials": {"recipe": {"text": value}}}), render_feedback("Neuron inspect failed: " + value)] for source in sources: markup = Markup(source) assert value in markup.text or value in str(markup.elements) assert not markup.tagged("script") and not markup.tagged("img") assert not any(key.lower().startswith("on") for _, attrs in markup.elements for key in attrs) assert "evil"}, {"layer": 4, "neuron": 5, "operation": "clamp", "upper": 4.}], "scope": ""} markup = Markup(render_stack(recipe)) assert not markup.tagged("script") and not markup.tagged("img") assert "Unavailable" in markup.text and "Scope unrecorded" in markup.text assert "[−∞, 4]" in markup.text def test_comparison_cards_keep_real_zero_metrics_and_quality_claims_separate(): recipe = NeuronRecipe("model", (NeuronEdit(1, 2, "zero"),)).to_dict() comparison = { "prompt": "A shared question", "decoding": {"do_sample": False, "use_cache": False, "chat_template_applied": True}, "trials": { "baseline": {"text": "First answer", "tokens": 0, "seconds": 0., "first_token_kl_from_baseline": 0., "repeated_token_fraction": 0., "invalid_output_heuristic": False}, "recipe": {"text": "Another answer", "recipe": recipe, "tokens": 12, "seconds": 1.25, "first_token_kl_from_baseline": .032, "repeated_token_fraction": .25, "invalid_output_heuristic": True}, }, } markup = Markup(render_comparison(comparison)) assert "First answer" in markup.text and "Another answer" in markup.text assert "0.00s" in markup.text and "1.25s" in markup.text and "0.032" in markup.text assert "Repeated tokens 0%" in markup.text and "Repeated tokens 25%" in markup.text assert "Output heuristic flagged" in markup.text and "No output heuristic flag" in markup.text assert "1 ENABLED EDIT / ALL TOKENS" in markup.text assert "A shared question" in markup.text and "Model chat template" in markup.text assert "Greedy decoding" in markup.text and "Fresh no-cache forwards" in markup.text assert len(markup.classes("nl-trial-metrics")) == 2 assert len(markup.classes("nl-waiting")) == 0 def test_missing_output_and_nonfinite_metrics_are_unavailable_not_zero(): markup = Markup(render_comparison({"trials": { "baseline": {"tokens": float("nan"), "seconds": float("inf"), "first_token_kl_from_baseline": float("nan"), "repeated_token_fraction": 2.}, "recipe": {"text": ""}, "control": {"text": "control response"}, }})) assert "Output text was not included" in markup.text assert "No visible text was generated" in markup.text assert "control response" in markup.text assert "Repeated tokens —" in markup.text and "Output heuristic unrecorded" in markup.text assert "Greedy decoding" not in markup.text and "Fresh no-cache forwards" not in markup.text assert "nan" not in markup.text and "inf" not in markup.text def test_feedback_is_concise_and_does_not_mislabel_cancellation_or_failure(): assert "Connect a model" in Markup(render_feedback()).text assert "Choose a channel" in Markup(render_feedback(loaded=True)).text done = Markup(render_feedback("Neuron inspect complete.\n\nMemory admission warning: long details")) assert "DONE" in done.text and "long details" not in done.text assert done.classes("nl-feedback")[0]["role"] == "status" failed = Markup(render_feedback("Neuron compare failed: Load a neuron model first")) assert "ATTENTION" in failed.text and "Load a neuron model first" in failed.text assert failed.classes("nl-feedback-error") cancelled = Markup(render_feedback("Cancellation requested; waiting for forward.")) assert "CANCELLATION" in cancelled.text and "DONE" not in cancelled.text assert len(Markup(render_feedback("x" * 2000)).text) < 400 def test_style_is_scoped_and_does_not_load_external_resources(): assert "@import" not in LAB_CSS and "url(" not in LAB_CSS scopes = [] for block in re.finditer(r"([^{}]+)\{|}", LAB_CSS): if block.group(1) is None: scopes.pop() continue selector = block.group(1).strip() if selector.startswith("@keyframes"): assert re.fullmatch(r"@keyframes nl-[a-z-]+", selector) elif selector.startswith("@media"): pass elif any(scope.startswith("@keyframes") for scope in scopes): assert selector in {"from", "to"} else: assert all(part.strip().startswith("#neuron-lab") for part in selector.split(",")) scopes.append(selector) assert not scopes assert "prefers-reduced-motion" in LAB_CSS assert '#neuron-lab[data-motion="paused"]' in LAB_CSS assert "animation-play-state: paused !important" in LAB_CSS def test_selection_and_slider_styles_override_the_native_theme_explicitly(): # Checked controls need a visible fill and mark after the general input # background reset; keyboard focus intentionally uses a different color. for kind in ("checkbox", "radio"): assert f'#neuron-lab input[type="{kind}"]:checked::after' in LAB_CSS assert f'#neuron-lab input[type="{kind}"]:focus-visible' in LAB_CSS assert "background-color: var(--nl-violet) !important; background-image: none !important" in LAB_CSS assert "outline: 2px solid var(--nl-cyan) !important" in LAB_CSS assert '--slider-color: var(--nl-violet) !important' in LAB_CSS assert '::-webkit-slider-runnable-track' in LAB_CSS assert 'var(--range_progress, 0%)' in LAB_CSS assert '::-moz-range-progress { background-color: var(--nl-violet)' in LAB_CSS def test_receipt_requires_all_checks_and_only_renders_present_conditions(): receipt = {"run_id": "abc", "trials": {"baseline": {"text": "hello"}, "recipe": {"text": "hola"}}, "verification": {"status": "not_run"}} markup = Markup(render_comparison(receipt)) assert len(markup.classes("nl-condition")) == 2 assert "Reset not checked yet" in markup.text receipt["verification"] = {"status": "passed"} assert "Reset differs" in Markup(render_comparison(receipt)).text receipt["trials"]["reset"] = {"text": "hello", "stop_reason": "token_limit"} receipt["verification"].update({k: True for k in ("token_ids_exact", "first_logits_exact", "hook_registry_restored", "input_replayed_exactly")}) markup = Markup(render_comparison(receipt)) assert "Reset verified" in markup.text and "Token limit reached" in markup.text assert len(markup.classes("nl-condition")) == 3 assert not markup.tagged("unsafe") receipt["verification"]["status"] = "failed" assert "Reset check incomplete" in Markup(render_comparison(receipt)).text