"""focus_on — the agent's ability to point at something on the bench. The failure this tool exists to prevent: the agent naming a specific variant or residue in prose while the canvas sits on whatever tab it was already on, so the user reads "the clash is at 1099" with 1099 nowhere on screen. The failure these TESTS exist to prevent is subtler and has already happened twice in this codebase: the tool's vocabulary is the user-facing tab LABEL ("Library", "Map", "Guides") while the routes underneath are named differently ("design", "plasmid", "crispr"). A label-to-label mapping resolves to nothing and fails silently — no error, no scroll, no way to notice. """ import re from dee.core import agent_tools as t from dee.core import orchestrator as orch _COCKPIT = "dee/static/cockpit.js" _APP_JS = "dee/static/app.js" _INDEX = "dee/static/index.html" def _read(p): with open(p, encoding="utf-8") as fh: return fh.read() def test_it_is_registered_and_needs_no_account(): """Pointing at something changes nothing and stores nothing. Gating it behind sign-in would make the trial run feel dead.""" assert t._TOOLS["focus_on"]["requires_signin"] is False assert any(s["function"]["name"] == "focus_on" for s in orch.TOOL_SPECS) def test_unknown_targets_are_refused_with_the_valid_list(): out = t.execute_tool("focus_on", {"target": "everything"}, auth_anonymous=True) assert out["ok"] is False and "must be one of" in out["error"] def test_targets_that_name_one_thing_require_an_identifier(): """'Look at a variant' is not an instruction. Without this the client would scroll to an arbitrary first row and call it a hit.""" for target in ("variant", "residue", "feature", "guide"): out = t.execute_tool("focus_on", {"target": target}, auth_anonymous=True) assert out["ok"] is False, target def test_whole_tab_targets_do_not_require_one(): for target in ("library", "structure", "map", "guides", "primers"): assert t.execute_tool("focus_on", {"target": target}, auth_anonymous=True)["ok"] is True, target def test_the_payload_reaches_the_client_channel(): """The cockpit reads ev.ui.panel.focus, and ui.panel is literally the _ui key (orchestrator.py: "panel": result.get("_ui")).""" out = t.execute_tool("focus_on", {"target": "residue", "identifier": "1099"}, auth_anonymous=True) assert out["_ui"]["focus"] == {"target": "residue", "identifier": "1099"} def test_it_claims_no_view_of_its_own(): """It points at what is already there. Claiming a view would make the cockpit switch tabs twice and fight its own resolver.""" assert orch._tool_view("focus_on") is None def test_it_does_not_claim_the_scroll_happened(): """Only the browser knows whether the thing is rendered. The system prompt forbids claiming an action with no tool behind it; this is that rule applied to the tool's own return copy.""" out = t.execute_tool("focus_on", {"target": "library"}, auth_anonymous=True) assert "scrolled" not in out["note"].lower() def test_identifiers_are_length_bounded(): out = t.execute_tool("focus_on", {"target": "feature", "identifier": "x" * 500}, auth_anonymous=True) assert len(out["_ui"]["focus"]["identifier"]) <= 64 # --------------------------------------------------------------------------- # # The label/route seam — where this feature silently breaks. # --------------------------------------------------------------------------- # def test_every_focus_target_maps_to_a_real_route_id(): """FOCUS_VIEW in cockpit.js must land on a data-view that exists in index.html. The first draft mapped 'library'->'library' and 'guides'-> 'guides'; neither is a route, so focus_on would have scrolled nowhere.""" real = set(re.findall(r'data-view="([a-z0-9-]+)"', _read(_INDEX))) block = re.search(r"var FOCUS_VIEW = \{(.*?)\};", _read(_COCKPIT), re.S).group(1) routes = re.findall(r":\s*\"([a-z0-9-]+)\"", block) assert routes, "FOCUS_VIEW parsed empty — the regex or the shape changed" unknown = sorted(set(routes) - real) assert not unknown, f"FOCUS_VIEW points at non-existent views: {unknown}" def test_the_tools_enum_and_the_clients_map_agree(): """A target the model can emit but the client cannot resolve is a dead tool call that looks successful.""" spec = next(s for s in orch.TOOL_SPECS if s["function"]["name"] == "focus_on") enum = set(spec["function"]["parameters"]["properties"]["target"]["enum"]) block = re.search(r"var FOCUS_VIEW = \{(.*?)\};", _read(_COCKPIT), re.S).group(1) mapped = set(re.findall(r"(\w+)\s*:", block)) assert enum == mapped, f"enum-only={enum - mapped} map-only={mapped - enum}" assert enum == set(t._FOCUS_TARGETS) # --------------------------------------------------------------------------- # # The DOM anchors the resolver addresses. Guessing a selector here is a silent # no-op, which has already cost this codebase two fixes that could never fire. # --------------------------------------------------------------------------- # def test_the_selectors_the_resolver_uses_are_actually_emitted(): app = _read(_APP_JS) assert "tr.dataset.mut" in app # variant rows assert 'class="pos-mut" data-pos=' in app # mutated residues assert 'class="pm-feat" data-feat=' in app # plasmid features assert 'class="crispr-row" data-rank=' in app # guide rows def test_the_stage_chip_has_styles_to_render_with(): """An element with no CSS is invisible, and the Stage is built in JS — so nothing else in the app would fail if the stylesheet lost these.""" css = _read("dee/static/app.css") for cls in (".td-stage", ".td-stage--on", ".td-stage-dot", ".td-spot"): assert cls in css, cls # --------------------------------------------------------------------------- # # Two CSS decisions that look like sloppiness and are not. Both were found by # measuring the rendered page, and both fail SILENTLY when undone — the ring # simply doesn't draw, with no error anywhere. # --------------------------------------------------------------------------- # def test_the_spotlight_outranks_the_hosts_own_animation(): """A variant row already carries `.result-table tbody tr.expandable { animation: row-in }` at specificity (0,2,2); a bare `.td-spot` is (0,1,0) and loses. Verified in the browser: computed animation-name came back "row-in" and no ring appeared.""" css = _read("dee/static/app.css") rule = re.search(r"\.td-spot \{(.*?)\}", css, re.S).group(1) assert "!important" in rule def test_the_spotlight_ring_has_a_non_color_mix_fallback(): """color-mix() is used freely elsewhere in this file, but only for background TINTS — unsupported means no tint, and the element still reads. Here the mixed colour is the ring itself, so on an engine without color-mix (Safari < 16.2, which ships on macOS Monterey) the whole feature would be invisible. Each keyframe step must declare a plain rgba() first.""" css = _read("dee/static/app.css") frames = re.search(r"@keyframes tdSpot \{(.*?)\n\}", css, re.S).group(1) for step in re.findall(r"(\d+%\s*\{[^}]*\})", frames): if "color-mix" in step: assert step.index("rgba(") < step.index("color-mix"), step