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
|
;;; test-pearl-resolve.el --- Tests for name->id resolution helpers -*- 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 per-team cached collection fetch (`--team-collection') and
;; the name->id resolver (`--resolve-team-id'), which back the field commands,
;; the ad-hoc filter, and saved queries. The HTTP boundary is stubbed; the
;; cache is reset around each test.
;;; Code:
(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))
(require 'cl-lib)
(defmacro test-pearl--with-clean-caches (&rest body)
"Run BODY with the Linear caches reset and an API key set."
(declare (indent 0))
`(let ((pearl-api-key "test-key")
(pearl--cache-team-collections nil)
(pearl--cache-states nil)
(pearl--cache-teams nil))
,@body))
(defmacro test-pearl--counting-request (data counter &rest body)
"Run BODY with `request' stubbed to succeed with DATA, counting calls in COUNTER."
(declare (indent 2))
`(cl-letf (((symbol-function 'request)
(lambda (_url &rest args)
(cl-incf ,counter)
(let ((cb (plist-get args :success)))
(when cb (funcall cb :data ,data))))))
,@body))
;;; --team-collection
(ert-deftest test-pearl-team-collection-fetches-and-caches ()
"A collection is fetched once and served from cache on the second call."
(test-pearl--with-clean-caches
(let ((calls 0))
(test-pearl--counting-request
'((data (team (projects (nodes . [((id . "p1") (name . "Foo"))
((id . "p2") (name . "Bar"))]))))) calls
(let ((first (pearl--team-collection 'projects "team-1")))
(should (= 2 (length first)))
(should (string= "p1" (cdr (assoc 'id (car first)))))
(pearl--team-collection 'projects "team-1")
(should (= 1 calls)))))))
(ert-deftest test-pearl-team-collection-force-refetches ()
"A force refresh bypasses the cache and fetches again."
(test-pearl--with-clean-caches
(let ((calls 0))
(test-pearl--counting-request
'((data (team (labels (nodes . [((id . "l1") (name . "bug"))]))))) calls
(pearl--team-collection 'labels "team-1")
(pearl--team-collection 'labels "team-1" t)
(should (= 2 calls))))))
(ert-deftest test-pearl-team-collection-keyed-by-team ()
"Different teams cache separately."
(test-pearl--with-clean-caches
(let ((calls 0))
(test-pearl--counting-request
'((data (team (members (nodes . [((id . "u1") (name . "Craig"))]))))) calls
(pearl--team-collection 'members "team-1")
(pearl--team-collection 'members "team-2")
(should (= 2 calls))))))
;;; --resolve-team-id
(ert-deftest test-pearl-resolve-unique-name ()
"A name with one match resolves to that id."
(test-pearl--with-clean-caches
(cl-letf (((symbol-function 'pearl--team-collection)
(lambda (&rest _) '(((id . "p1") (name . "Foo"))
((id . "p2") (name . "Bar"))))))
(should (string= "p1" (pearl--resolve-team-id 'projects "Foo" "team-1")))
;; case-insensitive
(should (string= "p2" (pearl--resolve-team-id 'projects "bar" "team-1"))))))
(ert-deftest test-pearl-resolve-no-match-is-nil ()
"A name with no match resolves to nil."
(test-pearl--with-clean-caches
(cl-letf (((symbol-function 'pearl--team-collection)
(lambda (&rest _) '(((id . "p1") (name . "Foo"))))))
(should-not (pearl--resolve-team-id 'projects "Missing" "team-1")))))
(ert-deftest test-pearl-resolve-member-by-display-name-or-email ()
"Members match on name, displayName, or email."
(test-pearl--with-clean-caches
(cl-letf (((symbol-function 'pearl--team-collection)
(lambda (&rest _)
'(((id . "u1") (name . "Craig Jennings") (displayName . "cj")
(email . "c@x.com"))))))
(should (string= "u1" (pearl--resolve-team-id 'members "cj" "team-1")))
(should (string= "u1" (pearl--resolve-team-id 'members "c@x.com" "team-1"))))))
(ert-deftest test-pearl-resolve-ambiguous-prompts ()
"When several nodes share a name, the resolver prompts and returns the choice."
(test-pearl--with-clean-caches
(cl-letf (((symbol-function 'pearl--team-collection)
(lambda (&rest _) '(((id . "u1") (name . "Alex") (displayName . "alex-a"))
((id . "u2") (name . "Alex") (displayName . "alex-b")))))
((symbol-function 'completing-read)
(lambda (_prompt collection &rest _)
;; pick the entry whose id is u2
(car (rassoc "u2" collection)))))
(should (string= "u2" (pearl--resolve-team-id 'members "Alex" "team-1"))))))
;;; clear-cache
(ert-deftest test-pearl-clear-cache-resets ()
"Clearing the cache empties the collection and per-team caches."
(let ((pearl--cache-team-collections '(((projects . "t") . x)))
(pearl--cache-states '(("t" . y)))
(pearl--cache-teams '(z)))
(pearl-clear-cache)
(should-not pearl--cache-team-collections)
(should-not pearl--cache-states)
(should-not pearl--cache-teams)))
;;; failure / malformed cases
(ert-deftest test-pearl-team-collection-malformed-returns-nil-no-cache ()
"A response missing the collection nodes returns nil and caches nothing."
(test-pearl--with-clean-caches
(let ((calls 0))
(test-pearl--counting-request '((data (team nil))) calls
(should-not (pearl--team-collection 'projects "team-1"))
(should-not pearl--cache-team-collections)
;; nothing cached, so a second call refetches
(pearl--team-collection 'projects "team-1")
(should (= 2 calls))))))
(ert-deftest test-pearl-team-collection-nil-response-returns-nil ()
"A failed request (nil response) returns nil and caches nothing, without erroring."
(test-pearl--with-clean-caches
(let ((calls 0))
(test-pearl--counting-request nil calls
(should-not (pearl--team-collection 'members "team-1"))
(should-not pearl--cache-team-collections)))))
(ert-deftest test-pearl-team-collection-recovers-after-failure ()
"A failed fetch caches nothing, so a later successful fetch populates the cache."
(test-pearl--with-clean-caches
(let ((responses (list nil
'((data (team (projects (nodes . [((id . "p1") (name . "Foo"))]))))))))
(cl-letf (((symbol-function 'request)
(lambda (_url &rest args)
(let ((cb (plist-get args :success)) (d (pop responses)))
(when cb (funcall cb :data d))))))
(should-not (pearl--team-collection 'projects "team-1"))
(let ((second (pearl--team-collection 'projects "team-1")))
(should (= 1 (length second)))
(should (string= "p1" (cdr (assoc 'id (car second))))))))))
(ert-deftest test-pearl-resolve-failed-collection-is-nil ()
"When the backing collection fetch fails (empty), resolution returns nil, not an error."
(test-pearl--with-clean-caches
(cl-letf (((symbol-function 'pearl--team-collection) (lambda (&rest _) nil)))
(should-not (pearl--resolve-team-id 'labels "bug" "team-1")))))
(ert-deftest test-pearl-custom-views-recovers-after-failure ()
"A failed custom-views fetch caches nothing; a later call recovers."
(let ((pearl-api-key "test-key")
(pearl--cache-views nil)
(responses (list nil
'((data (customViews (nodes . [((id . "v1") (name . "My View") (url . "u"))])))))))
(cl-letf (((symbol-function 'request)
(lambda (_url &rest args)
(let ((cb (plist-get args :success)) (d (pop responses)))
(when cb (funcall cb :data d))))))
(should-not (pearl--custom-views))
(should-not pearl--cache-views)
(let ((views (pearl--custom-views)))
(should (= 1 (length views)))))))
(provide 'test-pearl-resolve)
;;; test-pearl-resolve.el ends here
|