"""Test dashboard routes.""" from __future__ import annotations import asyncio from quart import Blueprint, Response, make_response, request def register(url_prefix: str = "/") -> Blueprint: bp = Blueprint("dashboard", __name__, url_prefix=url_prefix) @bp.get("/") async def index(): """Full page dashboard with last results.""" from shared.sexp.page import get_template_context from shared.browser.app.csrf import generate_csrf_token from sexp.sexp_components import render_dashboard_page import runner ctx = await get_template_context() result = runner.get_results() running = runner.is_running() csrf = generate_csrf_token() html = await render_dashboard_page(ctx, result, running, csrf) return await make_response(html, 200) @bp.post("/run") async def run(): """Trigger a test run, redirect to /.""" import runner if not runner.is_running(): asyncio.create_task(runner.run_tests()) # HX-Redirect for HTMX, regular redirect for non-HTMX if request.headers.get("HX-Request"): resp = Response("", status=200) resp.headers["HX-Redirect"] = "/" return resp from quart import redirect as qredirect return qredirect("/") @bp.get("/results") async def results(): """HTMX partial — poll target for results table.""" from shared.browser.app.csrf import generate_csrf_token from sexp.sexp_components import render_results_partial import runner result = runner.get_results() running = runner.is_running() csrf = generate_csrf_token() html = await render_results_partial(result, running, csrf) resp = Response(html, status=200, content_type="text/html") # If still running, tell HTMX to keep polling if running: resp.headers["HX-Trigger-After-Swap"] = "test-still-running" return resp return bp