;; _hyperscript integration tests
;; Tests hs-handler: source → callable function via eval-expr-cek.
;; DOM-dependent tests (hs-boot!, hs-activate!) deferred to Playwright.

;; ── Handler compilation ───────────────────────────────────────
(defsuite
  "hs-handler-compilation"
  (deftest
    "handler returns a function"
    (let ((h (hs-handler "log 'hello'"))) (assert= true (procedure? h))))
  (deftest
    "handler accepts me argument"
    (let
      ((h (hs-handler "set x to 42")))
      (h "mock-element")
      (assert= 42 x)))
  (deftest
    "handler can return a value"
    (let ((h (hs-handler "return 99"))) (assert= 99 (h "el"))))
  (deftest
    "handler binds me from argument"
    (let
      ((h (hs-handler "set x to me")))
      (h "my-element")
      (assert= "my-element" x)))
  (deftest
    "handler initializes it to nil"
    (let ((h (hs-handler "set x to it"))) (h "el") (assert= nil x))))

;; ── Arithmetic in handler ─────────────────────────────────────
(defsuite
  "hs-handler-expressions"
  (deftest
    "arithmetic works in handler"
    (let ((h (hs-handler "set x to 2 + 3"))) (h "el") (assert= 5 x)))
  (deftest
    "string concatenation via set"
    (let
      ((h (hs-handler "set x to 'hello'")))
      (h "el")
      (assert= "hello" x)))
  (deftest
    "conditional in handler"
    (let
      ((h (hs-handler "if true set x to 1 end")))
      (h "el")
      (assert= 1 x)))
  (deftest
    "if-else in handler"
    (let
      ((h (hs-handler "if false set x to 1 else set x to 2 end")))
      (h "el")
      (assert= 2 x))))

;; ── Sequence / control flow ───────────────────────────────────
(defsuite
  "hs-handler-control"
  (deftest
    "then chains commands"
    (let
      ((h (hs-handler "set x to 1 then set y to 2")))
      (h "el")
      (assert= 1 x)
      (assert= 2 y)))
  (deftest
    "for iterates"
    (let
      ((h (hs-handler "set total to 0 then for n in [1, 2, 3] set total to total + n end")))
      (h "el")
      (assert= 6 total)))
  (deftest
    "repeat counts"
    (let
      ((h (hs-handler "set count to 0 then repeat 3 times set count to count + 1 end")))
      (h "el")
      (assert= 3 count))))