diff options
| author | Craig Jennings <c@cjennings.net> | 2026-04-28 18:10:17 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-04-28 19:09:22 -0500 |
| commit | dc0db0f0e12d8af6d1d54a5dde1cd16cf890a33d (patch) | |
| tree | 9d174e136d62981c1bded06fe423d08bddc91e04 /tests/testutil-gloss-fetch.el | |
| parent | 3a846506399dc12ab219bfa8047947c122dd1d04 (diff) | |
| download | gloss-dc0db0f0e12d8af6d1d54a5dde1cd16cf890a33d.tar.gz gloss-dc0db0f0e12d8af6d1d54a5dde1cd16cf890a33d.zip | |
test: add gloss-fetch test suite (red phase)
Eight test files cover the network layer's public and internal contract.
The boundary mock is `url-retrieve-synchronously', wrapped by a small
`testutil-gloss-fetch' helper that builds response buffers in the shape
the url library returns.
Tests cover the 200 happy paths (anaphora and SBIR fixtures), 404 to
:no-defs, 5xx and 4xx-other and malformed JSON to :server-error, 429 to
:rate-limited, nil-from-url to :unreachable, the libxml availability
probe (one-shot, signals user-error when absent), the registry walker
ordering, and the pure HTML strip helper across N/B/E.
Tests fail on missing `gloss-fetch--*' functions, as expected for red
phase.
Diffstat (limited to 'tests/testutil-gloss-fetch.el')
| -rw-r--r-- | tests/testutil-gloss-fetch.el | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/testutil-gloss-fetch.el b/tests/testutil-gloss-fetch.el new file mode 100644 index 0000000..a144d91 --- /dev/null +++ b/tests/testutil-gloss-fetch.el @@ -0,0 +1,48 @@ +;;; testutil-gloss-fetch.el --- Test helpers for gloss-fetch -*- lexical-binding: t -*- + +;; SPDX-License-Identifier: GPL-3.0-or-later + +;;; Commentary: + +;; Helpers for tests that exercise `gloss-fetch'. The boundary mock is +;; `url-retrieve-synchronously', so these helpers build response buffers +;; in the shape Emacs's url library returns: status line, blank line, +;; body. The body comes from a JSON fixture loaded via +;; `gloss-test--load-wiktionary-fixture' (provided by testutil-gloss.el +;; on a parallel branch). + +;;; Code: + +(defun gloss-fetch-test--make-response-buffer (status-line body) + "Return a fresh buffer containing STATUS-LINE, a blank line, and BODY. +STATUS-LINE is an HTTP status line such as \"HTTP/1.1 200 OK\". BODY +is the response body as a string. The buffer is unibyte so that +multibyte handling is exercised end-to-end." + (let ((buf (generate-new-buffer " *gloss-fetch-test-response*"))) + (with-current-buffer buf + (set-buffer-multibyte nil) + (insert status-line "\n") + (insert "Content-Type: application/json\n") + (insert "\n") + (insert (encode-coding-string (or body "") 'utf-8))) + buf)) + +(defmacro gloss-fetch-test--with-mocked-url (response-fn &rest body) + "Run BODY with `url-retrieve-synchronously' replaced by RESPONSE-FN. +RESPONSE-FN takes the URL string and returns either a buffer (the +response) or nil (to simulate timeout / unreachable)." + (declare (indent 1) (debug t)) + `(cl-letf (((symbol-function 'url-retrieve-synchronously) + (lambda (url &rest _args) (funcall ,response-fn url)))) + ,@body)) + +(defun gloss-fetch-test--ok-response (body) + "Return a 200 OK response buffer with BODY." + (gloss-fetch-test--make-response-buffer "HTTP/1.1 200 OK" body)) + +(defun gloss-fetch-test--status-response (status-line &optional body) + "Return a response buffer with STATUS-LINE and optional BODY (default empty)." + (gloss-fetch-test--make-response-buffer status-line (or body ""))) + +(provide 'testutil-gloss-fetch) +;;; testutil-gloss-fetch.el ends here |
