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
|
;;; test-pearl-comments.el --- Tests for issue comments -*- 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 comment thread: rendering a normalized comment and the
;; Comments subtree (ordered per `pearl-comment-sort-order'), including comments
;; in the issue render, the commentCreate helper (stubbed at the HTTP boundary),
;; the in-place append under the Comments subtree (creating it when absent), and the
;; `pearl-add-comment' command.
;;; 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)))
;;; --format-comment / --format-comments
(ert-deftest test-pearl-format-comment-renders-author-time-body ()
"A comment renders as a level-4 heading with author and timestamp, body below."
(let ((out (pearl--format-comment
'(:id "c1" :author "Craig" :created-at "2026-05-23T10:00:00.000Z"
:body "Looks **good** to me"))))
(should (string-match-p "^\\*\\*\\*\\* Craig — 2026-05-23T10:00:00.000Z$" out))
;; body runs through the md->org tier
(should (string-match-p "Looks \\*good\\* to me" out))))
(ert-deftest test-pearl-format-comment-null-author ()
"A comment with no resolved author renders a placeholder, not an error."
(let ((out (pearl--format-comment
'(:id "c1" :author nil :created-at "2026-05-23T10:00:00.000Z" :body "hi"))))
(should (string-match-p "^\\*\\*\\*\\* (unknown) — 2026-05-23T10:00:00.000Z$" out))))
(ert-deftest test-pearl-format-comments-empty-is-blank ()
"No comments renders nothing (no empty Comments subtree)."
(should (string= "" (pearl--format-comments nil))))
(defconst test-pearl--two-comments
'((:id "c1" :author "A" :created-at "2026-05-23T09:00:00.000Z" :body "first")
(:id "c2" :author "B" :created-at "2026-05-23T12:00:00.000Z" :body "second"))
"Two comments, c1 older than c2, in input order regardless of render order.")
(ert-deftest test-pearl-format-comments-newest-first ()
"With newest-first order, the most recent comment renders on top."
(let* ((pearl-comment-sort-order 'newest-first)
(out (pearl--format-comments test-pearl--two-comments)))
(should (string-match-p "^\\*\\*\\* Comments$" out))
(should (< (string-match "second" out) (string-match "first" out)))))
(ert-deftest test-pearl-format-comments-oldest-first ()
"With oldest-first order, comments render chronologically, oldest on top."
(let* ((pearl-comment-sort-order 'oldest-first)
(out (pearl--format-comments test-pearl--two-comments)))
(should (< (string-match "first" out) (string-match "second" out)))))
;;; comments in the issue render
(ert-deftest test-pearl-format-issue-includes-comments ()
"A normalized issue carrying comments renders the Comments subtree after the body."
(test-pearl--in-org ""
(let ((out (pearl--format-issue-as-org-entry
'(:id "u" :identifier "ENG-1" :title "Title" :priority 2
:state (:name "Todo") :description "Body text."
:comments ((:id "c1" :author "A" :created-at "2026-05-23T09:00:00.000Z"
:body "a comment"))))))
(should (string-match-p "^\\*\\*\\* Comments$" out))
(should (< (string-match "Body text." out) (string-match "a comment" out))))))
;;; --create-comment-async
(ert-deftest test-pearl-create-comment-parses-payload ()
"A successful commentCreate yields the normalized comment."
(testutil-linear-with-response
'((data (commentCreate
(success . t)
(comment (id . "c9") (body . "new one")
(createdAt . "2026-05-23T13:00:00.000Z")
(user (name . "Craig"))))))
(let (result)
(pearl--create-comment-async "issue-a" "new one" (lambda (r) (setq result r)))
(should (string= "c9" (plist-get result :id)))
(should (string= "Craig" (plist-get result :author))))))
(ert-deftest test-pearl-create-comment-soft-fail ()
"A non-success commentCreate yields nil rather than erroring."
(testutil-linear-with-response
'((data (commentCreate (success . :json-false) (comment . nil))))
(let ((called nil) (result 'untouched))
(pearl--create-comment-async "issue-a" "x" (lambda (r) (setq called t result r)))
(should called)
(should (null result)))))
;;; --append-comment-to-issue
(ert-deftest test-pearl-append-comment-creates-subtree ()
"Appending to an issue with no Comments subtree creates one."
(test-pearl--in-org
"*** TODO [#B] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\nBody.\n"
(pearl--append-comment-to-issue
'(:id "c1" :author "A" :created-at "2026-05-23T09:00:00.000Z" :body "first comment"))
(goto-char (point-min))
(should (re-search-forward "^\\*\\*\\* Comments$" nil t))
(should (re-search-forward "first comment" nil t))))
(ert-deftest test-pearl-append-comment-newest-first-inserts-at-top ()
"With newest-first order, a new comment lands above the existing ones."
(let ((pearl-comment-sort-order 'newest-first))
(test-pearl--in-org
"** TODO [#B] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\nBody.\n*** Comments\n**** A — 2026-05-23T09:00:00.000Z\nfirst\n"
(pearl--append-comment-to-issue
'(:id "c2" :author "B" :created-at "2026-05-23T12:00:00.000Z" :body "second"))
(goto-char (point-min))
;; still exactly one Comments heading
(should (re-search-forward "^\\*\\*\\* Comments$" nil t))
(should-not (re-search-forward "^\\*\\*\\* Comments$" nil t))
(goto-char (point-min))
;; the new comment is above the existing one
(should (< (progn (re-search-forward "second") (point))
(progn (re-search-forward "first") (point)))))))
(ert-deftest test-pearl-append-comment-oldest-first-appends-at-bottom ()
"With oldest-first order, a new comment appends after the existing ones."
(let ((pearl-comment-sort-order 'oldest-first))
(test-pearl--in-org
"** TODO [#B] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\nBody.\n*** Comments\n**** A — 2026-05-23T09:00:00.000Z\nfirst\n"
(pearl--append-comment-to-issue
'(:id "c2" :author "B" :created-at "2026-05-23T12:00:00.000Z" :body "second"))
(goto-char (point-min))
(should (< (progn (re-search-forward "first") (point))
(progn (re-search-forward "second") (point)))))))
;;; pearl-add-comment
(ert-deftest test-pearl-add-comment-appends-returned-comment ()
"The command creates a comment and inserts the returned one in the buffer."
(test-pearl--in-org
"*** TODO [#B] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\nBody.\n"
(cl-letf (((symbol-function 'pearl--create-comment-async)
(lambda (_id body cb)
(funcall cb (list :id "c1" :author "Craig"
:created-at "2026-05-23T14:00:00.000Z" :body body)))))
(re-search-forward "Body.")
(pearl-add-comment "my new comment")
(goto-char (point-min))
(should (re-search-forward "^\\*\\*\\* Comments$" nil t))
(should (re-search-forward "my new comment" nil t)))))
(ert-deftest test-pearl-add-comment-reports-failure ()
"A failed create does not insert a Comments subtree."
(test-pearl--in-org
"*** TODO [#B] Title\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\nBody.\n"
(cl-letf (((symbol-function 'pearl--create-comment-async)
(lambda (_id _body cb) (funcall cb nil))))
(pearl-add-comment "x")
(goto-char (point-min))
(should-not (re-search-forward "^\\*\\*\\* Comments$" nil t)))))
(ert-deftest test-pearl-add-comment-not-on-issue-errors ()
"Adding a comment outside a Linear issue heading signals a user error."
(test-pearl--in-org "* Plain heading\nno id\n"
(should-error (pearl-add-comment "x") :type 'user-error)))
(ert-deftest test-pearl-append-comment-targets-current-issue-not-sibling ()
"Appending adds under the current issue's Comments, not a later issue's subtree."
(test-pearl--in-org
(concat "** TODO Issue A\n:PROPERTIES:\n:LINEAR-ID: a\n:END:\nBody A.\n"
"*** Comments\n**** X — t\n:PROPERTIES:\n:LINEAR-COMMENT-ID: cx\n:END:\nfirst A\n"
"** TODO Issue B\n:PROPERTIES:\n:LINEAR-ID: b\n:END:\nBody B.\n"
"*** Comments\n**** Y — t\n:PROPERTIES:\n:LINEAR-COMMENT-ID: cy\n:END:\nfirst B\n")
(goto-char (point-min))
(re-search-forward "Body A\\.")
(pearl--append-comment-to-issue
'(:id "cnew" :author "Z" :created-at "2026-05-24T12:00:00.000Z" :body "new on A"))
;; the new comment landed under issue A, before issue B
(goto-char (point-min))
(let ((new-pos (progn (re-search-forward "new on A") (point)))
(b-pos (progn (goto-char (point-min)) (re-search-forward "Issue B") (point))))
(should (< new-pos b-pos)))
;; issue B still has exactly one Comments heading (not a second one)
(goto-char (point-min))
(re-search-forward "Issue B")
(should (re-search-forward "^\\*\\*\\* Comments$" nil t))))
(provide 'test-pearl-comments)
;;; test-pearl-comments.el ends here
|