(defmodule lamina_m6_tests
(export all)
(include-lib "eunit/include/eunit.hrl"))
(defun remote-clone-and-git-export_test ()
(let* ((dir (tmpdir))
(source (filename:join dir "source.fossil"))
(clone (filename:join dir "clone.fossil"))
(target (filename:join dir "target.git")))
(case (lamina_fossil_repo:init source "admin")
(`#(ok ,_)
(case (lamina_fossil_repo:clone (lists:flatten (list "file://" source)) clone "admin")
(`#(ok ,_)
(os:cmd (lists:flatten (list "git init --bare '" target "' >/dev/null 2>&1")))
(case (lamina_fossil_git:push clone (lists:flatten (list "file://" target)) "" "")
(`#(ok ,_)
(let ((log (os:cmd (lists:flatten (list "git --git-dir='" target "' log --oneline -1 2>/dev/null")))))
(cleanup dir)
(if (=:= "" (string:trim log))
(erlang:error "git export did not push a commit")
'ok)))
(other (cleanup dir) (erlang:error (list "git export failed" other)))))
(other (cleanup dir) (erlang:error (list "remote clone failed" other)))))
(other (cleanup dir) (erlang:error (list "source init failed" other))))))
(defun sync-task-updates-status-and-log_test ()
(let* ((dir (tmpdir))
(source (filename:join dir "source.fossil"))
(clone (filename:join dir "clone.fossil"))
(db (filename:join dir "platform.db"))
(key (crypto:hash 'sha256 #"m6-test-key")))
(lamina_auth:put-secret-key key)
(case (lamina_db:open db)
(`#(ok ,_)
(case (lamina_fossil_repo:init source "admin")
(`#(ok ,_)
(case (lamina_fossil_repo:clone (lists:flatten (list "file://" source)) clone "admin")
(`#(ok ,_)
(let ((encrypted (term_to_binary (lamina_crypto:encrypt #"pw" key))))
(lamina_db:q
"INSERT INTO repositories(name,display_name,description,source,remote_url,sync_interval_minutes,setup_username,setup_password_enc,fossil_path,created_at,updated_at) VALUES('remote','remote','test','remote',?,60,'admin',?,?,?,?);"
(list (lists:flatten (list "file://" source)) encrypted clone "now" "now"))
(let ((repo-id (lamina_db:last-rowid)))
(case (lamina_sync_task:run-sync repo-id)
('ok
(let ((rows (lamina_db:q "SELECT last_sync_status FROM repositories WHERE id=?;" (list repo-id)))
(logs (lamina_db:q "SELECT status,finished_at FROM sync_log WHERE repository_id=?;" (list repo-id))))
(lamina_db:close)
(cleanup dir)
(if (and (=:= `((#"success")) rows)
(=:= #"success" (lists:nth 1 (hd logs)))
(not (=:= 'null (lists:nth 2 (hd logs)))))
'ok
(erlang:error (list "sync status/log invalid" rows logs)))))
(other (lamina_db:close) (cleanup dir) (erlang:error (list "sync failed" other)))))))
(other (lamina_db:close) (cleanup dir) (erlang:error (list "clone failed" other)))))
(other (lamina_db:close) (cleanup dir) (erlang:error (list "init failed" other)))))
(other (cleanup dir) (erlang:error (list "db open failed" other))))))
(defun repo-settings-clear-local-remote-fields_test ()
(let* ((dir (tmpdir))
(repo (filename:join dir "local.fossil"))
(db (filename:join dir "platform.db"))
(key (crypto:hash 'sha256 #"m6-settings-key")))
(lamina_auth:put-secret-key key)
(lamina_config:put (maps:from_list (list (tuple 'repo_dir dir))))
(case (lamina_db:open db)
(`#(ok ,_)
(case (lamina_fossil_repo:init repo "admin")
(`#(ok ,_)
(let ((encrypted (term_to_binary (lamina_crypto:encrypt #"pw" key))))
(lamina_db:q
"INSERT INTO repositories(name,display_name,description,source,setup_username,setup_password_enc,fossil_path,created_at,updated_at) VALUES('local','local','old','local','admin',?,?,?,?);"
(list encrypted repo "now" "now"))
(let ((id (lamina_db:last-rowid)))
(case (lamina_repo_ctl:update-repo-settings id "local" "new" "admin" #"" "" "" "0" "" "" #"" "")
('ok
(let ((row (hd (lamina_db:q "SELECT remote_url,sync_interval_minutes,git_enabled FROM repositories WHERE id=?;" (list id)))) )
(lamina_db:close)
(cleanup dir)
(if (=:= '(undefined undefined 0) row)
'ok
(erlang:error (list "local settings were not cleared" row)))))
(other (lamina_db:close) (cleanup dir) (erlang:error (list "settings update failed" other)))))))
(other (lamina_db:close) (cleanup dir) (erlang:error (list "init failed" other)))))
(other (cleanup dir) (erlang:error (list "db open failed" other))))))
(defun push-task-updates-status-and-log_test ()
(let* ((dir (tmpdir))
(repo (filename:join dir "local.fossil"))
(target (filename:join dir "target.git"))
(db (filename:join dir "platform.db"))
(key (crypto:hash 'sha256 #"m6-push-key")))
(lamina_auth:put-secret-key key)
(case (lamina_db:open db)
(`#(ok ,_)
(case (lamina_fossil_repo:init repo "admin")
(`#(ok ,_)
(os:cmd (lists:flatten (list "git init --bare '" target "' >/dev/null 2>&1")))
(let ((encrypted (term_to_binary (lamina_crypto:encrypt #"" key))))
(lamina_db:q
"INSERT INTO repositories(name,display_name,description,source,setup_username,setup_password_enc,git_enabled,git_url,git_username,git_token_enc,fossil_path,created_at,updated_at) VALUES('push','push','test','local','admin',?,1,?,?,?, ?,?,?);"
(list encrypted (lists:flatten (list "file://" target)) "" encrypted repo "now" "now"))
(let ((repo-id (lamina_db:last-rowid)))
(case (lamina_sync_task:run-push repo-id)
('ok
(let ((row (hd (lamina_db:q "SELECT last_push_status FROM repositories WHERE id=?;" (list repo-id))))
(logs (lamina_db:q "SELECT status,finished_at FROM push_log WHERE repository_id=?;" (list repo-id))))
(lamina_db:close)
(cleanup dir)
(if (and (=:= '(#"success") row)
(=:= #"success" (lists:nth 1 (hd logs)))
(not (=:= 'null (lists:nth 2 (hd logs)))))
'ok
(erlang:error (list "push status/log invalid" row logs)))))
(other (lamina_db:close) (cleanup dir) (erlang:error (list "push failed" other)))))))
(other (lamina_db:close) (cleanup dir) (erlang:error (list "init failed" other)))))
(other (cleanup dir) (erlang:error (list "db open failed" other))))))
(defun tmpdir ()
(let* ((cwd (element 2 (file:get_cwd)))
(base (filename:join cwd "_build/tmp"))
(name (lists:flatten (io_lib:format "m6-~p" (list (os:system_time 'microsecond))))))
(filelib:ensure_dir (filename:join base "x"))
(let ((dir (filename:join base name)))
(file:make_dir dir)
dir)))
(defun cleanup (dir)
(catch (file:del_dir_r dir)))