blob: 1cc6266e1286f5f0605e1f569ceb85322bd89af5 (
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
;;; test-chime-notification-text.el --- Tests for chime--notification-text -*- lexical-binding: t; -*-
;; Copyright (C) 2024-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 chime--notification-text 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"))
(require 'testutil-time (expand-file-name "testutil-time.el"))
;;; Setup and Teardown
(defun test-chime-notification-text-setup ()
"Setup function run before each test."
(chime-create-test-base-dir)
;; Reset display format to default
(setq chime-display-time-format-string "%I:%M %p")
;; Reset notification text format to default
(setq chime-notification-text-format "%t at %T (%u)")
;; Reset time-left formats to defaults
(setq chime-time-left-format-at-event "right now")
(setq chime-time-left-format-short "in %M")
(setq chime-time-left-format-long "in %H %M")
;; Reset title truncation to default (no truncation)
(setq chime-max-title-length nil))
(defun test-chime-notification-text-teardown ()
"Teardown function run after each test."
(chime-delete-test-base-dir))
;;; Normal Cases
(ert-deftest test-chime-notification-text-standard-event-formats-correctly ()
"Test that standard event formats correctly.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Team Meeting")))
(result (chime--notification-text str-interval event)))
;; Should format: "Team Meeting at 02:30 PM (in X minutes)"
(should (stringp result))
(should (string-match-p "Team Meeting" result))
(should (string-match-p "02:30 PM" result))
(should (string-match-p "in 10 minutes" result)))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-morning-time-formats-with-am ()
"Test that morning time uses AM.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 9 15))
(str-interval (cons (test-timestamp-string time) '(5 . medium)))
(event '((title . "Standup")))
(result (chime--notification-text str-interval event)))
(should (string-match-p "Standup" result))
(should (string-match-p "09:15 AM" result))
(should (string-match-p "in 5 minutes" result)))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-midnight-formats-correctly ()
"Test that midnight time formats correctly.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 0 0))
(str-interval (cons (test-timestamp-string time) '(30 . medium)))
(event '((title . "Midnight Event")))
(result (chime--notification-text str-interval event)))
(should (string-match-p "Midnight Event" result))
(should (string-match-p "12:00 AM" result))
(should (string-match-p "in 30 minutes" result)))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-noon-formats-correctly ()
"Test that noon time formats correctly.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 12 0))
(str-interval (cons (test-timestamp-string time) '(15 . medium)))
(event '((title . "Lunch")))
(result (chime--notification-text str-interval event)))
(should (string-match-p "Lunch" result))
(should (string-match-p "12:00 PM" result))
(should (string-match-p "in 15 minutes" result)))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-zero-minutes-shows-right-now ()
"Test that zero minutes shows 'right now'.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 0))
(str-interval (cons (test-timestamp-string time) '(0 . high)))
(event '((title . "Current Event")))
(result (chime--notification-text str-interval event)))
(should (string-match-p "Current Event" result))
(should (string-match-p "02:00 PM" result))
(should (string-match-p "right now" result)))
(test-chime-notification-text-teardown)))
;;; Boundary Cases
(ert-deftest test-chime-notification-text-very-long-title-included ()
"Test that very long titles are included in full.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 15 45))
(str-interval (cons (test-timestamp-string time) '(20 . medium)))
(long-title "This is a very long event title that contains many words and might wrap in the notification display")
(event `((title . ,long-title)))
(result (chime--notification-text str-interval event)))
;; Should include the full title
(should (string-match-p long-title result))
(should (string-match-p "03:45 PM" result))
(should (string-match-p "in 20 minutes" result)))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-title-with-special-characters ()
"Test that titles with special characters work correctly.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 16 30))
(str-interval (cons (test-timestamp-string time) '(5 . medium)))
(event '((title . "Review: Alice's PR #123 (urgent!)")))
(result (chime--notification-text str-interval event)))
(should (string-match-p "Review: Alice's PR #123 (urgent!)" result))
(should (string-match-p "04:30 PM" result)))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-custom-time-format ()
"Test that custom time format string is respected.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Meeting")))
(chime-display-time-format-string "%H:%M") ; 24-hour format
(result (chime--notification-text str-interval event)))
;; Should use 24-hour format
(should (string-match-p "Meeting" result))
(should (string-match-p "14:30" result))
(should-not (string-match-p "PM" result)))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-large-interval-shows-hours ()
"Test that large intervals show hours.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 18 0))
(str-interval (cons (test-timestamp-string time) '(120 . low))) ; 2 hours
(event '((title . "Evening Event")))
(result (chime--notification-text str-interval event)))
(should (string-match-p "Evening Event" result))
(should (string-match-p "06:00 PM" result))
;; Should show hours format
(should (string-match-p "in 2 hours" result)))
(test-chime-notification-text-teardown)))
;;; Error Cases
(ert-deftest test-chime-notification-text-empty-title-shows-empty ()
"Test that empty title still generates output.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "")))
(result (chime--notification-text str-interval event)))
;; Should still format, even with empty title
(should (stringp result))
(should (string-match-p "02:30 PM" result))
(should (string-match-p "in 10 minutes" result)))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-missing-title-shows-nil ()
"Test that missing title shows nil in output.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '()) ; No title
(result (chime--notification-text str-interval event)))
;; Should still generate output with nil title
(should (stringp result))
(should (string-match-p "02:30 PM" result))
(should (string-match-p "in 10 minutes" result)))
(test-chime-notification-text-teardown)))
;;; Custom Format Cases
(ert-deftest test-chime-notification-text-custom-title-only ()
"Test custom format showing title only.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Team Meeting")))
(chime-notification-text-format "%t"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-equal "Team Meeting" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-custom-title-and-time-no-countdown ()
"Test custom format with title and time, no countdown.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Team Meeting")))
(chime-notification-text-format "%t at %T"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-equal "Team Meeting at 02:30 PM" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-custom-title-and-countdown-no-time ()
"Test custom format with title and countdown, no time.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Team Meeting")))
(chime-notification-text-format "%t (%u)"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-equal "Team Meeting (in 10 minutes)" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-custom-separator ()
"Test custom format with custom separator.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Team Meeting")))
(chime-notification-text-format "%t - %T"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-equal "Team Meeting - 02:30 PM" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-custom-order-time-first ()
"Test custom format with time before title.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Team Meeting")))
(chime-notification-text-format "%T: %t"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-equal "02:30 PM: Team Meeting" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-custom-compact-format ()
"Test custom compact format.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Meeting")))
(chime-notification-text-format "%t@%T"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-equal "Meeting@02:30 PM" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-custom-with-compact-time-left ()
"Test custom format with compact time-left format.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Meeting")))
(chime-notification-text-format "%t (%u)")
(chime-time-left-format-short "in %mm"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-equal "Meeting (in 10m)" result))))
(test-chime-notification-text-teardown)))
;;; Time Format Cases
(ert-deftest test-chime-notification-text-24-hour-time-format ()
"Test 24-hour time format (14:30).
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Meeting")))
(chime-display-time-format-string "%H:%M"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "14:30" result))
(should-not (string-match-p "PM" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-12-hour-no-space-before-ampm ()
"Test 12-hour format without space before AM/PM (02:30PM).
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Meeting")))
(chime-display-time-format-string "%I:%M%p"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "02:30PM" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-lowercase-ampm ()
"Test 12-hour format with lowercase am/pm (02:30 pm).
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Meeting")))
(chime-display-time-format-string "%I:%M %P"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "02:30 pm" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-24-hour-morning ()
"Test 24-hour format for morning time (09:15).
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 9 15))
(str-interval (cons (test-timestamp-string time) '(5 . medium)))
(event '((title . "Standup")))
(chime-display-time-format-string "%H:%M"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "09:15" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-24-hour-midnight ()
"Test 24-hour format for midnight (00:00).
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 0 0))
(str-interval (cons (test-timestamp-string time) '(30 . medium)))
(event '((title . "Midnight")))
(chime-display-time-format-string "%H:%M"))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "00:00" result))))
(test-chime-notification-text-teardown)))
;;; Title Truncation Cases
(ert-deftest test-chime-notification-text-truncate-nil-no-truncation ()
"Test that nil chime-max-title-length shows full title.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Very Long Meeting Title That Goes On And On")))
(chime-max-title-length nil))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "Very Long Meeting Title That Goes On And On" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-truncate-25-chars ()
"Test truncation to 25 characters.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Very Long Meeting Title That Goes On")))
(chime-max-title-length 25))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "Very Long Meeting Titl\\.\\.\\." result))
(should-not (string-match-p "That Goes On" result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-truncate-15-chars ()
"Test truncation to 15 characters.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Very Long Meeting Title")))
(chime-max-title-length 15))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "Very Long Me\\.\\.\\." result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-truncate-10-chars ()
"Test truncation to 10 characters.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Very Long Title")))
(chime-max-title-length 10))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "Very Lo\\.\\.\\." result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-truncate-short-title-unchanged ()
"Test that short titles are not truncated.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Short")))
(chime-max-title-length 25))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "Short" result))
(should-not (string-match-p "\\.\\.\\." result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-truncate-exact-length-unchanged ()
"Test that title exactly at max length is not truncated.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '((title . "Exactly Twenty-Five C"))) ; 21 chars
(chime-max-title-length 21))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "Exactly Twenty-Five C" result))
(should-not (string-match-p "\\.\\.\\." result))))
(test-chime-notification-text-teardown)))
(ert-deftest test-chime-notification-text-truncate-nil-title-handled ()
"Test that nil title is handled gracefully with truncation enabled.
REFACTORED: Uses dynamic timestamps"
(test-chime-notification-text-setup)
(unwind-protect
(let* ((time (test-time-tomorrow-at 14 30))
(str-interval (cons (test-timestamp-string time) '(10 . medium)))
(event '()) ; No title
(chime-max-title-length 25))
(let ((result (chime--notification-text str-interval event)))
(should (stringp result))
(should (string-match-p "02:30 PM" result))))
(test-chime-notification-text-teardown)))
(provide 'test-chime-notification-text)
;;; test-chime-notification-text.el ends here
|