aboutsummaryrefslogtreecommitdiff
path: root/tests/test-pearl-delete.el
blob: 02d3380b483f66e0da383942f68ebd8064934564 (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
;;; test-pearl-delete.el --- Tests for deleting an issue -*- 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 `pearl--delete-issue-async' (the issueDelete mutation,
;; stubbed) and `pearl-delete-current-issue', which confirms, deletes,
;; and removes the issue subtree from the buffer on success.

;;; Code:

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

(defmacro test-pearl--in-org (content &rest body)
  "Run BODY in an org-mode temp buffer holding CONTENT at point-min."
  (declare (indent 1))
  `(with-temp-buffer
     (insert ,content)
     (org-mode)
     (goto-char (point-min))
     ,@body))

;;; --delete-issue-async

(ert-deftest test-pearl-delete-issue-async-success ()
  "A successful issueDelete reports success."
  (testutil-linear-with-response
      '((data (issueDelete (success . t))))
    (let (result)
      (pearl--delete-issue-async "id-1" (lambda (r) (setq result r)))
      (should (eq t (plist-get result :success))))))

(ert-deftest test-pearl-delete-issue-async-soft-fail ()
  "A non-success issueDelete reports failure rather than erroring."
  (testutil-linear-with-response
      '((data (issueDelete (success . :json-false))))
    (let ((called nil) result)
      (pearl--delete-issue-async "id-1" (lambda (r) (setq called t result r)))
      (should called)
      (should-not (plist-get result :success)))))

;;; delete-current-issue

(ert-deftest test-pearl-delete-current-issue-confirmed-removes-subtree ()
  "Confirming the delete removes the issue subtree from the buffer."
  (test-pearl--in-org
      "* Header\n\n*** TODO ENG-1 Doomed\n:PROPERTIES:\n:LINEAR-ID: a\n:LINEAR-IDENTIFIER: ENG-1\n:END:\nbody\n*** TODO ENG-2 Survivor\n:PROPERTIES:\n:LINEAR-ID: b\n:END:\n"
    (cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest _) t))
              ((symbol-function 'pearl--delete-issue-async)
               (lambda (_id cb) (funcall cb '(:success t)))))
      (re-search-forward "Doomed")
      (pearl-delete-current-issue)
      (goto-char (point-min))
      (should-not (re-search-forward "Doomed" nil t))
      ;; the sibling issue is untouched
      (goto-char (point-min))
      (should (re-search-forward "Survivor" nil t)))))

(ert-deftest test-pearl-delete-current-issue-declined-keeps-subtree ()
  "Declining the confirmation makes no API call and leaves the subtree."
  (let ((called nil))
    (test-pearl--in-org
        "*** TODO ENG-1 Keepme\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\nbody\n"
      (cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest _) nil))
                ((symbol-function 'pearl--delete-issue-async)
                 (lambda (&rest _) (setq called t))))
        (pearl-delete-current-issue)
        (should-not called)
        (goto-char (point-min))
        (should (re-search-forward "Keepme" nil t))))))

(ert-deftest test-pearl-delete-current-issue-not-on-issue-errors ()
  "Deleting outside a Linear issue heading signals a user error."
  (test-pearl--in-org "* Plain heading\nno id\n"
    (cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest _) t)))
      (should-error (pearl-delete-current-issue) :type 'user-error))))

(ert-deftest test-pearl-delete-current-issue-failure-keeps-subtree ()
  "A confirmed delete that fails on the remote leaves the subtree and its sibling intact."
  (test-pearl--in-org
      "* Header\n\n*** TODO ENG-1 Doomed\n:PROPERTIES:\n:LINEAR-ID: a\n:LINEAR-IDENTIFIER: ENG-1\n:END:\nbody\n*** TODO ENG-2 Survivor\n:PROPERTIES:\n:LINEAR-ID: b\n:END:\n"
    (cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest _) t))
              ((symbol-function 'pearl--delete-issue-async)
               (lambda (_id cb) (funcall cb '(:success nil)))))
      (re-search-forward "Doomed")
      (pearl-delete-current-issue)
      ;; the failed delete leaves the issue in place ...
      (goto-char (point-min))
      (should (re-search-forward "Doomed" nil t))
      ;; ... and the sibling subtree is untouched.
      (goto-char (point-min))
      (should (re-search-forward "Survivor" nil t)))))

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