blob: 9f87856f8a385c85adc7fdd811015023b1e7215a (
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
|
;;; test-calendar-sync--event-to-org.el --- Tests for updated event-to-org formatter -*- lexical-binding: t; -*-
;;; Commentary:
;; Unit tests for calendar-sync--event-to-org function (updated version).
;; Tests the new property drawer format with LOCATION, ORGANIZER, STATUS, URL.
;; Covers Normal, Boundary, and Error cases.
;;; Code:
(require 'ert)
(require 'testutil-calendar-sync)
(require 'calendar-sync)
(defun test-calendar-sync--count-line-matches (regexp text)
"Count lines in TEXT that match REGEXP."
(with-temp-buffer
(insert text)
(goto-char (point-min))
(let ((count 0))
(while (re-search-forward regexp nil t)
(setq count (1+ count)))
count)))
;;; Normal Cases
(ert-deftest test-calendar-sync--event-to-org-normal-all-fields ()
"Test event with all fields produces property drawer + description."
(let* ((start (test-calendar-sync-time-days-from-now 5 14 0))
(end (test-calendar-sync-time-days-from-now 5 15 0))
(event (list :summary "Team Standup"
:start start
:end end
:description "Daily sync meeting"
:location "Conference Room A"
:organizer (list :cn "John Smith" :email "john@example.com")
:status "accepted"
:url "https://meet.google.com/abc-defg-hij")))
(let ((result (calendar-sync--event-to-org event)))
;; Verify heading comes before timestamp
(should (string-match "\\* Team Standup" result))
(let ((heading-pos (match-beginning 0)))
(should (string-match "<[0-9]" result))
(should (< heading-pos (match-beginning 0))))
(should (string-match-p ":PROPERTIES:" result))
(should (string-match-p ":LOCATION: Conference Room A" result))
(should (string-match-p ":ORGANIZER: John Smith" result))
(should (string-match-p ":STATUS: accepted" result))
(should (string-match-p ":URL: https://meet.google.com/abc-defg-hij" result))
(should (string-match-p ":END:" result))
(should (string-match-p "Daily sync meeting" result)))))
(ert-deftest test-calendar-sync--event-to-org-normal-summary-and-time-only ()
"Test event with only summary and time (no drawer)."
(let* ((start (test-calendar-sync-time-days-from-now 5 14 0))
(end (test-calendar-sync-time-days-from-now 5 15 0))
(event (list :summary "Quick Chat"
:start start
:end end)))
(let ((result (calendar-sync--event-to-org event)))
(should (string-match-p "\\* Quick Chat" result))
(should-not (string-match-p ":PROPERTIES:" result)))))
;;; Boundary Cases
(ert-deftest test-calendar-sync--event-to-org-boundary-no-description ()
"Test event with location but no description."
(let* ((start (test-calendar-sync-time-days-from-now 5 14 0))
(end (test-calendar-sync-time-days-from-now 5 15 0))
(event (list :summary "Meeting"
:start start
:end end
:location "Room B")))
(let ((result (calendar-sync--event-to-org event)))
(should (string-match-p ":LOCATION: Room B" result))
;; After :END: there should be no body text
(should-not (string-match-p ":END:\n." result)))))
(ert-deftest test-calendar-sync--event-to-org-boundary-no-location-no-attendees ()
"Test event without location or attendees produces no drawer."
(let* ((start (test-calendar-sync-time-days-from-now 5 14 0))
(end (test-calendar-sync-time-days-from-now 5 15 0))
(event (list :summary "Simple Event"
:start start
:end end
:description "Just a note")))
(let ((result (calendar-sync--event-to-org event)))
(should (string-match-p "\\* Simple Event" result))
;; Description goes after timestamp, no drawer needed
(should (string-match-p "Just a note" result)))))
(ert-deftest test-calendar-sync--event-to-org-boundary-only-status ()
"Test event with only status produces drawer."
(let* ((start (test-calendar-sync-time-days-from-now 5 14 0))
(end (test-calendar-sync-time-days-from-now 5 15 0))
(event (list :summary "Status Only"
:start start
:end end
:status "tentative")))
(let ((result (calendar-sync--event-to-org event)))
(should (string-match-p ":PROPERTIES:" result))
(should (string-match-p ":STATUS: tentative" result))
(should (string-match-p ":END:" result)))))
(ert-deftest test-calendar-sync--event-to-org-boundary-description-with-asterisks ()
"Test event description containing org-special asterisks are sanitized."
(let* ((start (test-calendar-sync-time-days-from-now 5 14 0))
(end (test-calendar-sync-time-days-from-now 5 15 0))
(event (list :summary "Meeting"
:start start
:end end
:description "* agenda item 1\n** sub-item")))
(let ((result (calendar-sync--event-to-org event)))
;; Description content should be present
(should (string-match-p "agenda item" result))
;; Leading asterisks replaced with dashes to prevent org heading conflicts
(should (string-match-p "^- agenda item 1" result))
(should (string-match-p "^-- sub-item" result))
;; Only the event heading should use *
(should (= 1 (length (split-string result "^\\* " t)))))))
(ert-deftest test-calendar-sync--event-to-org-boundary-summary-structure-is-flattened ()
"Test event summary cannot create additional Org headings."
(let* ((start (test-calendar-sync-time-days-from-now 5 14 0))
(end (test-calendar-sync-time-days-from-now 5 15 0))
(event (list :summary "* Planning\n** Hidden task"
:start start
:end end)))
(let ((result (calendar-sync--event-to-org event)))
(should (string-match-p "^\\* - Planning -- Hidden task$" result))
(should-not (string-match-p "^\\*\\* Hidden task" result))
(should (= 1 (length (split-string result "^\\* " t)))))))
(ert-deftest test-calendar-sync--event-to-org-boundary-property-structure-is-flattened ()
"Test property values cannot create extra drawer lines or close the drawer."
(let* ((start (test-calendar-sync-time-days-from-now 5 14 0))
(end (test-calendar-sync-time-days-from-now 5 15 0))
(event (list :summary "Meeting"
:start start
:end end
:location "Room 1\n:END:\n* Not a real heading"
:organizer (list :cn "Jane\n:STATUS: fake"
:email "jane@example.com")
:status "accepted\n:LOCATION: fake"
:url "https://example.com/a\n:PROPERTIES:")))
(let ((result (calendar-sync--event-to-org event)))
(should (string-match-p ":LOCATION: Room 1 :END: \\* Not a real heading" result))
(should (string-match-p ":ORGANIZER: Jane :STATUS: fake" result))
(should (string-match-p ":STATUS: accepted :LOCATION: fake" result))
(should (string-match-p ":URL: https://example.com/a :PROPERTIES:" result))
(should (= 1 (test-calendar-sync--count-line-matches "^:LOCATION:" result)))
(should (= 1 (test-calendar-sync--count-line-matches "^:STATUS:" result)))
(should (= 1 (test-calendar-sync--count-line-matches "^:END:$" result)))
(should-not (string-match-p "^\\* Not a real heading" result)))))
(ert-deftest test-calendar-sync--event-to-org-boundary-organizer-email-only ()
"Test organizer without CN shows email."
(let* ((start (test-calendar-sync-time-days-from-now 5 14 0))
(end (test-calendar-sync-time-days-from-now 5 15 0))
(event (list :summary "Meeting"
:start start
:end end
:organizer (list :cn nil :email "org@example.com"))))
(let ((result (calendar-sync--event-to-org event)))
(should (string-match-p ":ORGANIZER: org@example.com" result)))))
;;; Error Cases
(ert-deftest test-calendar-sync--event-to-org-error-missing-summary ()
"Test event with nil summary still produces output."
(let* ((start (test-calendar-sync-time-days-from-now 5 14 0))
(end (test-calendar-sync-time-days-from-now 5 15 0))
(event (list :summary nil
:start start
:end end)))
;; Should not error - produce some output
(should (stringp (calendar-sync--event-to-org event)))))
(provide 'test-calendar-sync--event-to-org)
;;; test-calendar-sync--event-to-org.el ends here
|