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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
|
;;; test-calendar-sync.el --- Tests for calendar-sync -*- lexical-binding: t; -*-
;;; Commentary:
;; Comprehensive tests for calendar-sync module.
;; Covers Normal, Boundary, and Error cases for all parsing functions.
;; Uses dynamic timestamps (no hardcoded dates).
;;; Code:
(require 'ert)
(require 'calendar-sync)
(require 'testutil-calendar-sync)
;;; Test Data
(defconst test-calendar-sync-sample-ics
"BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Google Inc//Google Calendar 70.9054//EN
BEGIN:VEVENT
SUMMARY:Test Meeting
DTSTART:20251116T140000Z
DTEND:20251116T150000Z
DESCRIPTION:Discuss project status
LOCATION:Conference Room A
END:VEVENT
END:VCALENDAR"
"Sample .ics content for testing.")
;;; Helper Functions
(defmacro with-test-time (time &rest body)
"Execute BODY with `current-time` mocked to TIME."
`(cl-letf (((symbol-function 'current-time)
(lambda () ,time)))
,@body))
;;; Tests: calendar-sync--split-events
(ert-deftest test-calendar-sync--split-events-normal-single-event-returns-one ()
"Test that single event is extracted correctly."
(let* ((ics test-calendar-sync-sample-ics)
(events (calendar-sync--split-events ics)))
(should (= 1 (length events)))
(should (string-match-p "BEGIN:VEVENT" (car events)))
(should (string-match-p "END:VEVENT" (car events)))))
(ert-deftest test-calendar-sync--split-events-normal-multiple-events-returns-all ()
"Test that multiple events are all extracted."
(let* ((event1 (test-calendar-sync-make-vevent
"Event 1"
(test-calendar-sync-time-today-at 14 0)
(test-calendar-sync-time-today-at 15 0)))
(event2 (test-calendar-sync-make-vevent
"Event 2"
(test-calendar-sync-time-tomorrow-at 10 0)
(test-calendar-sync-time-tomorrow-at 11 0)))
(ics (test-calendar-sync-make-ics event1 event2))
(events (calendar-sync--split-events ics)))
(should (= 2 (length events)))
(should (string-match-p "Event 1" (nth 0 events)))
(should (string-match-p "Event 2" (nth 1 events)))))
(ert-deftest test-calendar-sync--split-events-boundary-empty-string-returns-nil ()
"Test that empty string returns empty list."
(should (null (calendar-sync--split-events ""))))
(ert-deftest test-calendar-sync--split-events-boundary-no-events-returns-nil ()
"Test that .ics with no VEVENT returns empty list."
(let ((ics "BEGIN:VCALENDAR\nVERSION:2.0\nEND:VCALENDAR"))
(should (null (calendar-sync--split-events ics)))))
;;; Tests: calendar-sync--get-property
(ert-deftest test-calendar-sync--get-property-normal-summary-returns-value ()
"Test extracting SUMMARY property."
(let ((event "BEGIN:VEVENT\nSUMMARY:Test Event\nEND:VEVENT"))
(should (string= "Test Event" (calendar-sync--get-property event "SUMMARY")))))
(ert-deftest test-calendar-sync--get-property-normal-description-with-spaces ()
"Test extracting DESCRIPTION with spaces."
(let ((event "DESCRIPTION:Multi word description"))
(should (string= "Multi word description"
(calendar-sync--get-property event "DESCRIPTION")))))
(ert-deftest test-calendar-sync--get-property-boundary-missing-property-returns-nil ()
"Test that missing property returns nil."
(let ((event "BEGIN:VEVENT\nSUMMARY:Test\nEND:VEVENT"))
(should (null (calendar-sync--get-property event "LOCATION")))))
(ert-deftest test-calendar-sync--get-property-error-empty-string-returns-nil ()
"Test that empty event string returns nil."
(should (null (calendar-sync--get-property "" "SUMMARY"))))
;;; Tests: calendar-sync--parse-timestamp
(ert-deftest test-calendar-sync--parse-timestamp-normal-datetime-returns-full-time ()
"Test parsing full datetime with time component.
UTC timestamp (with Z suffix) is converted to local time."
(let* ((parsed (calendar-sync--parse-timestamp "20251116T140000Z"))
;; Compute expected local time from UTC
(utc-time (encode-time 0 0 14 16 11 2025 0))
(local-time (decode-time utc-time))
(expected-hour (nth 2 local-time))
(expected-minute (nth 1 local-time)))
(should (= 5 (length parsed)))
(should (= 2025 (nth 0 parsed)))
(should (= 11 (nth 1 parsed)))
(should (= 16 (nth 2 parsed)))
(should (= expected-hour (nth 3 parsed)))
(should (= expected-minute (nth 4 parsed)))))
(ert-deftest test-calendar-sync--parse-timestamp-normal-datetime-without-z ()
"Test parsing datetime without Z suffix."
(let* ((parsed (calendar-sync--parse-timestamp "20251116T140000")))
(should (= 5 (length parsed)))
(should (= 14 (nth 3 parsed)))))
(ert-deftest test-calendar-sync--parse-timestamp-boundary-date-only-returns-three-parts ()
"Test parsing date-only timestamp (all-day event)."
(let* ((parsed (calendar-sync--parse-timestamp "20251116")))
(should (= 3 (length parsed)))
(should (= 2025 (nth 0 parsed)))
(should (= 11 (nth 1 parsed)))
(should (= 16 (nth 2 parsed)))))
(ert-deftest test-calendar-sync--parse-timestamp-error-invalid-format-returns-nil ()
"Test that invalid timestamp returns nil."
(should (null (calendar-sync--parse-timestamp "invalid")))
(should (null (calendar-sync--parse-timestamp "2025-11-16")))
(should (null (calendar-sync--parse-timestamp ""))))
(ert-deftest test-calendar-sync--parse-timestamp-boundary-leap-year-feb-29 ()
"Test parsing Feb 29 on leap year."
(let* ((parsed (calendar-sync--parse-timestamp "20240229T120000Z")))
(should parsed)
(should (= 2024 (nth 0 parsed)))
(should (= 2 (nth 1 parsed)))
(should (= 29 (nth 2 parsed)))))
;;; Tests: Timezone Conversion
(ert-deftest test-calendar-sync--parse-timestamp-utc-conversion-actually-converts ()
"Test that UTC timestamp (with Z) is actually converted to local time.
This test verifies the conversion happened by checking that the result
differs from the original UTC time (unless we happen to be in UTC timezone)."
(let* ((utc-timestamp "20251116T140000Z") ; 14:00 UTC
(parsed (calendar-sync--parse-timestamp utc-timestamp))
;; Get what the UTC time would be without conversion
(utc-hour 14)
(parsed-hour (nth 3 parsed)))
;; The parsed hour should match what decode-time gives us for this UTC time
(let* ((utc-time (encode-time 0 0 14 16 11 2025 0))
(local-time (decode-time utc-time))
(expected-local-hour (nth 2 local-time)))
(should (= expected-local-hour parsed-hour)))))
(ert-deftest test-calendar-sync--parse-timestamp-local-time-not-converted ()
"Test that timestamp without Z suffix is NOT converted.
Local times should pass through unchanged."
(let* ((local-timestamp "20251116T140000") ; 14:00 local (no Z)
(parsed (calendar-sync--parse-timestamp local-timestamp)))
;; Should return exactly 14:00, not converted
(should (= 14 (nth 3 parsed)))
(should (= 0 (nth 4 parsed)))))
(ert-deftest test-calendar-sync--parse-timestamp-utc-midnight-converts-correctly ()
"Test UTC midnight conversion handles day boundaries correctly."
(let* ((parsed (calendar-sync--parse-timestamp "20251116T000000Z"))
;; Compute expected local time
(utc-time (encode-time 0 0 0 16 11 2025 0))
(local-time (decode-time utc-time))
(expected-year (nth 5 local-time))
(expected-month (nth 4 local-time))
(expected-day (nth 3 local-time))
(expected-hour (nth 2 local-time)))
(should (= expected-year (nth 0 parsed)))
(should (= expected-month (nth 1 parsed)))
(should (= expected-day (nth 2 parsed)))
(should (= expected-hour (nth 3 parsed)))))
;;; Tests: UTC to Local Conversion
(ert-deftest test-calendar-sync--convert-utc-to-local-basic-conversion ()
"Test basic UTC to local time conversion."
(let* ((result (calendar-sync--convert-utc-to-local 2025 11 16 14 30 0))
;; Compute expected local time
(utc-time (encode-time 0 30 14 16 11 2025 0))
(local-time (decode-time utc-time))
(expected-year (nth 5 local-time))
(expected-month (nth 4 local-time))
(expected-day (nth 3 local-time))
(expected-hour (nth 2 local-time))
(expected-minute (nth 1 local-time)))
;; Verify conversion happened correctly
(should (= expected-year (nth 0 result)))
(should (= expected-month (nth 1 result)))
(should (= expected-day (nth 2 result)))
(should (= expected-hour (nth 3 result)))
(should (= expected-minute (nth 4 result)))))
(ert-deftest test-calendar-sync--convert-utc-to-local-midnight-boundary ()
"Test UTC midnight conversion handles day boundary correctly."
(let* ((result (calendar-sync--convert-utc-to-local 2025 11 16 0 0 0))
;; Compute expected local time
(utc-time (encode-time 0 0 0 16 11 2025 0))
(local-time (decode-time utc-time))
(expected-year (nth 5 local-time))
(expected-month (nth 4 local-time))
(expected-day (nth 3 local-time))
(expected-hour (nth 2 local-time)))
;; In timezones west of UTC, midnight UTC becomes previous day
(should (= expected-year (nth 0 result)))
(should (= expected-month (nth 1 result)))
(should (= expected-day (nth 2 result)))
(should (= expected-hour (nth 3 result)))))
(ert-deftest test-calendar-sync--convert-utc-to-local-preserves-minutes ()
"Test that minute component is preserved during conversion."
(let* ((result (calendar-sync--convert-utc-to-local 2025 11 16 20 45 0))
(expected-minute 45))
;; Minutes should always be preserved (timezone offsets are in hours)
(should (= expected-minute (nth 4 result)))))
(ert-deftest test-calendar-sync--convert-utc-to-local-returns-five-elements ()
"Test that conversion returns exactly 5 elements (year month day hour minute)."
(let ((result (calendar-sync--convert-utc-to-local 2025 11 16 14 30 0)))
(should (= 5 (length result)))
;; All elements should be numbers
(should (numberp (nth 0 result))) ; year
(should (numberp (nth 1 result))) ; month
(should (numberp (nth 2 result))) ; day
(should (numberp (nth 3 result))) ; hour
(should (numberp (nth 4 result))))) ; minute
(ert-deftest test-calendar-sync--convert-utc-to-local-end-of-day ()
"Test conversion near end of day (23:59 UTC)."
(let* ((result (calendar-sync--convert-utc-to-local 2025 11 16 23 59 0))
;; Compute expected local time
(utc-time (encode-time 0 59 23 16 11 2025 0))
(local-time (decode-time utc-time))
(expected-year (nth 5 local-time))
(expected-month (nth 4 local-time))
(expected-day (nth 3 local-time))
(expected-hour (nth 2 local-time))
(expected-minute (nth 1 local-time)))
;; In timezones east of UTC, this might roll to next day
(should (= expected-year (nth 0 result)))
(should (= expected-month (nth 1 result)))
(should (= expected-day (nth 2 result)))
(should (= expected-hour (nth 3 result)))
(should (= expected-minute (nth 4 result)))))
;;; Tests: Chronological Sorting
(ert-deftest test-calendar-sync--event-start-time-extracts-comparable-time ()
"Test that event start time can be extracted for comparison."
(let* ((event (list :start (list 2025 11 16 14 30)))
(time-value (calendar-sync--event-start-time event))
(event-earlier (list :start (list 2025 11 16 10 0)))
(time-earlier (calendar-sync--event-start-time event-earlier)))
;; Should return a valid time value (cons cell for Emacs time)
(should (consp time-value))
;; Should be comparable - later time should not be less than earlier
(should (time-less-p time-earlier time-value))))
(ert-deftest test-calendar-sync--event-start-time-handles-all-day-events ()
"Test that all-day events (no time component) work for comparison."
(let* ((event (list :start (list 2025 11 16))) ; No hour/minute
(time-value (calendar-sync--event-start-time event))
(event-next-day (list :start (list 2025 11 17)))
(time-next-day (calendar-sync--event-start-time event-next-day)))
;; Should return a valid time value (cons cell)
(should (consp time-value))
;; Next day should be later than current day
(should (time-less-p time-value time-next-day))))
(ert-deftest test-calendar-sync--parse-ics-sorts-chronologically ()
"Test that parsed events are returned in chronological order.
Earlier events should appear first in the output."
(let* ((event-future (test-calendar-sync-make-vevent
"Future Event"
(test-calendar-sync-time-days-from-now 7 10 0)
(test-calendar-sync-time-days-from-now 7 11 0)))
(event-past (test-calendar-sync-make-vevent
"Past Event"
(test-calendar-sync-time-days-ago 1 14 0)
(test-calendar-sync-time-days-ago 1 15 0)))
(event-today (test-calendar-sync-make-vevent
"Today Event"
(test-calendar-sync-time-today-at 9 0)
(test-calendar-sync-time-today-at 10 0)))
;; Create .ics with events in wrong order (future, past, today)
(ics (test-calendar-sync-make-ics event-future event-past event-today))
(org-content (calendar-sync--parse-ics ics))
;; Find positions of each event in output
(past-pos (string-match "Past Event" org-content))
(today-pos (string-match "Today Event" org-content))
(future-pos (string-match "Future Event" org-content)))
;; All events should be found
(should past-pos)
(should today-pos)
(should future-pos)
;; Order should be: past < today < future
(should (< past-pos today-pos))
(should (< today-pos future-pos))))
(ert-deftest test-calendar-sync--parse-ics-sorts-same-day-events-by-time ()
"Test that events on the same day are sorted by time."
(let* ((event-morning (test-calendar-sync-make-vevent
"Morning Event"
(test-calendar-sync-time-today-at 9 0)
(test-calendar-sync-time-today-at 10 0)))
(event-evening (test-calendar-sync-make-vevent
"Evening Event"
(test-calendar-sync-time-today-at 18 0)
(test-calendar-sync-time-today-at 19 0)))
(event-afternoon (test-calendar-sync-make-vevent
"Afternoon Event"
(test-calendar-sync-time-today-at 14 0)
(test-calendar-sync-time-today-at 15 0)))
;; Create .ics with events in wrong order
(ics (test-calendar-sync-make-ics event-evening event-morning event-afternoon))
(org-content (calendar-sync--parse-ics ics))
(morning-pos (string-match "Morning Event" org-content))
(afternoon-pos (string-match "Afternoon Event" org-content))
(evening-pos (string-match "Evening Event" org-content)))
(should (< morning-pos afternoon-pos))
(should (< afternoon-pos evening-pos))))
;;; Tests: calendar-sync--format-timestamp
(ert-deftest test-calendar-sync--format-timestamp-normal-timed-event-includes-times ()
"Test formatting timed event with start and end times."
(let* ((start (list 2025 11 16 14 0))
(end (list 2025 11 16 15 30))
(formatted (calendar-sync--format-timestamp start end)))
(should (string-match-p "<2025-11-16 \\w\\{3\\} 14:00-15:30>" formatted))))
(ert-deftest test-calendar-sync--format-timestamp-boundary-all-day-event-no-times ()
"Test formatting all-day event (date only, no times)."
(let* ((start (list 2025 11 16))
(formatted (calendar-sync--format-timestamp start nil)))
(should (string-match-p "<2025-11-16 \\w\\{3\\}>" formatted))
(should-not (string-match-p "[0-9]:[0-9]" formatted))))
(ert-deftest test-calendar-sync--format-timestamp-normal-includes-day-of-week ()
"Test that formatted timestamp includes day of week."
(let* ((start (list 2025 11 16 14 0))
(end (list 2025 11 16 15 0))
(formatted (calendar-sync--format-timestamp start end)))
(should (string-match-p "Sun" formatted))))
;;; Tests: calendar-sync--parse-event
(ert-deftest test-calendar-sync--parse-event-normal-complete-event-returns-plist ()
"Test parsing complete event with all fields."
(let* ((event (test-calendar-sync-make-vevent
"Meeting"
(test-calendar-sync-time-today-at 14 0)
(test-calendar-sync-time-today-at 15 0)
"Discussion"
"Room A"))
(parsed (calendar-sync--parse-event event)))
(should parsed)
(should (string= "Meeting" (plist-get parsed :summary)))
(should (string= "Discussion" (plist-get parsed :description)))
(should (string= "Room A" (plist-get parsed :location)))
(should (plist-get parsed :start))
(should (plist-get parsed :end))))
(ert-deftest test-calendar-sync--parse-event-boundary-minimal-event-no-optional-fields ()
"Test parsing event with only required fields (SUMMARY, DTSTART)."
(let* ((event (test-calendar-sync-make-vevent
"Simple Event"
(test-calendar-sync-time-today-at 10 0)
nil))
(parsed (calendar-sync--parse-event event)))
(should parsed)
(should (string= "Simple Event" (plist-get parsed :summary)))
(should (null (plist-get parsed :description)))
(should (null (plist-get parsed :location)))
(should (plist-get parsed :start))))
(ert-deftest test-calendar-sync--parse-event-boundary-emoji-in-summary-preserved ()
"Test that emoji in summary are preserved."
(let* ((event (test-calendar-sync-make-vevent
"Meeting 🎉"
(test-calendar-sync-time-today-at 14 0)
(test-calendar-sync-time-today-at 15 0)))
(parsed (calendar-sync--parse-event event)))
(should (string-match-p "🎉" (plist-get parsed :summary)))))
(ert-deftest test-calendar-sync--parse-event-error-missing-summary-returns-nil ()
"Test that event without SUMMARY returns nil."
(let ((event "BEGIN:VEVENT\nDTSTART:20251116T140000Z\nEND:VEVENT"))
(should (null (calendar-sync--parse-event event)))))
(ert-deftest test-calendar-sync--parse-event-error-missing-dtstart-returns-nil ()
"Test that event without DTSTART returns nil."
(let ((event "BEGIN:VEVENT\nSUMMARY:Test\nEND:VEVENT"))
(should (null (calendar-sync--parse-event event)))))
(ert-deftest test-calendar-sync--parse-event-error-invalid-dtstart-returns-nil ()
"Test that event with invalid DTSTART returns nil."
(let ((event "BEGIN:VEVENT\nSUMMARY:Test\nDTSTART:invalid\nEND:VEVENT"))
(should (null (calendar-sync--parse-event event)))))
;;; Tests: calendar-sync--event-to-org
(ert-deftest test-calendar-sync--event-to-org-normal-complete-event-formats-correctly ()
"Test converting complete event to org format."
(let* ((event (list :summary "Meeting"
:description "Discuss project"
:location "Room A"
:start (list 2025 11 16 14 0)
:end (list 2025 11 16 15 30)))
(org-str (calendar-sync--event-to-org event)))
(should (string-match-p "^\\* Meeting$" org-str))
(should (string-match-p "<2025-11-16 \\w\\{3\\} 14:00-15:30>" org-str))
(should (string-match-p "Discuss project" org-str))
(should (string-match-p "Location: Room A" org-str))))
(ert-deftest test-calendar-sync--event-to-org-boundary-minimal-event-no-description ()
"Test converting minimal event without optional fields."
(let* ((event (list :summary "Simple Event"
:start (list 2025 11 16 10 0)
:end (list 2025 11 16 11 0)))
(org-str (calendar-sync--event-to-org event)))
(should (string-match-p "^\\* Simple Event$" org-str))
(should-not (string-match-p "Location:" org-str))
;; Check timestamp is present
(should (string-match-p "<2025-11-16" org-str))))
(ert-deftest test-calendar-sync--event-to-org-boundary-all-day-event-no-times ()
"Test converting all-day event."
(let* ((event (list :summary "All Day Event"
:start (list 2025 11 16)))
(org-str (calendar-sync--event-to-org event)))
(should (string-match-p "^\\* All Day Event$" org-str))
(should (string-match-p "<2025-11-16" org-str))
(should-not (string-match-p "[0-9][0-9]:[0-9][0-9]" org-str))))
;;; Tests: calendar-sync--parse-ics
(ert-deftest test-calendar-sync--parse-ics-normal-single-event-returns-org ()
"Test parsing .ics with single event returns org format."
(let* ((event (test-calendar-sync-make-vevent
"Test Event"
(test-calendar-sync-time-today-at 14 0)
(test-calendar-sync-time-today-at 15 0)))
(ics (test-calendar-sync-make-ics event))
(org-content (calendar-sync--parse-ics ics)))
(should org-content)
(should (string-match-p "^# Calendar Events" org-content))
(should (string-match-p "\\* Test Event" org-content))))
(ert-deftest test-calendar-sync--parse-ics-normal-multiple-events-all-included ()
"Test parsing .ics with multiple events."
(let* ((event1 (test-calendar-sync-make-vevent
"Event 1"
(test-calendar-sync-time-today-at 9 0)
(test-calendar-sync-time-today-at 10 0)))
(event2 (test-calendar-sync-make-vevent
"Event 2"
(test-calendar-sync-time-today-at 14 0)
(test-calendar-sync-time-today-at 15 0)))
(ics (test-calendar-sync-make-ics event1 event2))
(org-content (calendar-sync--parse-ics ics)))
(should org-content)
(should (string-match-p "\\* Event 1" org-content))
(should (string-match-p "\\* Event 2" org-content))))
(ert-deftest test-calendar-sync--parse-ics-boundary-empty-calendar-returns-nil ()
"Test parsing empty calendar (no events)."
(let* ((ics "BEGIN:VCALENDAR\nVERSION:2.0\nEND:VCALENDAR")
(org-content (calendar-sync--parse-ics ics)))
(should (null org-content))))
(ert-deftest test-calendar-sync--parse-ics-error-malformed-ics-returns-nil ()
"Test that malformed .ics returns nil and sets error."
(setq calendar-sync--last-error nil)
(let ((result (calendar-sync--parse-ics "malformed content")))
;; Function should handle error gracefully
(should (or (null result) (stringp result)))))
(ert-deftest test-calendar-sync--parse-ics-boundary-mixed-valid-invalid-events ()
"Test parsing .ics with mix of valid and invalid events.
Valid events should be parsed, invalid ones skipped."
(let* ((valid-event (test-calendar-sync-make-vevent
"Valid Event"
(test-calendar-sync-time-today-at 14 0)
(test-calendar-sync-time-today-at 15 0)))
(invalid-event "BEGIN:VEVENT\nDTSTART:20251116T140000Z\nEND:VEVENT") ;; No SUMMARY
(ics (test-calendar-sync-make-ics valid-event invalid-event))
(org-content (calendar-sync--parse-ics ics)))
(should org-content)
(should (string-match-p "\\* Valid Event" org-content))))
;;; Tests: Timezone Detection
(ert-deftest test-calendar-sync--current-timezone-offset-returns-number ()
"Test that current timezone offset returns a number in seconds."
(let ((offset (calendar-sync--current-timezone-offset)))
;; Should be a number
(should (numberp offset))
;; Should be reasonable (between -12 and +14 hours in seconds)
(should (>= offset (* -12 3600)))
(should (<= offset (* 14 3600)))))
(ert-deftest test-calendar-sync--timezone-name-returns-string ()
"Test that timezone name returns a string."
(let ((name (calendar-sync--timezone-name)))
;; Should be a string
(should (stringp name))
;; Should not be empty
(should (> (length name) 0))))
(ert-deftest test-calendar-sync--format-timezone-offset-handles-negative ()
"Test formatting negative timezone offsets (west of UTC)."
;; CST: UTC-6 = -21600 seconds
(should (string= "UTC-6" (calendar-sync--format-timezone-offset -21600)))
;; PST: UTC-8 = -28800 seconds
(should (string= "UTC-8" (calendar-sync--format-timezone-offset -28800)))
;; EST: UTC-5 = -18000 seconds
(should (string= "UTC-5" (calendar-sync--format-timezone-offset -18000))))
(ert-deftest test-calendar-sync--format-timezone-offset-handles-positive ()
"Test formatting positive timezone offsets (east of UTC)."
;; CET: UTC+1 = 3600 seconds
(should (string= "UTC+1" (calendar-sync--format-timezone-offset 3600)))
;; JST: UTC+9 = 32400 seconds
(should (string= "UTC+9" (calendar-sync--format-timezone-offset 32400)))
;; AEST: UTC+10 = 36000 seconds
(should (string= "UTC+10" (calendar-sync--format-timezone-offset 36000))))
(ert-deftest test-calendar-sync--format-timezone-offset-handles-utc ()
"Test formatting UTC (zero offset)."
(should (string= "UTC+0" (calendar-sync--format-timezone-offset 0))))
(ert-deftest test-calendar-sync--format-timezone-offset-handles-fractional ()
"Test formatting timezone offsets with fractional hours."
;; IST: UTC+5:30 = 19800 seconds
(should (string= "UTC+5:30" (calendar-sync--format-timezone-offset 19800)))
;; ACST: UTC+9:30 = 34200 seconds
(should (string= "UTC+9:30" (calendar-sync--format-timezone-offset 34200)))
;; NFT: UTC+11:30 = 41400 seconds
(should (string= "UTC+11:30" (calendar-sync--format-timezone-offset 41400))))
(ert-deftest test-calendar-sync--format-timezone-offset-handles-nil ()
"Test formatting nil timezone offset."
(should (string= "unknown" (calendar-sync--format-timezone-offset nil))))
(ert-deftest test-calendar-sync--timezone-changed-p-detects-no-change ()
"Test that timezone-changed-p returns nil when timezone hasn't changed."
(let ((calendar-sync--last-timezone-offset (calendar-sync--current-timezone-offset)))
(should-not (calendar-sync--timezone-changed-p))))
(ert-deftest test-calendar-sync--timezone-changed-p-detects-change ()
"Test that timezone-changed-p detects timezone changes."
(let* ((current (calendar-sync--current-timezone-offset))
;; Simulate different timezone (shift by 3 hours)
(calendar-sync--last-timezone-offset (+ current (* 3 3600))))
(should (calendar-sync--timezone-changed-p))))
(ert-deftest test-calendar-sync--timezone-changed-p-handles-nil-last ()
"Test that timezone-changed-p returns nil when no previous timezone."
(let ((calendar-sync--last-timezone-offset nil))
(should-not (calendar-sync--timezone-changed-p))))
;;; Tests: State Persistence
(ert-deftest test-calendar-sync--save-and-load-state-roundtrip ()
"Test that state can be saved and loaded correctly."
(let* ((test-state-file (make-temp-file "calendar-sync-test-state"))
(calendar-sync--state-file test-state-file)
(original-offset -21600) ; CST
(original-time (current-time))
(calendar-sync--last-timezone-offset original-offset)
(calendar-sync--calendar-states (make-hash-table :test 'equal)))
;; Set up per-calendar state
(puthash "test-calendar"
(list :status 'ok :last-sync original-time :last-error nil)
calendar-sync--calendar-states)
(unwind-protect
(progn
;; Save state
(calendar-sync--save-state)
(should (file-exists-p test-state-file))
;; Clear variables
(setq calendar-sync--last-timezone-offset nil)
(clrhash calendar-sync--calendar-states)
;; Load state
(calendar-sync--load-state)
;; Verify loaded correctly
(should (= original-offset calendar-sync--last-timezone-offset))
(let ((loaded-state (gethash "test-calendar" calendar-sync--calendar-states)))
(should loaded-state)
(should (eq 'ok (plist-get loaded-state :status)))
(should (equal original-time (plist-get loaded-state :last-sync)))))
;; Cleanup
(when (file-exists-p test-state-file)
(delete-file test-state-file)))))
(ert-deftest test-calendar-sync--save-state-creates-directory ()
"Test that save-state creates parent directory if needed."
(let* ((test-dir (make-temp-file "calendar-sync-test-dir" t))
(test-state-file (expand-file-name "subdir/state.el" test-dir))
(calendar-sync--state-file test-state-file)
(calendar-sync--last-timezone-offset -21600)
(calendar-sync--calendar-states (make-hash-table :test 'equal)))
(unwind-protect
(progn
(calendar-sync--save-state)
(should (file-exists-p test-state-file))
(should (file-directory-p (file-name-directory test-state-file))))
;; Cleanup
(when (file-exists-p test-dir)
(delete-directory test-dir t)))))
(ert-deftest test-calendar-sync--load-state-handles-missing-file ()
"Test that load-state handles missing file gracefully."
(let ((calendar-sync--state-file "/nonexistent/path/state.el")
(calendar-sync--last-timezone-offset nil)
(calendar-sync--calendar-states (make-hash-table :test 'equal)))
;; Should not error
(should-not (calendar-sync--load-state))
;; Variables should remain nil/empty
(should-not calendar-sync--last-timezone-offset)
(should (= 0 (hash-table-count calendar-sync--calendar-states)))))
(ert-deftest test-calendar-sync--load-state-handles-corrupted-file ()
"Test that load-state handles corrupted state file gracefully."
(let* ((test-state-file (make-temp-file "calendar-sync-test-state"))
(calendar-sync--state-file test-state-file)
(calendar-sync--last-timezone-offset nil)
(calendar-sync--calendar-states (make-hash-table :test 'equal)))
(unwind-protect
(progn
;; Write corrupted data
(with-temp-file test-state-file
(insert "this is not valid elisp {[}"))
;; Should handle error gracefully (catches error, logs message, returns nil)
;; Function logs to *Messages* but returns nil (doesn't crash)
(should-not (calendar-sync--load-state))
;; Variables should remain nil/empty (not loaded from corrupted file)
(should-not calendar-sync--last-timezone-offset)
(should (= 0 (hash-table-count calendar-sync--calendar-states))))
;; Cleanup
(when (file-exists-p test-state-file)
(delete-file test-state-file)))))
;;; Tests: Multi-Calendar Configuration
(ert-deftest test-calendar-sync--calendar-names-returns-names ()
"Test that calendar-names returns list of calendar names."
(let ((calendar-sync-calendars
'((:name "cal1" :url "http://example.com/1" :file "/tmp/cal1.org")
(:name "cal2" :url "http://example.com/2" :file "/tmp/cal2.org"))))
(should (equal '("cal1" "cal2") (calendar-sync--calendar-names)))))
(ert-deftest test-calendar-sync--calendar-names-empty-when-no-calendars ()
"Test that calendar-names returns empty list when no calendars configured."
(let ((calendar-sync-calendars nil))
(should (null (calendar-sync--calendar-names)))))
(ert-deftest test-calendar-sync--get-calendar-by-name-finds-calendar ()
"Test that get-calendar-by-name finds correct calendar."
(let ((calendar-sync-calendars
'((:name "google" :url "http://google.com" :file "/tmp/gcal.org")
(:name "proton" :url "http://proton.me" :file "/tmp/pcal.org"))))
(let ((found (calendar-sync--get-calendar-by-name "proton")))
(should found)
(should (string= "proton" (plist-get found :name)))
(should (string= "http://proton.me" (plist-get found :url))))))
(ert-deftest test-calendar-sync--get-calendar-by-name-returns-nil-for-unknown ()
"Test that get-calendar-by-name returns nil for unknown calendar."
(let ((calendar-sync-calendars
'((:name "google" :url "http://google.com" :file "/tmp/gcal.org"))))
(should (null (calendar-sync--get-calendar-by-name "nonexistent")))))
(ert-deftest test-calendar-sync--get-calendar-state-returns-nil-for-new ()
"Test that get-calendar-state returns nil for calendar without state."
(let ((calendar-sync--calendar-states (make-hash-table :test 'equal)))
(should (null (calendar-sync--get-calendar-state "new-calendar")))))
(ert-deftest test-calendar-sync--set-and-get-calendar-state ()
"Test setting and getting calendar state."
(let ((calendar-sync--calendar-states (make-hash-table :test 'equal))
(test-state '(:status ok :last-sync (0 0 0) :last-error nil)))
(calendar-sync--set-calendar-state "test-cal" test-state)
(let ((retrieved (calendar-sync--get-calendar-state "test-cal")))
(should retrieved)
(should (eq 'ok (plist-get retrieved :status))))))
(provide 'test-calendar-sync)
;;; test-calendar-sync.el ends here
|