aboutsummaryrefslogtreecommitdiff
path: root/tests/test-gloss-fetch--definitions-200-returns-ok.el
blob: fee997b0f3a80bc55080d72fbab080c35567c10a (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
;;; test-gloss-fetch--definitions-200-returns-ok.el --- 200 path tests -*- lexical-binding: t -*-

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

;;; Commentary:
;; Normal/Boundary cases: a 200 response with valid JSON returns
;; (:ok DEFS) and each def is a plist with :source and :text.  Uses the
;; captured Wiktionary fixtures replayed through a mocked
;; `url-retrieve-synchronously'.

;;; Code:

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

(ert-deftest test-gloss-fetch-definitions-200-anaphora-returns-ok ()
  "Normal: anaphora fixture (single English sense) returns (:ok DEFS)."
  (let ((body (gloss-test--load-wiktionary-fixture "anaphora")))
    (gloss-fetch-test--with-mocked-url
        (lambda (_url) (gloss-fetch-test--ok-response body))
      (let* ((result (gloss-fetch-definitions "anaphora"))
             (defs (plist-get result :ok)))
        (should (eq (car result) :ok))
        (should (consp defs))
        (should (>= (length defs) 1))
        (let ((first (car defs)))
          (should (eq (plist-get first :source) 'wiktionary))
          (should (stringp (plist-get first :text)))
          (should (> (length (plist-get first :text)) 0))
          ;; HTML stripped — no angle brackets in the text.
          (should-not (string-match-p "<" (plist-get first :text))))))))

(ert-deftest test-gloss-fetch-definitions-200-sbir-returns-multiple-senses ()
  "Boundary: SBIR fixture has multiple senses; all returned as separate plists."
  (let ((body (gloss-test--load-wiktionary-fixture "SBIR")))
    (gloss-fetch-test--with-mocked-url
        (lambda (_url) (gloss-fetch-test--ok-response body))
      (let* ((result (gloss-fetch-definitions "SBIR"))
             (defs (plist-get result :ok)))
        (should (eq (car result) :ok))
        (should (>= (length defs) 1))
        (dolist (d defs)
          (should (eq (plist-get d :source) 'wiktionary))
          (should (stringp (plist-get d :text))))))))

(ert-deftest test-gloss-fetch-definitions-200-encodes-spaces-in-url ()
  "Boundary: a multi-word term URL-encodes the space."
  (let ((seen-url nil)
        (body "{}"))
    (gloss-fetch-test--with-mocked-url
        (lambda (url)
          (setq seen-url url)
          (gloss-fetch-test--ok-response body))
      (gloss-fetch-definitions "hapax legomenon"))
    (should seen-url)
    (should (string-match-p "hapax%20legomenon\\|hapax_legomenon" seen-url))))

(provide 'test-gloss-fetch--definitions-200-returns-ok)
;;; test-gloss-fetch--definitions-200-returns-ok.el ends here