From dc0db0f0e12d8af6d1d54a5dde1cd16cf890a33d Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Tue, 28 Apr 2026 18:10:17 -0500 Subject: 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. --- tests/test-gloss-fetch--strip-html.el | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/test-gloss-fetch--strip-html.el (limited to 'tests/test-gloss-fetch--strip-html.el') diff --git a/tests/test-gloss-fetch--strip-html.el b/tests/test-gloss-fetch--strip-html.el new file mode 100644 index 0000000..61f7e70 --- /dev/null +++ b/tests/test-gloss-fetch--strip-html.el @@ -0,0 +1,59 @@ +;;; test-gloss-fetch--strip-html.el --- HTML strip helper tests -*- lexical-binding: t -*- + +;; SPDX-License-Identifier: GPL-3.0-or-later + +;;; Commentary: +;; `gloss-fetch--strip-html' converts a fragment of HTML to plain text +;; using `libxml-parse-html-region' (no italic/bold preservation). +;; Returns nil when libxml fails on the fragment so the caller can drop +;; that sense. + +;;; Code: + +(require 'ert) +(require 'gloss-fetch) + +(ert-deftest test-gloss-fetch-strip-html-plain-text-roundtrips () + "Normal: a string with no markup comes back unchanged (modulo whitespace)." + (skip-unless (fboundp 'libxml-parse-html-region)) + (should (equal (gloss-fetch--strip-html "Just plain text.") + "Just plain text."))) + +(ert-deftest test-gloss-fetch-strip-html-removes-tags () + "Normal: anchor and span tags are stripped, leaving inner text." + (skip-unless (fboundp 'libxml-parse-html-region)) + (let ((stripped (gloss-fetch--strip-html + "The repetition of a phrase."))) + (should (string-match-p "repetition" stripped)) + (should (string-match-p "phrase" stripped)) + (should-not (string-match-p "<" stripped)) + (should-not (string-match-p "href" stripped)))) + +(ert-deftest test-gloss-fetch-strip-html-empty-string-returns-empty () + "Boundary: empty input returns an empty string (or nil)." + (skip-unless (fboundp 'libxml-parse-html-region)) + (let ((result (gloss-fetch--strip-html ""))) + (should (or (null result) (equal result ""))))) + +(ert-deftest test-gloss-fetch-strip-html-entities-decoded () + "Boundary: HTML entities decode to their characters." + (skip-unless (fboundp 'libxml-parse-html-region)) + (let ((stripped (gloss-fetch--strip-html "Smith & Wesson"))) + (should (string-match-p "&" stripped)) + (should-not (string-match-p "&" stripped)))) + +(ert-deftest test-gloss-fetch-strip-html-collapses-whitespace () + "Boundary: runs of internal whitespace collapse to single spaces; result is trimmed." + (skip-unless (fboundp 'libxml-parse-html-region)) + (let ((stripped (gloss-fetch--strip-html " hello world "))) + (should (equal stripped "hello world")))) + +(ert-deftest test-gloss-fetch-strip-html-failure-returns-nil () + "Error: when libxml-parse-html-region raises, return nil so the caller can drop the sense." + (skip-unless (fboundp 'libxml-parse-html-region)) + (cl-letf (((symbol-function 'libxml-parse-html-region) + (lambda (&rest _) (error "libxml exploded")))) + (should-not (gloss-fetch--strip-html "any input")))) + +(provide 'test-gloss-fetch--strip-html) +;;; test-gloss-fetch--strip-html.el ends here -- cgit v1.2.3