summaryrefslogtreecommitdiff
path: root/tests/test-chime-apply-blacklist.el
blob: 6f785efaaab1934a10388dbe9300b114ec73cb89 (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
;;; test-chime-apply-blacklist.el --- Tests for chime--apply-blacklist -*- lexical-binding: t; -*-

;; Copyright (C) 2024 Craig Jennings

;; Author: Craig Jennings <c@cjennings.net>

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Unit tests for chime--apply-blacklist function.
;; Tests cover normal cases, boundary cases, and error cases.

;;; Code:

;; Initialize package system for batch mode
(when noninteractive
  (package-initialize))

(require 'ert)

;; Load dependencies required by chime
(require 'dash)
(require 'alert)
(require 'async)
(require 'org-agenda)

;; Load chime from parent directory
(load (expand-file-name "../chime.el") nil t)

;; Load test utilities
(require 'testutil-general (expand-file-name "testutil-general.el"))

;;; Setup and Teardown

(defun test-chime-apply-blacklist-setup ()
  "Setup function run before each test."
  (chime-create-test-base-dir)
  ;; Reset blacklist settings
  (setq chime-keyword-blacklist nil)
  (setq chime-tags-blacklist nil)
  (setq chime-predicate-blacklist nil))

(defun test-chime-apply-blacklist-teardown ()
  "Teardown function run after each test."
  (chime-delete-test-base-dir)
  (setq chime-keyword-blacklist nil)
  (setq chime-tags-blacklist nil)
  (setq chime-predicate-blacklist nil))

;;; Normal Cases

(ert-deftest test-chime-apply-blacklist-nil-blacklist-returns-all-markers ()
  "Test that nil blacklist returns all markers unchanged."
  (test-chime-apply-blacklist-setup)
  (unwind-protect
      (let* ((chime-keyword-blacklist nil)
             (chime-tags-blacklist nil)
             (markers (list (make-marker) (make-marker) (make-marker)))
             (result (chime--apply-blacklist markers)))
        ;; Should return all markers when blacklist is nil
        (should (equal (length result) 3)))
    (test-chime-apply-blacklist-teardown)))

(ert-deftest test-chime-apply-blacklist-keyword-blacklist-filters-correctly ()
  "Test that keyword blacklist filters out markers correctly."
  (test-chime-apply-blacklist-setup)
  (unwind-protect
      (with-temp-buffer
        (org-mode)
        (insert "* TODO Task 1\n")
        (let ((marker1 (copy-marker (point))))
          (insert "* DONE Task 2\n")
          (let ((marker2 (copy-marker (point))))
            (insert "* TODO Task 3\n")
            (let ((marker3 (copy-marker (point)))
                  (chime-keyword-blacklist '("DONE")))
              ;; Mock org-entry-get to return appropriate keywords
              (cl-letf (((symbol-function 'org-entry-get)
                         (lambda (pom property &optional inherit literal-nil)
                           (when (equal property "TODO")
                             (cond
                              ((equal pom marker1) "TODO")
                              ((equal pom marker2) "DONE")
                              ((equal pom marker3) "TODO")
                              (t nil))))))
                (let ((result (chime--apply-blacklist (list marker1 marker2 marker3))))
                  ;; Should filter out DONE marker
                  (should (= (length result) 2))
                  (should (member marker1 result))
                  (should-not (member marker2 result))
                  (should (member marker3 result))))))))
    (test-chime-apply-blacklist-teardown)))

(ert-deftest test-chime-apply-blacklist-tags-blacklist-filters-correctly ()
  "Test that tags blacklist filters out markers correctly."
  (test-chime-apply-blacklist-setup)
  (unwind-protect
      (with-temp-buffer
        (org-mode)
        (insert "* Task 1\n")
        (let ((marker1 (copy-marker (point))))
          (insert "* Task 2\n")
          (let ((marker2 (copy-marker (point))))
            (insert "* Task 3\n")
            (let ((marker3 (copy-marker (point)))
                  (chime-tags-blacklist '("personal")))
              ;; Mock chime--get-tags to return appropriate tags
              (cl-letf (((symbol-function 'chime--get-tags)
                         (lambda (pom)
                           (cond
                            ((equal pom marker1) '("work" "urgent"))
                            ((equal pom marker2) '("personal"))
                            ((equal pom marker3) '("work"))
                            (t nil)))))
                (let ((result (chime--apply-blacklist (list marker1 marker2 marker3))))
                  ;; Should filter out marker with "personal" tag
                  (should (= (length result) 2))
                  (should (member marker1 result))
                  (should-not (member marker2 result))
                  (should (member marker3 result))))))))
    (test-chime-apply-blacklist-teardown)))

(ert-deftest test-chime-apply-blacklist-keyword-and-tags-blacklist-uses-or-logic ()
  "Test that both keyword and tags blacklists use OR logic."
  (test-chime-apply-blacklist-setup)
  (unwind-protect
      (with-temp-buffer
        (org-mode)
        (insert "* TODO Task 1\n")
        (let ((marker1 (copy-marker (point))))
          (insert "* DONE Task 2\n")
          (let ((marker2 (copy-marker (point))))
            (insert "* NEXT Task 3\n")
            (let ((marker3 (copy-marker (point)))
                  (chime-keyword-blacklist '("DONE"))
                  (chime-tags-blacklist '("archive")))
              ;; Mock functions
              (cl-letf (((symbol-function 'org-entry-get)
                         (lambda (pom property &optional inherit literal-nil)
                           (when (equal property "TODO")
                             (cond
                              ((equal pom marker1) "TODO")
                              ((equal pom marker2) "DONE")
                              ((equal pom marker3) "NEXT")
                              (t nil)))))
                        ((symbol-function 'chime--get-tags)
                         (lambda (pom)
                           (cond
                            ((equal pom marker1) nil)
                            ((equal pom marker2) nil)
                            ((equal pom marker3) '("archive"))
                            (t nil)))))
                (let ((result (chime--apply-blacklist (list marker1 marker2 marker3))))
                  ;; Should filter out marker2 (DONE) and marker3 (archive tag)
                  (should (= (length result) 1))
                  (should (member marker1 result))
                  (should-not (member marker2 result))
                  (should-not (member marker3 result))))))))
    (test-chime-apply-blacklist-teardown)))

(ert-deftest test-chime-apply-blacklist-multiple-keywords-filters-all ()
  "Test that multiple keywords in blacklist filters all matching."
  (test-chime-apply-blacklist-setup)
  (unwind-protect
      (with-temp-buffer
        (org-mode)
        (insert "* TODO Task 1\n")
        (let ((marker1 (copy-marker (point))))
          (insert "* DONE Task 2\n")
          (let ((marker2 (copy-marker (point))))
            (insert "* CANCELLED Task 3\n")
            (let ((marker3 (copy-marker (point)))
                  (chime-keyword-blacklist '("DONE" "CANCELLED")))
              (cl-letf (((symbol-function 'org-entry-get)
                         (lambda (pom property &optional inherit literal-nil)
                           (when (equal property "TODO")
                             (cond
                              ((equal pom marker1) "TODO")
                              ((equal pom marker2) "DONE")
                              ((equal pom marker3) "CANCELLED")
                              (t nil))))))
                (let ((result (chime--apply-blacklist (list marker1 marker2 marker3))))
                  ;; Should only keep TODO marker
                  (should (= (length result) 1))
                  (should (member marker1 result))))))))
    (test-chime-apply-blacklist-teardown)))

;;; Boundary Cases

(ert-deftest test-chime-apply-blacklist-empty-markers-list-returns-empty ()
  "Test that empty markers list returns empty."
  (test-chime-apply-blacklist-setup)
  (unwind-protect
      (let ((chime-keyword-blacklist '("DONE"))
            (result (chime--apply-blacklist '())))
        (should (equal result '())))
    (test-chime-apply-blacklist-teardown)))

(ert-deftest test-chime-apply-blacklist-single-item-blacklist-works ()
  "Test that single-item blacklist works correctly."
  (test-chime-apply-blacklist-setup)
  (unwind-protect
      (with-temp-buffer
        (org-mode)
        (insert "* TODO Task 1\n")
        (let ((marker1 (copy-marker (point))))
          (insert "* DONE Task 2\n")
          (let ((marker2 (copy-marker (point)))
                (chime-keyword-blacklist '("DONE")))
            (cl-letf (((symbol-function 'org-entry-get)
                       (lambda (pom property &optional inherit literal-nil)
                         (when (equal property "TODO")
                           (cond
                            ((equal pom marker1) "TODO")
                            ((equal pom marker2) "DONE")
                            (t nil))))))
              (let ((result (chime--apply-blacklist (list marker1 marker2))))
                (should (= (length result) 1))
                (should (member marker1 result)))))))
    (test-chime-apply-blacklist-teardown)))

(ert-deftest test-chime-apply-blacklist-all-markers-blacklisted-returns-empty ()
  "Test that blacklisting all markers returns empty list."
  (test-chime-apply-blacklist-setup)
  (unwind-protect
      (with-temp-buffer
        (org-mode)
        (insert "* DONE Task 1\n")
        (let ((marker1 (copy-marker (point))))
          (insert "* DONE Task 2\n")
          (let ((marker2 (copy-marker (point)))
                (chime-keyword-blacklist '("DONE")))
            (cl-letf (((symbol-function 'org-entry-get)
                       (lambda (pom property &optional inherit literal-nil)
                         (when (equal property "TODO")
                           (cond
                            ((equal pom marker1) "DONE")
                            ((equal pom marker2) "DONE")
                            (t nil))))))
              (let ((result (chime--apply-blacklist (list marker1 marker2))))
                (should (equal result '())))))))
    (test-chime-apply-blacklist-teardown)))

;;; Error Cases

(ert-deftest test-chime-apply-blacklist-handles-nil-keyword-gracefully ()
  "Test that nil keyword in marker is handled gracefully."
  (test-chime-apply-blacklist-setup)
  (unwind-protect
      (with-temp-buffer
        (org-mode)
        (insert "* Entry without TODO keyword\n")
        (let ((marker1 (copy-marker (point)))
              (chime-keyword-blacklist '("DONE")))
          (cl-letf (((symbol-function 'org-entry-get)
                     (lambda (pom property &optional inherit literal-nil)
                       nil)))
            (let ((result (chime--apply-blacklist (list marker1))))
              ;; Should keep marker with nil keyword (not in blacklist)
              (should (= (length result) 1))))))
    (test-chime-apply-blacklist-teardown)))

(ert-deftest test-chime-apply-blacklist-handles-nil-tags-gracefully ()
  "Test that nil tags in marker is handled gracefully."
  (test-chime-apply-blacklist-setup)
  (unwind-protect
      (with-temp-buffer
        (org-mode)
        (insert "* Entry without tags\n")
        (let ((marker1 (copy-marker (point)))
              (chime-tags-blacklist '("archive")))
          (cl-letf (((symbol-function 'chime--get-tags)
                     (lambda (pom) nil)))
            (let ((result (chime--apply-blacklist (list marker1))))
              ;; Should keep marker with nil tags (not in blacklist)
              (should (= (length result) 1))))))
    (test-chime-apply-blacklist-teardown)))

(provide 'test-chime-apply-blacklist)
;;; test-chime-apply-blacklist.el ends here