aboutsummaryrefslogtreecommitdiff
path: root/tests/test-org-agenda-config-skip-functions.el
blob: aec1e71bea5848c2a0330d0c86fdb0c23c9b77f9 (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
;;; test-org-agenda-config-skip-functions.el --- Tests for org-agenda skip functions -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the agenda skip functions in org-agenda-config.el:
;; - cj/org-skip-subtree-if-habit
;; - cj/org-skip-subtree-if-priority
;; - cj/org-skip-subtree-if-keyword
;; - cj/org-agenda-skip-subtree-if-not-overdue
;;
;; Also covers the "d" command's SCHEDULE block config -- the skip-function
;; that filters CANCELLED entries from the forward-looking agenda.
;;
;; Uses dynamic timestamp generation (no hardcoded dates) via testutil-org.

;;; Code:

(require 'ert)
(require 'org)

(add-to-list 'load-path (expand-file-name "tests" user-emacs-directory))
(require 'testutil-org)

(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'org-agenda-config)

;; Suppress org-mode hooks that load packages unavailable in batch mode
(defmacro test-org-agenda--with-org-buffer (content &rest body)
  "Execute BODY in a temp org buffer with CONTENT, point at first heading.
Suppresses org-mode hooks to avoid loading packages not available in batch."
  (declare (indent 1))
  `(with-temp-buffer
     (let ((org-mode-hook nil)
           (text-mode-hook nil))
       (org-mode))
     (insert ,content)
     (goto-char (point-min))
     ,@body))

;;; ---------- cj/org-skip-subtree-if-habit ----------

;;; Normal Cases

(ert-deftest test-org-agenda-config-skip-habit-normal-habit-entry-skips ()
  "Entry with STYLE=habit should return subtree-end (skip)."
  (test-org-agenda--with-org-buffer
      (concat "* TODO Daily exercise\n"
              ":PROPERTIES:\n"
              ":STYLE: habit\n"
              ":END:\n")
    (should (integerp (cj/org-skip-subtree-if-habit)))))

(ert-deftest test-org-agenda-config-skip-habit-normal-non-habit-style-keeps ()
  "Entry with STYLE set to something other than habit should return nil (keep)."
  (test-org-agenda--with-org-buffer
      (concat "* TODO Regular task\n"
              ":PROPERTIES:\n"
              ":STYLE: other\n"
              ":END:\n")
    (should (null (cj/org-skip-subtree-if-habit)))))

(ert-deftest test-org-agenda-config-skip-habit-normal-no-style-property-keeps ()
  "Entry with no STYLE property should return nil (keep)."
  (test-org-agenda--with-org-buffer "* TODO Task without style\n"
    (should (null (cj/org-skip-subtree-if-habit)))))

;;; Boundary Cases

(ert-deftest test-org-agenda-config-skip-habit-boundary-returns-subtree-end-position ()
  "Return value should be the position after the entire subtree."
  (test-org-agenda--with-org-buffer
      (concat "* TODO Habit task\n"
              ":PROPERTIES:\n"
              ":STYLE: habit\n"
              ":END:\n"
              "** Sub-heading\n"
              "Some content\n"
              "* Next task\n")
    (let ((skip-pos (cj/org-skip-subtree-if-habit)))
      (should (integerp skip-pos))
      ;; Skip position should be past the sub-heading content
      (should (> skip-pos (point)))
      ;; But not past the next top-level heading
      (goto-char (point-min))
      (re-search-forward "^\\* Next task")
      (beginning-of-line)
      (should (<= skip-pos (point))))))

;;; ---------- cj/org-skip-subtree-if-priority ----------

;;; Normal Cases

(ert-deftest test-org-agenda-config-skip-priority-normal-matching-priority-skips ()
  "Entry with priority A should be skipped when filtering for A."
  (test-org-agenda--with-org-buffer "* TODO [#A] Important task\n"
    (should (integerp (cj/org-skip-subtree-if-priority ?A)))))

(ert-deftest test-org-agenda-config-skip-priority-normal-different-priority-keeps ()
  "Entry with priority B should not be skipped when filtering for A."
  (test-org-agenda--with-org-buffer "* TODO [#B] Normal task\n"
    (should (null (cj/org-skip-subtree-if-priority ?A)))))

(ert-deftest test-org-agenda-config-skip-priority-normal-no-priority-keeps ()
  "Entry with no priority cookie should not be skipped."
  (test-org-agenda--with-org-buffer "* TODO Plain task\n"
    (should (null (cj/org-skip-subtree-if-priority ?A)))))

(ert-deftest test-org-agenda-config-skip-priority-normal-filter-b-skips-b ()
  "Entry with priority B should be skipped when filtering for B."
  (test-org-agenda--with-org-buffer "* TODO [#B] Normal task\n"
    (should (integerp (cj/org-skip-subtree-if-priority ?B)))))

(ert-deftest test-org-agenda-config-skip-priority-normal-filter-c-skips-c ()
  "Entry with priority C should be skipped when filtering for C."
  (test-org-agenda--with-org-buffer "* TODO [#C] Low priority task\n"
    (should (integerp (cj/org-skip-subtree-if-priority ?C)))))

;;; ---------- cj/org-skip-subtree-if-keyword ----------

;;; Normal Cases

(ert-deftest test-org-agenda-config-skip-keyword-normal-matching-keyword-skips ()
  "Entry with TODO keyword in list should return subtree-end (skip)."
  (test-org-agenda--with-org-buffer "* TODO Some task\n"
    (should (integerp (cj/org-skip-subtree-if-keyword '("TODO"))))))

(ert-deftest test-org-agenda-config-skip-keyword-normal-done-in-list-skips ()
  "Entry with DONE keyword in list should return subtree-end (skip)."
  (test-org-agenda--with-org-buffer "* DONE Completed task\n"
    (should (integerp (cj/org-skip-subtree-if-keyword '("DONE"))))))

(ert-deftest test-org-agenda-config-skip-keyword-normal-keyword-not-in-list-keeps ()
  "Entry with keyword not in filter list should return nil (keep)."
  (test-org-agenda--with-org-buffer "* TODO Some task\n"
    (should (null (cj/org-skip-subtree-if-keyword '("DONE" "CANCELLED"))))))

(ert-deftest test-org-agenda-config-skip-keyword-normal-no-keyword-keeps ()
  "Entry with no TODO keyword should return nil (keep)."
  (test-org-agenda--with-org-buffer "* Just a heading\n"
    (should (null (cj/org-skip-subtree-if-keyword '("TODO" "DONE"))))))

;;; Boundary Cases

(ert-deftest test-org-agenda-config-skip-keyword-boundary-multiple-keywords-in-list ()
  "Filter list with multiple keywords should match any of them."
  (test-org-agenda--with-org-buffer "* DONE Finished task\n"
    (should (integerp (cj/org-skip-subtree-if-keyword '("TODO" "DONE" "CANCELLED"))))))

;;; ---------- cj/org-agenda-skip-subtree-if-not-overdue ----------

;;; Normal Cases

(ert-deftest test-org-agenda-config-skip-overdue-normal-past-scheduled-keeps ()
  "Entry scheduled in the past with TODO keyword is overdue — keep it."
  (test-org-agenda--with-org-buffer
      (concat "* TODO Overdue task\n"
              "SCHEDULED: " (test-org-timestamp-days-ago 7) "\n")
    (should (null (cj/org-agenda-skip-subtree-if-not-overdue)))))

(ert-deftest test-org-agenda-config-skip-overdue-normal-future-scheduled-skips ()
  "Entry scheduled in the future is not overdue — skip it."
  (test-org-agenda--with-org-buffer
      (concat "* TODO Future task\n"
              "SCHEDULED: " (test-org-timestamp-days-ahead 7) "\n")
    (should (integerp (cj/org-agenda-skip-subtree-if-not-overdue)))))

(ert-deftest test-org-agenda-config-skip-overdue-normal-past-deadline-keeps ()
  "Entry with past deadline and TODO keyword is overdue — keep it."
  (test-org-agenda--with-org-buffer
      (concat "* TODO Missed deadline\n"
              "DEADLINE: " (test-org-timestamp-days-ago 3) "\n")
    (should (null (cj/org-agenda-skip-subtree-if-not-overdue)))))

(ert-deftest test-org-agenda-config-skip-overdue-normal-done-task-skips ()
  "Done task should be skipped even if overdue."
  (test-org-agenda--with-org-buffer
      (concat "* DONE Completed task\n"
              "SCHEDULED: " (test-org-timestamp-days-ago 7) "\n")
    (should (integerp (cj/org-agenda-skip-subtree-if-not-overdue)))))

(ert-deftest test-org-agenda-config-skip-overdue-normal-habit-skips ()
  "Habit should be skipped even if overdue."
  (test-org-agenda--with-org-buffer
      (concat "* TODO Daily habit\n"
              "SCHEDULED: " (test-org-timestamp-days-ago 7) "\n"
              ":PROPERTIES:\n"
              ":STYLE: habit\n"
              ":END:\n")
    (should (integerp (cj/org-agenda-skip-subtree-if-not-overdue)))))

(ert-deftest test-org-agenda-config-skip-overdue-normal-no-todo-keyword-skips ()
  "Entry without a TODO keyword should be skipped."
  (test-org-agenda--with-org-buffer
      (concat "* Just a heading\n"
              "SCHEDULED: " (test-org-timestamp-days-ago 7) "\n")
    (should (integerp (cj/org-agenda-skip-subtree-if-not-overdue)))))

;;; Boundary Cases

(ert-deftest test-org-agenda-config-skip-overdue-boundary-today-scheduled-skips ()
  "Entry scheduled today is NOT overdue (not strictly before today) — skip."
  (test-org-agenda--with-org-buffer
      (concat "* TODO Today task\n"
              "SCHEDULED: " (test-org-timestamp-today) "\n")
    (should (integerp (cj/org-agenda-skip-subtree-if-not-overdue)))))

(ert-deftest test-org-agenda-config-skip-overdue-boundary-no-date-skips ()
  "Entry with TODO but no scheduled/deadline date — not overdue, skip."
  (test-org-agenda--with-org-buffer "* TODO Undated task\n"
    (should (integerp (cj/org-agenda-skip-subtree-if-not-overdue)))))

(ert-deftest test-org-agenda-config-skip-overdue-boundary-future-deadline-skips ()
  "Entry with future deadline is not overdue — skip."
  (test-org-agenda--with-org-buffer
      (concat "* TODO Future deadline\n"
              "DEADLINE: " (test-org-timestamp-days-ahead 14) "\n")
    (should (integerp (cj/org-agenda-skip-subtree-if-not-overdue)))))

;;; ---------- "d" command SCHEDULE block: CANCELLED skip ----------

;;; Normal Cases

(ert-deftest test-org-agenda-config-schedule-block-skips-cancelled ()
  "Normal: main-agenda \"d\" SCHEDULE block has a skip-function for CANCELLED.
This is the configuration check that locks in the fix for the
CANCELLED-in-schedule bug: without the skip-function form on the
(agenda ...) block, cancelled tasks render in the schedule view."
  (let* ((entry (assoc "d" org-agenda-custom-commands))
         (blocks (nth 2 entry))
         (agenda-block (seq-find (lambda (b) (eq (car b) 'agenda)) blocks))
         (opts (nth 2 agenda-block))
         (skip-form (cadr (assoc 'org-agenda-skip-function opts))))
    (should (equal skip-form
                   '(quote (org-agenda-skip-entry-if 'todo '("CANCELLED")))))))

;;; Boundary Cases

(ert-deftest test-org-agenda-config-schedule-cancelled-skip-scoped-to-agenda-block ()
  "Boundary: only the (agenda ...) block carries the CANCELLED skip.
The fix is deliberately scoped to the SCHEDULE block; the overdue/hi-pri/
priority-B blocks must not pick up the same skip-function form."
  (let* ((entry (assoc "d" org-agenda-custom-commands))
         (blocks (nth 2 entry))
         (non-agenda (seq-remove (lambda (b) (eq (car b) 'agenda)) blocks))
         (cancelled-form '(quote (org-agenda-skip-entry-if 'todo '("CANCELLED")))))
    (dolist (b non-agenda)
      (let* ((opts (nth 2 b))
             (skip (cadr (assoc 'org-agenda-skip-function opts))))
        (should-not (equal skip cancelled-form))))))

;;; ---------- prefix-format extracted into a defvar ----------

;;; Normal Cases

(ert-deftest test-org-agenda-config-prefix-format-extracted-to-defvar ()
  "Normal: every block of the \"d\" command references the shared prefix
format symbol rather than inlining the literal string.  Catches a
regression where one block diverges from the others on the format."
  (let* ((entry (assoc "d" org-agenda-custom-commands))
         (blocks (nth 2 entry)))
    (should (boundp 'cj/--main-agenda-prefix-format))
    (should (stringp cj/--main-agenda-prefix-format))
    (dolist (b blocks)
      (let* ((opts (nth 2 b))
             (fmt-form (cadr (assoc 'org-agenda-prefix-format opts))))
        (should (eq fmt-form 'cj/--main-agenda-prefix-format))))))

;;; ---------- VERIFICATION and IN-PROGRESS blocks ----------

;;; Normal Cases

(ert-deftest test-org-agenda-config-d-command-has-six-blocks-in-expected-order ()
  "Normal: the \"d\" command runs six blocks in the expected order --
OVERDUE -> HIGH PRIORITY -> VERIFICATION -> SCHEDULE -> IN-PROGRESS -> PRIORITY B."
  (let* ((entry (assoc "d" org-agenda-custom-commands))
         (blocks (nth 2 entry))
         (shapes (mapcar (lambda (b) (list (car b) (cadr b))) blocks)))
    (should (equal shapes
                   '((alltodo "")
                     (tags "PRIORITY=\"A\"")
                     (todo "VERIFY")
                     (agenda "")
                     (todo "DOING")
                     (alltodo ""))))))

(ert-deftest test-org-agenda-config-verify-block-options ()
  "Normal: the VERIFY block carries the VERIFICATION header, the shared
prefix format, and the habit skip function."
  (let* ((entry (assoc "d" org-agenda-custom-commands))
         (blocks (nth 2 entry))
         (verify (seq-find
                  (lambda (b) (and (eq (car b) 'todo)
                                   (equal (cadr b) "VERIFY")))
                  blocks))
         (opts (nth 2 verify)))
    (should verify)
    (should (eq (cadr (assoc 'org-agenda-overriding-header opts))
                'cj/main-agenda-verify-title))
    (should (equal cj/main-agenda-verify-title "VERIFICATION"))
    (should (eq (cadr (assoc 'org-agenda-prefix-format opts))
                'cj/--main-agenda-prefix-format))
    (should (equal (cadr (assoc 'org-agenda-skip-function opts))
                   ''cj/org-skip-subtree-if-habit))))

(ert-deftest test-org-agenda-config-doing-block-options ()
  "Normal: the DOING block carries the IN-PROGRESS header, the shared
prefix format, and the habit skip function."
  (let* ((entry (assoc "d" org-agenda-custom-commands))
         (blocks (nth 2 entry))
         (doing (seq-find
                 (lambda (b) (and (eq (car b) 'todo)
                                  (equal (cadr b) "DOING")))
                 blocks))
         (opts (nth 2 doing)))
    (should doing)
    (should (eq (cadr (assoc 'org-agenda-overriding-header opts))
                'cj/main-agenda-doing-title))
    (should (equal cj/main-agenda-doing-title "IN-PROGRESS"))
    (should (eq (cadr (assoc 'org-agenda-prefix-format opts))
                'cj/--main-agenda-prefix-format))
    (should (equal (cadr (assoc 'org-agenda-skip-function opts))
                   ''cj/org-skip-subtree-if-habit))))

;;; Boundary Cases

(ert-deftest test-org-agenda-config-verify-block-includes-scheduled-entries ()
  "Boundary: the VERIFY block has no scheduled/deadline skip, so a VERIFY
task with a scheduled date appears here as well as in the SCHEDULE block.
Mirrors the HIGH PRIORITY block's behaviour."
  (let* ((entry (assoc "d" org-agenda-custom-commands))
         (blocks (nth 2 entry))
         (verify (seq-find
                  (lambda (b) (and (eq (car b) 'todo)
                                   (equal (cadr b) "VERIFY")))
                  blocks))
         (opts (nth 2 verify))
         (skip (cadr (assoc 'org-agenda-skip-function opts))))
    ;; Single skip function (no compound `or' with scheduled/deadline).
    (should (equal skip ''cj/org-skip-subtree-if-habit))))

(ert-deftest test-org-agenda-config-doing-block-includes-scheduled-entries ()
  "Boundary: same contract as VERIFY -- no scheduled/deadline skip, so a
DOING task with a scheduled date appears in both blocks."
  (let* ((entry (assoc "d" org-agenda-custom-commands))
         (blocks (nth 2 entry))
         (doing (seq-find
                 (lambda (b) (and (eq (car b) 'todo)
                                  (equal (cadr b) "DOING")))
                 blocks))
         (opts (nth 2 doing))
         (skip (cadr (assoc 'org-agenda-skip-function opts))))
    (should (equal skip ''cj/org-skip-subtree-if-habit))))

(provide 'test-org-agenda-config-skip-functions)
;;; test-org-agenda-config-skip-functions.el ends here