aboutsummaryrefslogtreecommitdiff
path: root/tests/test-pearl-adhoc.el
blob: 2bcc5758c25d57092d11ded703e0833f7d997d16 (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
154
155
156
157
;;; test-pearl-adhoc.el --- Tests for the ad-hoc filtered command -*- 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 ad-hoc filter command: the pure `--assemble-filter' that
;; turns chosen dimension values into a filter plist, `--save-query', and
;; `pearl-list-issues-filtered' running and optionally saving.

;;; Code:

(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))
(require 'cl-lib)

;;; --filter-none sentinel helpers

(ert-deftest test-pearl-filter-none-value-p-recognizes-sentinel ()
  "The sentinel string reads as a `no constraint' value."
  (should (pearl--filter-none-value-p pearl--filter-none)))

(ert-deftest test-pearl-filter-none-value-p-recognizes-empty-and-nil ()
  "Empty input and nil also read as `no constraint' (for back-compat)."
  (should (pearl--filter-none-value-p ""))
  (should (pearl--filter-none-value-p nil)))

(ert-deftest test-pearl-filter-none-value-p-rejects-real-values ()
  "An actual picked value does not read as `no constraint'."
  (should-not (pearl--filter-none-value-p "In Progress"))
  (should-not (pearl--filter-none-value-p "Platform"))
  (should-not (pearl--filter-none-value-p "bug")))

(ert-deftest test-pearl-with-none-prepends-sentinel ()
  "`pearl--with-none' puts the sentinel first so it sits at the top of the
completing-read candidate list."
  (let ((wrapped (pearl--with-none '("alpha" "beta"))))
    (should (string= pearl--filter-none (car wrapped)))
    (should (equal '("alpha" "beta") (cdr wrapped)))))

(ert-deftest test-pearl-with-none-empty-list-yields-just-the-sentinel ()
  "An empty candidate list still yields a one-element list with the sentinel."
  (let ((wrapped (pearl--with-none '())))
    (should (equal (list pearl--filter-none) wrapped))))

(ert-deftest test-pearl-completion-table-keep-order-metadata-pins-identity ()
  "Action `metadata' returns an alist with `display-sort-function' bound to
`identity', the standard Emacs hook saying \"these are pre-sorted; don't re-sort.\""
  (let* ((tbl (pearl--completion-table-keep-order '("a" "b" "c")))
         (meta (funcall tbl "" nil 'metadata)))
    (should (eq 'metadata (car meta)))
    (should (eq #'identity (alist-get 'display-sort-function (cdr meta))))
    (should (eq #'identity (alist-get 'cycle-sort-function (cdr meta))))))

(ert-deftest test-pearl-completion-table-keep-order-delegates-all-completions ()
  "Non-metadata actions delegate to `complete-with-action' over the original
candidate list, so completion still works the way the framework expects."
  (let ((tbl (pearl--completion-table-keep-order '("alpha" "beta" "gamma"))))
    (should (equal '("alpha") (all-completions "a" tbl)))
    (should (equal '("beta") (all-completions "b" tbl)))))

;;; --assemble-filter

(ert-deftest test-pearl-assemble-filter-only-set-keys ()
  "Only the dimensions that were chosen appear in the filter plist."
  (let ((f (pearl--assemble-filter nil t nil nil nil nil)))
    (should (eq t (plist-get f :open)))
    (should-not (plist-member f :team))
    (should-not (plist-member f :labels))))

(ert-deftest test-pearl-assemble-filter-full ()
  "All chosen dimensions land in the plist."
  (let ((f (pearl--assemble-filter "ENG" t "In Progress" "Foo" '("bug" "p1") :me)))
    (should (string= "ENG" (plist-get f :team)))
    (should (eq t (plist-get f :open)))
    (should (string= "In Progress" (plist-get f :state)))
    (should (string= "Foo" (plist-get f :project)))
    (should (equal '("bug" "p1") (plist-get f :labels)))
    (should (eq :me (plist-get f :assignee)))))

(ert-deftest test-pearl-assemble-filter-empty-labels-omitted ()
  "An empty label list does not add a :labels key."
  (let ((f (pearl--assemble-filter nil nil nil nil '() nil)))
    (should-not (plist-member f :labels))))

(ert-deftest test-pearl-assemble-filter-string-assignee-is-assignee-id ()
  "A string assignee (resolved member id) lands as :assignee-id, not :assignee."
  (let ((f (pearl--assemble-filter nil t nil nil nil "user-1")))
    (should (string= "user-1" (plist-get f :assignee-id)))
    (should-not (plist-member f :assignee))))

;;; --save-query

(ert-deftest test-pearl-save-query-adds-entry ()
  "Saving a query adds it to the saved-queries variable."
  (let ((pearl-saved-queries nil))
    (cl-letf (((symbol-function 'customize-save-variable) (lambda (&rest _) nil)))
      (pearl--save-query "My filter" '(:open t :labels ("bug")))
      (let ((entry (assoc "My filter" pearl-saved-queries)))
        (should entry)
        (should (equal '(:open t :labels ("bug")) (plist-get (cdr entry) :filter)))))))

(ert-deftest test-pearl-save-query-replaces-same-name ()
  "Saving under an existing name replaces that entry rather than duplicating."
  (let ((pearl-saved-queries '(("Dup" :filter (:open t)))))
    (cl-letf (((symbol-function 'customize-save-variable) (lambda (&rest _) nil)))
      (pearl--save-query "Dup" '(:priority 1))
      (should (= 1 (cl-count "Dup" pearl-saved-queries
                             :key #'car :test #'string=)))
      (should (equal '(:priority 1)
                     (plist-get (cdr (assoc "Dup" pearl-saved-queries)) :filter))))))

;;; pearl-list-issues-filtered

(ert-deftest test-pearl-list-issues-filtered-runs-with-source ()
  "Running an ad-hoc filter compiles it and renders with a filter source."
  (let ((built nil) (rendered-source nil))
    (cl-letf (((symbol-function 'pearl--build-issue-filter)
               (lambda (plist) (setq built plist) '((compiled . t))))
              ((symbol-function 'pearl--query-issues-async)
               (lambda (_filter cb &optional _ord)
                 (funcall cb (pearl--make-query-result 'ok :issues nil))))
              ((symbol-function 'pearl--render-query-result)
               (lambda (_result source) (setq rendered-source source))))
      (pearl-list-issues-filtered '(:assignee :me :open t) nil)
      (should (equal '(:assignee :me :open t) built))
      (should (eq 'filter (plist-get rendered-source :type)))
      (should (equal '(:assignee :me :open t) (plist-get rendered-source :filter))))))

(ert-deftest test-pearl-list-issues-filtered-saves-when-named ()
  "Passing a save name persists the ad-hoc filter as a saved query."
  (let ((pearl-saved-queries nil))
    (cl-letf (((symbol-function 'pearl--build-issue-filter) (lambda (_p) nil))
              ((symbol-function 'pearl--query-issues-async)
               (lambda (_f cb &optional _o)
                 (funcall cb (pearl--make-query-result 'empty :issues nil))))
              ((symbol-function 'pearl--render-query-result) (lambda (&rest _) nil))
              ((symbol-function 'customize-save-variable) (lambda (&rest _) nil)))
      (pearl-list-issues-filtered '(:open t) "Saved adhoc")
      (should (assoc "Saved adhoc" pearl-saved-queries)))))

(provide 'test-pearl-adhoc)
;;; test-pearl-adhoc.el ends here