blob: 7322e4f2268ba9d283ab87dc527a95731542a1a6 (
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
|
;;; test-pearl-fields.el --- Tests for command-managed drawer fields -*- 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 command-managed drawer fields that need no name->id
;; resolution helper: set-priority and set-state. Covers the generic
;; issueUpdate helper (stubbed at the HTTP boundary), the heading cookie and
;; keyword/drawer mutators, and the two commands' push + buffer-update paths.
;;; 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, default mapping bound."
(declare (indent 1))
`(let ((pearl-state-to-todo-mapping
'(("Todo" . "TODO") ("In Progress" . "IN-PROGRESS") ("Done" . "DONE")))
(org-todo-keywords '((sequence "TODO" "IN-PROGRESS" "|" "DONE"))))
(with-temp-buffer
(insert ,content)
(org-mode)
(goto-char (point-min))
,@body)))
;;; --update-issue-async (generic issueUpdate)
(ert-deftest test-pearl-update-issue-async-success ()
"A successful generic issueUpdate reports success and the timestamp."
(testutil-linear-with-response
'((data (issueUpdate (success . t) (issue (id . "a") (updatedAt . "t1")))))
(let (result)
(pearl--update-issue-async "a" '(("priority" . 2)) (lambda (r) (setq result r)))
(should (eq t (plist-get result :success)))
(should (string= "t1" (plist-get result :updated-at))))))
(ert-deftest test-pearl-update-issue-async-soft-fail ()
"A non-success generic issueUpdate reports failure rather than erroring."
(testutil-linear-with-response
'((data (issueUpdate (success . :json-false) (issue . nil))))
(let ((called nil) result)
(pearl--update-issue-async "a" '(("priority" . 2)) (lambda (r) (setq called t result r)))
(should called)
(should-not (plist-get result :success)))))
;;; --set-priority-cookie
(ert-deftest test-pearl-set-priority-cookie-replaces ()
"Setting a priority rewrites the heading cookie."
(test-pearl--in-org "*** TODO [#C] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\n"
(pearl--set-priority-cookie 1)
(goto-char (point-min))
(should (string-match-p "^\\*\\*\\* TODO \\[#A\\] Title" (thing-at-point 'line t)))))
(ert-deftest test-pearl-set-priority-cookie-low ()
"Low priority renders the #D cookie."
(test-pearl--in-org "*** TODO [#C] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\n"
(pearl--set-priority-cookie 4)
(goto-char (point-min))
(should (string-match-p "^\\*\\*\\* TODO \\[#D\\] Title" (thing-at-point 'line t)))))
(ert-deftest test-pearl-set-priority-cookie-none-removes ()
"Priority None removes the cookie."
(test-pearl--in-org "*** TODO [#C] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\n"
(pearl--set-priority-cookie 0)
(goto-char (point-min))
(should (string-match-p "^\\*\\*\\* TODO Title" (thing-at-point 'line t)))
(should-not (string-match-p "\\[#" (thing-at-point 'line t)))))
;;; pearl-set-priority
(ert-deftest test-pearl-set-priority-pushes-and-updates-cookie ()
"Setting priority pushes the numeric value and rewrites the cookie."
(let ((pushed nil))
(test-pearl--in-org "*** TODO [#C] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\n"
(cl-letf (((symbol-function 'pearl--update-issue-async)
(lambda (_id input cb) (setq pushed input) (funcall cb '(:success t)))))
(re-search-forward "Title")
(pearl-set-priority "High")
(should (equal 2 (cdr (assoc "priority" pushed))))
(goto-char (point-min))
(should (string-match-p "\\[#B\\]" (thing-at-point 'line t)))))))
(ert-deftest test-pearl-set-priority-not-on-issue-errors ()
"Setting priority outside a Linear issue heading signals a user error."
(test-pearl--in-org "* Plain heading\nno id\n"
(should-error (pearl-set-priority "High") :type 'user-error)))
;;; --set-heading-state
(ert-deftest test-pearl-set-heading-state-updates-keyword-and-drawer ()
"Setting the heading state updates the TODO keyword and the LINEAR-STATE drawer."
(test-pearl--in-org
"*** TODO [#B] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:LINEAR-STATE-ID: old\n:LINEAR-STATE-NAME: Todo\n:END:\n"
(pearl--set-heading-state "In Progress" "s2")
(goto-char (point-min))
(should (string-match-p "^\\*\\*\\* IN-PROGRESS " (thing-at-point 'line t)))
(should (string= "In Progress" (org-entry-get nil "LINEAR-STATE-NAME")))
(should (string= "s2" (org-entry-get nil "LINEAR-STATE-ID")))))
(ert-deftest test-pearl-set-heading-state-does-not-fire-sync-hook ()
"Setting the keyword must not trigger the Linear org-todo sync hook."
(let ((fired nil))
(test-pearl--in-org
"*** TODO [#B] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\n"
(let ((org-after-todo-state-change-hook (list (lambda () (setq fired t)))))
(pearl--set-heading-state "Done" "s3")
(should-not fired)))))
;;; pearl-set-state
(ert-deftest test-pearl-set-state-pushes-id-and-updates-heading ()
"Setting state resolves the name to an id, pushes it, and updates the heading."
(let ((pushed nil))
(test-pearl--in-org
"*** TODO [#B] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:LINEAR-TEAM-ID: team-1\n:END:\n"
(cl-letf (((symbol-function 'pearl--team-states)
(lambda (_team) '(((id . "s1") (name . "Todo"))
((id . "s2") (name . "In Progress")))))
((symbol-function 'pearl--update-issue-async)
(lambda (_id input cb) (setq pushed input) (funcall cb '(:success t)))))
(pearl-set-state "In Progress")
(should (string= "s2" (cdr (assoc "stateId" pushed))))
(goto-char (point-min))
(should (string-match-p "^\\*\\*\\* IN-PROGRESS " (thing-at-point 'line t)))
(should (string= "In Progress" (org-entry-get nil "LINEAR-STATE-NAME")))))))
(ert-deftest test-pearl-set-state-not-on-issue-errors ()
"Setting state outside a Linear issue heading signals a user error."
(test-pearl--in-org "* Plain heading\nno id\n"
(should-error (pearl-set-state "Done") :type 'user-error)))
(provide 'test-pearl-fields)
;;; test-pearl-fields.el ends here
|