(define random-int (fn (min max) min))

(define set-response-header (fn (k v) nil))

(when
  (not (callable? callable?))
  (define callable? (fn (x) (or (lambda? x) (component? x) (island? x)))))

(defsuite
  "ref-api:time"
  (deftest
    "returns server time"
    (reset-mocks!)
    (let
      ((result (run-handler handler:ref-time)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:greet"
  (deftest
    "greets with name"
    (reset-mocks!)
    (set! _mock-form {:name "Alice"})
    (let
      ((result (run-handler handler:ref-greet)))
      (assert-true (contains? result "Alice")))))

(defsuite
  "ref-api:status"
  (deftest
    "returns content"
    (reset-mocks!)
    (set! _mock-form {:status "active"})
    (let
      ((result (run-handler handler:ref-status)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:theme"
  (deftest
    "returns content"
    (reset-mocks!)
    (set! _mock-form {:theme "dark"})
    (let
      ((result (run-handler handler:ref-theme)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:delete-item"
  (deftest
    "returns content"
    (reset-mocks!)
    (let
      ((result (run-handler handler:ref-delete-item)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:trigger-search"
  (deftest
    "echoes query"
    (reset-mocks!)
    (set! _mock-args {:q "test"})
    (let
      ((result (run-handler handler:ref-trigger-search)))
      (assert-true (contains? result "test")))))

(defsuite
  "ref-api:swap-item"
  (deftest
    "returns content"
    (reset-mocks!)
    (let
      ((result (run-handler handler:ref-swap-item)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:oob"
  (deftest
    "returns OOB content"
    (reset-mocks!)
    (let
      ((result (run-handler handler:ref-oob)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:select-page"
  (deftest
    "returns page content"
    (reset-mocks!)
    (set! _mock-args {:page "2"})
    (let
      ((result (run-handler handler:ref-select-page)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:slow-echo"
  (deftest
    "returns after sleep"
    (reset-mocks!)
    (let
      ((result (run-handler handler:ref-slow-echo)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:prompt-echo"
  (deftest
    "returns content"
    (reset-mocks!)
    (let
      ((result (run-handler handler:ref-prompt-echo)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:upload-name"
  (deftest
    "returns content"
    (reset-mocks!)
    (set! _mock-form {:file "photo.jpg"})
    (let
      ((result (run-handler handler:ref-upload-name)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:echo-headers"
  (deftest
    "returns content"
    (reset-mocks!)
    (let
      ((result (run-handler handler:ref-echo-headers)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:echo-vals"
  (deftest
    "GET returns content"
    (reset-mocks!)
    (set! _mock-args {:foo "bar"})
    (let
      ((result (run-handler handler:ref-echo-vals-get)))
      (assert-true (string? result))))
  (deftest
    "POST returns content"
    (reset-mocks!)
    (set! _mock-form {:x "1"})
    (let
      ((result (run-handler handler:ref-echo-vals-post)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:flaky"
  (deftest
    "returns content"
    (reset-mocks!)
    (let
      ((result (run-handler handler:ref-flaky)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:trigger-event"
  (deftest
    "returns content"
    (reset-mocks!)
    (let
      ((result (run-handler handler:ref-trigger-event)))
      (assert-true (string? result)))))

(defsuite
  "ref-api:retarget"
  (deftest
    "returns content"
    (reset-mocks!)
    (let
      ((result (run-handler handler:ref-retarget)))
      (assert-true (string? result)))))

(defsuite
  "reactive-api:flash-sale"
  (deftest
    "returns price data"
    (reset-mocks!)
    (let
      ((result (run-handler handler:reactive-flash-sale)))
      (assert-true (string? result)))))

(defsuite
  "reactive-api:settle-data"
  (deftest
    "returns settle items"
    (reset-mocks!)
    (let
      ((result (run-handler handler:reactive-settle-data)))
      (assert-true (string? result)))))

(defsuite
  "reactive-api:search-products"
  (deftest
    "returns results"
    (reset-mocks!)
    (set! _mock-args {:q "phone"})
    (let
      ((result (run-handler handler:reactive-search-products)))
      (assert-true (string? result)))))

(defsuite
  "reactive-api:search-events"
  (deftest
    "returns results"
    (reset-mocks!)
    (set! _mock-args {:q "concert"})
    (let
      ((result (run-handler handler:reactive-search-events)))
      (assert-true (string? result)))))

(defsuite
  "reactive-api:search-posts"
  (deftest
    "returns results"
    (reset-mocks!)
    (set! _mock-args {:q "blog"})
    (let
      ((result (run-handler handler:reactive-search-posts)))
      (assert-true (string? result)))))

(defsuite
  "reactive-api:catalog"
  (deftest
    "returns catalog"
    (reset-mocks!)
    (let
      ((result (run-handler handler:reactive-catalog)))
      (assert-true (string? result)))))

(defsuite
  "scopes"
  (deftest
    "demo-scope-basic defined"
    (assert-true (component? ~geography/scopes/demo-scope-basic)))
  (deftest
    "demo-scope-emit defined"
    (assert-true (component? ~geography/scopes/demo-scope-emit)))
  (deftest
    "demo-scope-dedup defined"
    (assert-true (component? ~geography/scopes/demo-scope-dedup)))
  (deftest
    "scopes-demo-example defined"
    (assert-true (component? ~geography/scopes/scopes-demo-example))))

(defsuite
  "provide"
  (deftest
    "demo-provide-basic defined"
    (assert-true (component? ~geography/provide/demo-provide-basic)))
  (deftest
    "demo-emit-collect defined"
    (assert-true (component? ~geography/provide/demo-emit-collect)))
  (deftest
    "demo-nested-provide defined"
    (assert-true (component? ~geography/provide/demo-nested-provide)))
  (deftest
    "demo-spread-mechanism defined"
    (assert-true (component? ~geography/provide/demo-spread-mechanism))))

(defsuite
  "spreads"
  (deftest
    "demo-spread-basic defined"
    (assert-true (component? ~geography/spreads/demo-spread-basic)))
  (deftest
    "demo-cssx-tw defined"
    (assert-true (component? ~geography/spreads/demo-cssx-tw)))
  (deftest
    "demo-semantic-vars defined"
    (assert-true (component? ~geography/spreads/demo-semantic-vars))))

(defsuite
  "cek:islands"
  (deftest
    "demo-counter"
    (assert-true (island? ~geography/cek/demo-counter)))
  (deftest "demo-chain" (assert-true (island? ~geography/cek/demo-chain)))
  (deftest
    "demo-reactive-attr"
    (assert-true (island? ~geography/cek/demo-reactive-attr)))
  (deftest
    "demo-stopwatch"
    (assert-true (island? ~geography/cek/demo-stopwatch)))
  (deftest "demo-batch" (assert-true (island? ~geography/cek/demo-batch)))
  (deftest
    "demo-stepper"
    (assert-true (island? ~geography/cek/demo-stepper)))
  (deftest
    "demo-render-stepper"
    (assert-true (island? ~geography/cek/demo-render-stepper)))
  (deftest
    "content-address-demo"
    (assert-true (island? ~geography/cek/content-address-demo)))
  (deftest "freeze-demo" (assert-true (island? ~geography/cek/freeze-demo))))

(defsuite
  "cek:content-pages"
  (deftest "cek-index" (assert-true (component? ~geography/cek)))
  (deftest "cek-content" (assert-true (component? ~geography/cek/content)))
  (deftest "cek-demo" (assert-true (component? ~geography/cek/demo)))
  (deftest "cek-freeze" (assert-true (component? ~geography/cek/freeze))))

(defsuite
  "reactive:islands"
  (deftest
    "counter"
    (assert-true (island? ~geography/reactive/demo-counter)))
  (deftest
    "temperature"
    (assert-true (island? ~geography/reactive/demo-temperature)))
  (deftest
    "stopwatch"
    (assert-true (island? ~geography/reactive/demo-stopwatch)))
  (deftest
    "reactive-list"
    (assert-true (island? ~geography/reactive/demo-reactive-list)))
  (deftest
    "input-binding"
    (assert-true (island? ~geography/reactive/demo-input-binding)))
  (deftest
    "error-boundary"
    (assert-true (island? ~geography/reactive/demo-error-boundary)))
  (deftest
    "dynamic-class"
    (assert-true (island? ~geography/reactive/demo-dynamic-class)))
  (deftest
    "store-writer"
    (assert-true (island? ~geography/reactive/demo-store-writer)))
  (deftest
    "store-reader"
    (assert-true (island? ~geography/reactive/demo-store-reader))))

(defsuite
  "marshes:islands"
  (deftest
    "demo-marsh-product"
    (assert-true (island? ~geography/marshes/demo-marsh-product)))
  (deftest
    "demo-marsh-settle"
    (assert-true (island? ~geography/marshes/demo-marsh-settle))))

(defsuite
  "reactive-runtime:islands"
  (deftest
    "demo-ref"
    (assert-true (island? ~geography/reactive-runtime/demo-ref)))
  (deftest
    "demo-foreign"
    (assert-true (island? ~geography/reactive-runtime/demo-foreign)))
  (deftest
    "demo-machine"
    (assert-true (island? ~geography/reactive-runtime/demo-machine)))
  (deftest
    "demo-commands"
    (assert-true (island? ~geography/reactive-runtime/demo-commands)))
  (deftest
    "demo-loop"
    (assert-true (island? ~geography/reactive-runtime/demo-loop)))
  (deftest
    "demo-keyed-lists"
    (assert-true (island? ~geography/reactive-runtime/demo-keyed-lists)))
  (deftest
    "demo-app-shell"
    (assert-true (island? ~geography/reactive-runtime/demo-app-shell))))
