aboutsummaryrefslogtreecommitdiff
path: root/tests/test-gloss-fetch--multi-source-walks-registry.el
blob: cb2b730028cb72fb3bb193259fcb0559ac3f5964 (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
;;; test-gloss-fetch--multi-source-walks-registry.el --- Registry walk tests -*- lexical-binding: t -*-

;; SPDX-License-Identifier: GPL-3.0-or-later

;;; Commentary:
;; `gloss-fetch--sources' is an alist (source-symbol → fetcher fn).
;; `gloss-fetch-sources' is the user-facing defcustom that orders the
;; walk.  Each fetcher returns a per-source result plist.  The walker
;; aggregates results across all configured sources.

;;; Code:

(require 'ert)
(require 'cl-lib)
(require 'gloss-fetch)

(ert-deftest test-gloss-fetch-collect-walks-registry-in-defcustom-order ()
  "Normal: walker visits each source in `gloss-fetch-sources' order, gathers a result per source."
  (let* ((calls nil)
         (gloss-fetch--sources
          `((alpha . ,(lambda (term)
                        (push 'alpha calls)
                        (list :source 'alpha :status :ok :defs
                              (list (list :source 'alpha :text (concat "A:" term))))))
            (beta . ,(lambda (term)
                       (push 'beta calls)
                       (list :source 'beta :status :no-defs
                             :reason (concat "no entry for " term))))))
         (gloss-fetch-sources '(alpha beta))
         (per-source (gloss-fetch--collect "x")))
    (should (equal (reverse calls) '(alpha beta)))
    (should (= 2 (length per-source)))
    (should (eq 'alpha (plist-get (nth 0 per-source) :source)))
    (should (eq 'beta  (plist-get (nth 1 per-source) :source)))))

(ert-deftest test-gloss-fetch-rollup-mixes-no-defs-and-failed ()
  "Boundary: a mix of :no-defs and :server-error rolls up with both lists populated."
  (let* ((gloss-fetch--sources
          `((alpha . ,(lambda (_term)
                        (list :source 'alpha :status :no-defs :reason "404")))
            (beta . ,(lambda (_term)
                       (list :source 'beta :status :server-error :reason "HTTP 503")))))
         (gloss-fetch-sources '(alpha beta))
         (result (gloss-fetch-definitions "x")))
    (should (eq (car result) :empty))
    (should (member 'alpha (plist-get (cdr result) :no-defs)))
    (should (member 'beta (plist-get (cdr result) :failed)))))

(ert-deftest test-gloss-fetch-rollup-any-ok-yields-ok ()
  "Boundary: if any source returns :ok with defs, the rollup is (:ok DEFS)."
  (let* ((gloss-fetch--sources
          `((alpha . ,(lambda (_term)
                        (list :source 'alpha :status :no-defs :reason "404")))
            (beta . ,(lambda (_term)
                       (list :source 'beta :status :ok :defs
                             (list (list :source 'beta :text "got it")))))))
         (gloss-fetch-sources '(alpha beta))
         (result (gloss-fetch-definitions "x")))
    (should (eq (car result) :ok))
    (should (= 1 (length (plist-get result :ok))))
    (should (equal "got it" (plist-get (car (plist-get result :ok)) :text)))))

(ert-deftest test-gloss-fetch-collect-skips-source-not-in-defcustom ()
  "Error: a source registered in `gloss-fetch--sources' but not in `gloss-fetch-sources' is skipped."
  (let* ((called nil)
         (gloss-fetch--sources
          `((alpha . ,(lambda (_term)
                        (setq called t)
                        (list :source 'alpha :status :ok :defs
                              (list (list :source 'alpha :text "x")))))))
         (gloss-fetch-sources '()) ; empty — skip everything
         (per-source (gloss-fetch--collect "x")))
    (should-not called)
    (should (null per-source))))

(ert-deftest test-gloss-fetch-collect-skips-unknown-source-symbol ()
  "Error: a symbol in `gloss-fetch-sources' with no registered fetcher is silently skipped."
  (let* ((gloss-fetch--sources
          `((alpha . ,(lambda (_term)
                        (list :source 'alpha :status :ok :defs
                              (list (list :source 'alpha :text "x")))))))
         (gloss-fetch-sources '(zeta alpha))
         (per-source (gloss-fetch--collect "x")))
    (should (= 1 (length per-source)))
    (should (eq 'alpha (plist-get (car per-source) :source)))))

(provide 'test-gloss-fetch--multi-source-walks-registry)
;;; test-gloss-fetch--multi-source-walks-registry.el ends here