aboutsummaryrefslogtreecommitdiff
path: root/tests/test-cj-cache-lib.el
blob: aeb329dda6b5096db909e4aad6f9be05833eba6f (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;;; test-cj-cache-lib.el --- Tests for cj-cache-lib.el -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for the TTL+building cache helper.  Covers cache-make /
;; cache-valid-p / cache-value-or-rebuild / cache-building-p /
;; cache-invalidate against the contract in
;; docs/design/cache-helper-design.org.

;;; Code:

(require 'ert)
(require 'cl-lib)

(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'cj-cache-lib)

;;; cj/cache-make

(ert-deftest test-cj-cache-make-default-ttl ()
  "Normal: a fresh cache has the default TTL when none specified."
  (let ((c (cj/cache-make)))
    (should (= 3600 (plist-get c :ttl)))
    (should-not (plist-get c :value))
    (should-not (plist-get c :time))
    (should-not (plist-get c :building))))

(ert-deftest test-cj-cache-make-custom-ttl ()
  "Normal: an explicit :ttl keyword sets the TTL field."
  (let ((c (cj/cache-make :ttl 60)))
    (should (= 60 (plist-get c :ttl)))))

;;; cj/cache-valid-p

(ert-deftest test-cj-cache-valid-fresh-cache-invalid ()
  "Boundary: a fresh cache with no value is not valid."
  (let ((c (cj/cache-make)))
    (should-not (cj/cache-valid-p c))))

(ert-deftest test-cj-cache-valid-recent-build-valid ()
  "Normal: a cache built one second ago is valid."
  (let ((c (cj/cache-make :ttl 60)))
    (plist-put c :value '(file1 file2))
    (plist-put c :time (- (float-time) 1))
    (should (cj/cache-valid-p c))))

(ert-deftest test-cj-cache-valid-expired-cache-invalid ()
  "Boundary: a cache older than TTL is invalid."
  (let ((c (cj/cache-make :ttl 60)))
    (plist-put c :value '(file1))
    (plist-put c :time (- (float-time) 120))
    (should-not (cj/cache-valid-p c))))

(ert-deftest test-cj-cache-valid-nil-value-treated-invalid ()
  "Boundary: a nil cached value reads as invalid -- a build that
returned nil legitimately will rebuild on the next request, matching
the prior agenda/refile contract."
  (let ((c (cj/cache-make :ttl 60)))
    (plist-put c :value nil)
    (plist-put c :time (float-time))
    (should-not (cj/cache-valid-p c))))

;;; cj/cache-value-or-rebuild

(ert-deftest test-cj-cache-value-or-rebuild-miss-calls-build ()
  "Normal: a fresh cache calls BUILD-FN and stores its result."
  (let* ((c (cj/cache-make))
         (build-calls 0)
         (result (cj/cache-value-or-rebuild
                  c
                  (lambda () (cl-incf build-calls) '(a b c)))))
    (should (= 1 build-calls))
    (should (equal '(a b c) result))
    (should (equal '(a b c) (plist-get c :value)))
    (should (numberp (plist-get c :time)))))

(ert-deftest test-cj-cache-value-or-rebuild-hit-skips-build ()
  "Normal: a valid cache returns the stored value without calling BUILD-FN."
  (let* ((c (cj/cache-make :ttl 60))
         (build-calls 0))
    (plist-put c :value '(cached))
    (plist-put c :time (- (float-time) 1))
    (let ((result (cj/cache-value-or-rebuild
                   c
                   (lambda () (cl-incf build-calls) '(rebuilt)))))
      (should (= 0 build-calls))
      (should (equal '(cached) result)))))

(ert-deftest test-cj-cache-value-or-rebuild-force-rebuild-overrides-hit ()
  "Normal: :force-rebuild bypasses a valid cache."
  (let* ((c (cj/cache-make :ttl 60))
         (build-calls 0))
    (plist-put c :value '(cached))
    (plist-put c :time (- (float-time) 1))
    (let ((result (cj/cache-value-or-rebuild
                   c
                   (lambda () (cl-incf build-calls) '(rebuilt))
                   :force-rebuild t)))
      (should (= 1 build-calls))
      (should (equal '(rebuilt) result)))))

(ert-deftest test-cj-cache-value-or-rebuild-on-hit-fires ()
  "Normal: :on-hit fires with the cached value when valid."
  (let* ((c (cj/cache-make :ttl 60))
         (hit-with nil))
    (plist-put c :value '(cached))
    (plist-put c :time (- (float-time) 1))
    (cj/cache-value-or-rebuild
     c
     (lambda () '(rebuilt))
     :on-hit (lambda (v) (setq hit-with v)))
    (should (equal '(cached) hit-with))))

(ert-deftest test-cj-cache-value-or-rebuild-on-build-callbacks-fire ()
  "Normal: :on-build-start and :on-build-success fire on a miss."
  (let* ((c (cj/cache-make))
         (events nil))
    (cj/cache-value-or-rebuild
     c
     (lambda () '(built))
     :on-build-start (lambda () (push 'start events))
     :on-build-success (lambda (v) (push (cons 'success v) events)))
    (should (equal '((success built) start) events))))

(ert-deftest test-cj-cache-value-or-rebuild-on-build-error-fires-and-rethrows ()
  "Error: :on-build-error fires with the error and the helper rethrows."
  (let* ((c (cj/cache-make))
         (caught-err nil))
    (should-error
     (cj/cache-value-or-rebuild
      c
      (lambda () (error "boom"))
      :on-build-error (lambda (err) (setq caught-err err))))
    (should caught-err)))

(ert-deftest test-cj-cache-value-or-rebuild-clears-building-flag-on-error ()
  "Boundary: building flag is cleared even when BUILD-FN signals."
  (let ((c (cj/cache-make)))
    (ignore-errors
      (cj/cache-value-or-rebuild
       c
       (lambda () (error "boom"))))
    (should-not (cj/cache-building-p c))))

(ert-deftest test-cj-cache-value-or-rebuild-clears-building-flag-on-success ()
  "Normal: building flag is cleared after a successful build."
  (let ((c (cj/cache-make)))
    (cj/cache-value-or-rebuild c (lambda () 'ok))
    (should-not (cj/cache-building-p c))))

;;; cj/cache-invalidate

(ert-deftest test-cj-cache-invalidate-clears-value-and-time ()
  "Normal: invalidate resets value and time, keeps TTL."
  (let ((c (cj/cache-make :ttl 60)))
    (plist-put c :value '(some))
    (plist-put c :time (float-time))
    (cj/cache-invalidate c)
    (should-not (plist-get c :value))
    (should-not (plist-get c :time))
    (should (= 60 (plist-get c :ttl)))))

(provide 'test-cj-cache-lib)
;;; test-cj-cache-lib.el ends here