Convert test/cart/blog/market layouts to use _ctx_to_env + render_to_sx_with_env

Phase 4 (Test): Update ~test-layout-full and ~test-detail-layout-full defcomps
to use ~root-header with env free variables. Switch render functions to
render_to_sx_with_env.

Phase 5 (Cart): Convert cart-page, cart-admin, and order render functions.
Update cart .sx layout defcomps to use ~root-header from free variables.

Phase 6 (Blog): Convert all 7 blog layouts (blog, settings, sub-settings x5).
Remove all root_header_sx calls from blog.

Phase 7 (Market): Convert market and market-admin layouts plus browse/product
render functions. Remove root_header_sx import.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-04 15:02:59 +00:00
parent a30e7228d8
commit 1dbf600af2
8 changed files with 507 additions and 350 deletions

104
test/sx/components.sx Normal file
View File

@@ -0,0 +1,104 @@
;; Test service composition defcomps — replaces Python string concatenation
;; in test/sxc/pages/__init__.py.
;; Service filter nav links
(defcomp ~test-service-nav (&key services active-service)
(<>
(~nav-link :href "/" :label "all"
:is-selected (if (not active-service) "true" nil)
:select-colours "aria-selected:bg-sky-200 aria-selected:text-sky-900")
(map (lambda (svc)
(~nav-link :href (str "/?service=" svc) :label svc
:is-selected (if (= active-service svc) "true" nil)
:select-colours "aria-selected:bg-sky-200 aria-selected:text-sky-900"))
services)))
;; Test header menu row
(defcomp ~test-header-row (&key services active-service)
(~menu-row-sx :id "test-row" :level 1 :colour "sky"
:link-href "/" :link-label "Tests" :icon "fa fa-flask"
:nav (~test-service-nav :services services :active-service active-service)
:child-id "test-header-child"))
;; Layout: full page header stack (reads root header values from env free variables)
(defcomp ~test-layout-full (&key services active-service)
(<> (~root-header :cart-mini cart-mini :blog-url blog-url :site-title site-title
:app-label app-label :nav-tree nav-tree :auth-menu auth-menu
:nav-panel nav-panel :settings-url settings-url :is-admin is-admin)
(~header-child-sx
:inner (~test-header-row :services services :active-service active-service))))
;; Map test dicts to test-row components
(defcomp ~test-rows (&key tests)
(<> (map (lambda (t)
(~test-row
:nodeid (get t "nodeid")
:outcome (get t "outcome")
:duration (str (get t "duration"))
:longrepr (or (get t "longrepr") "")))
tests)))
;; Grouped test rows with service headers
(defcomp ~test-grouped-rows (&key sections)
(<> (map (lambda (sec)
(<> (~test-service-header
:service (get sec "service")
:total (str (get sec "total"))
:passed (str (get sec "passed"))
:failed (str (get sec "failed")))
(~test-rows :tests (get sec "tests"))))
sections)))
;; Results partial: conditional rendering based on running/result state
(defcomp ~test-results-partial (&key status summary-data tests sections has-failures)
(let* ((state (get summary-data "state")))
(<>
(~test-summary
:status (get summary-data "status")
:passed (get summary-data "passed")
:failed (get summary-data "failed")
:errors (get summary-data "errors")
:skipped (get summary-data "skipped")
:total (get summary-data "total")
:duration (get summary-data "duration")
:last-run (get summary-data "last_run")
:running (get summary-data "running")
:csrf (get summary-data "csrf")
:active-filter (get summary-data "active_filter"))
(cond
((= state "running") (~test-running-indicator))
((= state "no-results") (~test-no-results))
((= state "empty-filtered") (~test-no-results))
(true (~test-results-table
:rows (~test-grouped-rows :sections sections)
:has-failures has-failures))))))
;; Wrap results in a div with optional HTMX polling
(defcomp ~test-results-wrap (&key running inner)
(div :id "test-results" :class "space-y-6 p-4"
:sx-get (when running "/results")
:sx-trigger (when running "every 2s")
:sx-swap (when running "outerHTML")
inner))
;; Test detail section wrapper
(defcomp ~test-detail-section (&key test)
(section :id "main-panel"
:class "flex-1 md:h-full md:min-h-0 overflow-y-auto overscroll-contain js-grid-viewport"
(~test-detail
:nodeid (get test "nodeid")
:outcome (get test "outcome")
:duration (str (get test "duration"))
:longrepr (or (get test "longrepr") ""))))
;; Detail page header stack (reads root header values from env free variables)
(defcomp ~test-detail-layout-full (&key services test-nodeid test-label)
(<> (~root-header :cart-mini cart-mini :blog-url blog-url :site-title site-title
:app-label app-label :nav-tree nav-tree :auth-menu auth-menu
:nav-panel nav-panel :settings-url settings-url :is-admin is-admin)
(~header-child-sx
:inner (<> (~test-header-row :services services)
(~header-child-sx :id "test-header-child"
:inner (~menu-row-sx :id "test-detail-row" :level 2 :colour "sky"
:link-href (str "/test/" test-nodeid)
:link-label test-label))))))

View File

@@ -5,10 +5,7 @@ import os
from datetime import datetime
from shared.sx.jinja_bridge import load_service_components
from shared.sx.helpers import (
render_to_sx, SxExpr,
root_header_sx, full_page_sx, header_child_sx,
)
from shared.sx.helpers import render_to_sx, SxExpr, render_to_sx_with_env, _ctx_to_env, full_page_sx
# Load test-specific .sx components at import time
load_service_components(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
@@ -21,10 +18,6 @@ def _format_time(ts: float | None) -> str:
return datetime.fromtimestamp(ts).strftime("%-d %b %Y, %H:%M:%S")
# ---------------------------------------------------------------------------
# Menu / header
# ---------------------------------------------------------------------------
_FILTER_MAP = {
"passed": "passed",
"failed": "failed",
@@ -46,120 +39,27 @@ def _filter_tests(tests: list[dict], active_filter: str | None,
return filtered
# ---------------------------------------------------------------------------
# Results partial
# ---------------------------------------------------------------------------
async def test_detail_sx(test: dict) -> str:
"""Return s-expression wire format for a test detail view."""
inner = await render_to_sx(
"test-detail",
nodeid=test["nodeid"],
outcome=test["outcome"],
duration=str(test["duration"]),
longrepr=test.get("longrepr", ""),
)
return (
f'(section :id "main-panel"'
f' :class "flex-1 md:h-full md:min-h-0 overflow-y-auto overscroll-contain js-grid-viewport"'
f' {inner})'
)
# ---------------------------------------------------------------------------
# Sx-native versions — return sx source (not HTML)
# ---------------------------------------------------------------------------
async def _test_header_sx(ctx: dict, active_service: str | None = None) -> str:
"""Build the Tests menu-row as sx call."""
nav = await _service_nav_sx(ctx, active_service)
return await render_to_sx("menu-row-sx",
id="test-row", level=1, colour="sky",
link_href="/", link_label="Tests", icon="fa fa-flask",
nav=SxExpr(nav),
child_id="test-header-child",
)
async def _service_nav_sx(ctx: dict, active_service: str | None = None) -> str:
"""Service filter nav as sx."""
def _service_list() -> list[str]:
from runner import _SERVICE_ORDER
parts = []
parts.append(await render_to_sx("nav-link",
href="/", label="all",
is_selected="true" if not active_service else None,
select_colours="aria-selected:bg-sky-200 aria-selected:text-sky-900",
))
for svc in _SERVICE_ORDER:
parts.append(await render_to_sx("nav-link",
href=f"/?service={svc}", label=svc,
is_selected="true" if active_service == svc else None,
select_colours="aria-selected:bg-sky-200 aria-selected:text-sky-900",
))
return "(<> " + " ".join(parts) + ")"
return list(_SERVICE_ORDER)
async def _header_stack_sx(ctx: dict, active_service: str | None = None) -> str:
"""Full header stack as sx."""
hdr = await root_header_sx(ctx)
inner = await _test_header_sx(ctx, active_service)
child = await header_child_sx(inner)
return "(<> " + hdr + " " + child + ")"
async def _test_rows_sx(tests: list[dict]) -> str:
"""Render all test result rows as sx."""
parts = []
for t in tests:
parts.append(await render_to_sx("test-row",
nodeid=t["nodeid"],
outcome=t["outcome"],
duration=str(t["duration"]),
longrepr=t.get("longrepr", ""),
))
return "(<> " + " ".join(parts) + ")"
async def _grouped_rows_sx(tests: list[dict]) -> str:
"""Test rows grouped by service as sx."""
from runner import group_tests_by_service
sections = group_tests_by_service(tests)
parts = []
for sec in sections:
parts.append(await render_to_sx("test-service-header",
service=sec["service"],
total=str(sec["total"]),
passed=str(sec["passed"]),
failed=str(sec["failed"]),
))
parts.append(await _test_rows_sx(sec["tests"]))
return "(<> " + " ".join(parts) + ")"
async def _results_partial_sx(result: dict | None, running: bool, csrf: str,
active_filter: str | None = None,
active_service: str | None = None) -> str:
"""Results section as sx."""
def _build_summary_data(result: dict | None, running: bool, csrf: str,
active_filter: str | None) -> dict:
"""Prepare summary data dict for the ~test-results-partial defcomp."""
if running and not result:
summary = await render_to_sx("test-summary",
status="running", passed="0", failed="0", errors="0",
skipped="0", total="0", duration="...",
last_run="in progress", running=True, csrf=csrf,
active_filter=active_filter,
)
return "(<> " + summary + " " + await render_to_sx("test-running-indicator") + ")"
return dict(state="running", status="running", passed="0", failed="0",
errors="0", skipped="0", total="0", duration="...",
last_run="in progress", running=True, csrf=csrf,
active_filter=active_filter)
if not result:
summary = await render_to_sx("test-summary",
status=None, passed="0", failed="0", errors="0",
skipped="0", total="0", duration="0",
last_run="never", running=running, csrf=csrf,
active_filter=active_filter,
)
return "(<> " + summary + " " + await render_to_sx("test-no-results") + ")"
return dict(state="no-results", status=None, passed="0", failed="0",
errors="0", skipped="0", total="0", duration="0",
last_run="never", running=running, csrf=csrf,
active_filter=active_filter)
status = "running" if running else result["status"]
summary = await render_to_sx("test-summary",
return dict(
state="running" if running else "has-results",
status=status,
passed=str(result["passed"]),
failed=str(result["failed"]),
@@ -168,34 +68,14 @@ async def _results_partial_sx(result: dict | None, running: bool, csrf: str,
total=str(result["total"]),
duration=str(result["duration"]),
last_run=_format_time(result["finished_at"]) if not running else "in progress",
running=running,
csrf=csrf,
running=running, csrf=csrf,
active_filter=active_filter,
)
if running:
return "(<> " + summary + " " + await render_to_sx("test-running-indicator") + ")"
tests = result.get("tests", [])
tests = _filter_tests(tests, active_filter, active_service)
if not tests:
return "(<> " + summary + " " + await render_to_sx("test-no-results") + ")"
has_failures = result["failed"] > 0 or result["errors"] > 0
rows = await _grouped_rows_sx(tests)
table = await render_to_sx("test-results-table",
rows=SxExpr(rows),
has_failures=str(has_failures).lower(),
)
return "(<> " + summary + " " + table + ")"
def _wrap_results_div_sx(inner: str, running: bool) -> str:
"""Wrap results in a div with HTMX polling (sx)."""
attrs = ':id "test-results" :class "space-y-6 p-4"'
if running:
attrs += ' :sx-get "/results" :sx-trigger "every 2s" :sx-swap "outerHTML"'
return f'(div {attrs} {inner})'
async def test_detail_sx(test: dict) -> str:
"""Return s-expression wire format for a test detail view."""
return await render_to_sx("test-detail-section", test=test)
async def render_dashboard_page_sx(ctx: dict, result: dict | None,
@@ -203,9 +83,26 @@ async def render_dashboard_page_sx(ctx: dict, result: dict | None,
active_filter: str | None = None,
active_service: str | None = None) -> str:
"""Full page: test dashboard (sx wire format)."""
hdr = await _header_stack_sx(ctx, active_service)
inner = await _results_partial_sx(result, running, csrf, active_filter, active_service)
content = _wrap_results_div_sx(inner, running)
from runner import group_tests_by_service
summary_data = _build_summary_data(result, running, csrf, active_filter)
sections = []
has_failures = "false"
if result and not running:
tests = _filter_tests(result.get("tests", []), active_filter, active_service)
if tests:
sections = group_tests_by_service(tests)
has_failures = str(result["failed"] > 0 or result["errors"] > 0).lower()
else:
summary_data["state"] = "empty-filtered"
inner = await render_to_sx("test-results-partial",
summary_data=summary_data, sections=sections, has_failures=has_failures)
content = await render_to_sx("test-results-wrap", running=running, inner=SxExpr(inner))
hdr = await render_to_sx_with_env("test-layout-full", _ctx_to_env(ctx),
services=_service_list(),
active_service=active_service,
)
return await full_page_sx(ctx, header_rows=hdr, content=content)
@@ -214,23 +111,31 @@ async def render_results_partial_sx(result: dict | None, running: bool,
active_filter: str | None = None,
active_service: str | None = None) -> str:
"""HTMX partial: results section (sx wire format)."""
inner = await _results_partial_sx(result, running, csrf, active_filter, active_service)
return _wrap_results_div_sx(inner, running)
from runner import group_tests_by_service
summary_data = _build_summary_data(result, running, csrf, active_filter)
sections = []
has_failures = "false"
if result and not running:
tests = _filter_tests(result.get("tests", []), active_filter, active_service)
if tests:
sections = group_tests_by_service(tests)
has_failures = str(result["failed"] > 0 or result["errors"] > 0).lower()
else:
summary_data["state"] = "empty-filtered"
inner = await render_to_sx("test-results-partial",
summary_data=summary_data, sections=sections, has_failures=has_failures)
return await render_to_sx("test-results-wrap", running=running, inner=SxExpr(inner))
async def render_test_detail_page_sx(ctx: dict, test: dict) -> str:
"""Full page: test detail (sx wire format)."""
root_hdr = await root_header_sx(ctx)
test_row = await _test_header_sx(ctx)
detail_row = await render_to_sx("menu-row-sx",
id="test-detail-row", level=2, colour="sky",
link_href=f"/test/{test['nodeid']}",
link_label=test["nodeid"].rsplit("::", 1)[-1],
hdr = await render_to_sx_with_env("test-detail-layout-full", _ctx_to_env(ctx),
services=_service_list(),
test_nodeid=test["nodeid"],
test_label=test["nodeid"].rsplit("::", 1)[-1],
)
hdr_child_detail = await header_child_sx(detail_row, id="test-header-child")
inner = "(<> " + test_row + " " + hdr_child_detail + ")"
hdr_child_inner = await header_child_sx(inner)
hdr = "(<> " + root_hdr + " " + hdr_child_inner + ")"
content = await render_to_sx("test-detail",
nodeid=test["nodeid"],
outcome=test["outcome"],