aboutsummaryrefslogtreecommitdiff
path: root/tests/test-chime-day-wide-time-matching.el
blob: 88242e261d77c92c538fcddeb898976a4f4408c0 (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
;;; test-chime-day-wide-time-matching.el --- Tests for day-wide time matching -*- lexical-binding: t; -*-

;; Copyright (C) 2026 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 day-wide time matching functions:
;; - chime--current-time-matches-time-of-day-string
;; - chime--current-time-is-day-wide-time
;;
;; These functions determine when to fire notifications for all-day events
;; by comparing the current time to configured alert times like "08:00".
;;
;; IMPORTANT: Mock times must be computed BEFORE entering with-test-time,
;; because test-time-today-at calls current-time internally.

;;; Code:

(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))

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

(defmacro test-chime-with-restored-day-wide-alert-times (&rest body)
  "Run BODY and restore default `chime-day-wide-alert-times' afterwards."
  (declare (indent 0) (debug t))
  `(let ((original-value (default-value 'chime-day-wide-alert-times)))
     (unwind-protect
         (progn ,@body)
       (set-default 'chime-day-wide-alert-times original-value))))

;;;; Tests for chime--validate-day-wide-alert-times

(ert-deftest test-chime-validate-day-wide-alert-times-accepts-24-hour ()
  "Normal: 24-hour HH:MM entries are valid."
  (should (equal '("08:00" "17:00")
                 (chime--validate-day-wide-alert-times
                  'fake-symbol '("08:00" "17:00")))))

(ert-deftest test-chime-validate-day-wide-alert-times-accepts-12-hour ()
  "Normal: Org-supported 12-hour entries are valid."
  (should (equal '("8:00am" "5:30pm")
                 (chime--validate-day-wide-alert-times
                  'fake-symbol '("8:00am" "5:30pm")))))

(ert-deftest test-chime-validate-day-wide-alert-times-accepts-nil ()
  "Boundary: nil disables day-wide alerts."
  (should (null (chime--validate-day-wide-alert-times 'fake-symbol nil))))

(ert-deftest test-chime-validate-day-wide-alert-times-accepts-empty-list ()
  "Boundary: an empty list disables day-wide alerts."
  (should (equal '()
                 (chime--validate-day-wide-alert-times 'fake-symbol '()))))

(ert-deftest test-chime-validate-day-wide-alert-times-rejects-invalid-string ()
  "Error: unparseable strings fail before timer matching."
  (should-error (chime--validate-day-wide-alert-times
                 'fake-symbol '("08:00" "not-a-time"))
                :type 'user-error))

(ert-deftest test-chime-validate-day-wide-alert-times-rejects-non-list ()
  "Error: the value must be nil or a list."
  (should-error (chime--validate-day-wide-alert-times
                 'fake-symbol "08:00")
                :type 'user-error))

(ert-deftest test-chime-validate-day-wide-alert-times-rejects-non-string-entry ()
  "Error: every configured alert time must be a string."
  (should-error (chime--validate-day-wide-alert-times
                 'fake-symbol '("08:00" 1700))
                :type 'user-error))

(ert-deftest test-chime-validate-day-wide-alert-times-rejects-out-of-day-time ()
  "Error: Org durations beyond 23:59 are not valid clock times."
  (should-error (chime--validate-day-wide-alert-times
                 'fake-symbol '("25:00"))
                :type 'user-error))

(ert-deftest test-chime-day-wide-alert-times-setter-accepts-valid-list ()
  "Normal: customize-time setter accepts valid alert times."
  (test-chime-with-restored-day-wide-alert-times
    (customize-set-variable 'chime-day-wide-alert-times '("08:00" "5:30pm"))
    (should (equal '("08:00" "5:30pm") chime-day-wide-alert-times))))

(ert-deftest test-chime-day-wide-alert-times-setter-accepts-nil ()
  "Boundary: customize-time setter accepts nil."
  (test-chime-with-restored-day-wide-alert-times
    (customize-set-variable 'chime-day-wide-alert-times nil)
    (should (null chime-day-wide-alert-times))))

(ert-deftest test-chime-day-wide-alert-times-setter-rejects-invalid-list ()
  "Error: customize-time setter rejects invalid alert times."
  (test-chime-with-restored-day-wide-alert-times
    (should-error (customize-set-variable
                   'chime-day-wide-alert-times '("08:00" "nope"))
                  :type 'user-error)))

;;;; Tests for chime--current-time-matches-time-of-day-string

;;; Normal Cases

(ert-deftest test-chime-time-matches-string-exact-match ()
  "Should return truthy when current time matches the time-of-day string."
  (let ((mock-time (test-time-today-at 8 0)))
    (with-test-time mock-time
      (should (chime--current-time-matches-time-of-day-string "8:00")))))

(ert-deftest test-chime-time-matches-string-no-match ()
  "Should return nil when current time does not match."
  (let ((mock-time (test-time-today-at 9 0)))
    (with-test-time mock-time
      (should-not (chime--current-time-matches-time-of-day-string "8:00")))))

(ert-deftest test-chime-time-matches-string-afternoon ()
  "Should match afternoon times correctly."
  (let ((mock-time (test-time-today-at 17 0)))
    (with-test-time mock-time
      (should (chime--current-time-matches-time-of-day-string "17:00")))))

;;; Boundary Cases

(ert-deftest test-chime-time-matches-string-midnight ()
  "Should match midnight (00:00)."
  (let ((mock-time (test-time-today-at 0 0)))
    (with-test-time mock-time
      (should (chime--current-time-matches-time-of-day-string "0:00")))))

(ert-deftest test-chime-time-matches-string-end-of-day ()
  "Should match 23:59."
  (let ((mock-time (test-time-today-at 23 59)))
    (with-test-time mock-time
      (should (chime--current-time-matches-time-of-day-string "23:59")))))

(ert-deftest test-chime-time-matches-string-off-by-one-minute ()
  "One minute off should not match."
  (let ((mock-time (test-time-today-at 8 1)))
    (with-test-time mock-time
      (should-not (chime--current-time-matches-time-of-day-string "8:00")))))

(ert-deftest test-chime-time-matches-string-leading-zero ()
  "Should match with leading zero in time string (08:00)."
  (let ((mock-time (test-time-today-at 8 0)))
    (with-test-time mock-time
      (should (chime--current-time-matches-time-of-day-string "08:00")))))

;;;; Tests for chime--current-time-is-day-wide-time

;;; Normal Cases

(ert-deftest test-chime-is-day-wide-time-matches-single-entry ()
  "Should return truthy when current time matches the configured alert time."
  (let ((mock-time (test-time-today-at 8 0)))
    (with-test-time mock-time
      (let ((chime-day-wide-alert-times '("08:00")))
        (should (chime--current-time-is-day-wide-time))))))

(ert-deftest test-chime-is-day-wide-time-matches-second-entry ()
  "Should return truthy when current time matches any entry, not just first."
  (let ((mock-time (test-time-today-at 17 0)))
    (with-test-time mock-time
      (let ((chime-day-wide-alert-times '("08:00" "17:00")))
        (should (chime--current-time-is-day-wide-time))))))

(ert-deftest test-chime-is-day-wide-time-no-match ()
  "Should return nil when current time matches no configured alert times."
  (let ((mock-time (test-time-today-at 12 0)))
    (with-test-time mock-time
      (let ((chime-day-wide-alert-times '("08:00" "17:00")))
        (should-not (chime--current-time-is-day-wide-time))))))

;;; Boundary Cases

(ert-deftest test-chime-is-day-wide-time-empty-list ()
  "Should return nil when alert times list is empty."
  (let ((mock-time (test-time-today-at 8 0)))
    (with-test-time mock-time
      (let ((chime-day-wide-alert-times '()))
        (should-not (chime--current-time-is-day-wide-time))))))

(ert-deftest test-chime-is-day-wide-time-nil-list ()
  "Should return nil when alert times list is nil."
  (let ((mock-time (test-time-today-at 8 0)))
    (with-test-time mock-time
      (let ((chime-day-wide-alert-times nil))
        (should-not (chime--current-time-is-day-wide-time))))))

(ert-deftest test-chime-is-day-wide-time-matches-first-of-many ()
  "Should return truthy when matching the first of several alert times."
  (let ((mock-time (test-time-today-at 8 0)))
    (with-test-time mock-time
      (let ((chime-day-wide-alert-times '("08:00" "12:00" "17:00")))
        (should (chime--current-time-is-day-wide-time))))))

(provide 'test-chime-day-wide-time-matching)
;;; test-chime-day-wide-time-matching.el ends here