diff options
| author | Craig Jennings <c@cjennings.net> | 2026-06-28 03:50:53 -0400 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-06-28 03:50:53 -0400 |
| commit | 944a52f0e33087dafba383edb3d97ec19a06d361 (patch) | |
| tree | 53528fb582b2b3fa044d02cb46c57390f50924b4 /tests | |
| parent | 38b7812b3bce45372fd2f11a8da710cb73717092 (diff) | |
| download | emacs-wttrin-944a52f0e33087dafba383edb3d97ec19a06d361.tar.gz emacs-wttrin-944a52f0e33087dafba383edb3d97ec19a06d361.zip | |
fix: key the cache on all response-shaping settings
wttrin--make-cache-key keyed only on location and unit system, but the request
body also depends on wttrin-display-options and wttrin-default-languages.
Changing display options or language mid-session served the old cached response,
and the staleness header then claimed it was current for the new settings. The
key now includes every setting that shapes the response.
Cache-key tests rewritten to assert behavior (distinct key per varying setting,
stable key for identical settings) instead of an exact key string.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test-wttrin--make-cache-key.el | 135 |
1 files changed, 63 insertions, 72 deletions
diff --git a/tests/test-wttrin--make-cache-key.el b/tests/test-wttrin--make-cache-key.el index 1e41eb7..79c78d9 100644 --- a/tests/test-wttrin--make-cache-key.el +++ b/tests/test-wttrin--make-cache-key.el @@ -3,8 +3,11 @@ ;; Copyright (C) 2024-2026 Craig Jennings ;;; Commentary: -;; Unit tests for wttrin--make-cache-key function. -;; Tests cache key generation to ensure different configurations produce different keys. +;; 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: @@ -12,8 +15,6 @@ (require 'wttrin) (require 'testutil-wttrin) -;;; Setup and Teardown - (defun test-wttrin--make-cache-key-setup () "Setup for make-cache-key tests." (testutil-wttrin-setup)) @@ -24,82 +25,72 @@ ;;; Normal Cases -(ert-deftest test-wttrin--make-cache-key-normal-location-with-metric () - "Test cache key generation with metric unit system." +(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= "Paris|m" (wttrin--make-cache-key "Paris"))))) - -(ert-deftest test-wttrin--make-cache-key-normal-location-with-imperial () - "Test cache key generation with USCS/imperial unit system." - (let ((wttrin-unit-system "u")) - (should (string= "London|u" (wttrin--make-cache-key "London"))))) - -(ert-deftest test-wttrin--make-cache-key-normal-location-with-nil-system () - "Test cache key generation with nil unit system (default)." - (let ((wttrin-unit-system nil)) - (should (string= "Tokyo|default" (wttrin--make-cache-key "Tokyo"))))) + (should (string= (wttrin--make-cache-key "Paris") + (wttrin--make-cache-key "Paris"))))) -(ert-deftest test-wttrin--make-cache-key-normal-different-locations-different-keys () - "Test that different locations produce different cache keys." +(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"))))) - -;;; Boundary Cases - -(ert-deftest test-wttrin--make-cache-key-boundary-same-location-different-units () - "Test that same location with different unit systems produces different keys. -This is critical - cache misses would occur if keys were the same." - (let* ((location "Paris") - (key-metric (let ((wttrin-unit-system "m")) - (wttrin--make-cache-key location))) - (key-imperial (let ((wttrin-unit-system "u")) - (wttrin--make-cache-key location))) - (key-default (let ((wttrin-unit-system nil)) - (wttrin--make-cache-key location)))) - (should-not (string= key-metric key-imperial)) - (should-not (string= key-metric key-default)) - (should-not (string= key-imperial key-default)))) - -(ert-deftest test-wttrin--make-cache-key-boundary-location-with-special-chars () - "Test cache key generation with special characters in location." - (let ((wttrin-unit-system "m")) - (should (string= "São Paulo|m" (wttrin--make-cache-key "São Paulo"))))) - -(ert-deftest test-wttrin--make-cache-key-boundary-location-with-unicode () - "Test cache key generation with Unicode characters." - (let ((wttrin-unit-system nil)) - (should (string= "北京|default" (wttrin--make-cache-key "北京"))))) - -(ert-deftest test-wttrin--make-cache-key-boundary-location-with-comma () - "Test cache key generation with comma in location (e.g., 'City, Country')." - (let ((wttrin-unit-system "m")) - (should (string= "London, GB|m" (wttrin--make-cache-key "London, GB"))))) - -(ert-deftest test-wttrin--make-cache-key-boundary-empty-string-location () - "Test cache key generation with empty string location." - (let ((wttrin-unit-system "m")) - (should (string= "|m" (wttrin--make-cache-key ""))))) - -(ert-deftest test-wttrin--make-cache-key-boundary-location-with-pipe-char () - "Test cache key with location containing pipe character (separator). -This could potentially cause cache key parsing issues." - (let ((wttrin-unit-system "m")) - (let ((key (wttrin--make-cache-key "Test|Location"))) - (should (string-match-p "|" key)) - ;; Should contain TWO pipe characters: one from location, one as separator - (should (= 2 (cl-count ?| key)))))) + (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-consistent-keys () - "Test that calling with same inputs always produces same key (idempotent)." +(ert-deftest test-wttrin--make-cache-key-error-idempotent () + "Error: repeated calls with the same inputs are stable." (let ((wttrin-unit-system "m")) - (let ((key1 (wttrin--make-cache-key "Paris")) - (key2 (wttrin--make-cache-key "Paris")) - (key3 (wttrin--make-cache-key "Paris"))) - (should (string= key1 key2)) - (should (string= key2 key3))))) + (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 |
