Add macros, declarative handlers (defhandler), and convert all fragment routes to sx
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled

Phase 1 — Macros: defmacro + quasiquote syntax (`, ,, ,@) in parser,
evaluator, HTML renderer, and JS mirror. Macro type, expansion, and
round-trip serialization.

Phase 2 — Expanded primitives: app-url, url-for, asset-url, config,
format-date, parse-int (pure); service, request-arg, request-path,
nav-tree, get-children (I/O); jinja-global, relations-from (pure).
Updated _io_service to accept (service "registry-name" "method" :kwargs)
with auto kebab→snake conversion. DTO-to-dict now expands datetime fields
into year/month/day convenience keys. Tuple returns converted to lists.

Phase 3 — Declarative handlers: HandlerDef type, defhandler special form,
handler registry (service → name → HandlerDef), async evaluator+renderer
(async_eval.py) that awaits I/O primitives inline within control flow.
Handler loading from .sx files, execute_handler, blueprint factory.

Phase 4 — Convert all fragment routes: 13 Python fragment handlers across
8 services replaced with declarative .sx handler files. All routes.py
simplified to uniform sx dispatch pattern. Two Jinja HTML handlers
(events/container-cards, events/account-page) kept as Python.

New files: shared/sx/async_eval.py, shared/sx/handlers.py,
shared/sx/tests/test_handlers.py, plus 13 handler .sx files under
{service}/sx/handlers/. MarketService.product_by_slug() added.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-03 00:22:18 +00:00
parent 13bcf755f6
commit ab75e505a8
48 changed files with 2538 additions and 638 deletions

View File

@@ -2,7 +2,7 @@
import pytest
from shared.sx import parse, evaluate, EvalError, Symbol, Keyword, NIL
from shared.sx.types import Lambda, Component
from shared.sx.types import Lambda, Component, Macro, HandlerDef
# ---------------------------------------------------------------------------
@@ -324,3 +324,82 @@ class TestSetBang:
env = {"x": 1}
ev("(set! x 42)", env)
assert env["x"] == 42
# ---------------------------------------------------------------------------
# Macros
# ---------------------------------------------------------------------------
class TestMacro:
def test_defmacro_creates_macro(self):
env = {}
ev("(defmacro double (x) `(+ ,x ,x))", env)
assert isinstance(env["double"], Macro)
assert env["double"].name == "double"
def test_simple_expansion(self):
env = {}
ev("(defmacro double (x) `(+ ,x ,x))", env)
assert ev("(double 5)", env) == 10
def test_quasiquote_with_splice(self):
env = {}
ev("(defmacro add-all (&rest nums) `(+ ,@nums))", env)
assert ev("(add-all 1 2 3)", env) == 6
def test_rest_param(self):
env = {}
ev("(defmacro my-list (&rest items) `(list ,@items))", env)
assert ev("(my-list 1 2 3)", env) == [1, 2, 3]
def test_macro_with_let(self):
env = {}
ev("(defmacro bind-and-add (name val) `(let ((,name ,val)) (+ ,name 1)))", env)
assert ev("(bind-and-add x 10)", env) == 11
def test_quasiquote_standalone(self):
"""Quasiquote without defmacro works for template expansion."""
env = {"x": 42}
result = ev("`(a ,x b)", env)
assert result == [Symbol("a"), 42, Symbol("b")]
def test_quasiquote_splice(self):
env = {"rest": [1, 2, 3]}
result = ev("`(a ,@rest b)", env)
assert result == [Symbol("a"), 1, 2, 3, Symbol("b")]
def test_macro_wrong_arity(self):
"""Macro with too few args gets NIL for missing params."""
env = {}
ev("(defmacro needs-two (a b) `(+ ,a ,b))", env)
# Calling with 1 arg — b becomes NIL
with pytest.raises(Exception):
ev("(needs-two 5)", env)
def test_macro_in_html_render(self):
"""Macros expand correctly in HTML render context."""
from shared.sx.html import render as html_render
env = {}
ev('(defmacro bold (text) `(strong ,text))', env)
expr = parse('(bold "hello")')
result = html_render(expr, env)
assert result == "<strong>hello</strong>"
# ---------------------------------------------------------------------------
# defhandler
# ---------------------------------------------------------------------------
class TestDefhandler:
def test_defhandler_creates_handler(self):
env = {}
ev("(defhandler link-card (&key slug keys) slug)", env)
assert isinstance(env["handler:link-card"], HandlerDef)
assert env["handler:link-card"].name == "link-card"
assert env["handler:link-card"].params == ["slug", "keys"]
def test_defhandler_body_preserved(self):
env = {}
ev("(defhandler test-handler (&key id) (str id))", env)
handler = env["handler:test-handler"]
assert handler.body is not None