aboutsummaryrefslogtreecommitdiff
path: root/tests/test-mail-config-transport.el
blob: 2244b6dd2ad152de644737340636dd4338e26c6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
;;; test-mail-config-transport.el --- Tests for mail transport setup -*- lexical-binding: t; -*-

;;; Commentary:
;; Verifies mail transport debug logging is opt-in and external mail
;; executables are validated before command variables are assigned.

;;; Code:

(require 'ert)
(require 'cl-lib)

(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'mail-config)

(defmacro test-mail-config--with-executables (executables &rest body)
  "Run BODY with `executable-find' returning paths from EXECUTABLES.
EXECUTABLES is an alist of program name strings to executable paths."
  (declare (indent 1))
  `(let (test-mail-config--warnings)
     (cl-letf (((symbol-function 'executable-find)
                (lambda (program)
                  (cdr (assoc program ,executables))))
               ((symbol-function 'display-warning)
                (lambda (type message &rest _args)
                  (push (cons type message) test-mail-config--warnings))))
       ,@body)))

(ert-deftest test-mail-config-transport-smtpmail-debug-defaults-off ()
  "SMTP transport debug logging should default to disabled."
  (test-mail-config--with-executables '(("msmtp" . "/usr/bin/msmtp"))
    (let ((cj/smtpmail-debug-enabled nil)
          (smtpmail-debug-info :before-load))
      (cj/mail-configure-smtpmail)
      (should-not smtpmail-debug-info))))

(ert-deftest test-mail-config-transport-smtpmail-debug-toggle ()
  "Troubleshooting command should update both SMTP debug variables."
  (let ((cj/smtpmail-debug-enabled nil)
        (smtpmail-debug-info nil))
    (cl-letf (((symbol-function 'message) (lambda (&rest _args) nil)))
      (cj/set-smtpmail-debug t)
      (should cj/smtpmail-debug-enabled)
      (should smtpmail-debug-info)
      (cj/set-smtpmail-debug nil)
      (should-not cj/smtpmail-debug-enabled)
      (should-not smtpmail-debug-info))))

(ert-deftest test-mail-config-transport-msmtp-present-configures-sendmail ()
  "When msmtp exists, sendmail transport variables should be configured."
  (test-mail-config--with-executables '(("msmtp" . "/usr/bin/msmtp"))
    (let (sendmail-program send-mail-function message-send-mail-function
                           message-sendmail-envelope-from)
      (cj/mail-configure-smtpmail)
      (should (equal sendmail-program "/usr/bin/msmtp"))
      (should (eq send-mail-function 'message-send-mail-with-sendmail))
      (should (eq message-send-mail-function 'message-send-mail-with-sendmail))
      (should (eq message-sendmail-envelope-from 'header))
      (should-not test-mail-config--warnings))))

(ert-deftest test-mail-config-transport-msmtp-missing-warns-and-disables-program ()
  "When msmtp is missing, sendmail-program should be nil and a warning emitted."
  (test-mail-config--with-executables nil
    (let ((sendmail-program "/old/msmtp"))
      (cj/mail-configure-smtpmail)
      (should-not sendmail-program)
      (should (equal test-mail-config--warnings
                     '((mail-config . "msmtp not found; SMTP mail sending unavailable")))))))

(ert-deftest test-mail-config-transport-mbsync-present-builds-command ()
  "When mbsync exists, build the mu4e sync command."
  (test-mail-config--with-executables '(("mbsync" . "/usr/bin/mbsync"))
    (should (equal (cj/mail--mbsync-command) "/usr/bin/mbsync -a"))
    (should-not test-mail-config--warnings)))

(ert-deftest test-mail-config-transport-mbsync-path-with-spaces-is-quoted ()
  "The mu4e sync command should quote unusual executable paths."
  (test-mail-config--with-executables '(("mbsync" . "/opt/mail tools/mbsync"))
    (should (equal (cj/mail--mbsync-command) "/opt/mail\\ tools/mbsync -a"))))

(ert-deftest test-mail-config-transport-mbsync-missing-returns-nil-and-warns ()
  "When mbsync is missing, no unusable sync command should be assigned."
  (test-mail-config--with-executables nil
    (should-not (cj/mail--mbsync-command))
    (should (equal test-mail-config--warnings
                   '((mail-config . "mbsync not found; mu4e mail synchronization unavailable"))))))

(provide 'test-mail-config-transport)
;;; test-mail-config-transport.el ends here