(defmodule lamina_admin_pages_tests
(export all)
(include-lib "eunit/include/eunit.hrl"))
(defun org-edit-renders_test ()
(let ((dir (tmpdir)))
(case (lamina_db:open (filename:join dir "test.db"))
(`#(ok ,_)
(lamina_db:q "INSERT INTO organizations(name,description,created_at) VALUES(?,?,?);"
(list "Acme" "A team" "2026-01-01T00:00:00Z"))
(lamina_db:q "INSERT INTO organizations(name,description,created_at) VALUES(?,?,?);"
(list "NigFossil" #"仓库分组" "2026-01-01T00:00:00Z"))
(let ((page (lamina_admin_pages:org-edit (req 1)))
(page2 (lamina_admin_pages:org-edit (req 2)))
(missing (lamina_admin_pages:org-edit (req 99))))
(lamina_db:close)
(file:del_dir_r dir)
(assert-page 200 "Acme" "/admin/orgs/1/edit" page)
(assert-page 200 "NigFossil" "/admin/orgs/2/edit" page2)
(assert-gap page)
(assert-light-buttons page)
(case missing
(`#(status 404 ,_ ,_) 'ok)
(other (erlang:error (list "missing org" other))))))
(other
(file:del_dir_r dir)
(erlang:error (list "database setup failed" other))))))
(defun req (id)
(maps:put 'bindings
(maps:put #"id" (integer_to_binary id) #M())
#M(headers #M())))
(defun assert-gap (result)
(case result
(`#(status 200 ,_ ,body)
(if (and (=/= 'nomatch (string:find body "class=\"org-form\""))
(=/= 'nomatch (string:find body "class=\"form-actions\""))
(=/= 'nomatch (string:find body "org-form .form-actions{margin-top:22px}")))
'ok
(erlang:error "organization form button has no gap")))
(other (erlang:error (list "org edit result" other)))))
(defun assert-light-buttons (result)
(case result
(`#(status 200 ,_ ,body)
(if (and (=/= 'nomatch (string:find body "color-scheme:light only"))
(=/= 'nomatch (string:find body ".topline a.btn,.topline-actions a.btn{color:#fff"))
(=/= 'nomatch (string:find body ".topline a.btn.secondary,.topline-actions a.btn.secondary{color:var(--ink)")))
'ok
(erlang:error "primary header buttons lost white text")))
(other (erlang:error (list "org edit result" other)))))
(defun assert-page (code needle action result)
(case result
(`#(status ,got ,_ ,body)
(if (and (=:= code got)
(is_binary body)
(=/= 'nomatch (string:find body needle))
(=/= 'nomatch (string:find body action)))
'ok
(erlang:error (list "org edit page" got needle action))))
(other (erlang:error (list "org edit result" other)))))
(defun tmpdir ()
(let* ((cwd (element 2 (file:get_cwd)))
(base (filename:join cwd "_build/tmp"))
(name (lists:flatten (io_lib:format "admin-~p" (list (os:system_time 'microsecond))))))
(filelib:ensure_dir (filename:join base "x"))
(let ((dir (filename:join base name)))
(file:make_dir dir)
dir)))