summaryrefslogtreecommitdiff
path: root/tests/test-chime-notifications.el
blob: f3aa5d790e5d9efe1db92dc1877dfe34eb3458a2 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
;;; test-chime-notifications.el --- Tests for chime--notifications -*- lexical-binding: t; -*-

;; Copyright (C) 2024 Craig Jennings

;; Author: Craig Jennings <c@cjennings.net>

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Unit tests for chime--notifications function.
;; Tests cover normal cases, boundary cases, and error cases.

;;; Code:

;; Initialize package system for batch mode
(when noninteractive
  (package-initialize))

(require 'ert)

;; Load dependencies required by chime
(require 'dash)
(require 'alert)
(require 'async)
(require 'org-agenda)

;; Load chime from parent directory
(load (expand-file-name "../chime.el") nil t)

;; Load test utilities
(require 'testutil-general (expand-file-name "testutil-general.el"))

;;; Setup and Teardown

(defun test-chime-notifications-setup ()
  "Setup function run before each test."
  (chime-create-test-base-dir))

(defun test-chime-notifications-teardown ()
  "Teardown function run after each test."
  (chime-delete-test-base-dir))

;;; Normal Cases

(ert-deftest test-chime-notifications-single-time-single-interval-returns-pair ()
  "Test that single time with single interval returns one notification pair."
  (test-chime-notifications-setup)
  (unwind-protect
      (cl-letf* ((mock-time (encode-time 0 0 14 24 10 2025))
                 ((symbol-function 'current-time) (lambda () mock-time))
                 ;; Event at 14:10 (10 minutes from now)
                 (event-time (encode-time 0 10 14 24 10 2025))
                 (event `((times . ((("<2025-10-24 Fri 14:10>" . ,event-time))))
                          (title . "Test Event")
                          (intervals . (10))))
                 (result (chime--notifications event)))
        ;; Should return list with one pair
        (should (listp result))
        (should (= 1 (length result))))
    (test-chime-notifications-teardown)))

(ert-deftest test-chime-notifications-single-time-multiple-intervals-returns-multiple-pairs ()
  "Test that single time with multiple intervals returns multiple notification pairs."
  (test-chime-notifications-setup)
  (unwind-protect
      (cl-letf* ((mock-time (encode-time 0 0 14 24 10 2025))
                 ((symbol-function 'current-time) (lambda () mock-time))
                 ;; Event at 14:10
                 (event-time (encode-time 0 10 14 24 10 2025))
                 (event `((times . ((("<2025-10-24 Fri 14:10>" . ,event-time))))
                          (title . "Test Event")
                          (intervals . (10 5))))  ; Two intervals, only 10 matches
                 (result (chime--notifications event)))
        ;; Should return only matching interval
        (should (listp result))
        (should (= 1 (length result))))
    (test-chime-notifications-teardown)))

(ert-deftest test-chime-notifications-multiple-times-single-interval-returns-matching-pairs ()
  "Test that multiple times with single interval returns matching notifications."
  (test-chime-notifications-setup)
  (unwind-protect
      (cl-letf* ((mock-time (encode-time 0 0 14 24 10 2025))
                 ((symbol-function 'current-time) (lambda () mock-time))
                 ;; Two events: one at 14:10, one at 14:05
                 (event-time-1 (encode-time 0 10 14 24 10 2025))
                 (event-time-2 (encode-time 0 5 14 24 10 2025))
                 (event `((times . ((("<2025-10-24 Fri 14:10>" . ,event-time-1)
                                     ("<2025-10-24 Fri 14:05>" . ,event-time-2))))
                          (title . "Test Event")
                          (intervals . (10))))  ; Only first time matches
                 (result (chime--notifications event)))
        ;; Should return only matching time
        (should (listp result))
        (should (= 1 (length result))))
    (test-chime-notifications-teardown)))

(ert-deftest test-chime-notifications-multiple-times-multiple-intervals-returns-all-matches ()
  "Test that multiple times and intervals return all matching combinations."
  (test-chime-notifications-setup)
  (unwind-protect
      (cl-letf* ((mock-time (encode-time 0 0 14 24 10 2025))
                 ((symbol-function 'current-time) (lambda () mock-time))
                 ;; Event at 14:10 and 14:05
                 (event-time-1 (encode-time 0 10 14 24 10 2025))
                 (event-time-2 (encode-time 0 5 14 24 10 2025))
                 (event `((times . ((("<2025-10-24 Fri 14:10>" . ,event-time-1)
                                     ("<2025-10-24 Fri 14:05>" . ,event-time-2))))
                          (title . "Test Event")
                          (intervals . (10 5))))  ; Both match (10 with first, 5 with second)
                 (result (chime--notifications event)))
        ;; Should return both matching pairs
        (should (listp result))
        (should (= 2 (length result))))
    (test-chime-notifications-teardown)))

(ert-deftest test-chime-notifications-zero-interval-returns-current-time-match ()
  "Test that zero interval (notify now) works correctly."
  (test-chime-notifications-setup)
  (unwind-protect
      (cl-letf* ((mock-time (encode-time 0 0 14 24 10 2025))
                 ((symbol-function 'current-time) (lambda () mock-time))
                 ;; Event at exactly current time
                 (event-time (encode-time 0 0 14 24 10 2025))
                 (event `((times . ((("<2025-10-24 Fri 14:00>" . ,event-time))))
                          (title . "Test Event")
                          (intervals . (0))))
                 (result (chime--notifications event)))
        ;; Should return one matching pair
        (should (listp result))
        (should (= 1 (length result))))
    (test-chime-notifications-teardown)))

(ert-deftest test-chime-notifications-filters-day-wide-events ()
  "Test that day-wide events (without time) are filtered out."
  (test-chime-notifications-setup)
  (unwind-protect
      (cl-letf* ((mock-time (encode-time 0 0 14 24 10 2025))
                 ((symbol-function 'current-time) (lambda () mock-time))
                 ;; Mix of day-wide and timed events
                 (event-time (encode-time 0 10 14 24 10 2025))
                 (event `((times . ((("<2025-10-24 Fri>" . ,event-time)  ; Day-wide
                                     ("<2025-10-24 Fri 14:10>" . ,event-time))))  ; Timed
                          (title . "Test Event")
                          (intervals . (10))))
                 (result (chime--notifications event)))
        ;; Should return only timed event
        (should (listp result))
        (should (= 1 (length result))))
    (test-chime-notifications-teardown)))

;;; Boundary Cases

(ert-deftest test-chime-notifications-empty-times-returns-empty-list ()
  "Test that event with no times returns empty list."
  (test-chime-notifications-setup)
  (unwind-protect
      (cl-letf* ((mock-time (encode-time 0 0 14 24 10 2025))
                 ((symbol-function 'current-time) (lambda () mock-time))
                 (event `((times . (()))
                          (title . "Test Event")
                          (intervals . (10))))
                 (result (chime--notifications event)))
        (should (listp result))
        (should (= 0 (length result))))
    (test-chime-notifications-teardown)))

(ert-deftest test-chime-notifications-empty-intervals-returns-empty-list ()
  "Test that event with no intervals returns empty list."
  (test-chime-notifications-setup)
  (unwind-protect
      (cl-letf* ((mock-time (encode-time 0 0 14 24 10 2025))
                 ((symbol-function 'current-time) (lambda () mock-time))
                 (event-time (encode-time 0 10 14 24 10 2025))
                 (event `((times . ((("<2025-10-24 Fri 14:10>" . ,event-time))))
                          (title . "Test Event")
                          (intervals . ())))
                 (result (chime--notifications event)))
        (should (listp result))
        (should (= 0 (length result))))
    (test-chime-notifications-teardown)))

;;; Error Cases

(ert-deftest test-chime-notifications-nil-times-returns-empty-list ()
  "Test that event with nil times returns empty list."
  (test-chime-notifications-setup)
  (unwind-protect
      (cl-letf* ((mock-time (encode-time 0 0 14 24 10 2025))
                 ((symbol-function 'current-time) (lambda () mock-time))
                 (event `((times . (nil))
                          (title . "Test Event")
                          (intervals . (10))))
                 (result (chime--notifications event)))
        (should (listp result))
        (should (= 0 (length result))))
    (test-chime-notifications-teardown)))

(ert-deftest test-chime-notifications-nil-intervals-returns-empty-list ()
  "Test that event with nil intervals returns empty list."
  (test-chime-notifications-setup)
  (unwind-protect
      (cl-letf* ((mock-time (encode-time 0 0 14 24 10 2025))
                 ((symbol-function 'current-time) (lambda () mock-time))
                 (event-time (encode-time 0 10 14 24 10 2025))
                 (event `((times . ((("<2025-10-24 Fri 14:10>" . ,event-time))))
                          (title . "Test Event")
                          (intervals . nil)))
                 (result (chime--notifications event)))
        (should (listp result))
        (should (= 0 (length result))))
    (test-chime-notifications-teardown)))

(provide 'test-chime-notifications)
;;; test-chime-notifications.el ends here