Files
rose-ash/lib/hyperscript/debug.sx
giles a9066c0653 Persistent Lisp image for sx_eval: smart file reload + IO tracing
sx_eval now accepts files (smart-loaded by mtime — unchanged files skip),
trace_io (harness-wrapped IO capture), mock (evaluated platform overrides),
and setup params. Definitions survive between calls. sx_harness_eval also
uses smart loading. sx_write_file can create new files.

New lib/hyperscript/debug.sx: mock DOM platform for instant hyperscript
testing — compile and execute HS expressions against simulated elements,
see every DOM mutation and wait in the IO trace.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 19:56:38 +00:00

39 lines
2.1 KiB
Plaintext

;; Hyperscript debug harness — mock DOM for instant testing
;;
;; Load once into the image, then repeatedly call hs-run.
;; All DOM ops are intercepted and logged via the test harness.
;; ── Mock element ────────────────────────────────────────────────
(define
hs-mock-element
(fn
(tag id classes)
(let
((cls-set (reduce (fn (d c) (dict-set d c true)) {} classes)))
{:children () :_hs-activated true :tag tag :classes cls-set :text "" :id id :attrs {}})))
;; ── Mock platform ───────────────────────────────────────────────
(define hs-mock-platform {:hs-wait (fn (ms) nil) :hs-wait-for (fn (target event) nil) :dom-get-attr (fn (el attr) (get (get el "attrs") attr)) :dom-has-class? (fn (el cls) (dict-has? (get el "classes") cls)) :dom-set-text (fn (el text) (dict-set! el "text" text) nil) :hs-settle (fn (el) nil) :dom-add-class (fn (el cls) (dict-set! (get el "classes") cls true) nil) :dom-query (fn (sel) nil) :dom-remove-class (fn (el cls) (dict-delete! (get el "classes") cls) nil) :dom-listen (fn (target event-name handler) (handler {:target target :type event-name})) :dom-set-attr (fn (el attr val) (dict-set! (get el "attrs") attr val) nil) :dom-query-all (fn (sel) ())})
;; ── Convenience runner ──────────────────────────────────────────
(define
hs-run
(fn
(src)
(let
((me (hs-mock-element "div" "test" ()))
(sx (hs-to-sx-from-source src)))
(let
((handler (eval-expr (list (quote fn) (quote (me)) (list (quote let) (quote ((it nil) (event {:target me :type "click"}))) sx)))))
(handler me)
me))))
;; ── Element inspection ──────────────────────────────────────────
(define hs-classes (fn (el) (keys (get el "classes"))))
(define hs-has-class? (fn (el cls) (dict-has? (get el "classes") cls)))