(defsuite
  "page-helpers-extract-kwargs"
  (deftest
    "extracts keyword from expression"
    (let
      ((result (extract-define-kwargs (quote (define-special-form "if" :category "Control" :arity "3")))))
      (assert-true (dict? result))
      (assert-equal "Control" (get result "category"))
      (assert-equal "3" (get result "arity"))))
  (deftest
    "no kwargs returns empty-ish dict"
    (let
      ((result (extract-define-kwargs (quote (define-special-form "begin")))))
      (assert-true (dict? result)))))

(defsuite
  "page-helpers-categorize"
  (deftest
    "groups special forms into categories"
    (let
      ((cats (categorize-special-forms (list (quote (define-special-form "if" :category "Control")) (quote (define-special-form "when" :category "Control")) (quote (define-special-form "let" :category "Binding"))))))
      (assert-true (dict? cats))
      (assert-true (> (len (keys cats)) 0))))
  (deftest
    "ignores non-special-form expressions"
    (let
      ((cats (categorize-special-forms (list (quote (define x 1)) (quote (defcomp ~c () (div)))))))
      (assert-true (dict? cats))
      (assert-equal 0 (len (keys cats)))))
  (deftest
    "empty input returns empty dict"
    (let
      ((cats (categorize-special-forms (list))))
      (assert-true (dict? cats))
      (assert-equal 0 (len (keys cats))))))

(defsuite
  "page-helpers-ref-items"
  (deftest
    "builds 2-field items"
    (let
      ((items (build-ref-items-with-href (list (list "alpha" "Description") (list "beta" "Another")) "/ref" (list) 2)))
      (assert-equal 2 (len items))
      (assert-true (dict? (first items)))
      (assert-true (has-key? (first items) "name"))
      (assert-equal "alpha" (get (first items) "name"))))
  (deftest
    "builds 3-field items"
    (let
      ((items (build-ref-items-with-href (list (list "add" "(a b)" "Adds")) "/ref" (list) 3)))
      (assert-equal 1 (len items))
      (assert-true (dict? (first items)))))
  (deftest
    "empty input returns empty"
    (assert-equal
      (list)
      (build-ref-items-with-href (list) "/ref" (list) 2))))

(defsuite
  "page-helpers-component-source"
  (deftest
    "builds defcomp source string"
    (let
      ((src (build-component-source {:has-children false :body-sx "(div title)" :params (list "&key" "title") :type "component" :name "~my-card" :affinity nil})))
      (assert-true (string? src))
      (assert-true (string-contains? src "defcomp"))
      (assert-true (string-contains? src "~my-card"))))
  (deftest
    "builds defisland source string"
    (let
      ((src (build-component-source {:has-children false :body-sx "(span count)" :params (list "&key" "count") :type "island" :name "~counter" :affinity nil})))
      (assert-true (string-contains? src "defisland"))))
  (deftest
    "not-found returns comment"
    (let
      ((src (build-component-source {:has-children false :body-sx "" :params (list) :type "not-found" :name "~missing" :affinity nil})))
      (assert-true (string-contains? src "not found")))))

(defsuite
  "page-helpers-routing-analysis"
  (deftest
    "pages list is returned"
    (let
      ((result (build-routing-analysis (list {:content-src "(div)" :name "home"}))))
      (assert-true (dict? result))
      (assert-true (has-key? result "pages"))
      (assert-equal 1 (len (get result "pages")))))
  (deftest
    "classifies page without content-src as server"
    (let
      ((result (build-routing-analysis (list {:content-src "" :name "empty"}))))
      (let
        ((page (first (get result "pages"))))
        (assert-equal "server" (get page "mode")))))
  (deftest
    "counts pages"
    (let
      ((result (build-routing-analysis (list {:content-src "(div)" :name "a"} {:content-src "(div)" :name "b"}))))
      (assert-equal 2 (len (get result "pages")))))
  (deftest
    "empty pages"
    (let
      ((result (build-routing-analysis (list))))
      (assert-true (dict? result))
      (assert-equal 0 (len (get result "pages"))))))

(defsuite
  "page-helpers-bundle-analysis"
  (deftest
    "returns analysis dict"
    (let
      ((analysis (build-bundle-analysis (list {:needed-components (list "card") :name "home"}) {:card {:io-refs (list) :is-pure true}} 10 5 7 3)))
      (assert-true (dict? analysis))
      (assert-true (has-key? analysis "total-components"))
      (assert-equal 10 (get analysis "total-components"))))
  (deftest
    "empty pages returns summary"
    (let
      ((analysis (build-bundle-analysis (list) {} 0 0 0 0)))
      (assert-true (dict? analysis)))))

(defsuite
  "page-helpers-category-map"
  (deftest
    "special-form-category-map is a dict"
    (assert-true (dict? special-form-category-map)))
  (deftest
    "maps known forms"
    (assert-true (has-key? special-form-category-map "if"))
    (assert-true (has-key? special-form-category-map "let"))
    (assert-true (has-key? special-form-category-map "define"))))
