aboutsummaryrefslogtreecommitdiff
path: root/tests/test-wttrin--make-cache-key.el
blob: 79c78d91b84d0f2f95ca9c134b91148eab892885 (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
;;; test-wttrin--make-cache-key.el --- Tests for wttrin--make-cache-key -*- lexical-binding: t; -*-

;; Copyright (C) 2024-2026 Craig Jennings

;;; Commentary:
;; Unit tests for wttrin--make-cache-key.  The key must change whenever a
;; setting that shapes the requested response changes (location, unit system,
;; display options, Accept-Language), so a settings change can't serve a
;; stale-format cached response.  Tests assert behavior (distinctness and
;; stability), not the exact key string, so the format can evolve.

;;; Code:

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

(defun test-wttrin--make-cache-key-setup ()
  "Setup for make-cache-key tests."
  (testutil-wttrin-setup))

(defun test-wttrin--make-cache-key-teardown ()
  "Teardown for make-cache-key tests."
  (testutil-wttrin-teardown))

;;; Normal Cases

(ert-deftest test-wttrin--make-cache-key-normal-same-inputs-same-key ()
  "Normal: identical location and settings produce the same key."
  (let ((wttrin-unit-system "m"))
    (should (string= (wttrin--make-cache-key "Paris")
                     (wttrin--make-cache-key "Paris")))))

(ert-deftest test-wttrin--make-cache-key-normal-different-locations-differ ()
  "Normal: different locations produce different keys."
  (let ((wttrin-unit-system "m"))
    (should-not (string= (wttrin--make-cache-key "Paris")
                         (wttrin--make-cache-key "London")))))

(ert-deftest test-wttrin--make-cache-key-normal-key-contains-location ()
  "Normal: the key carries the location so distinct places never collide."
  (should (string-match-p "Paris" (wttrin--make-cache-key "Paris"))))

;;; Boundary Cases — every response-shaping setting changes the key

(ert-deftest test-wttrin--make-cache-key-boundary-unit-system-changes-key ()
  "Boundary: the same location under different unit systems differs."
  (let ((m (let ((wttrin-unit-system "m")) (wttrin--make-cache-key "Paris")))
        (u (let ((wttrin-unit-system "u")) (wttrin--make-cache-key "Paris")))
        (d (let ((wttrin-unit-system nil)) (wttrin--make-cache-key "Paris"))))
    (should-not (string= m u))
    (should-not (string= m d))
    (should-not (string= u d))))

(ert-deftest test-wttrin--make-cache-key-boundary-display-options-changes-key ()
  "Boundary: changing display options yields a distinct key.
Without this, changing the forecast format keeps serving the old cached output."
  (let ((a (let ((wttrin-display-options nil)) (wttrin--make-cache-key "Paris")))
        (b (let ((wttrin-display-options "F")) (wttrin--make-cache-key "Paris")))
        (c (let ((wttrin-display-options "0")) (wttrin--make-cache-key "Paris"))))
    (should-not (string= a b))
    (should-not (string= a c))
    (should-not (string= b c))))

(ert-deftest test-wttrin--make-cache-key-boundary-language-changes-key ()
  "Boundary: changing Accept-Language yields a distinct key."
  (let ((en (let ((wttrin-default-languages '("Accept-Language" . "en-US")))
              (wttrin--make-cache-key "Paris")))
        (fr (let ((wttrin-default-languages '("Accept-Language" . "fr-FR")))
              (wttrin--make-cache-key "Paris"))))
    (should-not (string= en fr))))

(ert-deftest test-wttrin--make-cache-key-boundary-special-chars-in-location ()
  "Boundary: special characters in the location do not break key generation."
  (dolist (loc '("São Paulo" "北京" "London, GB" "Test|Location" ""))
    (let ((key (wttrin--make-cache-key loc)))
      (should (stringp key))
      (should (> (length key) 0)))))

;;; Error Cases

(ert-deftest test-wttrin--make-cache-key-error-idempotent ()
  "Error: repeated calls with the same inputs are stable."
  (let ((wttrin-unit-system "m"))
    (let ((k1 (wttrin--make-cache-key "Paris"))
          (k2 (wttrin--make-cache-key "Paris")))
      (should (string= k1 k2)))))

(ert-deftest test-wttrin--make-cache-key-error-nil-settings-no-error ()
  "Error: nil unit system and nil display options produce a key, no signal."
  (let ((wttrin-unit-system nil)
        (wttrin-display-options nil))
    (should (stringp (wttrin--make-cache-key "Paris")))))

(provide 'test-wttrin--make-cache-key)
;;; test-wttrin--make-cache-key.el ends here