"""The renderers, over every recorded state, and against text a visitor could type.""" from __future__ import annotations import copy import json import re from pathlib import Path import pytest import recorded from steering.core import ALPHAS from viz import render as R TRACES = [t for rec in recorded.records().values() for t in rec["scenarios"]] def panels(trace, alpha): return [R.formula_md(trace, alpha), R.cards_html(trace, alpha), R.response_html(trace, alpha), R.chart_html(trace, alpha), R.sweep_html(trace, alpha), R.schematic_html(trace, alpha), R.takeaway_html(trace), R.contrast_html(trace), R.explain_html(trace)] @pytest.mark.parametrize("trace", TRACES, ids=[t["model"].split("/")[-1] + ":" + t["id"] for t in TRACES]) def test_every_state_renders_and_shows_its_own_numbers(trace): for alpha in ALPHAS: state = R.state_at(trace, alpha) out = panels(trace, alpha) token = R.label(trace["candidates"][state["selected_index"]]) assert token in out[1] and token in out[2] assert R.fmt_pct(state["percents"][state["selected_index"]]) in out[3] # Each bar is as wide as its probability, and its marker sits at the alpha = 0 value. widths = [float(w) for w in re.findall(r'sd-fill [a-z]+" style="width:([0-9.]+)%', out[3])] assert widths == pytest.approx([*state["percents"], state["other"]], abs=1e-3) assert f"α = {R.fmt_alpha(alpha)}" in out[1] def test_html_panels_carry_no_template_syntax(): """In Gradio 6 gr.HTML is a template: a stray ${...} or {{...}} would be evaluated.""" for trace in TRACES: for alpha in (-2.0, 0.0, 2.0): for html in panels(trace, alpha)[1:]: assert "{" not in html and "}" not in html and "$" not in html def test_text_a_visitor_typed_is_escaped(): trace = copy.deepcopy(TRACES[0]) hostile = ' ${window.x} {{y}}' trace.update(prompt=hostile, prefix=hostile, custom=True, source="live", cached=False, stale=False) for alpha in (0.0, 2.0): for html in panels(trace, alpha)[1:]: assert "