"""Unit tests for the report-proxy route and the inline iframe viewer. The Space exposes two paths for the per-submission HTML report: - ``/reports/{submission_id}.html`` (FastAPI route): re-serves the file with ``Content-Type: text/html``. Works under Bearer auth for programmatic clients; gets 404'd by HF's edge for logged-in browser users on a private Space (no auth carryover across same-origin pathname navigations). Kept as a backdoor / for the future public migration. - ``_format_detail_and_report`` (row-click handler): server-side fetches the report via ``hf_hub_download`` and inlines it into an ``") monkeypatch.setattr(app.gr, "Info", lambda *a, **k: None) admin_df, validated, unvalidated, gallery_html, _confirm, _delete_btn, _stop_btn = ( app._admin_delete(table_df, True, types.SimpleNamespace(username="michaelr27")) ) assert deleted == [["validated-old-row"]] assert admin_df is empty_admin assert validated is empty_validated assert unvalidated is empty_unvalidated assert gallery_html == "" def test_data_error_banner_md_present_on_error_empty_otherwise(): """Banner markdown is non-empty (and names the cause) only on error.""" assert app._data_error_banner_md(None) == "" assert app._data_error_banner_md("") == "" banner = app._data_error_banner_md("boom: 403 Forbidden") assert "boom: 403 Forbidden" in banner assert "unavailable" in banner.lower() def test_refresh_handler_shows_banner_and_warns_on_error(monkeypatch): """Manual refresh surfaces the failure loudly: visible banner + warning toast. ``gr.Warning`` / ``gr.Info`` are stubbed so the test runs outside a Gradio request context; the assertion is that a failure path fires a warning (not an info) and flips the banner visible. """ def boom(): raise leaderboard.LeaderboardDataError("simulated hub failure") monkeypatch.setattr(app, "load_leaderboard_split", boom) toasts = {"warning": 0, "info": 0} monkeypatch.setattr(app.gr, "Warning", lambda *a, **k: toasts.__setitem__("warning", toasts["warning"] + 1)) monkeypatch.setattr(app.gr, "Info", lambda *a, **k: toasts.__setitem__("info", toasts["info"] + 1)) validated, unvalidated, banner = app._refresh_leaderboard_with_toast() assert toasts["warning"] == 1 assert toasts["info"] == 0 assert len(validated) == 0 and len(unvalidated) == 0 # The banner output is a gr.Markdown update flipped visible. assert getattr(banner, "visible", None) is True def test_iframe_escape_is_attribute_safe(monkeypatch): """Quotes / ampersands inside the report HTML are escaped properly. A `"` inside the report would otherwise terminate the srcdoc attribute prematurely and break parsing. Regression guard. """ monkeypatch.setattr( app, "_fetch_report_html", lambda sid: b'tag: x & co.', ) df = _stub_row() _md, iframe = app._format_detail_and_report(df, _fake_evt()) # Within the srcdoc value, double-quotes must be HTML-escaped. srcdoc = re.search(r'srcdoc="(.*)"\s+style=', iframe, re.DOTALL) assert srcdoc is not None inner = srcdoc.group(1) # No unescaped " inside the attribute value. assert '"' not in inner assert """ in inner assert "&" in inner