aboutsummaryrefslogtreecommitdiff
path: root/tests/test-wttrin--get-cached-or-fetch.el
blob: 9bc393dfbc041497e905951b1fc40e608f5c6246 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
;;; test-wttrin--get-cached-or-fetch.el --- Tests for wttrin--get-cached-or-fetch -*- lexical-binding: t; -*-

;; Copyright (C) 2025-2026 Craig Jennings

;;; Commentary:

;; Unit tests for wttrin--get-cached-or-fetch function.
;; Tests the core cache workflow: cache hits, misses, and error fallback.
;; TTL-based expiration has been removed — cached data is served regardless
;; of age, with proactive refresh keeping data fresh in the background.

;;; Code:

(require 'ert)
(require 'wttrin)
(require 'testutil-wttrin)

;;; Test Data Fixtures

(defconst test-wttrin--get-cached-or-fetch-sample-weather
  "Weather for Paris: Sunny 22°C"
  "Sample weather data for testing.")

(defconst test-wttrin--get-cached-or-fetch-new-weather
  "Weather for Paris: Cloudy 18°C"
  "Updated weather data for testing cache updates.")

;;; Test Setup and Teardown

(defun test-wttrin--get-cached-or-fetch-setup ()
  "Setup for get-cached-or-fetch tests."
  (testutil-wttrin-setup))

(defun test-wttrin--get-cached-or-fetch-teardown ()
  "Teardown for get-cached-or-fetch tests."
  (testutil-wttrin-teardown))

;;; Normal Cases

(ert-deftest test-wttrin--get-cached-or-fetch-normal-cache-hit-returns-cached-data ()
  "Test that cached data is returned without fetching."
  (test-wttrin--get-cached-or-fetch-setup)
  (unwind-protect
      (let* ((location "Paris")
             (cache-key (wttrin--make-cache-key location))
             (now 1000.0)
             (callback-result nil)
             (fetch-called nil))
        ;; Pre-populate cache with data
        (puthash cache-key (cons now test-wttrin--get-cached-or-fetch-sample-weather)
                 wttrin--cache)

        ;; Mock time to be 100 seconds later
        (cl-letf (((symbol-function 'float-time)
                   (lambda () (+ now 100.0)))
                  ((symbol-function 'wttrin-fetch-raw-string)
                   (lambda (_location _callback)
                     (setq fetch-called t))))

          (wttrin--get-cached-or-fetch
           location
           (lambda (data &optional _error-msg) (setq callback-result data)))

          ;; Should return cached data immediately
          (should (equal callback-result test-wttrin--get-cached-or-fetch-sample-weather))
          ;; Should NOT call fetch
          (should-not fetch-called)))
    (test-wttrin--get-cached-or-fetch-teardown)))

(ert-deftest test-wttrin--get-cached-or-fetch-normal-cache-miss-fetches-new-data ()
  "Test that missing cache entry triggers fetch and stores result."
  (test-wttrin--get-cached-or-fetch-setup)
  (unwind-protect
      (let* ((location "London")
             (cache-key (wttrin--make-cache-key location))
             (now 1000.0)
             (callback-result nil)
             (fetch-called nil))

        (cl-letf (((symbol-function 'float-time)
                   (lambda () now))
                  ((symbol-function 'wttrin-fetch-raw-string)
                   (lambda (_location callback)
                     (setq fetch-called t)
                     (funcall callback test-wttrin--get-cached-or-fetch-new-weather)))
                  ((symbol-function 'wttrin--cleanup-cache-if-needed)
                   (lambda () nil)))

          (wttrin--get-cached-or-fetch
           location
           (lambda (data &optional _error-msg) (setq callback-result data)))

          ;; Should call fetch
          (should fetch-called)
          ;; Should return fresh data
          (should (equal callback-result test-wttrin--get-cached-or-fetch-new-weather))
          ;; Should store in cache
          (let ((cached (gethash cache-key wttrin--cache)))
            (should cached)
            (should (equal (car cached) now))
            (should (equal (cdr cached) test-wttrin--get-cached-or-fetch-new-weather)))))
    (test-wttrin--get-cached-or-fetch-teardown)))

(ert-deftest test-wttrin--get-cached-or-fetch-normal-old-data-still-served ()
  "Test that old cached data is served without fetching (no TTL expiration).
Proactive refresh keeps data fresh; on-demand reads always use cache."
  (test-wttrin--get-cached-or-fetch-setup)
  (unwind-protect
      (let* ((location "Tokyo")
             (cache-key (wttrin--make-cache-key location))
             (old-time 1000.0)
             (new-time (+ old-time 10000.0)) ; Very old data
             (callback-result nil)
             (fetch-called nil))

        ;; Pre-populate cache with old data
        (puthash cache-key (cons old-time test-wttrin--get-cached-or-fetch-sample-weather)
                 wttrin--cache)

        (cl-letf (((symbol-function 'float-time)
                   (lambda () new-time))
                  ((symbol-function 'wttrin-fetch-raw-string)
                   (lambda (_location _callback)
                     (setq fetch-called t))))

          (wttrin--get-cached-or-fetch
           location
           (lambda (data &optional _error-msg) (setq callback-result data)))

          ;; Should serve old data without fetching
          (should-not fetch-called)
          (should (equal callback-result test-wttrin--get-cached-or-fetch-sample-weather))))
    (test-wttrin--get-cached-or-fetch-teardown)))

;;; Boundary Cases

(ert-deftest test-wttrin--get-cached-or-fetch-boundary-force-refresh-bypasses-fresh-cache ()
  "Test that force refresh flag bypasses fresh cache."
  (test-wttrin--get-cached-or-fetch-setup)
  (unwind-protect
      (let* ((location "Rome")
             (cache-key (wttrin--make-cache-key location))
             (now 1000.0)
             (callback-result nil)
             (fetch-called nil)
             (wttrin--force-refresh t)) ; Force refresh enabled

        ;; Pre-populate cache with fresh data
        (puthash cache-key (cons now test-wttrin--get-cached-or-fetch-sample-weather)
                 wttrin--cache)

        (cl-letf (((symbol-function 'float-time)
                   (lambda () (+ now 100.0)))
                  ((symbol-function 'wttrin-fetch-raw-string)
                   (lambda (_location callback)
                     (setq fetch-called t)
                     (funcall callback test-wttrin--get-cached-or-fetch-new-weather)))
                  ((symbol-function 'wttrin--cleanup-cache-if-needed)
                   (lambda () nil)))

          (wttrin--get-cached-or-fetch
           location
           (lambda (data &optional _error-msg) (setq callback-result data)))

          ;; Should fetch despite fresh cache
          (should fetch-called)
          (should (equal callback-result test-wttrin--get-cached-or-fetch-new-weather))))
    (test-wttrin--get-cached-or-fetch-teardown)))

(ert-deftest test-wttrin--get-cached-or-fetch-boundary-empty-cache-fetches ()
  "Test that completely empty cache triggers fetch."
  (test-wttrin--get-cached-or-fetch-setup)
  (unwind-protect
      (let* ((location "Athens")
             (callback-result nil)
             (fetch-called nil))

        ;; Cache is already empty from setup
        (should (= (hash-table-count wttrin--cache) 0))

        (cl-letf (((symbol-function 'float-time)
                   (lambda () 1000.0))
                  ((symbol-function 'wttrin-fetch-raw-string)
                   (lambda (_location callback)
                     (setq fetch-called t)
                     (funcall callback test-wttrin--get-cached-or-fetch-new-weather)))
                  ((symbol-function 'wttrin--cleanup-cache-if-needed)
                   (lambda () nil)))

          (wttrin--get-cached-or-fetch
           location
           (lambda (data &optional _error-msg) (setq callback-result data)))

          ;; Should fetch
          (should fetch-called)
          (should (equal callback-result test-wttrin--get-cached-or-fetch-new-weather))))
    (test-wttrin--get-cached-or-fetch-teardown)))

(ert-deftest test-wttrin--get-cached-or-fetch-boundary-cache-key-includes-unit-system ()
  "Test that cache keys differentiate by unit system."
  (test-wttrin--get-cached-or-fetch-setup)
  (unwind-protect
      (let* ((location "Oslo")
             (now 1000.0)
             (metric-data "Weather: 20°C")
             (imperial-data "Weather: 68°F")
             (callback-result nil))

        ;; Cache metric version
        (let ((wttrin-unit-system "m"))
          (puthash (wttrin--make-cache-key location)
                   (cons now metric-data)
                   wttrin--cache))

        ;; Cache imperial version
        (let ((wttrin-unit-system "u"))
          (puthash (wttrin--make-cache-key location)
                   (cons now imperial-data)
                   wttrin--cache))

        (cl-letf (((symbol-function 'float-time)
                   (lambda () (+ now 100.0))))

          ;; Fetch with metric - should get metric cache
          (let ((wttrin-unit-system "m"))
            (wttrin--get-cached-or-fetch
             location
             (lambda (data &optional _error-msg) (setq callback-result data)))
            (should (equal callback-result metric-data)))

          ;; Fetch with imperial - should get imperial cache
          (let ((wttrin-unit-system "u"))
            (wttrin--get-cached-or-fetch
             location
             (lambda (data &optional _error-msg) (setq callback-result data)))
            (should (equal callback-result imperial-data)))))
    (test-wttrin--get-cached-or-fetch-teardown)))

;;; Error Cases

(ert-deftest test-wttrin--get-cached-or-fetch-error-fetch-fails-with-stale-cache-returns-stale ()
  "Test that fetch failure with stale cache falls back to stale data."
  (test-wttrin--get-cached-or-fetch-setup)
  (unwind-protect
      (let* ((location "Vienna")
             (cache-key (wttrin--make-cache-key location))
             (callback-result nil)
             (message-shown nil)
             (wttrin--force-refresh t)) ; Force refresh to trigger fetch

        ;; Pre-populate cache with data
        (puthash cache-key (cons 1000.0 test-wttrin--get-cached-or-fetch-sample-weather)
                 wttrin--cache)

        (cl-letf (((symbol-function 'float-time)
                   (lambda () 3000.0))
                  ((symbol-function 'wttrin-fetch-raw-string)
                   (lambda (_location callback)
                     ;; Simulate network failure
                     (funcall callback nil)))
                  ((symbol-function 'wttrin--cleanup-cache-if-needed)
                   (lambda () nil))
                  ((symbol-function 'message)
                   (lambda (format-string &rest _args)
                     (setq message-shown format-string))))

          (wttrin--get-cached-or-fetch
           location
           (lambda (data &optional _error-msg) (setq callback-result data)))

          ;; Should fall back to stale cache
          (should (equal callback-result test-wttrin--get-cached-or-fetch-sample-weather))
          ;; Should show message about using cached version
          (should message-shown)
          (should (string-match-p "cached" message-shown))))
    (test-wttrin--get-cached-or-fetch-teardown)))

(ert-deftest test-wttrin--get-cached-or-fetch-error-fetch-fails-with-no-cache-returns-nil ()
  "Test that fetch failure with no cache returns nil."
  (test-wttrin--get-cached-or-fetch-setup)
  (unwind-protect
      (let* ((location "Dublin")
             (callback-result 'unset))

        ;; Cache is empty
        (should (= (hash-table-count wttrin--cache) 0))

        (cl-letf (((symbol-function 'float-time)
                   (lambda () 1000.0))
                  ((symbol-function 'wttrin-fetch-raw-string)
                   (lambda (_location callback)
                     ;; Simulate network failure
                     (funcall callback nil)))
                  ((symbol-function 'wttrin--cleanup-cache-if-needed)
                   (lambda () nil)))

          (wttrin--get-cached-or-fetch
           location
           (lambda (data &optional _error-msg) (setq callback-result data)))

          ;; Should return nil (no fallback available)
          (should (null callback-result))))
    (test-wttrin--get-cached-or-fetch-teardown)))

(ert-deftest test-wttrin--get-cached-or-fetch-error-nil-location-creates-cache-key ()
  "Test that nil location is handled (creates cache key with nil)."
  (test-wttrin--get-cached-or-fetch-setup)
  (unwind-protect
      (let* ((location nil)
             (callback-result nil)
             (fetch-called nil))

        (cl-letf (((symbol-function 'float-time)
                   (lambda () 1000.0))
                  ((symbol-function 'wttrin-fetch-raw-string)
                   (lambda (_location callback)
                     (setq fetch-called t)
                     (funcall callback test-wttrin--get-cached-or-fetch-new-weather)))
                  ((symbol-function 'wttrin--cleanup-cache-if-needed)
                   (lambda () nil)))

          (wttrin--get-cached-or-fetch
           location
           (lambda (data &optional _error-msg) (setq callback-result data)))

          ;; Should attempt to fetch (nil is a valid location input)
          (should fetch-called)
          (should (equal callback-result test-wttrin--get-cached-or-fetch-new-weather))))
    (test-wttrin--get-cached-or-fetch-teardown)))

(ert-deftest test-wttrin--get-cached-or-fetch-normal-timestamp-reflects-response-time ()
  "Cache timestamp should reflect when the response arrived, not when
the request was initiated.  A stale request-time timestamp would make
the cached data appear older than it actually is."
  (test-wttrin--get-cached-or-fetch-setup)
  (unwind-protect
      (let* ((location "Paris")
             (cache-key (wttrin--make-cache-key location))
             (response-time 5000.0))
        (cl-letf (((symbol-function 'float-time)
                   (lambda () response-time))
                  ((symbol-function 'wttrin-fetch-raw-string)
                   (lambda (_query callback)
                     ;; Simulate time passing: response arrives later
                     (setq response-time 5005.0)
                     (funcall callback "fresh data")))
                  ((symbol-function 'wttrin--cleanup-cache-if-needed)
                   (lambda () nil)))
          (let ((wttrin--force-refresh t))
            (wttrin--get-cached-or-fetch
             location
             (lambda (_data) nil)))
          ;; Timestamp should be 5005 (when callback ran), not 5000 (when request started)
          (let ((cached (gethash cache-key wttrin--cache)))
            (should cached)
            (should (= (car cached) 5005.0)))))
    (test-wttrin--get-cached-or-fetch-teardown)))

(provide 'test-wttrin--get-cached-or-fetch)
;;; test-wttrin--get-cached-or-fetch.el ends here