aboutsummaryrefslogtreecommitdiff
path: root/tests/test-chime-timestamp-parse.el
blob: 5dc30bf4fb6a4d392e6da354f4298411d809728f (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
;;; test-chime-timestamp-parse.el --- Tests for chime--timestamp-parse -*- 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--timestamp-parse function.
;; Tests cover normal cases, boundary cases, and error cases.

;;; 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"))

;;; Setup and Teardown

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

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

;;; Normal Cases

(ert-deftest test-chime-timestamp-parse-standard-timestamp-returns-time-list ()
  "Test that a standard timestamp with time component returns a time list.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time (test-time-tomorrow-at 14 30))
             (timestamp (test-timestamp-string time))
             (result (chime--timestamp-parse timestamp)))
        ;; Should return a time list (list of integers)
        (should (listp result))
        (should (= (length result) 2))
        (should (integerp (car result)))
        (should (integerp (cadr result)))
        ;; Result should not be nil
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-scheduled-timestamp-returns-time-list ()
  "Test that a SCHEDULED timestamp parses correctly.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time (test-time-tomorrow-at 9 0))
             (timestamp (test-timestamp-string time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-deadline-timestamp-returns-time-list ()
  "Test that a DEADLINE timestamp parses correctly.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time (test-time-tomorrow-at 17 0))
             (timestamp (test-timestamp-string time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-timestamp-with-weekly-repeater-returns-time-list ()
  "Test that a timestamp with +1w repeater parses correctly.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time (test-time-tomorrow-at 14 0))
             (timestamp (format-time-string "<%Y-%m-%d %a %H:%M +1w>" time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-timestamp-with-completion-repeater-returns-time-list ()
  "Test that a timestamp with .+1d repeater parses correctly.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time (test-time-tomorrow-at 8 0))
             (timestamp (format-time-string "<%Y-%m-%d %a %H:%M .+1d>" time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-timestamp-with-catchup-repeater-returns-time-list ()
  "Test that a timestamp with ++1w repeater parses correctly.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time (test-time-tomorrow-at 10 30))
             (timestamp (format-time-string "<%Y-%m-%d %a %H:%M ++1w>" time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-timestamp-with-time-range-returns-start-time ()
  "Test that a timestamp with time range returns the start time.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time (test-time-tomorrow-at 14 0))
             (timestamp (format-time-string "<%Y-%m-%d %a %H:%M-15:30>" time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-timestamp-with-date-range-returns-start-date ()
  "Test that a timestamp with date range returns start date time.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time1 (test-time-tomorrow-at 10 0))
             (time2 (test-time-days-from-now 2))
             (timestamp (concat (test-timestamp-string time1) "--"
                                (format-time-string "<%Y-%m-%d %a %H:%M>" time2)))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

;;; Boundary Cases

(ert-deftest test-chime-timestamp-parse-midnight-timestamp-returns-time-list ()
  "Test that midnight (00:00) timestamp parses correctly.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time (test-time-tomorrow-at 0 0))
             (timestamp (test-timestamp-string time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-last-minute-of-day-returns-time-list ()
  "Test that last minute of day (23:59) timestamp parses correctly.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time (test-time-tomorrow-at 23 59))
             (timestamp (test-timestamp-string time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-year-boundary-new-years-eve-returns-time-list ()
  "Test that New Year's Eve timestamp parses correctly.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((now (test-time-now))
             (decoded (decode-time now))
             (year (nth 5 decoded))
             ;; Create Dec 31 at 23:30 for current test year
             (time (encode-time 0 30 23 31 12 year))
             (timestamp (test-timestamp-string time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-year-boundary-new-years-day-returns-time-list ()
  "Test that New Year's Day timestamp parses correctly.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((now (test-time-now))
             (decoded (decode-time now))
             (year (1+ (nth 5 decoded)))  ; Next year
             ;; Create Jan 1 at 00:30 for next test year
             (time (encode-time 0 30 0 1 1 year))
             (timestamp (test-timestamp-string time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-single-digit-time-returns-time-list ()
  "Test that single-digit hours and minutes parse correctly.

REFACTORED: Uses dynamic timestamps"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((time (test-time-tomorrow-at 1 5))
             (timestamp (test-timestamp-string time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-leap-year-feb-29-returns-time-list ()
  "Test that Feb 29 in leap year parses correctly.

REFACTORED: Uses dynamic timestamps (2024 leap year)"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* (;; Use 2024 as a known leap year
             (time (encode-time 0 0 14 29 2 2024))
             (timestamp (test-timestamp-string time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-month-boundary-end-of-month-returns-time-list ()
  "Test that end of month timestamp parses correctly.

REFACTORED: Uses dynamic timestamps (Oct 31)"
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((now (test-time-now))
             (decoded (decode-time now))
             (year (nth 5 decoded))
             ;; Create Oct 31 at 14:00 for current test year
             (time (encode-time 0 0 14 31 10 year))
             (timestamp (test-timestamp-string time))
             (result (chime--timestamp-parse timestamp)))
        (should (listp result))
        (should (= (length result) 2))
        (should result))
    (test-chime-timestamp-parse-teardown)))

;;; Bug Reproduction Tests

(ert-deftest test-chime-timestamp-parse-tomorrow-timestamp-returns-correct-date ()
  "Test that a tomorrow timestamp is parsed as tomorrow, not today.
This reproduces the bug where timestamps like <2025-11-03 Mon 10:00-10:30>
on Nov 02 are incorrectly grouped as 'Today' instead of 'Tomorrow'."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (with-test-time (encode-time 0 23 11 2 11 2025) ; Nov 02, 2025 11:23:00 AM
        (let* ((tomorrow-timestamp "<2025-11-03 Mon 10:00-10:30>")
               (parsed (chime--timestamp-parse tomorrow-timestamp))
               (now (current-time)))
          ;; Should parse successfully
          (should parsed)
          ;; Convert parsed time (HIGH LOW) to full time by appending (0 0)
          (let* ((parsed-time (append parsed '(0 0)))
                 (parsed-decoded (decode-time parsed-time))
                 (time-diff-seconds (- (time-to-seconds parsed-time)
                                      (time-to-seconds now))))
            ;; Verify the parsed date is Nov 03, 2025 (not Nov 02!)
            (should (= 3 (decoded-time-day parsed-decoded)))
            (should (= 11 (decoded-time-month parsed-decoded)))
            (should (= 2025 (decoded-time-year parsed-decoded)))
            ;; Verify the parsed time is 10:00
            (should (= 10 (decoded-time-hour parsed-decoded)))
            (should (= 0 (decoded-time-minute parsed-decoded)))
            ;; Time difference should be ~22h 37m (81420 seconds)
            (should (> time-diff-seconds 81360))  ; At least 22h 36m
            (should (< time-diff-seconds 81480)))))  ; At most 22h 38m
    (test-chime-timestamp-parse-teardown)))

;;; Error Cases

(ert-deftest test-chime-timestamp-parse-empty-string-returns-nil ()
  "Test that empty string returns nil."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((timestamp "")
             (result (chime--timestamp-parse timestamp)))
        (should (null result)))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-nil-input-returns-nil ()
  "Test that nil input returns nil."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((timestamp nil)
             (result (chime--timestamp-parse timestamp)))
        (should (null result)))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-missing-opening-bracket-returns-nil ()
  "Test that timestamp missing opening bracket returns nil."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((timestamp "2025-10-24 Fri 14:00>")
             (result (chime--timestamp-parse timestamp)))
        (should (null result)))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-missing-closing-bracket-returns-nil ()
  "Test that timestamp missing closing bracket returns nil."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((timestamp "<2025-10-24 Fri 14:00")
             (result (chime--timestamp-parse timestamp)))
        (should (null result)))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-invalid-date-format-returns-nil ()
  "Test that invalid date format returns nil."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((timestamp "<10-24-2025 Fri 14:00>")
             (result (chime--timestamp-parse timestamp)))
        (should (null result)))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-invalid-month-returns-nil ()
  "Test that invalid month value returns nil."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((timestamp "<2025-13-24 Fri 14:00>")
             (result (chime--timestamp-parse timestamp)))
        (should (null result)))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-invalid-day-returns-nil ()
  "Test that invalid day value returns nil."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((timestamp "<2025-10-32 Fri 14:00>")
             (result (chime--timestamp-parse timestamp)))
        (should (null result)))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-invalid-time-hour-returns-nil ()
  "Test that invalid hour value returns nil."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((timestamp "<2025-10-24 Fri 25:00>")
             (result (chime--timestamp-parse timestamp)))
        (should (null result)))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-invalid-time-minute-returns-nil ()
  "Test that invalid minute value returns nil."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((timestamp "<2025-10-24 Fri 14:60>")
             (result (chime--timestamp-parse timestamp)))
        (should (null result)))
    (test-chime-timestamp-parse-teardown)))

(ert-deftest test-chime-timestamp-parse-date-only-no-time-returns-nil ()
  "Test that day-wide timestamp without time returns nil."
  (test-chime-timestamp-parse-setup)
  (unwind-protect
      (let* ((timestamp "<2025-10-24 Fri>")
             (result (chime--timestamp-parse timestamp)))
        (should (null result)))
    (test-chime-timestamp-parse-teardown)))

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