blob: 6de1ba09e8f34498bf3c5bf3a05b3ac3942960e5 (
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
|
;;; test-chime-update-modeline-helpers.el --- Tests for modeline helper functions -*- lexical-binding: t; -*-
;;; Commentary:
;; Unit tests for the refactored modeline helper functions:
;; - chime--find-soonest-time-in-window
;; - chime--build-upcoming-events-list
;; - chime--find-soonest-modeline-event
;;; Code:
(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))
(require 'testutil-time (expand-file-name "testutil-time.el"))
(require 'testutil-general (expand-file-name "testutil-general.el"))
(require 'testutil-events (expand-file-name "testutil-events.el"))
;;;; Tests for chime--find-soonest-time-in-window
(ert-deftest test-chime-find-soonest-time-empty-list ()
"Test that empty times list returns nil."
(let ((now (test-time-now))
(times '()))
(should (null (chime--find-soonest-time-in-window times now 60)))))
(ert-deftest test-chime-find-soonest-time-single-within-window ()
"Test single time within window returns that time."
(let* ((now (test-time-now))
(event-time (time-add now (seconds-to-time 1800))) ; 30 minutes
(times (list (cons "<2025-01-01 Wed 12:30>" event-time))))
(let ((result (chime--find-soonest-time-in-window times now 60)))
(should result)
(should (equal (nth 0 result) "<2025-01-01 Wed 12:30>"))
(should (time-equal-p (nth 1 result) event-time))
(should (< (abs (- (nth 2 result) 30)) 1))))) ; ~30 minutes
(ert-deftest test-chime-find-soonest-time-multiple-returns-soonest ()
"Test multiple times returns the soonest one."
(let* ((now (test-time-now))
(time1 (time-add now (seconds-to-time 3600))) ; 60 min
(time2 (time-add now (seconds-to-time 1800))) ; 30 min (soonest)
(time3 (time-add now (seconds-to-time 5400))) ; 90 min
(times (list (cons "<2025-01-01 Wed 13:00>" time1)
(cons "<2025-01-01 Wed 12:30>" time2)
(cons "<2025-01-01 Wed 13:30>" time3))))
(let ((result (chime--find-soonest-time-in-window times now 120)))
(should result)
(should (equal (nth 0 result) "<2025-01-01 Wed 12:30>"))
(should (< (abs (- (nth 2 result) 30)) 1))))) ; ~30 minutes
(ert-deftest test-chime-find-soonest-time-outside-window ()
"Test times outside window returns nil."
(let* ((now (test-time-now))
(event-time (time-add now (seconds-to-time 7200))) ; 120 minutes
(times (list (cons "<2025-01-01 Wed 14:00>" event-time))))
(should (null (chime--find-soonest-time-in-window times now 60)))))
(ert-deftest test-chime-find-soonest-time-mix-inside-outside ()
"Test mix of times inside/outside window returns soonest inside."
(let* ((now (test-time-now))
(time-outside (time-add now (seconds-to-time 7200))) ; 120 min (outside)
(time-inside (time-add now (seconds-to-time 1800))) ; 30 min (inside, soonest)
(times (list (cons "<2025-01-01 Wed 14:00>" time-outside)
(cons "<2025-01-01 Wed 12:30>" time-inside))))
(let ((result (chime--find-soonest-time-in-window times now 60)))
(should result)
(should (equal (nth 0 result) "<2025-01-01 Wed 12:30>")))))
(ert-deftest test-chime-find-soonest-time-past-event ()
"Test past events are excluded."
(let* ((now (test-time-now))
(past-time (time-subtract now (seconds-to-time 1800))) ; -30 minutes
(times (list (cons "<2025-01-01 Wed 11:30>" past-time))))
(should (null (chime--find-soonest-time-in-window times now 60)))))
;;;; Tests for chime--build-upcoming-events-list
(ert-deftest test-chime-build-upcoming-empty-events ()
"Test empty events list returns empty."
(let ((now (test-time-now))
(events '()))
(should (null (chime--build-upcoming-events-list events now 1440 t)))))
(ert-deftest test-chime-build-upcoming-single-event ()
"Test single event within window is included."
(with-test-setup
(let* ((now (test-time-now))
(event-time (time-add now (seconds-to-time 1800)))
(content (test-create-org-event "Meeting" event-time))
(events (test-gather-events-from-content content))
(result (chime--build-upcoming-events-list events now 1440 t)))
(should (= (length result) 1))
(should (string= (cdr (assoc 'title (car (car result)))) "Meeting")))))
(ert-deftest test-chime-build-upcoming-sorted-by-time ()
"Test multiple events are sorted by time (soonest first)."
(with-test-setup
(let* ((now (test-time-now))
(time1 (time-add now (seconds-to-time 5400))) ; 90 min
(time2 (time-add now (seconds-to-time 1800))) ; 30 min (soonest)
(time3 (time-add now (seconds-to-time 3600))) ; 60 min
(content (test-create-org-events
`(("Meeting 1" ,time1)
("Meeting 2" ,time2)
("Meeting 3" ,time3))))
(events (test-gather-events-from-content content))
(result (chime--build-upcoming-events-list events now 1440 t)))
(should (= (length result) 3))
;; First should be Meeting 2 (soonest at 30 min)
(should (string= (cdr (assoc 'title (car (nth 0 result)))) "Meeting 2"))
;; Second should be Meeting 3 (60 min)
(should (string= (cdr (assoc 'title (car (nth 1 result)))) "Meeting 3"))
;; Third should be Meeting 1 (90 min)
(should (string= (cdr (assoc 'title (car (nth 2 result)))) "Meeting 1")))))
(ert-deftest test-chime-build-upcoming-excludes-outside-window ()
"Test events outside lookahead window are excluded."
(with-test-setup
(let* ((now (test-time-now))
(near-time (time-add now (seconds-to-time 1800))) ; 30 min (included)
(far-time (time-add now (seconds-to-time 10800))) ; 180 min (excluded)
(content (test-create-org-events
`(("Near Meeting" ,near-time)
("Far Meeting" ,far-time))))
(events (test-gather-events-from-content content))
(result (chime--build-upcoming-events-list events now 60 t))) ; 60 min window
(should (= (length result) 1))
(should (string= (cdr (assoc 'title (car (car result)))) "Near Meeting")))))
;;;; Tests for chime--find-soonest-modeline-event
(ert-deftest test-chime-find-soonest-modeline-empty-events ()
"Test empty events list returns nil."
(let ((now (test-time-now))
(events '()))
(should (null (chime--find-soonest-modeline-event events now 60)))))
(ert-deftest test-chime-find-soonest-modeline-single-timed-event ()
"Test single timed event within window is returned."
(with-test-setup
(let* ((now (test-time-now))
(event-time (time-add now (seconds-to-time 1800)))
(content (test-create-org-event "Meeting" event-time))
(events (test-gather-events-from-content content))
(result (chime--find-soonest-modeline-event events now 60)))
(should result)
(should (string= (cdr (assoc 'title (nth 0 result))) "Meeting")))))
(ert-deftest test-chime-find-soonest-modeline-excludes-all-day ()
"Test all-day events are excluded from modeline."
(with-test-setup
(let* ((now (test-time-today-at 10 0))
(all-day-time (test-time-today-at 0 0))
(timed-time (time-add now (seconds-to-time 1800)))
(content (test-create-org-events
`(("All Day Event" ,all-day-time nil t)
("Timed Event" ,timed-time))))
(events (test-gather-events-from-content content))
(result (chime--find-soonest-modeline-event events now 60)))
(should result)
(should (string= (cdr (assoc 'title (nth 0 result))) "Timed Event")))))
(provide 'test-chime-update-modeline-helpers)
;;; test-chime-update-modeline-helpers.el ends here
|