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
|
;;; test-pearl-list-comments.el --- Tests for comments in the bulk list -*- 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 rendering comments in the bulk issue list: the per-issue cap
;; (`pearl--cap-issue-list-comments'), the ` shown/total' marker
;; (`pearl--comment-count-marker', the Comments heading), and the way the
;; append locator tolerates the marked heading so a later add-comment finds the
;; existing subtree instead of creating a second one.
;;; Code:
(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))
(defun test-pearl--comments (n)
"Return N comment plists, newest-first (createdAt descending), as the fetch returns."
(let (out)
(dotimes (i n)
;; i=0 is the newest; pad the index so string< orders correctly
(push (list :id (format "c%02d" (- n i))
:author "A"
:created-at (format "2026-05-%02dT00:00:00.000Z" (- n i))
:body (format "comment %d" (- n i)))
out))
(nreverse out)))
;;; --cap-issue-list-comments
(ert-deftest test-pearl-cap-comments-under-shown ()
"Fewer comments than the shown cap keeps them all with an exact total."
(let ((pearl-list-comments-shown 5) (pearl-list-comments-count-cap 25))
(let ((out (pearl--cap-issue-list-comments
(list :id "u" :comments (test-pearl--comments 3)))))
(should (= 3 (length (plist-get out :comments))))
(should (equal '(:shown 3 :total 3 :overflow nil)
(plist-get out :comment-count))))))
(ert-deftest test-pearl-cap-comments-over-shown-under-cap ()
"More than the shown cap but within the count cap: show 5, total exact."
(let ((pearl-list-comments-shown 5) (pearl-list-comments-count-cap 25))
(let ((out (pearl--cap-issue-list-comments
(list :id "u" :comments (test-pearl--comments 18)))))
(should (= 5 (length (plist-get out :comments))))
(should (equal '(:shown 5 :total 18 :overflow nil)
(plist-get out :comment-count))))))
(ert-deftest test-pearl-cap-comments-over-count-cap-overflows ()
"More than the count cap: total pins to the cap and overflows."
(let ((pearl-list-comments-shown 5) (pearl-list-comments-count-cap 25))
;; the fetch pulls cap+1 = 26 to detect overflow
(let ((out (pearl--cap-issue-list-comments
(list :id "u" :comments (test-pearl--comments 26)))))
(should (= 5 (length (plist-get out :comments))))
(should (equal '(:shown 5 :total 25 :overflow t)
(plist-get out :comment-count))))))
(ert-deftest test-pearl-cap-comments-none-unchanged ()
"An issue with no fetched comments is returned untouched, with no marker."
(let ((issue (list :id "u" :comments nil)))
(let ((out (pearl--cap-issue-list-comments issue)))
(should-not (plist-get out :comments))
(should-not (plist-get out :comment-count)))))
(ert-deftest test-pearl-cap-comments-keeps-the-newest ()
"The cap keeps the newest `shown' comments (input arrives newest-first)."
(let ((pearl-list-comments-shown 3) (pearl-list-comments-count-cap 25))
(let* ((out (pearl--cap-issue-list-comments
(list :id "u" :comments (test-pearl--comments 7))))
(ids (mapcar (lambda (c) (plist-get c :id)) (plist-get out :comments))))
;; newest three are c07, c06, c05 (the head of the newest-first list)
(should (equal '("c07" "c06" "c05") ids)))))
;;; --comment-count-marker
(ert-deftest test-pearl-comment-count-marker-forms ()
"The marker renders shown/total, with a `+' past the cap and nothing for nil."
(should (string= " 5/18" (pearl--comment-count-marker '(:shown 5 :total 18 :overflow nil))))
(should (string= " 5/25+" (pearl--comment-count-marker '(:shown 5 :total 25 :overflow t))))
(should (string= " 3/3" (pearl--comment-count-marker '(:shown 3 :total 3 :overflow nil))))
(should (string= "" (pearl--comment-count-marker nil))))
;;; --format-comments with a marker
(ert-deftest test-pearl-format-comments-renders-marker-and-order ()
"With count-info the Comments heading carries the marker; bodies honor the sort order."
(let* ((pearl-comment-sort-order 'newest-first)
(comments (test-pearl--comments 5)) ; newest-first as fetched
(out (pearl--format-comments comments '(:shown 5 :total 12 :overflow nil))))
(should (string-match-p "^\\*\\*\\* Comments 5/12$" out))
;; newest (comment 5) renders before oldest (comment 1)
(should (< (string-match "comment 5\\b" out) (string-match "comment 1\\b" out)))))
(ert-deftest test-pearl-format-comments-no-marker-without-count ()
"Without count-info (the single-issue thread), the heading has no marker."
(let ((out (pearl--format-comments (test-pearl--comments 2))))
(should (string-match-p "^\\*\\*\\* Comments$" out))))
(ert-deftest test-pearl-format-comments-nests-under-issue-level ()
"The Comments subtree nests one level under the issue: a flat issue (level 2,
the default) gets Comments at level 3 and bodies at level 4; a grouped issue at
level 3 gets Comments at level 4 and bodies at level 5. Regression guard for
the grouping bug where comments rendered as siblings of their issue."
(let ((comments (test-pearl--comments 1)))
;; flat (default level 2)
(should (string-match-p "^\\*\\*\\* Comments$" (pearl--format-comments comments nil 2)))
(should (string-match-p "^\\*\\*\\*\\* " (pearl--format-comments comments nil 2)))
;; grouped (issue at level 3)
(let ((out (pearl--format-comments comments nil 3)))
(should (string-match-p "^\\*\\*\\*\\* Comments$" out))
(should (string-match-p "^\\*\\*\\*\\*\\* " out))
(should-not (string-match-p "^\\*\\*\\* Comments$" out)))))
(ert-deftest test-pearl-format-issue-grouped-nests-comments-under-issue ()
"A level-3 issue with comments renders its Comments subtree at level 4 (a
child), not level 3 (a sibling) -- the grouped-view regression."
(let* ((issue (list :id "u" :identifier "ENG-1" :title "Fix it"
:priority 2 :state '(:name "Todo")
:comments (test-pearl--comments 1)))
(out (pearl--format-issue-as-org-entry issue 3)))
(should (string-match-p "^\\*\\*\\* TODO " out)) ; issue at level 3
(should (string-match-p "^\\*\\*\\*\\* Comments$" out)) ; Comments nested at 4
(should-not (string-match-p "^\\*\\*\\* Comments$" out))))
;;; the marked heading is still found by the append locator
(ert-deftest test-pearl-append-finds-marked-comments-heading ()
"Adding a comment to an issue whose Comments heading carries a marker reuses it."
(let ((pearl-list-comments-shown 5) (pearl-list-comments-count-cap 25))
(let ((entry (pearl--format-issue-as-org-entry
(pearl--cap-issue-list-comments
(list :id "a" :identifier "ENG-1" :title "issue"
:priority 3 :state '(:name "Todo")
:comments (test-pearl--comments 8))))))
(with-temp-buffer
(insert entry)
(org-mode)
(goto-char (point-min))
;; the rendered Comments heading carries the marker
(should (re-search-forward "^\\*\\*\\* Comments 5/8$" nil t))
(goto-char (point-min))
(re-search-forward "issue")
(pearl--append-comment-to-issue
'(:id "cnew" :author "Z" :created-at "2026-06-01T00:00:00.000Z" :body "appended"))
;; exactly one Comments heading, and the new comment landed under it
(goto-char (point-min))
(should (re-search-forward "^\\*\\*\\* Comments" nil t))
(should-not (re-search-forward "^\\*\\*\\* Comments" nil t))
(should (string-match-p "appended" (buffer-string)))))))
(provide 'test-pearl-list-comments)
;;; test-pearl-list-comments.el ends here
|