From 4cd1e8e63d885d8de2728dc76d4f35f0eb597037 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Tue, 28 Apr 2026 14:21:18 -0500 Subject: test: add gloss-core test suite (red phase) The commit lands eight per-function test files and a shared testutil. 32 tests across Normal/Boundary/Error categories cover the public API (lookup, save, list, find-buffer-position) and the internals that need observable behavior tests (mtime invalidation, corrupt-file resilience, alphabetical insert, first-call file creation). All 32 fail with void-function on the gloss-core symbols. That is the intended red-phase signal. The next commit lands the implementation that turns them green. testutil-gloss provides a with-temp-glossary macro. It binds gloss-file to a temp file, resets the cache before and after, and cleans up the visiting buffer. --- tests/test-gloss-core--invalidate-on-mtime.el | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/test-gloss-core--invalidate-on-mtime.el (limited to 'tests/test-gloss-core--invalidate-on-mtime.el') diff --git a/tests/test-gloss-core--invalidate-on-mtime.el b/tests/test-gloss-core--invalidate-on-mtime.el new file mode 100644 index 0000000..69500dd --- /dev/null +++ b/tests/test-gloss-core--invalidate-on-mtime.el @@ -0,0 +1,49 @@ +;;; test-gloss-core--invalidate-on-mtime.el --- Tests for cache mtime invalidation -*- lexical-binding: t -*- + +;; SPDX-License-Identifier: GPL-3.0-or-later + +;;; Commentary: +;; Tests that the cache reloads when `gloss-file' is edited out-of-band. + +;;; Code: + +(require 'ert) +(require 'gloss-core) +(require 'testutil-gloss) + +(ert-deftest test-gloss-core-invalidate-on-mtime-new-content-detected () + "Normal: out-of-band edit + later lookup sees the new content." + (gloss-test--with-temp-glossary gloss-test--sample-content + ;; Prime cache. + (should (gloss-core-lookup "anaphora")) + ;; Append a new entry directly to disk. + (with-temp-buffer + (insert-file-contents gloss-file) + (goto-char (point-max)) + (insert "\n* hapax\n:PROPERTIES:\n:SOURCE: manual\n:ADDED: 2026-04-28\n:END:\nA word used only once in a corpus.\n") + (write-region (point-min) (point-max) gloss-file)) + ;; Force mtime change to be visible (1-second granularity on some FSes). + (set-file-times gloss-file (time-add (current-time) 5)) + ;; Lookup detects the mtime change and reloads. + (let ((entry (gloss-core-lookup "hapax"))) + (should entry) + (should (string-match-p "used only once" (plist-get entry :body)))))) + +(ert-deftest test-gloss-core-invalidate-on-mtime-unchanged-uses-cache () + "Boundary: unchanged mtime — two consecutive lookups both succeed." + (gloss-test--with-temp-glossary gloss-test--sample-content + (should (gloss-core-lookup "anaphora")) + (should (gloss-core-lookup "anaphora")))) + +(ert-deftest test-gloss-core-invalidate-on-mtime-deleted-file-clears-cache () + "Error: file deleted out-of-band — subsequent lookup returns nil." + (gloss-test--with-temp-glossary gloss-test--sample-content + (should (gloss-core-lookup "anaphora")) + (when-let ((buf (find-buffer-visiting gloss-file))) + (with-current-buffer buf (set-buffer-modified-p nil)) + (kill-buffer buf)) + (delete-file gloss-file) + (should-not (gloss-core-lookup "anaphora")))) + +(provide 'test-gloss-core--invalidate-on-mtime) +;;; test-gloss-core--invalidate-on-mtime.el ends here -- cgit v1.2.3