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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
|
;;; test-pearl-saved-query-sync.el --- Tests for syncing saved queries to Linear views -*- 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-sync-saved-query-to-linear' and its helpers
;; (`pearl--sync-scope-candidates', `pearl--find-view-by-name-in-scope',
;; `pearl--customview-create-async', `pearl--customview-update-async',
;; `pearl--save-query-mark-synced', `pearl--sync-record-or-orphan-error').
;; See docs/saved-query-sync-spec.org.
;;; Code:
(require 'test-bootstrap (expand-file-name "test-bootstrap.el"
(file-name-directory
(or load-file-name buffer-file-name))))
(require 'testutil-request (expand-file-name "testutil-request.el"
(file-name-directory
(or load-file-name buffer-file-name))))
(require 'cl-lib)
;;; pearl--sync-scope-candidates
(ert-deftest test-pearl-sync-scope-candidates-default-with-filter-team ()
"When the filter has `:team X', the default candidate is the team-shared row."
(let* ((teams '(((id . "team-eng-id") (key . "ENG") (name . "Engineering"))
((id . "team-mkt-id") (key . "MKT") (name . "Marketing"))))
(cands (pearl--sync-scope-candidates teams "ENG")))
(should (string= "[ Team: Engineering, visible to the team ]" (caar cands)))
(let ((plist (cdar cands)))
(should (equal "team-eng-id" (plist-get plist :team-id)))
(should (equal "Engineering" (plist-get plist :team-name)))
(should (eq t (plist-get plist :shared))))))
(ert-deftest test-pearl-sync-scope-candidates-default-personal-no-filter-team ()
"Without `:team' in the filter, the default candidate is Personal."
(let* ((teams '(((id . "team-eng-id") (key . "ENG") (name . "Engineering"))))
(cands (pearl--sync-scope-candidates teams nil)))
(should (string= "[ Personal, only I see it ]" (caar cands)))
(let ((plist (cdar cands)))
(should (null (plist-get plist :team-id)))
(should (equal "Personal" (plist-get plist :team-name)))
(should (null (plist-get plist :shared))))))
(ert-deftest test-pearl-sync-scope-candidates-unknown-filter-team-falls-back-to-personal ()
"An unknown `:team' key in the filter falls back to Personal as the default."
(let* ((teams '(((id . "id-a") (key . "A") (name . "Apple"))))
(cands (pearl--sync-scope-candidates teams "GONE")))
(should (string= "[ Personal, only I see it ]" (caar cands)))))
(ert-deftest test-pearl-sync-scope-candidates-no-duplicate-default-row ()
"The default candidate appears only once even when it matches a real row."
(let* ((teams '(((id . "team-eng-id") (key . "ENG") (name . "Engineering"))))
(cands (pearl--sync-scope-candidates teams "ENG"))
(count (cl-count "[ Team: Engineering, visible to the team ]" cands
:key #'car :test #'string=)))
(should (= 1 count))))
(ert-deftest test-pearl-sync-scope-candidates-includes-personal-and-both-team-rows ()
"The list carries one Personal row plus two rows per team."
(let* ((teams '(((id . "id-a") (key . "A") (name . "Apple"))
((id . "id-b") (key . "B") (name . "Banana"))))
(cands (pearl--sync-scope-candidates teams nil)))
;; 1 personal + 2 teams x 2 rows = 5
(should (= 5 (length cands)))
(dolist (display '("[ Personal, only I see it ]"
"[ Team: Apple, visible to the team ]"
"[ Team: Apple, only I see it ]"
"[ Team: Banana, visible to the team ]"
"[ Team: Banana, only I see it ]"))
(should (cl-find display cands :key #'car :test #'string=)))))
(ert-deftest test-pearl-sync-scope-candidates-no-meaningless-personal-shared ()
"No `[ Personal, visible to the team ]' row exists -- it would be meaningless."
(let* ((teams '(((id . "id-a") (key . "A") (name . "Apple"))))
(cands (pearl--sync-scope-candidates teams nil)))
(should-not (cl-find "[ Personal, visible to the team ]" cands
:key #'car :test #'string=))))
(ert-deftest test-pearl-sync-scope-candidates-teams-sorted-alphabetically ()
"Non-default team rows appear in alphabetical order by team name."
(let* ((teams '(((id . "id-z") (key . "Z") (name . "Zebra"))
((id . "id-a") (key . "A") (name . "Apple"))
((id . "id-m") (key . "M") (name . "Mango"))))
(cands (pearl--sync-scope-candidates teams nil))
(displays (mapcar #'car cands))
(apple-pos (cl-position "[ Team: Apple, visible to the team ]"
displays :test #'string=))
(mango-pos (cl-position "[ Team: Mango, visible to the team ]"
displays :test #'string=))
(zebra-pos (cl-position "[ Team: Zebra, visible to the team ]"
displays :test #'string=)))
(should (and apple-pos mango-pos zebra-pos
(< apple-pos mango-pos)
(< mango-pos zebra-pos)))))
;;; pearl--sync-scope-label
(ert-deftest test-pearl-sync-scope-label-personal-ignores-shared ()
"Personal scope always renders the personal label regardless of SHARED.
A `Personal + shared' combination is meaningless on Linear (there's no
team to share with), so the label collapses to the only sensible
phrasing."
(should (equal "[ Personal, only I see it ]"
(pearl--sync-scope-label "Personal" nil)))
(should (equal "[ Personal, only I see it ]"
(pearl--sync-scope-label "Personal" t))))
(ert-deftest test-pearl-sync-scope-label-team-renders-shared-or-private ()
"A team scope renders the shared or private variant per SHARED."
(should (equal "[ Team: Engineering, visible to the team ]"
(pearl--sync-scope-label "Engineering" t)))
(should (equal "[ Team: Engineering, only I see it ]"
(pearl--sync-scope-label "Engineering" nil))))
(ert-deftest test-pearl-sync-saved-query-pick-scope-returns-display-plist-pair ()
"`pick-scope' returns the chosen candidate's (DISPLAY . PLIST) pair, not
just the plist. The caller uses the display string for the success
message without rebuilding the candidate list to look it up."
(cl-letf (((symbol-function 'pearl--all-teams)
(lambda () '(((id . "t1") (key . "ENG") (name . "Engineering")))))
((symbol-function 'completing-read)
(lambda (&rest _) "[ Team: Engineering, visible to the team ]")))
(let ((pair (pearl--sync-saved-query-pick-scope '(:team "ENG"))))
(should (consp pair))
(should (equal "[ Team: Engineering, visible to the team ]"
(car pair)))
(should (equal "t1" (plist-get (cdr pair) :team-id)))
(should (eq t (plist-get (cdr pair) :shared))))))
(ert-deftest test-pearl-sync-saved-query-pick-scope-returns-personal-pair ()
"Picking the Personal row returns the personal pair: display = the personal
label, plist team-id nil + team-name \"Personal\" + shared nil."
(cl-letf (((symbol-function 'pearl--all-teams) (lambda () '()))
((symbol-function 'completing-read)
(lambda (&rest _) "[ Personal, only I see it ]")))
(let ((pair (pearl--sync-saved-query-pick-scope '())))
(should (consp pair))
(should (equal "[ Personal, only I see it ]" (car pair)))
(should (null (plist-get (cdr pair) :team-id)))
(should (equal "Personal" (plist-get (cdr pair) :team-name)))
(should (null (plist-get (cdr pair) :shared))))))
(ert-deftest test-pearl-sync-saved-query-pick-scope-returns-nil-on-cancel ()
"Picking the cancel sentinel returns nil so the caller can short-circuit."
(cl-letf (((symbol-function 'pearl--all-teams) (lambda () '()))
((symbol-function 'completing-read)
(lambda (&rest _) pearl--filter-cancel)))
(should (null (pearl--sync-saved-query-pick-scope '())))))
;;; pearl--find-view-by-name-in-scope
(ert-deftest test-pearl-find-view-by-name-in-scope-matches-team-shared ()
"Finds a view by name + matching team id + shared flag."
(let ((views '(((id . "v1") (name . "A") (shared . t)
(team (id . "t1")))
((id . "v2") (name . "A") (shared . :json-false)
(team (id . "t1")))
((id . "v3") (name . "B") (shared . t)
(team . nil)))))
(should (equal "v1" (cdr (assoc 'id
(pearl--find-view-by-name-in-scope
views "A" "t1" t)))))
(should (equal "v2" (cdr (assoc 'id
(pearl--find-view-by-name-in-scope
views "A" "t1" nil)))))))
(ert-deftest test-pearl-find-view-by-name-in-scope-matches-personal ()
"Finds a personal (no team) view when scope matches."
(let ((views '(((id . "v1") (name . "A") (shared . :json-false)
(team . nil)))))
(should (equal "v1" (cdr (assoc 'id
(pearl--find-view-by-name-in-scope
views "A" nil nil)))))))
(ert-deftest test-pearl-find-view-by-name-in-scope-no-match-returns-nil ()
"Returns nil when nothing matches name or scope."
(let ((views '(((id . "v1") (name . "A") (shared . t)
(team (id . "t1"))))))
(should (null (pearl--find-view-by-name-in-scope views "A" "t2" t)))
(should (null (pearl--find-view-by-name-in-scope views "Other" "t1" t)))
(should (null (pearl--find-view-by-name-in-scope views "A" nil nil)))))
;;; pearl--customview-create-async
(ert-deftest test-pearl-customview-create-async-success ()
"A successful `customViewCreate' invokes the callback with `:success' t and the view."
(testutil-linear-with-response
'((data
(customViewCreate
(success . t)
(customView (id . "view-uuid") (name . "X")
(shared . :json-false) (team)))))
(let (result)
(pearl--customview-create-async
"X" nil nil
'(("state" ("type" ("nin" . ["completed"]))))
(lambda (r) (setq result r)))
(should (eq t (plist-get result :success)))
(should (equal "view-uuid"
(cdr (assoc 'id (plist-get result :view))))))))
(ert-deftest test-pearl-customview-create-async-failure ()
"A non-success `customViewCreate' invokes the callback with `:success' nil."
(testutil-linear-with-response
'((data (customViewCreate (success . :json-false) (customView))))
(let (result)
(pearl--customview-create-async
"X" nil nil '() (lambda (r) (setq result r)))
(should-not (plist-get result :success)))))
;;; pearl--customview-update-async
(ert-deftest test-pearl-customview-update-async-success ()
"A successful `customViewUpdate' invokes the callback with `:success' t."
(testutil-linear-with-response
'((data
(customViewUpdate
(success . t)
(customView (id . "view-uuid") (name . "X")
(shared . t)
(team (id . "team-id"))))))
(let (result)
(pearl--customview-update-async
"view-uuid" '() (lambda (r) (setq result r)))
(should (eq t (plist-get result :success))))))
(ert-deftest test-pearl-customview-update-async-failure ()
"A non-success `customViewUpdate' invokes the callback with `:success' nil."
(testutil-linear-with-response
'((data (customViewUpdate (success . :json-false) (customView))))
(let (result)
(pearl--customview-update-async
"view-uuid" '() (lambda (r) (setq result r)))
(should-not (plist-get result :success)))))
;;; pearl--save-query-mark-synced
(ert-deftest test-pearl-save-query-mark-synced-adds-the-four-keys ()
"After mark-synced, the entry carries `:linear-view-id', `-team-id', `-shared', `-synced-at'."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t))))))
(cl-letf (((symbol-function 'customize-save-variable) (lambda (_ _) nil)))
(pearl--save-query-mark-synced "Q" "team-uuid" t "view-uuid"))
(let ((spec (cdr (assoc "Q" pearl-saved-queries))))
(should (equal "view-uuid" (plist-get spec :linear-view-id)))
(should (equal "team-uuid" (plist-get spec :linear-view-team-id)))
(should (eq t (plist-get spec :linear-view-shared)))
(should (stringp (plist-get spec :linear-view-synced-at)))
;; Original filter survives
(should (equal '(:open t) (plist-get spec :filter))))))
(ert-deftest test-pearl-save-query-mark-synced-preserves-sort-and-order ()
"Mark-synced doesn't drop `:sort' or `:order' from the entry."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t) :sort updated :order desc)))))
(cl-letf (((symbol-function 'customize-save-variable) (lambda (_ _) nil)))
(pearl--save-query-mark-synced "Q" nil nil "view-uuid"))
(let ((spec (cdr (assoc "Q" pearl-saved-queries))))
(should (eq 'updated (plist-get spec :sort)))
(should (eq 'desc (plist-get spec :order))))))
(ert-deftest test-pearl-save-query-mark-synced-personal-nil-team ()
"Personal scope persists `:linear-view-team-id' as nil and `:linear-view-shared' nil."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t))))))
(cl-letf (((symbol-function 'customize-save-variable) (lambda (_ _) nil)))
(pearl--save-query-mark-synced "Q" nil nil "view-uuid"))
(let ((spec (cdr (assoc "Q" pearl-saved-queries))))
(should (null (plist-get spec :linear-view-team-id)))
(should (null (plist-get spec :linear-view-shared))))))
(ert-deftest test-pearl-save-query-mark-synced-unknown-name-signals-user-error ()
"Mark-synced on a name that doesn't exist signals a `user-error'."
(let ((pearl-saved-queries '()))
(should-error
(pearl--save-query-mark-synced "Q" nil nil "view-uuid")
:type 'user-error)))
(ert-deftest test-pearl-save-query-mark-synced-stores-view-url-when-given ()
"The optional VIEW-URL arg lands in the entry as :linear-view-url."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t))))))
(cl-letf (((symbol-function 'customize-save-variable) (lambda (_ _) nil)))
(pearl--save-query-mark-synced
"Q" nil nil "view-uuid" "https://linear.app/ws/view/abc"))
(let ((spec (cdr (assoc "Q" pearl-saved-queries))))
(should (equal "https://linear.app/ws/view/abc"
(plist-get spec :linear-view-url))))))
(ert-deftest test-pearl-save-query-mark-synced-keeps-prior-url-when-omitted ()
"Omitting VIEW-URL on a re-sync preserves any previously stored URL.
Re-sync paths whose API response doesn't carry a URL shouldn't erase a
URL the create path already wrote."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t)
:linear-view-id "old-id"
:linear-view-team-id nil
:linear-view-shared :json-false
:linear-view-url "https://linear.app/ws/view/old")))))
(cl-letf (((symbol-function 'customize-save-variable) (lambda (_ _) nil)))
;; No VIEW-URL passed -- the prior URL should survive.
(pearl--save-query-mark-synced "Q" nil nil "new-id"))
(let ((spec (cdr (assoc "Q" pearl-saved-queries))))
(should (equal "https://linear.app/ws/view/old"
(plist-get spec :linear-view-url))))))
(ert-deftest test-pearl-save-query-mark-synced-preserves-unknown-plist-keys ()
"Unknown keys on a saved-query spec survive a mark-synced call.
The earlier implementation rebuilt the spec from a fixed key set and
silently dropped anything outside it. Copy-then-plist-put preserves
arbitrary keys so a future schema field or user annotation isn't
erased on every re-sync."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t)
:sort updated
:order desc
:description "private note about this query"
:user-tag "experimental")))))
(cl-letf (((symbol-function 'customize-save-variable) (lambda (_ _) nil)))
(pearl--save-query-mark-synced "Q" "team-uuid" t "view-uuid" "https://x"))
(let ((spec (cdr (assoc "Q" pearl-saved-queries))))
;; Original keys survive.
(should (equal '(:open t) (plist-get spec :filter)))
(should (eq 'updated (plist-get spec :sort)))
(should (eq 'desc (plist-get spec :order)))
(should (equal "private note about this query"
(plist-get spec :description)))
(should (equal "experimental" (plist-get spec :user-tag)))
;; New sync keys layered on top.
(should (equal "view-uuid" (plist-get spec :linear-view-id)))
(should (equal "team-uuid" (plist-get spec :linear-view-team-id)))
(should (eq t (plist-get spec :linear-view-shared)))
(should (equal "https://x" (plist-get spec :linear-view-url))))))
(ert-deftest test-pearl-save-query-mark-synced-overwrites-prior-sync-metadata ()
"Re-syncing an already-synced entry overwrites the four `:linear-view-*' keys."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t)
:linear-view-id "OLD-id"
:linear-view-team-id "OLD-team"
:linear-view-shared :json-false
:linear-view-synced-at "1970-01-01T00:00:00Z")))))
(cl-letf (((symbol-function 'customize-save-variable) (lambda (_ _) nil)))
(pearl--save-query-mark-synced "Q" "NEW-team" t "NEW-id"))
(let ((spec (cdr (assoc "Q" pearl-saved-queries))))
(should (equal "NEW-id" (plist-get spec :linear-view-id)))
(should (equal "NEW-team" (plist-get spec :linear-view-team-id)))
(should (eq t (plist-get spec :linear-view-shared)))
(should-not (equal "1970-01-01T00:00:00Z"
(plist-get spec :linear-view-synced-at))))))
;;; pearl--sync-record-or-orphan-error
(ert-deftest test-pearl-sync-record-orphan-error-names-view-id-on-persist-failure ()
"When persist fails, the message names the orphan view id explicitly."
(let ((msg nil))
(cl-letf (((symbol-function 'pearl--save-query-mark-synced)
(lambda (&rest _) (error "disk full")))
((symbol-function 'message)
(lambda (fmt &rest args)
(setq msg (apply #'format fmt args)))))
(let ((result (pearl--sync-record-or-orphan-error
"Q" nil nil "ORPHAN-UUID-XYZ" "Synced")))
(should (null result))
(should (string-match-p "ORPHAN-UUID-XYZ" msg))))))
(ert-deftest test-pearl-sync-record-orphan-error-success-returns-t-and-includes-view-id ()
"When persist succeeds, the helper returns t and the message names the view id."
(let ((msg nil))
(cl-letf (((symbol-function 'pearl--save-query-mark-synced)
(lambda (&rest _) t))
((symbol-function 'message)
(lambda (fmt &rest args)
(setq msg (apply #'format fmt args)))))
(let ((result (pearl--sync-record-or-orphan-error
"Q" nil nil "view-uuid" "Synced")))
(should (eq t result))
(should (string-match-p "view-uuid" msg))
(should (string-match-p "Q" msg))
(should (string-match-p "Synced" msg))))))
(ert-deftest test-pearl-sync-record-orphan-error-includes-scope-label-when-given ()
"An optional SCOPE-LABEL is appended in brackets to the success message."
(let ((msg nil))
(cl-letf (((symbol-function 'pearl--save-query-mark-synced)
(lambda (&rest _) t))
((symbol-function 'message)
(lambda (fmt &rest args)
(setq msg (apply #'format fmt args)))))
(pearl--sync-record-or-orphan-error
"Q" nil nil "view-uuid" "Synced" "[ Team: Eng, visible to the team ]")
(should (string-match-p "Team: Eng" msg)))))
(ert-deftest test-pearl-sync-record-orphan-error-update-verb-rewrites-failure-text ()
"The failure message uses the lowercase verb (\"updated\") on an update path."
(let ((msg nil))
(cl-letf (((symbol-function 'pearl--save-query-mark-synced)
(lambda (&rest _) (error "disk full")))
((symbol-function 'message)
(lambda (fmt &rest args)
(setq msg (apply #'format fmt args)))))
(pearl--sync-record-or-orphan-error
"Q" nil nil "view-uuid" "Updated")
(should (string-match-p "updated on Linear" msg)))))
;;; pearl--sync-saved-query-await — timeout detection
(ert-deftest test-pearl-sync-saved-query-await-marks-timeout-when-callback-misses ()
"A callback that never fires returns `(:success nil :timeout t)' rather than nil."
(let ((pearl-request-timeout 0.05)) ; ~50ms is plenty to fall through the wait loop
(cl-letf (((symbol-function 'noop-async)
(lambda (&rest _ignored) nil))) ; never invokes callback
(let ((result (pearl--sync-saved-query-await #'noop-async)))
(should (eq t (plist-get result :timeout)))
(should-not (plist-get result :success))))))
(ert-deftest test-pearl-sync-saved-query-await-returns-callback-result-on-completion ()
"When the callback fires, the helper returns that result verbatim (no timeout flag)."
(cl-letf (((symbol-function 'sync-fire-async)
(lambda (cb) (funcall cb (list :success t :view '((id . "X")))))))
(let ((result (pearl--sync-saved-query-await #'sync-fire-async)))
(should (eq t (plist-get result :success)))
(should-not (plist-get result :timeout)))))
;;; Re-sync normalizes a stored `:json-false' shared flag — guards the silent flip
(ert-deftest test-pearl-sync-resync-normalizes-json-false-shared-to-personal ()
"Re-sync of an entry with `:linear-view-shared :json-false' must NOT flip it shared.
`:json-false' is truthy in Elisp, so a stored `:json-false' would slip past
`(if shared t :json-false)' in the encoder and silently make the view team-shared."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t)
:linear-view-id "view-id"
:linear-view-team-id nil
:linear-view-shared :json-false
:linear-view-synced-at "2026-01-01T00:00:00Z"))))
(captured-input nil))
(cl-letf (((symbol-function 'pearl--all-teams) (lambda () '()))
((symbol-function 'pearl--validate-issue-filter) (lambda (_) t))
((symbol-function 'pearl--build-issue-filter)
(lambda (_) '(("state" ("type" ("nin" . ["completed"]))))))
((symbol-function 'pearl--progress) (lambda (&rest _) nil))
((symbol-function 'message) (lambda (&rest _) nil))
((symbol-function 'customize-save-variable) (lambda (_ _) nil))
((symbol-function 'pearl--customview-update-async)
(lambda (_view-id input cb)
(setq captured-input input)
(funcall cb (list :success t
:view '((id . "view-id")
(shared . :json-false)))))))
(pearl-sync-saved-query-to-linear "Q")
(let ((shared-value (cdr (assoc "shared" captured-input))))
(should (eq :json-false shared-value))))))
;;; pearl-sync-saved-query-to-linear — empty filter refusal
(ert-deftest test-pearl-sync-empty-filter-refuses-before-scope-prompt ()
"A first-time-sync saved query whose filter compiles to nil signals a clear
user-error before any scope prompt fires. Without the guard, the sync would
pass filterData=null to Linear, which rejects with an opaque error after the
user has already clicked through the scope picker."
(let ((pearl-saved-queries (copy-tree '(("EmptyFilter" :filter nil))))
(scope-prompt-fired nil)
(api-called nil))
(cl-letf (((symbol-function 'pearl--sync-saved-query-pick-scope)
(lambda (&rest _) (setq scope-prompt-fired t)
;; Pick-scope returns (DISPLAY . PLIST); the empty-filter
;; guard fires before this is consumed, but the stub
;; shape should still match production.
(cons "[ Personal, only I see it ]"
(list :team-id nil :team-name "Personal" :shared nil))))
((symbol-function 'pearl--customview-create-async)
(lambda (&rest _) (setq api-called t))))
(should-error (pearl-sync-saved-query-to-linear "EmptyFilter")
:type 'user-error)
(should-not scope-prompt-fired)
(should-not api-called))))
(ert-deftest test-pearl-sync-empty-filter-refuses-resync-too ()
"A re-sync (entry already has `:linear-view-id') whose filter has been
edited down to nil takes the same empty-filter guard, refusing without
calling customViewUpdate. Guards against a future refactor accidentally
hoisting the guard into the create-only branch."
(let ((pearl-saved-queries
(copy-tree '(("EmptyFilter" :filter nil
:linear-view-id "existing-view-uuid"
:linear-view-team-id nil
:linear-view-shared :json-false))))
(update-called nil))
(cl-letf (((symbol-function 'pearl--customview-update-async)
(lambda (&rest _) (setq update-called t))))
(should-error (pearl-sync-saved-query-to-linear "EmptyFilter")
:type 'user-error)
(should-not update-called))))
;;; pearl-publish-current-source
(defmacro test-pearl-publish--in-buffer (source-form &rest body)
"Run BODY in an org-mode temp buffer carrying SOURCE-FORM as #+LINEAR-SOURCE.
SOURCE-FORM is the source plist (or nil for no source header)."
(declare (indent 1))
`(with-temp-buffer
(when ,source-form
(insert (format "#+title: test\n#+LINEAR-SOURCE: %s\n\n"
(prin1-to-string ,source-form))))
(org-mode)
,@body))
(ert-deftest test-pearl-publish-current-source-no-source-header-errors ()
"A buffer with no #+LINEAR-SOURCE signals a clear user-error."
(test-pearl-publish--in-buffer nil
(should-error (pearl-publish-current-source) :type 'user-error)))
(ert-deftest test-pearl-publish-current-source-view-source-errors ()
"A view source signals user-error -- views are already on Linear."
(test-pearl-publish--in-buffer '(:type view :name "Eng dashboard" :id "v1")
(should-error (pearl-publish-current-source) :type 'user-error)))
(ert-deftest test-pearl-publish-current-source-transient-filter-no-name-errors ()
"A :type filter with no :name (transient ad-hoc) signals user-error."
(test-pearl-publish--in-buffer '(:type filter :filter (:open t))
(should-error (pearl-publish-current-source) :type 'user-error)))
(ert-deftest test-pearl-publish-current-source-no-matching-saved-query-errors ()
"A :type filter source whose name doesn't match any pearl-saved-queries entry errors."
(let ((pearl-saved-queries '(("Other" :filter (:open t)))))
(test-pearl-publish--in-buffer
'(:type filter :name "Gone" :filter (:open t))
(should-error (pearl-publish-current-source) :type 'user-error))))
(ert-deftest test-pearl-publish-current-source-dispatches-to-sync-with-name ()
"A :type filter source whose name matches dispatches to pearl-sync-saved-query-to-linear."
(let ((pearl-saved-queries '(("Mine" :filter (:open t))))
(sync-called-with nil))
(cl-letf (((symbol-function 'pearl-sync-saved-query-to-linear)
(lambda (n) (setq sync-called-with n))))
(test-pearl-publish--in-buffer
'(:type filter :name "Mine" :filter (:open t))
(pearl-publish-current-source))
(should (equal "Mine" sync-called-with)))))
(ert-deftest test-pearl-publish-current-source-empty-name-errors ()
"A :type filter source with an empty-string :name (degenerate) errors cleanly."
(test-pearl-publish--in-buffer
'(:type filter :name "" :filter (:open t))
(should-error (pearl-publish-current-source) :type 'user-error)))
(ert-deftest test-pearl-publish-current-source-whitespace-name-errors ()
"A :type filter source with a whitespace-only :name takes the no-name branch
rather than falling through to the no-matching-saved-query branch with a
visibly-blank name in the error message."
(let ((pearl-saved-queries '()))
(test-pearl-publish--in-buffer
'(:type filter :name " " :filter (:open t))
(let ((err (should-error (pearl-publish-current-source)
:type 'user-error)))
;; The message should name the source-has-no-name reason, not the
;; no-matching-query reason.
(should (string-match-p "no name" (error-message-string err)))))))
;;; Cache-shape parity — pearl-get-teams-async and pearl--all-teams must agree
(ert-deftest test-pearl-get-teams-async-query-fetches-key ()
"`pearl-get-teams-async' must fetch `key' so it can't poison the cache.
Both `pearl-get-teams-async' and `pearl--all-teams' write
`pearl--cache-teams', and downstream callers read `key' from cached
teams. If the async path queried only `id name', a fetch sequence
async→sync would leave the cache without `key' and silently break
team-key lookups."
(let (captured-query)
(cl-letf (((symbol-function 'pearl--graphql-request-async)
(lambda (query &rest _) (setq captured-query query))))
(pearl-get-teams-async)
(should captured-query)
(should (string-match-p "key" captured-query)))))
;;; pearl--customview-delete-async
(ert-deftest test-pearl-customview-delete-async-success ()
"A successful `customViewDelete' invokes the callback with `:success' t."
(testutil-linear-with-response
'((data (customViewDelete (success . t))))
(let (result)
(pearl--customview-delete-async "view-uuid" (lambda (r) (setq result r)))
(should (eq t (plist-get result :success))))))
(ert-deftest test-pearl-customview-delete-async-failure ()
"A non-success `customViewDelete' invokes the callback with `:success' nil."
(testutil-linear-with-response
'((data (customViewDelete (success . :json-false))))
(let (result)
(pearl--customview-delete-async "view-uuid" (lambda (r) (setq result r)))
(should-not (plist-get result :success)))))
;;; pearl-delete-saved-query — synced-entry second-stage prompt
(defun test-pearl--make-yes-no-stub (answers)
"Return a `yes-or-no-p' replacement that returns each ANSWERS in order.
Each call pops the head; the test fails the call sequence assertion if
more prompts fire than answers were prepared for."
(let ((remaining answers))
(lambda (&rest _)
(if remaining
(pop remaining)
(error "Unexpected extra yes-or-no-p prompt")))))
(ert-deftest test-pearl-delete-saved-query-synced-yes-yes-calls-customview-delete ()
"Synced entry, both prompts yes: customViewDelete fires and the local entry is removed."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t)
:linear-view-id "view-uuid"
:linear-view-team-id nil
:linear-view-shared :json-false))))
(delete-called-with nil))
(cl-letf (((symbol-function 'yes-or-no-p)
(test-pearl--make-yes-no-stub '(t t)))
((symbol-function 'customize-save-variable) (lambda (_ _) nil))
((symbol-function 'pearl--progress) (lambda (&rest _) nil))
((symbol-function 'message) (lambda (&rest _) nil))
((symbol-function 'pearl--customview-delete-async)
(lambda (vid cb)
(setq delete-called-with vid)
(funcall cb (list :success t)))))
(pearl-delete-saved-query "Q")
(should (equal "view-uuid" delete-called-with))
(should-not (assoc "Q" pearl-saved-queries)))))
(ert-deftest test-pearl-delete-saved-query-synced-yes-no-unlinks-only ()
"Synced entry, first prompt yes second prompt no: local entry removed, customViewDelete not called."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t)
:linear-view-id "view-uuid"))))
(delete-called nil))
(cl-letf (((symbol-function 'yes-or-no-p)
(test-pearl--make-yes-no-stub '(t nil)))
((symbol-function 'customize-save-variable) (lambda (_ _) nil))
((symbol-function 'pearl--progress) (lambda (&rest _) nil))
((symbol-function 'message) (lambda (&rest _) nil))
((symbol-function 'pearl--customview-delete-async)
(lambda (_ _) (setq delete-called t))))
(pearl-delete-saved-query "Q")
(should-not delete-called)
(should-not (assoc "Q" pearl-saved-queries)))))
(ert-deftest test-pearl-delete-saved-query-synced-api-failure-then-delete-anyway ()
"Synced entry, customViewDelete fails, user accepts delete-anyway prompt: local removed, message names the orphan id."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t)
:linear-view-id "ORPHAN-UUID"))))
(last-message nil))
(cl-letf (((symbol-function 'yes-or-no-p)
(test-pearl--make-yes-no-stub '(t t t))) ; delete? + also-on-Linear? + delete-anyway?
((symbol-function 'customize-save-variable) (lambda (_ _) nil))
((symbol-function 'pearl--progress) (lambda (&rest _) nil))
((symbol-function 'message)
(lambda (fmt &rest args)
(setq last-message (apply #'format fmt args))))
((symbol-function 'pearl--customview-delete-async)
(lambda (_ cb)
(funcall cb (list :success nil :error "permission denied")))))
(pearl-delete-saved-query "Q")
(should-not (assoc "Q" pearl-saved-queries))
(should (string-match-p "ORPHAN-UUID" last-message))
(should (string-match-p "orphan" last-message)))))
(ert-deftest test-pearl-delete-saved-query-synced-api-failure-then-keep ()
"Synced entry, customViewDelete fails, user rejects delete-anyway: both kept untouched.
Verifies the API call actually fires (so the keep branch only runs when the
delete-anyway prompt explicitly declines), guarding against a future refactor
that silently routes to the unlink branch and skips the API call."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t)
:linear-view-id "view-uuid"))))
(delete-called-with nil))
(cl-letf (((symbol-function 'yes-or-no-p)
(test-pearl--make-yes-no-stub '(t t nil))) ; yes / yes / no on delete-anyway
((symbol-function 'customize-save-variable) (lambda (_ _) nil))
((symbol-function 'pearl--progress) (lambda (&rest _) nil))
((symbol-function 'message) (lambda (&rest _) nil))
((symbol-function 'pearl--customview-delete-async)
(lambda (vid cb)
(setq delete-called-with vid)
(funcall cb (list :success nil :error "permission denied")))))
(pearl-delete-saved-query "Q")
(should (equal "view-uuid" delete-called-with))
(should (assoc "Q" pearl-saved-queries)))))
(ert-deftest test-pearl-delete-saved-query-local-only-entry-unchanged-behavior ()
"Local-only entry (no `:linear-view-id'): one yes-or-no-p, no second prompt, no API call."
(let ((pearl-saved-queries
(copy-tree '(("Q" :filter (:open t)))))
(delete-called nil)
(prompt-count 0))
(cl-letf (((symbol-function 'yes-or-no-p)
(lambda (&rest _) (cl-incf prompt-count) t))
((symbol-function 'customize-save-variable) (lambda (_ _) nil))
((symbol-function 'message) (lambda (&rest _) nil))
((symbol-function 'pearl--customview-delete-async)
(lambda (_ _) (setq delete-called t))))
(pearl-delete-saved-query "Q")
(should (= 1 prompt-count))
(should-not delete-called)
(should-not (assoc "Q" pearl-saved-queries)))))
(provide 'test-pearl-saved-query-sync)
;;; test-pearl-saved-query-sync.el ends here
|