blob: 0b01cbd92aa6bd3736e64a46a578680937ce7a7e (
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
|
;;; test-calendar-sync-properties.el --- Property-based tests for calendar-sync -*- lexical-binding: t; -*-
;;; Commentary:
;; Property-based tests for RRULE expansion functions.
;; These tests verify invariants hold across randomly generated inputs,
;; complementing the example-based tests in other test files.
;;
;; Each test runs multiple trials with random parameters to explore
;; the input space and find edge cases that example-based tests miss.
;;
;; Properties tested:
;; 1. COUNT always limits total occurrences
;; 2. UNTIL date bounds all occurrences
;; 3. BYDAY constrains weekly occurrences to specified weekdays
;; 4. INTERVAL creates correct spacing between occurrences
;; 5. All occurrences fall within the date range
;; 6. Expansion is deterministic (same inputs → same outputs)
;;; Code:
(require 'ert)
(require 'testutil-calendar-sync)
(require 'calendar-sync)
(defconst test-calendar-sync-property-trials 30
"Number of random trials to run for each property test.
Higher values give more confidence but slower tests.")
;;; Property 1: COUNT Ceiling
(ert-deftest test-calendar-sync-property-count-limits-daily ()
"Property: COUNT parameter limits daily occurrences.
For any COUNT value N, expansion never produces more than N occurrences."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((count (1+ (random 20)))
(start-date (test-calendar-sync-random-future-date))
(base-event (list :summary "Daily Test" :start start-date))
(rrule (list :freq 'daily :interval 1 :count count))
(range (test-calendar-sync-wide-range))
(occurrences (calendar-sync--expand-daily base-event rrule range)))
(should (<= (length occurrences) count)))))
(ert-deftest test-calendar-sync-property-count-limits-weekly ()
"Property: COUNT parameter limits weekly occurrences.
For any COUNT value N, expansion never produces more than N occurrences."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((count (1+ (random 20)))
(weekdays (test-calendar-sync-random-weekday-subset))
(start-date (test-calendar-sync-random-future-date))
(base-event (list :summary "Weekly Test" :start start-date))
(rrule (list :freq 'weekly :byday weekdays :interval 1 :count count))
(range (test-calendar-sync-wide-range))
(occurrences (calendar-sync--expand-weekly base-event rrule range)))
(should (<= (length occurrences) count)))))
(ert-deftest test-calendar-sync-property-count-limits-monthly ()
"Property: COUNT parameter limits monthly occurrences."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((count (1+ (random 15)))
(start-date (test-calendar-sync-random-future-date))
(base-event (list :summary "Monthly Test" :start start-date))
(rrule (list :freq 'monthly :interval 1 :count count))
(range (test-calendar-sync-wide-range))
(occurrences (calendar-sync--expand-monthly base-event rrule range)))
(should (<= (length occurrences) count)))))
(ert-deftest test-calendar-sync-property-count-limits-yearly ()
"Property: COUNT parameter limits yearly occurrences."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((count (1+ (random 5)))
(start-date (test-calendar-sync-random-future-date))
(base-event (list :summary "Yearly Test" :start start-date))
(rrule (list :freq 'yearly :interval 1 :count count))
(range (test-calendar-sync-wide-range))
(occurrences (calendar-sync--expand-yearly base-event rrule range)))
(should (<= (length occurrences) count)))))
;;; Property 2: UNTIL Boundary
;; These two asserted the wrong invariant until 2026-07-16: they required every
;; occurrence to fall strictly BEFORE UNTIL, which is the exclusive reading RFC
;; 5545 3.3.10 contradicts ("bounds the recurrence rule in an inclusive manner";
;; a UNTIL synchronized with the recurrence "becomes the last instance"). They
;; were written against the expansion loop's strict `before-date-p' guard and so
;; pinned the very defect that dropped the last instance of every bounded series.
;; The property is on-or-before; the upper bound is what UNTIL is for.
(ert-deftest test-calendar-sync-property-until-bounds-daily ()
"Property: no daily occurrence starts after the UNTIL date.
UNTIL is an inclusive bound (RFC 5545 3.3.10), so landing exactly on it is
correct and only a later date violates the property."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((start-date (test-calendar-sync-time-days-from-now 1 10 0))
(until-days (+ 10 (random 60)))
(until-date (test-calendar-sync-time-date-only until-days))
(base-event (list :summary "Until Test" :start start-date))
(rrule (list :freq 'daily :interval 1 :until until-date))
(range (test-calendar-sync-wide-range))
(occurrences (calendar-sync--expand-daily base-event rrule range)))
(dolist (occ occurrences)
(let ((occ-start (plist-get occ :start)))
(should (calendar-sync--date-on-or-before-p
(list (nth 0 occ-start) (nth 1 occ-start) (nth 2 occ-start))
until-date)))))))
(ert-deftest test-calendar-sync-property-until-bounds-daily-reaches-until ()
"Property: a daily series stepping by one day always reaches its UNTIL date.
With interval 1 the recurrence is synchronized with any UNTIL, so the last
instance must be UNTIL itself. This is the half the old exclusive property
could never have caught -- it only bounded from above, so silently dropping
the final occurrence satisfied it."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((start-date (test-calendar-sync-time-days-from-now 1 10 0))
(until-days (+ 10 (random 60)))
(until-date (test-calendar-sync-time-date-only until-days))
(base-event (list :summary "Until Test" :start start-date))
(rrule (list :freq 'daily :interval 1 :until until-date))
(range (test-calendar-sync-wide-range))
(occurrences (calendar-sync--expand-daily base-event rrule range))
(last-start (plist-get (car (last occurrences)) :start)))
(should occurrences)
(should (equal (seq-take last-start 3) until-date)))))
(ert-deftest test-calendar-sync-property-until-bounds-weekly ()
"Property: no weekly occurrence starts after the UNTIL date.
UNTIL is an inclusive bound (RFC 5545 3.3.10), so landing exactly on it is
correct and only a later date violates the property."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((start-date (test-calendar-sync-time-days-from-now 1 10 0))
(until-days (+ 14 (random 60)))
(until-date (test-calendar-sync-time-date-only until-days))
(weekdays (test-calendar-sync-random-weekday-subset))
(base-event (list :summary "Until Test" :start start-date))
(rrule (list :freq 'weekly :byday weekdays :interval 1 :until until-date))
(range (test-calendar-sync-wide-range))
(occurrences (calendar-sync--expand-weekly base-event rrule range)))
(dolist (occ occurrences)
(let ((occ-start (plist-get occ :start)))
(should (calendar-sync--date-on-or-before-p
(list (nth 0 occ-start) (nth 1 occ-start) (nth 2 occ-start))
until-date)))))))
;;; Property 3: BYDAY Constraint
(ert-deftest test-calendar-sync-property-byday-constrains-weekdays ()
"Property: Weekly occurrences only fall on BYDAY weekdays.
Every generated occurrence must be on one of the specified weekdays."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((weekdays (test-calendar-sync-random-weekday-subset))
(weekday-nums (mapcar #'calendar-sync--weekday-to-number weekdays))
(start-date (test-calendar-sync-random-future-date))
(base-event (list :summary "BYDAY Test" :start start-date))
(rrule (list :freq 'weekly :byday weekdays :interval 1))
(range (test-calendar-sync-narrow-range))
(occurrences (calendar-sync--expand-weekly base-event rrule range)))
(dolist (occ occurrences)
(let* ((occ-start (plist-get occ :start))
(occ-weekday (calendar-sync--date-weekday
(list (nth 0 occ-start) (nth 1 occ-start) (nth 2 occ-start)))))
(should (member occ-weekday weekday-nums)))))))
;;; Property 4: INTERVAL Spacing
(ert-deftest test-calendar-sync-property-interval-spacing-daily ()
"Property: Daily occurrences are spaced INTERVAL days apart.
Consecutive occurrences should be exactly INTERVAL days apart."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((interval (1+ (random 5)))
(start-date (test-calendar-sync-time-days-from-now 1 10 0))
(base-event (list :summary "Interval Test" :start start-date))
(rrule (list :freq 'daily :interval interval :count 10))
(range (test-calendar-sync-wide-range))
(occurrences (calendar-sync--expand-daily base-event rrule range)))
(when (> (length occurrences) 1)
(let ((dates (mapcar (lambda (o) (plist-get o :start)) occurrences)))
(cl-loop for i from 0 below (1- (length dates))
for d1 = (nth i dates)
for d2 = (nth (1+ i) dates)
do (let ((gap (round (test-calendar-sync-days-between d1 d2))))
(should (= interval gap)))))))))
(ert-deftest test-calendar-sync-property-interval-spacing-weekly-single-day ()
"Property: Weekly single-day occurrences are spaced INTERVAL weeks apart."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((interval (1+ (random 3)))
(weekday (nth (random 7) '("MO" "TU" "WE" "TH" "FR" "SA" "SU")))
(start-date (test-calendar-sync-time-days-from-now 1 10 0))
(base-event (list :summary "Weekly Interval Test" :start start-date))
(rrule (list :freq 'weekly :byday (list weekday) :interval interval :count 8))
(range (test-calendar-sync-wide-range))
(occurrences (calendar-sync--expand-weekly base-event rrule range)))
(when (> (length occurrences) 1)
(let ((dates (mapcar (lambda (o) (plist-get o :start)) occurrences)))
(cl-loop for i from 0 below (1- (length dates))
for d1 = (nth i dates)
for d2 = (nth (1+ i) dates)
do (let ((gap (round (test-calendar-sync-days-between d1 d2))))
(should (= (* 7 interval) gap)))))))))
;;; Property 5: Range Containment
(ert-deftest test-calendar-sync-property-occurrences-within-range ()
"Property: All occurrences fall within the date range.
No occurrence should be before range start or after range end."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((range-start-days (random 30))
(range-end-days (+ range-start-days 30 (random 60)))
(range (list (time-add (current-time) (* range-start-days 86400))
(time-add (current-time) (* range-end-days 86400))))
(start-date (test-calendar-sync-time-days-from-now (1+ range-start-days) 10 0))
(base-event (list :summary "Range Test" :start start-date))
(rrule (list :freq 'daily :interval 1))
(occurrences (calendar-sync--expand-daily base-event rrule range)))
(dolist (occ occurrences)
(let ((occ-start (plist-get occ :start)))
(should (calendar-sync--date-in-range-p occ-start range)))))))
(ert-deftest test-calendar-sync-property-weekly-occurrences-within-range ()
"Property: All weekly occurrences fall within the date range."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((range (test-calendar-sync-narrow-range))
(start-date (test-calendar-sync-time-days-from-now 1 10 0))
(weekdays (test-calendar-sync-random-weekday-subset))
(base-event (list :summary "Range Test" :start start-date))
(rrule (list :freq 'weekly :byday weekdays :interval 1))
(occurrences (calendar-sync--expand-weekly base-event rrule range)))
(dolist (occ occurrences)
(let ((occ-start (plist-get occ :start)))
(should (calendar-sync--date-in-range-p occ-start range)))))))
;;; Property 6: Determinism
(ert-deftest test-calendar-sync-property-expansion-deterministic-daily ()
"Property: Same inputs produce identical outputs for daily expansion."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((interval (1+ (random 3)))
(count (+ 5 (random 10)))
(start-date (test-calendar-sync-time-days-from-now 1 10 0))
(base-event (list :summary "Determinism Test" :start start-date))
(rrule (list :freq 'daily :interval interval :count count))
(range (test-calendar-sync-wide-range))
(result1 (calendar-sync--expand-daily base-event rrule range))
(result2 (calendar-sync--expand-daily base-event rrule range)))
(should (= (length result1) (length result2)))
(cl-loop for o1 in result1
for o2 in result2
do (should (equal (plist-get o1 :start) (plist-get o2 :start)))))))
(ert-deftest test-calendar-sync-property-expansion-deterministic-weekly ()
"Property: Same inputs produce identical outputs for weekly expansion."
(dotimes (_ test-calendar-sync-property-trials)
(let* ((interval (1+ (random 2)))
(weekdays (test-calendar-sync-random-weekday-subset))
(count (+ 5 (random 10)))
(start-date (test-calendar-sync-time-days-from-now 1 10 0))
(base-event (list :summary "Determinism Test" :start start-date))
(rrule (list :freq 'weekly :byday weekdays :interval interval :count count))
(range (test-calendar-sync-wide-range))
(result1 (calendar-sync--expand-weekly base-event rrule range))
(result2 (calendar-sync--expand-weekly base-event rrule range)))
(should (= (length result1) (length result2)))
(cl-loop for o1 in result1
for o2 in result2
do (should (equal (plist-get o1 :start) (plist-get o2 :start)))))))
(provide 'test-calendar-sync-properties)
;;; test-calendar-sync-properties.el ends here
|