(define _island-tests (list))

(define
  register-test
  (fn
    (name test-fn)
    (append! _island-tests (dict "name" name "fn" test-fn))))

(register-test
  "counter-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-counter :initial 0))))
      (assert (contains? html "−") "has minus button")
      (assert (contains? html "+") "has plus button")
      (assert (contains? html ">0<") "shows initial 0")
      (assert (contains? html "doubled") "has doubled label"))))

(register-test
  "counter-initial-42"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-counter :initial 42))))
      (assert (contains? html ">42<") "shows 42")
      (assert (contains? html ">84<") "doubled shows 84"))))

(register-test
  "counter-signals"
  (fn
    ()
    (let
      ((count (signal 0))
        (doubled (computed (fn () (* 2 (deref count))))))
      (assert= (deref count) 0 "initial count")
      (assert= (deref doubled) 0 "initial doubled")
      (swap! count inc)
      (assert= (deref count) 1 "after inc")
      (assert= (deref doubled) 2 "doubled reacts")
      (swap! count dec)
      (assert= (deref count) 0 "after dec"))))

(register-test
  "temperature-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-temperature :celsius 20))))
      (assert (contains? html "°C") "shows celsius")
      (assert (contains? html "°F") "shows fahrenheit")
      (assert (contains? html "20") "shows 20")
      (assert (contains? html "68") "shows 68F"))))

(register-test
  "temperature-conversion"
  (fn
    ()
    (let
      ((c (signal 100))
        (f (computed (fn () (+ (* (deref c) 9/5) 32)))))
      (assert= (deref f) 212 "100C = 212F")
      (reset! c 0)
      (assert= (deref f) 32 "0C = 32F"))))

(register-test
  "stopwatch-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-stopwatch))))
      (assert (contains? html "0") "shows 0"))))

(register-test
  "input-binding-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-input-binding))))
      (assert (contains? html "<input") "has input"))))

(register-test
  "dynamic-class-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-dynamic-class))))
      (assert (contains? html "Toggle") "has toggle button"))))

(register-test
  "dynamic-class-signal"
  (fn
    ()
    (let
      ((active (signal false)))
      (assert= (deref active) false "initial off")
      (swap! active not)
      (assert= (deref active) true "toggled on"))))

(register-test
  "reactive-list-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-reactive-list))))
      (assert (contains? html "Add") "has add button"))))

(register-test
  "reactive-list-signal"
  (fn
    ()
    (let
      ((items (signal (list "a" "b"))))
      (assert= (len (deref items)) 2 "initial count")
      (swap! items (fn (xs) (append xs (list "c"))))
      (assert= (len (deref items)) 3 "after add"))))

(register-test
  "store-writer-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-store-writer))))
      (assert (contains? html "Writer") "has writer label")
      (assert (contains? html "<select") "has select"))))

(register-test
  "store-reader-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-store-reader))))
      (assert (contains? html "Reader") "has reader label"))))

(register-test
  "refs-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-refs))))
      (assert (contains? html "<input") "has input")
      (assert (contains? html "Focus") "has focus button"))))

(register-test
  "portal-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-portal))))
      (assert
        (or (contains? html "Open") (contains? html "button"))
        "has toggle"))))

(register-test
  "error-boundary-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-error-boundary))))
      (assert (> (len html) 10) "renders content"))))

(register-test
  "resource-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-resource))))
      (assert (> (len html) 10) "renders content"))))

(register-test
  "transition-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-transition))))
      (assert (> (len html) 10) "renders content"))))

(register-test
  "event-bridge-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-event-bridge))))
      (assert (contains? html "Send") "has send button"))))

(register-test
  "imperative-renders"
  (fn
    ()
    (let
      ((html (render-to-html (~reactive-islands/index/demo-imperative))))
      (assert (contains? html "button") "has button"))))

(register-test
  "counter-dom"
  (fn
    ()
    (let
      ((container (dom-create-element "div" nil))
        (rendered
          (render-to-dom
            (~reactive-islands/index/demo-counter :initial 5)
            nil
            nil)))
      (dom-append container rendered)
      (assert= (len (dom-query-all container "button")) 2 "2 buttons")
      (assert (contains? (dom-text-content container) "5") "shows 5")
      (assert
        (contains? (dom-text-content container) "10")
        "doubled shows 10"))))

(register-test
  "temperature-dom"
  (fn
    ()
    (let
      ((container (dom-create-element "div" nil))
        (rendered
          (render-to-dom
            (~reactive-islands/index/demo-temperature :celsius 25)
            nil
            nil)))
      (dom-append container rendered)
      (assert (contains? (dom-text-content container) "25") "shows 25")
      (assert (contains? (dom-text-content container) "77") "shows 77F"))))
