summaryrefslogtreecommitdiff
path: root/tests/test-org-agenda-config-skip-functions.el
blob: 24eeb7e4a22702a2539f29fbed87b436bbc21631 (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
;;; 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
;;
;; 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)))))

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