(ns auto-ap.ssr.selmer-test
(:require
[auto-ap.ssr.selmer :as sut]
[clojure.string :as str]
[clojure.test :refer [deftest is testing]]
[hiccup2.core :as h2]))
(deftest hiccup->html
(testing "renders a Hiccup form to an HTML string"
(is (= "A & B"
(sut/hiccup->html [:span.label "A & B"])))))
(deftest selmer-embeds-hiccup
(testing "a Hiccup component renders inside a Selmer template via |safe"
(let [frag (sut/hiccup->html [:span.badge "from hiccup"])
out (sut/render-str "
{{frag|safe}}
" {:frag frag})]
(is (str/includes? out "from hiccup"))
;; without |safe the markup would be escaped; |safe keeps it verbatim
(is (not (str/includes? out "<span"))))))
(deftest selmer-fragment-inside-hiccup
(testing "a Selmer fragment renders inside a Hiccup tree without double-escaping"
(let [sel (sut/render-str "{{label}}" {:url "/x" :label "Go"})
out (str (h2/html {} [:div (sut/raw sel)]))]
(is (= "" out)))))
(deftest render-file-from-classpath
(testing "render-file resolves a template under resources/templates and keeps plain-HTML Alpine/HTMX attrs"
(let [out (sut/render "templates/interop-smoke.html"
{:title "Interop OK"
:hiccup_frag (sut/hiccup->html [:span.badge "from hiccup"])})]
(is (str/includes? out "Interop OK"))
(is (str/includes? out "from hiccup"))
;; plain-HTML attributes (the whole point of Selmer) survive unambiguously
(is (str/includes? out "x-model=\"value.value\""))
(is (str/includes? out "tippy?.show()")))))