;; Live test card — renders one upstream hyperscript test as an interactive demo.
;; :run-src is an SX SOURCE STRING like "(fn (sandbox) …)". The island parses
;; + evals it at Run time. Strings round-trip through aser cleanly; anything
;; callable-shaped (fn-list or actual lambda) gets rewritten to "<lambda>" and
;; blows up on re-parse.
(defisland
  ~hyperscript/hs-test-card
  (&key name html action check run-src)
  (let
    ((status (signal "idle"))
      (message (signal ""))
      (sandbox-ref (dict "current" nil)))
    (let
      ((run-test (fn (e) (let ((sandbox (get sandbox-ref "current"))) (when sandbox (dom-set-inner-html sandbox "") (reset! status "running") (reset! message "") (guard (err (true (do (reset! status "fail") (reset! message (str err))))) (let ((runner-fn (eval-expr-cek (first (sx-parse run-src))))) (runner-fn sandbox)) (reset! status "pass") (reset! message "all assertions held"))))))
        (reset-test
          (fn
            (e)
            (let
              ((sandbox (get sandbox-ref "current")))
              (when sandbox (dom-set-inner-html sandbox ""))
              (reset! status "idle")
              (reset! message ""))))
        (dot-colour
          (computed
            (fn
              ()
              (let
                ((s (deref status)))
                (cond
                  ((= s "pass") "#16a34a")
                  ((= s "fail") "#dc2626")
                  ((= s "running") "#7c3aed")
                  (true "#a8a29e"))))))
        (dot-glyph
          (computed
            (fn
              ()
              (let
                ((s (deref status)))
                (cond
                  ((= s "pass") "✓")
                  ((= s "fail") "✗")
                  ((= s "running") "⟳")
                  (true "○"))))))
        (result-text
          (computed
            (fn
              ()
              (let
                ((s (deref status)) (m (deref message)))
                (cond
                  ((= s "pass") (str "PASS — " m))
                  ((= s "fail") (str "FAIL — " m))
                  ((= s "running") "running…")
                  (true "not run")))))))
      (div
        :class "hs-test-card"
        :style "border:1px solid #e7e5e4;border-radius:0.5rem;padding:0.75rem;background:#fff;margin-bottom:0.75rem"
        (div
          :style "display:flex;align-items:center;gap:0.5rem;margin-bottom:0.5rem"
          (span
            :style (str
              "font-size:1.25rem;font-weight:700;color:"
              (deref dot-colour))
            (deref dot-glyph))
          (span :style "font-weight:500;color:#292524" name)
          (span
            :style "margin-left:auto;font-size:0.75rem;color:#78716c"
            (deref result-text)))
        (div
          :style "font-size:0.75rem;font-family:ui-monospace,monospace;background:#fafaf9;border:1px solid #e7e5e4;border-radius:0.25rem;padding:0.5rem;margin-bottom:0.5rem;white-space:pre-wrap;overflow-x:auto"
          (div :style "color:#78716c" "HTML")
          (div :style "color:#292524;margin-bottom:0.25rem" html)
          (when
            action
            (div
              (div :style "color:#78716c" "action")
              (div :style "color:#292524;margin-bottom:0.25rem" action)))
          (when
            check
            (div
              (div :style "color:#78716c" "check")
              (div :style "color:#292524" check))))
        (div
          :style "border:1px dashed #d6d3d1;border-radius:0.25rem;padding:0.5rem;margin-bottom:0.5rem;min-height:2.5rem;background:#fafaf9"
          (div
            :style "font-size:0.625rem;color:#a8a29e;margin-bottom:0.25rem"
            "sandbox")
          (div :ref sandbox-ref :class "hs-test-sandbox"))
        (div
          :style "display:flex;gap:0.5rem"
          (button
            :on-click run-test
            :style "padding:0.25rem 0.75rem;border:1px solid #7c3aed;background:#7c3aed;color:#fff;border-radius:0.25rem;font-size:0.875rem;cursor:pointer"
            "Run")
          (button
            :on-click reset-test
            :style "padding:0.25rem 0.75rem;border:1px solid #d6d3d1;background:#fff;color:#44403c;border-radius:0.25rem;font-size:0.875rem;cursor:pointer"
            "Reset"))))))
