"""Run explorer (inversion/explorer.py): stages follow the run, model text is escaped, gates are coloured.""" import re import pytest from inversion import DSGSceneSchema, EngineConfig, RewritePatch, iter_inversion from inversion.explorer import diff_html, render_explorer from test_engine import GRAPH, StubWorld GATES = dict(vqa_gate=0.90, dino_gate=0.80) def render(run, **kw): return render_explorer(run, og_url="OG.webp", og_size=(640, 480), image_url=lambda im: f"IMG-{id(im)}", run_id="run-1", **kw) def stages(html): """[(label, state)] in navigator order.""" found = re.findall(r'class="pre-x-step (\w+)"[^>]*>.*?([^<]+)', html) return [(label, state) for state, label in found] def live(html): return int(re.search(r'data-live="(\d+)"', html).group(1)) def snapshots(world, cfg): """Explorer HTML and (phase, phase_t) at every streamed yield.""" out = [] for run in iter_inversion(world.og, world.backends(), cfg): out.append((run.phase, run.phase_t, render(run))) return run, out def test_phase_is_set_at_every_yield(): _, snaps = snapshots(StubWorld(), EngineConfig(max_iters=5, **GATES)) assert [(p, t) for p, t, _ in snaps] == [ ("extract", 0), ("ground", 0), ("render", 1), ("verify", 1), ("rewrite", 1), ("render", 2), ("verify", 2), ("done", 2)] def test_stages_follow_the_run_and_live_stage_is_never_pending(): run, snaps = snapshots(StubWorld(), EngineConfig(max_iters=5, **GATES)) for phase, _, html in snaps: st = stages(html) assert st[live(html)][1] != "pending", phase first, last = snaps[0][2], snaps[-1][2] assert stages(first) == [("Extract scene graph", "live"), ("Ground on original", "pending"), ("Render proxy", "pending"), ("Verify", "pending"), ("Verified prompt", "pending")] assert stages(last) == [(label, "done") for label in ( "Extract scene graph", "Ground on original", "Render proxy", "Verify", "Rewrite failures", "Render proxy", "Verify", "Verified prompt")] assert live(last) == len(stages(last)) - 1 assert 'data-done="1"' in last and "Converged" in last def test_render_stage_shows_the_image_while_it_is_being_scored(): world = StubWorld() for run in iter_inversion(world.og, world.backends(), EngineConfig(max_iters=1, **GATES)): if run.phase == "verify": html = render(run) assert f"IMG-{id(run.live_proxy)}" in html assert stages(html)[2:4] == [("Render proxy", "done"), ("Verify", "live")] break else: pytest.fail("no verify phase") def test_verify_tiles_are_coloured_against_the_gates(): run, snaps = snapshots(StubWorld(), EngineConfig(max_iters=5, **GATES)) html = snaps[-1][2] # t=1: S_VQA 0.87 < 0.90 -> warn; S_DINO 1.00 >= 0.80 -> ok; joint has no gate. verify1 = html.split('aria-label="Iteration 1: Verify"')[1].split("")[0] tiles = re.findall(r'class="pre-x-tile (\w+)"', verify1) assert tiles == ["warn", "ok", "none"] assert "Below gate −0.03" in verify1 and "Meets gate +0.20" in verify1 assert verify1.count('class="pre-x-help"') == 3 and verify1.count('class="pre-x-pop"') == 3 assert "&lt;" not in verify1 # footer text is escaped once, not twice assert "S_VQA 0.87 < 0.90 · S_DINO 1.00 ≥ 0.80 → rewrite" in verify1 def test_dino_gate_off_and_max_iters_wording(): world = StubWorld(fix_on_rewrite=False) run, snaps = snapshots(world, EngineConfig(max_iters=2, vqa_gate=0.90, dino_gate=0.0)) html = snaps[-1][2] assert run.stop_reason == "max_iters" assert "Gates not met" in html and "Returning the best of 2 iterations" in html assert "Gate off" in html and "S_DINO gate off" in html assert "max iterations reached → return best (t = 1)" in html # the rewriter's unusable edit is shown as skipped, with its reason assert "original span not found verbatim" in html and ">Skipped<" in html def test_grounding_off_and_rewrite_error(): world = StubWorld() def boom(*args): raise RuntimeError("rewriter offline") backends = world.backends() backends.rewrite = boom run = None for run in iter_inversion(world.og, backends, EngineConfig(max_iters=2, ground=False, **GATES)): pass html = render(run) assert "Grounding is off for this run" in html assert "The rewriter failed: RuntimeError: rewriter offline" in html def test_model_text_is_escaped(): world = StubWorld() evil = {**GRAPH, "background": 'a wall', "nodes": [{**GRAPH["nodes"][0], "entity": 'a circle'}] + GRAPH["nodes"][1:]} world.extract = lambda image: DSGSceneSchema.model_validate(evil) run, snaps = snapshots(world, EngineConfig(max_iters=1, ground=False, **GATES)) html = snaps[-1][2] assert "