aboutsummaryrefslogtreecommitdiff
path: root/tests/test-pearl-output.el
blob: e3208e3fd2cc7069339c09bd3a67f08643fa8854 (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
;;; test-pearl-output.el --- Tests for the active-file output model -*- lexical-binding: t; -*-

;; Copyright (C) 2026 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:

;; Tests for the active-file output model: the filter summary, the
;; source-tracking header (with the affordance preamble) written by
;; `--build-org-content', reading the active source back from a buffer, and
;; `pearl-refresh-current-view' re-running the recorded source.

;;; Code:

(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))
(require 'cl-lib)

;;; --summarize-filter

(ert-deftest test-pearl-summarize-filter-fields ()
  "A filter plist summarizes its set dimensions in a readable string."
  (let ((s (pearl--summarize-filter '(:assignee :me :open t :state "In Progress"))))
    (should (string-match-p "assignee" s))
    (should (string-match-p "open" s))
    (should (string-match-p "In Progress" s))))

(ert-deftest test-pearl-summarize-filter-empty ()
  "An empty filter summarizes as all issues."
  (should (string-match-p "all" (pearl--summarize-filter nil))))

;;; --build-org-content with a source

(ert-deftest test-pearl-build-org-content-source-header ()
  "With a source, the header carries the title, serialized source, and count."
  (let* ((source '(:type filter :name "My open issues" :filter (:assignee :me :open t)))
         (out (pearl--build-org-content '() source)))
    (should (string-match-p "^#\\+title: Linear — My open issues$" out))
    (should (string-match-p "^#\\+LINEAR-SOURCE: " out))
    (should (string-match-p "^#\\+LINEAR-COUNT: 0$" out))
    ;; affordance preamble is present as org comments, not content -- match
    ;; the save cue loosely so a future rephrase doesn't break the test
    (should (string-match-p "^# .*save" out))))

(ert-deftest test-pearl-build-org-content-source-roundtrips ()
  "The serialized source in the header reads back to the original plist."
  (let* ((source '(:type filter :name "Bugs" :filter (:labels ("bug") :open t)))
         (out (pearl--build-org-content '() source)))
    (with-temp-buffer
      (insert out)
      (should (equal source (pearl--read-active-source))))))

(ert-deftest test-pearl-build-org-content-default-source-back-compat ()
  "Called with no source, the content still has a title and no entries."
  (let ((out (pearl--build-org-content '())))
    (should (string-match-p "^#\\+title:" out))
    (should-not (string-match-p "^\\*\\*\\* " out))))

(ert-deftest test-pearl-build-org-content-title-uses-view-name-verbatim ()
  "The view name in the file title matches Linear's capitalization verbatim,
independent of `pearl-title-case-headings' (which governs issue headings only)."
  (let ((case-fold-search nil)             ; strict so casing actually matters
        (source '(:type filter :name "my open bugs" :filter nil)))
    (let ((pearl-title-case-headings t))
      (should (string-match-p "^#\\+title: Linear — my open bugs$"
                              (pearl--build-org-content '() source))))
    (let ((pearl-title-case-headings nil))
      (should (string-match-p "^#\\+title: Linear — my open bugs$"
                              (pearl--build-org-content '() source))))))

;;; --read-active-source

(ert-deftest test-pearl-read-active-source-absent ()
  "A buffer with no source header reads back nil."
  (with-temp-buffer
    (insert "#+title: something\n\n* a heading\n")
    (should-not (pearl--read-active-source))))

;;; refresh-current-view

(ert-deftest test-pearl-refresh-current-view-reruns-source ()
  "Refresh reads the recorded filter source and merges the re-run result."
  (let ((ran nil) (merged-source nil)
        (source '(:type filter :name "My open issues" :filter (:assignee :me :open t))))
    (with-temp-buffer
      (insert (format "#+title: Linear — My open issues\n#+LINEAR-SOURCE: %s\n\n"
                      (prin1-to-string source)))
      (org-mode)
      (cl-letf (((symbol-function 'pearl--query-issues-async)
                 (lambda (_filter cb)
                   (setq ran t)
                   (funcall cb (pearl--make-query-result 'ok :issues nil))))
                ((symbol-function 'pearl--merge-query-result)
                 (lambda (_result src) (setq merged-source src))))
        (pearl-refresh-current-view)
        (should ran)
        (should (equal source merged-source))))))

(ert-deftest test-pearl-refresh-current-view-no-source-errors ()
  "Refresh with no recorded source signals a user error."
  (with-temp-buffer
    (insert "#+title: plain\n")
    (org-mode)
    (should-error (pearl-refresh-current-view) :type 'user-error)))

;;; malformed / unknown source headers

(ert-deftest test-pearl-read-active-source-malformed-is-nil ()
  "A syntactically malformed #+LINEAR-SOURCE: line reads back nil, not a reader error."
  (with-temp-buffer
    (insert "#+title: x\n#+LINEAR-SOURCE: (:type filter :filter (:assignee\n\n")
    (should-not (pearl--read-active-source))))

(ert-deftest test-pearl-refresh-current-view-malformed-source-errors ()
  "A malformed source header surfaces the no-source user error, not a reader error."
  (with-temp-buffer
    (insert "#+title: x\n#+LINEAR-SOURCE: (:type filter :filter (:assignee\n")
    (org-mode)
    (should-error (pearl-refresh-current-view) :type 'user-error)))

(ert-deftest test-pearl-refresh-current-view-unknown-type-errors ()
  "A source with a valid but unknown :type signals the unknown-type user error."
  (with-temp-buffer
    (insert (format "#+title: x\n#+LINEAR-SOURCE: %s\n"
                    (prin1-to-string '(:type bogus :name "x"))))
    (org-mode)
    (should-error (pearl-refresh-current-view) :type 'user-error)))

(ert-deftest test-pearl-source-header-roundtrips-special-chars ()
  "A source name/filter with quotes, parens, colons, and non-ASCII round-trips."
  (let ((source '(:type filter
                  :name "Bugs: \"urgent\" (P1) — café"
                  :filter (:labels ("a:b" "(paren)" "naïve")))))
    (with-temp-buffer
      (insert (pearl--build-org-content '() source))
      (should (equal source (pearl--read-active-source))))))

;;; --update-org-from-issues surfaces the result

(ert-deftest test-pearl-update-org-surfaces-fresh-buffer ()
  "With no buffer visiting the file, the write creates one and surfaces it."
  (let* ((tmp (make-temp-file "pearl-out" nil ".org"))
         (pearl-org-file-path tmp)
         (surfaced nil))
    (unwind-protect
        (progn
          (when (find-buffer-visiting tmp) (kill-buffer (find-buffer-visiting tmp)))
          (cl-letf (((symbol-function 'pearl--surface-buffer)
                     (lambda (b) (setq surfaced b))))
            (pearl--update-org-from-issues '() '(:type filter :name "X" :filter nil) nil))
          (should (bufferp surfaced))
          (should (buffer-live-p surfaced))
          (should (string= (file-truename tmp)
                           (file-truename (buffer-file-name surfaced)))))
      (when (find-buffer-visiting tmp) (kill-buffer (find-buffer-visiting tmp)))
      (ignore-errors (delete-file tmp)))))

(ert-deftest test-pearl-update-org-surfaces-existing-buffer ()
  "With a clean buffer visiting the file, the update surfaces that buffer."
  (let* ((tmp (make-temp-file "pearl-out" nil ".org"))
         (pearl-org-file-path tmp)
         (surfaced nil)
         (buf (find-file-noselect tmp)))
    (unwind-protect
        (progn
          (with-current-buffer buf (set-buffer-modified-p nil))
          (cl-letf (((symbol-function 'pearl--surface-buffer)
                     (lambda (b) (setq surfaced b))))
            (pearl--update-org-from-issues '() '(:type filter :name "X" :filter nil) nil))
          (should (eq surfaced buf)))
      (when (buffer-live-p buf)
        (with-current-buffer buf (set-buffer-modified-p nil))
        (kill-buffer buf))
      (ignore-errors (delete-file tmp)))))

;;; --update-org-from-issues on a dirty buffer (prompted: discard / merge / cancel)

(defconst test-pearl-output--issue
  '(:id "u" :identifier "ENG-1" :title "Hello" :priority 2 :state (:name "Todo"))
  "A minimal normalized issue for the dirty-buffer branch tests.")

(defmacro test-pearl-output--with-dirty-buffer (choice &rest body)
  "Run BODY with a modified buffer visiting a temp active file and the dirty
refresh prompt stubbed to return CHOICE.  BUF and TMP are bound in BODY."
  (declare (indent 1))
  `(let* ((tmp (make-temp-file "pearl-dirty" nil ".org"))
          (pearl-org-file-path tmp)
          (buf (find-file-noselect tmp)))
     (unwind-protect
         (with-current-buffer buf
           (insert "DIRTYMARKER not yet saved\n")   ; buffer is now modified
           (cl-letf (((symbol-function 'pearl--surface-buffer) #'ignore)
                     ((symbol-function 'pearl--dirty-refresh-choice)
                      (lambda (_path) ,choice)))
             ,@body))
       (when (buffer-live-p buf)
         (with-current-buffer buf (set-buffer-modified-p nil))
         (kill-buffer buf))
       (ignore-errors (delete-file tmp)))))

(ert-deftest test-pearl-update-org-dirty-discard-rebuilds ()
  "Choosing discard rebuilds the buffer from the fetch and clears the edits."
  (test-pearl-output--with-dirty-buffer ?d
    (pearl--update-org-from-issues (list test-pearl-output--issue)
                                   '(:type filter :name "X" :filter nil) nil)
    (should-not (buffer-modified-p))
    (should-not (string-match-p "DIRTYMARKER" (buffer-string)))
    (should (string-match-p "ENG-1" (buffer-string)))))

(ert-deftest test-pearl-update-org-dirty-cancel-leaves-buffer ()
  "Choosing cancel leaves the dirty buffer untouched."
  (test-pearl-output--with-dirty-buffer ?q
    (pearl--update-org-from-issues (list test-pearl-output--issue)
                                   '(:type filter :name "X" :filter nil) nil)
    (should (buffer-modified-p))
    (should (string-match-p "DIRTYMARKER" (buffer-string)))
    (should-not (string-match-p "ENG-1" (buffer-string)))))

(ert-deftest test-pearl-update-org-dirty-merge-routes-to-merge ()
  "Choosing merge routes to the by-LINEAR-ID merge instead of a rebuild."
  (let ((merged nil))
    (test-pearl-output--with-dirty-buffer ?m
      (cl-letf (((symbol-function 'pearl--merge-issues-into-buffer)
                 (lambda (_issues) (setq merged t) '(:updated 1 :added 0 :dropped 0 :skipped 0)))
                ((symbol-function 'pearl--update-source-header) #'ignore)
                ((symbol-function 'pearl--update-derived-todo-header) #'ignore)
                ((symbol-function 'pearl-highlight-comments) #'ignore)
                ((symbol-function 'pearl--restore-page-visibility) #'ignore))
        (pearl--update-org-from-issues (list test-pearl-output--issue)
                                       '(:type filter :name "X" :filter nil) nil))
      (should merged)
      ;; merge does not rebuild, so the marker is not wiped by a full replace
      (should (string-match-p "DIRTYMARKER" (buffer-string))))))

(provide 'test-pearl-output)
;;; test-pearl-output.el ends here