summaryrefslogtreecommitdiff
path: root/tests/test-org-sort-by-todo-and-priority.el
blob: 873f37c27bf5f13c496a77811ba95c025a423ae9 (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
;;; test-org-sort-by-todo-and-priority.el --- Tests for cj/org-sort-by-todo-and-priority -*- lexical-binding: t; -*-

;;; Commentary:

;; Unit tests for cj/org-sort-by-todo-and-priority function.
;; Tests multi-level sorting: TODO status (TODO before DONE) and priority (A before B before C).
;;
;; Testing approach:
;; - Use real org-mode buffers (don't mock org-sort-entries)
;; - Trust org-mode framework works correctly
;; - Test OUR integration logic: calling org-sort-entries twice in correct order
;; - Verify final sort order matches expected TODO/priority combination
;;
;; The function uses stable sorting:
;; 1. First sort by priority (A, B, C, D, none)
;; 2. Then sort by TODO status (TODO before DONE)
;; Result: Priority order preserved within each TODO state group

;;; Code:

(require 'ert)
(require 'org)
(require 'org-config)  ; Defines cj/org-sort-by-todo-and-priority

;;; Test Helpers

(defun test-org-sort-by-todo-and-priority--create-buffer (content)
  "Create a temporary org-mode buffer with CONTENT.
Returns the buffer object.
Disables org-mode hooks to avoid missing package dependencies in batch mode."
  (let ((buf (generate-new-buffer "*test-org-sort*")))
    (with-current-buffer buf
      ;; Disable hooks to prevent org-superstar and other package loads
      (let ((org-mode-hook nil))
        (org-mode))
      (insert content)
      (goto-char (point-min)))
    buf))

(defun test-org-sort-by-todo-and-priority--get-entry-order (buffer)
  "Extract ordered list of TODO states and priorities from BUFFER.
Returns list of strings like \"TODO [#A]\" or \"DONE\" for each heading."
  (with-current-buffer buffer
    (goto-char (point-min))
    (let (entries)
      (org-map-entries
       (lambda ()
         (let* ((todo-state (org-get-todo-state))
                ;; Get heading: no-tags, no-todo, KEEP priority, no-comment
                (heading (org-get-heading t t nil t))
                ;; Extract priority cookie from heading text
                (priority (when (string-match "\\[#\\([A-Z]\\)\\]" heading)
                           (match-string 1 heading))))
           (push (if priority
                     (format "%s [#%s]" (or todo-state "") priority)
                   (or todo-state ""))
                 entries)))
       nil 'tree)
      (nreverse entries))))

(defun test-org-sort-by-todo-and-priority--sort-children (buffer)
  "Position cursor on parent heading in BUFFER and sort its children.
Moves to first * heading (Parent) and calls sort function to sort children."
  (with-current-buffer buffer
    (goto-char (point-min))
    (when (re-search-forward "^\\* " nil t)
      (beginning-of-line)
      (cj/org-sort-by-todo-and-priority))))

;;; Normal Cases

(ert-deftest test-org-sort-by-todo-and-priority-normal-mixed-todo-done-sorts-correctly ()
  "Test mixed TODO and DONE entries with various priorities sort correctly.

Input: TODO [#A], DONE [#B], TODO [#C], DONE [#A]
Expected: TODO [#A], TODO [#C], DONE [#A], DONE [#B]"
  (let* ((content "* Parent
** TODO [#A] First task
** DONE [#B] Second task
** TODO [#C] Third task
** DONE [#A] Fourth task
")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (progn
          (test-org-sort-by-todo-and-priority--sort-children buf)
          (let ((order (test-org-sort-by-todo-and-priority--get-entry-order buf)))
            (should (equal order '("" "TODO [#A]" "TODO [#C]" "DONE [#A]" "DONE [#B]")))))
      (kill-buffer buf))))

(ert-deftest test-org-sort-by-todo-and-priority-normal-multiple-todos-sorts-by-priority ()
  "Test multiple TODO entries sort by priority A before B before C.

Input: TODO [#C], TODO [#A], TODO [#B]
Expected: TODO [#A], TODO [#B], TODO [#C]"
  (let* ((content "* Parent
** TODO [#C] Task C
** TODO [#A] Task A
** TODO [#B] Task B
")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (progn
          (test-org-sort-by-todo-and-priority--sort-children buf)
          (let ((order (test-org-sort-by-todo-and-priority--get-entry-order buf)))
            (should (equal order '("" "TODO [#A]" "TODO [#B]" "TODO [#C]")))))
      (kill-buffer buf))))

(ert-deftest test-org-sort-by-todo-and-priority-normal-multiple-dones-sorts-by-priority ()
  "Test multiple DONE entries sort by priority A before B before C.

Input: DONE [#C], DONE [#A], DONE [#B]
Expected: DONE [#A], DONE [#B], DONE [#C]"
  (let* ((content "* Parent
** DONE [#C] Done C
** DONE [#A] Done A
** DONE [#B] Done B
")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (progn
          (test-org-sort-by-todo-and-priority--sort-children buf)
          (let ((order (test-org-sort-by-todo-and-priority--get-entry-order buf)))
            (should (equal order '("" "DONE [#A]" "DONE [#B]" "DONE [#C]")))))
      (kill-buffer buf))))

(ert-deftest test-org-sort-by-todo-and-priority-normal-same-priority-todo-before-done ()
  "Test entries with same priority sort TODO before DONE.

Input: DONE [#A], TODO [#A]
Expected: TODO [#A], DONE [#A]"
  (let* ((content "* Parent
** DONE [#A] Done task
** TODO [#A] Todo task
")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (progn
          (test-org-sort-by-todo-and-priority--sort-children buf)
          (let ((order (test-org-sort-by-todo-and-priority--get-entry-order buf)))
            (should (equal order '("" "TODO [#A]" "DONE [#A]")))))
      (kill-buffer buf))))

;;; Boundary Cases

(ert-deftest test-org-sort-by-todo-and-priority-boundary-empty-section-no-error ()
  "Test sorting empty section does not signal error.

Input: Heading with no children
Expected: No error, no change"
  (let* ((content "* Parent\n")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (with-current-buffer buf
          (goto-char (point-min))
          (should-not (condition-case err
                          (progn
                            (cj/org-sort-by-todo-and-priority)
                            nil)
                        (error err))))
      (kill-buffer buf))))

(ert-deftest test-org-sort-by-todo-and-priority-boundary-single-todo-no-change ()
  "Test sorting single TODO entry does not change order.

Input: Single TODO [#A]
Expected: Same order (no change)"
  (let* ((content "* Parent
** TODO [#A] Only task
")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (progn
          (test-org-sort-by-todo-and-priority--sort-children buf)
          (let ((order (test-org-sort-by-todo-and-priority--get-entry-order buf)))
            (should (equal order '("" "TODO [#A]")))))
      (kill-buffer buf))))

(ert-deftest test-org-sort-by-todo-and-priority-boundary-single-done-no-change ()
  "Test sorting single DONE entry does not change order.

Input: Single DONE [#B]
Expected: Same order (no change)"
  (let* ((content "* Parent
** DONE [#B] Only task
")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (progn
          (test-org-sort-by-todo-and-priority--sort-children buf)
          (let ((order (test-org-sort-by-todo-and-priority--get-entry-order buf)))
            (should (equal order '("" "DONE [#B]")))))
      (kill-buffer buf))))

(ert-deftest test-org-sort-by-todo-and-priority-boundary-all-todos-sorts-by-priority ()
  "Test all TODO entries sort by priority only.

Input: TODO [#C], TODO [#A], TODO [#B]
Expected: TODO [#A], TODO [#B], TODO [#C]"
  (let* ((content "* Parent
** TODO [#C] Task C
** TODO [#A] Task A
** TODO [#B] Task B
")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (progn
          (test-org-sort-by-todo-and-priority--sort-children buf)
          (let ((order (test-org-sort-by-todo-and-priority--get-entry-order buf)))
            (should (equal order '("" "TODO [#A]" "TODO [#B]" "TODO [#C]")))))
      (kill-buffer buf))))

(ert-deftest test-org-sort-by-todo-and-priority-boundary-all-dones-sorts-by-priority ()
  "Test all DONE entries sort by priority only.

Input: DONE [#B], DONE [#D], DONE [#A]
Expected: DONE [#A], DONE [#B], DONE [#D]"
  (let* ((content "* Parent
** DONE [#B] Done B
** DONE [#D] Done D
** DONE [#A] Done A
")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (progn
          (test-org-sort-by-todo-and-priority--sort-children buf)
          (let ((order (test-org-sort-by-todo-and-priority--get-entry-order buf)))
            (should (equal order '("" "DONE [#A]" "DONE [#B]" "DONE [#D]")))))
      (kill-buffer buf))))

(ert-deftest test-org-sort-by-todo-and-priority-boundary-no-priorities-sorts-by-todo ()
  "Test entries without priorities sort by TODO status only.

Input: TODO (no priority), DONE (no priority), TODO (no priority)
Expected: TODO, TODO, DONE"
  (let* ((content "* Parent
** TODO Task 1
** DONE Task 2
** TODO Task 3
")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (progn
          (test-org-sort-by-todo-and-priority--sort-children buf)
          (let ((order (test-org-sort-by-todo-and-priority--get-entry-order buf)))
            (should (equal order '("" "TODO" "TODO" "DONE")))))
      (kill-buffer buf))))

(ert-deftest test-org-sort-by-todo-and-priority-boundary-unprioritized-after-prioritized ()
  "Test unprioritized entries appear after prioritized within TODO/DONE groups.

Input: TODO (no priority), TODO [#A], DONE [#B], DONE (no priority)
Expected: TODO [#A], TODO (no priority), DONE [#B], DONE (no priority)"
  (let* ((content "* Parent
** TODO Task no priority
** TODO [#A] Task A
** DONE [#B] Done B
** DONE Done no priority
")
         (buf (test-org-sort-by-todo-and-priority--create-buffer content)))
    (unwind-protect
        (progn
          (test-org-sort-by-todo-and-priority--sort-children buf)
          (let ((order (test-org-sort-by-todo-and-priority--get-entry-order buf)))
            (should (equal order '("" "TODO [#A]" "TODO" "DONE [#B]" "DONE")))))
      (kill-buffer buf))))

;;; Error Cases

(ert-deftest test-org-sort-by-todo-and-priority-error-non-org-buffer-signals-error ()
  "Test calling in non-org-mode buffer signals user-error.

Input: fundamental-mode buffer
Expected: user-error"
  (let ((buf (generate-new-buffer "*test-non-org*")))
    (unwind-protect
        (with-current-buffer buf
          (fundamental-mode)
          (should-error (cj/org-sort-by-todo-and-priority) :type 'user-error))
      (kill-buffer buf))))

(provide 'test-org-sort-by-todo-and-priority)
;;; test-org-sort-by-todo-and-priority.el ends here