aboutsummaryrefslogtreecommitdiff
path: root/tests/test-gloss-core--invalidate-on-mtime.el
blob: 69500dd04e8bc392d958a3ce2c8e0ffb80cc0f93 (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
;;; 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