;;; noise-db-test.el --- Tests for noise-db -*- lexical-binding: t; -*- (require 'ert) (require 'noise-db) (defvar noise-db-test-file (expand-file-name "noise-test.db" temporary-file-directory)) (defun noise-db-test--setup () "Set up a fresh test database." (when (file-exists-p noise-db-test-file) (delete-file noise-db-test-file)) (let ((noise-db-file noise-db-test-file)) (noise-db-init))) (defun noise-db-test--teardown () "Clean up the test database." (noise-db--close) (when (file-exists-p noise-db-test-file) (delete-file noise-db-test-file))) (ert-deftest noise-db-init-creates-file () "Test that noise-db-init creates the database file." (noise-db-test--setup) (unwind-protect (progn (should (file-exists-p noise-db-test-file)) (should noise-db--connection)) (noise-db-test--teardown))) (ert-deftest noise-db-tables-exist () "Test that all expected tables are created." (noise-db-test--setup) (unwind-protect (progn (dolist (table '("contacts" "conversations" "messages" "conversation_members")) (let ((sql (format "SELECT name FROM sqlite_master WHERE type='table' AND name='%s'" table))) (should (noise-db--query sql))))) (noise-db-test--teardown))) (ert-deftest noise-db-upsert-and-get-contact () "Test inserting and retrieving a contact." (noise-db-test--setup) (unwind-protect (progn (noise-db-upsert-contact "+1234567890" :name "Alice" :color "blue") (let ((contact (noise-db-get-contact "+1234567890"))) (should contact) (should (string= "Alice" (gethash "name" contact))) (should (string= "blue" (gethash "color" contact))))) (noise-db-test--teardown))) (ert-deftest noise-db-upsert-contact-update () "Test that upserting an existing contact updates rather than duplicates." (noise-db-test--setup) (unwind-protect (progn (noise-db-upsert-contact "+1234567890" :name "Alice") (noise-db-upsert-contact "+1234567890" :name "Alice Johnson") (let ((contact (noise-db-get-contact "+1234567890"))) (should (string= "Alice Johnson" (gethash "name" contact))))) (noise-db-test--teardown))) (ert-deftest noise-db-get-all-contacts () "Test fetching all contacts." (noise-db-test--setup) (unwind-protect (progn (noise-db-upsert-contact "+1" :name "Alice") (noise-db-upsert-contact "+2" :name "Bob") (let ((contacts (noise-db-get-all-contacts))) (should (= 2 (length contacts))))) (noise-db-test--teardown))) (ert-deftest noise-db-upsert-and-get-conversation () "Test inserting and retrieving a conversation." (noise-db-test--setup) (unwind-protect (progn (noise-db-upsert-contact "+1" :name "Alice") (noise-db-upsert-contact "+2" :name "Bob") (noise-db-upsert-conversation "group-abc" :type "group" :name "Team Chat" :members '("+1" "+2")) (let ((conv (noise-db-get-conversation "group-abc"))) (should conv) (should (string= "Team Chat" (gethash "name" conv))) (should (string= "group" (gethash "type" conv))))) (noise-db-test--teardown))) (ert-deftest noise-db-get-all-conversations-ordered () "Test fetching all conversations ordered by last message time." (noise-db-test--setup) (unwind-protect (progn (noise-db-upsert-contact "+1" :name "Alice") (noise-db-upsert-contact "+2" :name "Bob") (noise-db-upsert-conversation "conv-1" :type "direct" :name "Alice" :members '("+1" "+2") :last-message-time 1000) (noise-db-upsert-conversation "conv-2" :type "group" :name "Team" :members '("+1" "+2") :last-message-time 2000) (let ((convs (noise-db-get-all-conversations))) (should (= 2 (length convs))) (should (string= "conv-2" (gethash "id" (car convs)))))) (noise-db-test--teardown))) (ert-deftest noise-db-insert-message () "Test inserting a message and updating conversation preview." (noise-db-test--setup) (unwind-protect (progn (noise-db-upsert-contact "+1" :name "Alice") (noise-db-upsert-conversation "conv-1" :type "direct" :name "Alice" :members '("+1" "+2")) (noise-db-insert-message "conv-1" :timestamp 1234567890 :source "+1" :body "Hello!") (let ((msgs (noise-db-get-messages "conv-1" :limit 1))) (should (= 1 (length msgs))) (should (string= "Hello!" (gethash "body" (car msgs)))))) (noise-db-test--teardown))) (ert-deftest noise-db-search-contacts () "Test searching contacts by name." (noise-db-test--setup) (unwind-protect (progn (noise-db-upsert-contact "+111" :name "Sarah Connor") (noise-db-upsert-contact "+222" :name "Samir Darwish") (noise-db-upsert-contact "+333" :name "Bob Smith") (let ((results (noise-db-search-contacts "Sa"))) (should (= 2 (length results))))) (noise-db-test--teardown)))