;;; noise-switcher-test.el --- Tests for noise-switcher -*- lexical-binding: t; -*- (require 'ert) (require 'noise-switcher) (defun noise-switcher-test--setup () "Set up test data in the database." (require 'noise-db) (let ((noise-db-file (expand-file-name "noise-switcher-test.db" temporary-file-directory))) (when (file-exists-p noise-db-file) (delete-file noise-db-file)) (noise-db-init) ;; Insert test contacts (noise-db-upsert-contact "+1" :name "Sarah") (noise-db-upsert-contact "+2" :name "Samir Darwish") (noise-db-upsert-contact "+3" :name "Bob Smith") ;; Insert a conversation for Sarah (noise-db-upsert-conversation "conv-sarah" :type "direct" :name "Sarah" :members '("+1" "+2") :unread-count 1 :last-message-time (float-time)))) (defun noise-switcher-test--teardown () "Clean up test database." (require 'noise-db) (noise-db--close) (let ((noise-db-file (expand-file-name "noise-switcher-test.db" temporary-file-directory))) (when (file-exists-p noise-db-file) (delete-file noise-db-file)))) (ert-deftest noise-switcher--collection-includes-conversations () "Test that the switcher collection includes existing conversations." (unwind-protect (progn (noise-switcher-test--setup) (let ((coll (noise-switcher--collection))) (should coll) ;; Should include the conversation for Sarah (should (cl-find "Sarah" coll :test #'string= :key (lambda (c) (car c)))))) (noise-switcher-test--teardown))) (ert-deftest noise-switcher--collection-includes-contacts () "Test that contacts without conversations are included." (unwind-protect (progn (noise-switcher-test--setup) (let ((coll (noise-switcher--collection))) ;; Should include Bob Smith (contact without conversation) (should (cl-find "Bob Smith" coll :test #'string= :key (lambda (c) (car c)))))) (noise-switcher-test--teardown))) (ert-deftest noise-switcher--filter-matches-substring () "Test that the filter matches on substring." (let ((result (noise-switcher--filter "Sa" '("Sarah" "Samir Darwish" "Bob Smith")))) (should (= 2 (length result))) (should (member "Sarah" result)) (should (member "Samir Darwish" result)))) (ert-deftest noise-switcher--format-candidate-with-unread () "Test candidate formatting with unread count." (let* ((candidate `(("name" . "Sarah") ("type" . "direct") ("unread_count" . 3) ("last_message_time" . ,(float-time)))) (result (noise-switcher--format-candidate "Sarah" candidate))) (should (string-match "Sarah" result)) (should (string-match "direct" result)) (should (string-match "3" result)))) (ert-deftest noise-switcher--format-candidate-no-unread () "Test candidate formatting without unread count." (let* ((candidate `(("name" . "Bob Smith") ("type" . "direct") ("unread_count" . 0) ("last_message_time" . ,(float-time)))) (result (noise-switcher--format-candidate "Bob Smith" candidate))) (should (string-match "Bob Smith" result)) (should (string-match "direct" result))))