(defsuite
  "request-primitives"
  (deftest
    "now returns a string"
    (let
      ((result (now)))
      (assert-type "string" result)
      (assert-true (> (len result) 5))))
  (deftest
    "now with format returns time"
    (let
      ((result (now "%H:%M:%S")))
      (assert-type "string" result)
      (assert-true (contains? result ":"))))
  (deftest
    "state-get returns default for missing key"
    (assert-equal 42 (state-get "test-nonexistent-key-xyz" 42)))
  (deftest
    "state-set! and state-get round-trip"
    (state-set! "test-rt-abc" "hello")
    (assert-equal "hello" (state-get "test-rt-abc" nil))
    (state-clear! "test-rt-abc"))
  (deftest
    "state-set! overwrites previous value"
    (state-set! "test-ow-abc" "first")
    (state-set! "test-ow-abc" "second")
    (assert-equal "second" (state-get "test-ow-abc" nil))
    (state-clear! "test-ow-abc"))
  (deftest
    "state-clear! removes key"
    (state-set! "test-cl-abc" "value")
    (state-clear! "test-cl-abc")
    (assert-nil (state-get "test-cl-abc" nil)))
  (deftest
    "request-method returns string"
    (assert-type "string" (request-method)))
  (deftest
    "request-arg returns default for missing param"
    (assert-nil (request-arg "nonexistent-param-xyz")))
  (deftest
    "request-form returns default for empty body"
    (assert-equal "" (request-form "nonexistent-field-xyz")))
  (deftest
    "request-form with custom default"
    (assert-equal "fallback" (request-form "missing-xyz" "fallback")))
  (deftest
    "into list converts dict to pairs"
    (let
      ((d {:b 2 :a 1}) (result (into "list" d)))
      (assert-type "list" result)
      (assert-equal 2 (len result))))
  (deftest
    "into dict converts pairs to dict"
    (let
      ((pairs (list (list "x" 1) (list "y" 2)))
        (result (into "dict" pairs)))
      (assert-type "dict" result)
      (assert-equal 1 (get result "x"))
      (assert-equal 2 (get result "y"))))
  (deftest
    "into round-trips dict→list→dict"
    (let
      ((original {:age 36 :name "Ada"})
        (as-list (into "list" original))
        (back (into "dict" as-list)))
      (assert-equal "Ada" (get back "name"))
      (assert-equal 36 (get back "age"))))
  (deftest
    "request-headers-all returns dict"
    (assert-type "dict" (request-headers-all)))
  (deftest
    "request-form-all returns dict"
    (assert-type "dict" (request-form-all)))
  (deftest
    "request-args-all returns dict"
    (assert-type "dict" (request-args-all)))
  (deftest
    "request-content-type returns string"
    (assert-type "string" (request-content-type))))
