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
|
;;; test-wttrin-make-default.el --- Tests for promote-to-default command -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Craig Jennings
;;; Commentary:
;; Unit tests for wttrin--set-favorite-location and wttrin-make-default,
;; the weather-buffer command (bound to "d") that promotes the displayed
;; location to the persisted favorite. The favorite is written to the runtime
;; override `wttrin--favorite-override' (not the `wttrin-favorite-location'
;; defcustom), and read back through `wttrin--favorite-location'.
;;; Code:
(require 'ert)
(require 'cl-lib)
(require 'wttrin)
;;; --------------------------------------------------------------------------
;;; wttrin--set-favorite-location
;;; --------------------------------------------------------------------------
;;; Normal Cases
(ert-deftest test-wttrin--set-favorite-location-normal-sets-variable ()
"Normal: sets the runtime favorite (leaving the config option untouched)."
(let ((wttrin-favorite-location nil)
(wttrin--favorite-override nil)
(savehist-additional-variables nil))
(wttrin--set-favorite-location "Paris, FR")
(should (equal wttrin--favorite-override "Paris, FR"))
(should (equal "Paris, FR" (wttrin--favorite-location)))))
(ert-deftest test-wttrin--set-favorite-location-error-no-savehist-loaded ()
"Error: setting the favorite works even when savehist is not loaded.
The setter must not touch `savehist-additional-variables' directly (it may be
unbound); persistence is left to `wttrin--savehist-register'."
(let ((wttrin-favorite-location nil)
(wttrin--favorite-override nil))
;; Simulate savehist absent: the variable is unbound.
(cl-letf (((symbol-function 'wttrin--savehist-register)
(lambda () (error "Should not be called from the setter"))))
(wttrin--set-favorite-location "Oslo, NO")
(should (equal wttrin--favorite-override "Oslo, NO")))))
(ert-deftest test-wttrin--set-favorite-location-normal-drops-from-history ()
"Normal: promoting a location removes it from the search history."
(let ((wttrin-favorite-location nil)
(wttrin--favorite-override nil)
(wttrin--location-history '("Reykjavik" "Oslo, NO")))
(wttrin--set-favorite-location "Reykjavik")
(should-not (member "Reykjavik" wttrin--location-history))
(should (equal wttrin--location-history '("Oslo, NO")))))
(ert-deftest test-wttrin--set-favorite-location-boundary-not-in-history-is-noop ()
"Boundary: promoting a location absent from history leaves history intact."
(let ((wttrin-favorite-location nil)
(wttrin--favorite-override nil)
(wttrin--location-history '("Oslo, NO")))
(wttrin--set-favorite-location "Berkeley, CA")
(should (equal wttrin--location-history '("Oslo, NO")))))
(ert-deftest test-wttrin-favorite-savehist-register-excludes-favorite ()
"Normal: `wttrin--savehist-register' does not register the runtime override.
The favorite persists in `wttrin-state-file' instead — a savehist entry would
be scrubbed by any Emacs session that saves savehist without wttrin loaded."
(require 'savehist)
(let ((savehist-additional-variables '(kill-ring)))
(wttrin--savehist-register)
(should-not (memq 'wttrin--favorite-override savehist-additional-variables))
(should-not (memq 'wttrin-favorite-location savehist-additional-variables))))
;;; --------------------------------------------------------------------------
;;; mode-line refresh when the favorite changes
;;; --------------------------------------------------------------------------
;;; Normal Cases
(ert-deftest test-wttrin--set-favorite-location-normal-mode-line-on-refreshes ()
"Normal: changing the favorite while the mode-line is active clears the
stale cache and fetches fresh weather for the new location immediately."
(let ((wttrin-favorite-location "Oslo, NO")
(wttrin--favorite-override nil)
(wttrin-mode-line-mode t)
(wttrin--mode-line-cache (cons 0.0 "Oslo, NO: sun"))
(fetched nil))
(cl-letf (((symbol-function 'wttrin--mode-line-fetch-weather)
(lambda () (setq fetched t)))
((symbol-function 'wttrin--mode-line-set-placeholder)
(lambda () nil)))
(wttrin--set-favorite-location "Paris, FR")
(should (null wttrin--mode-line-cache))
(should fetched))))
;;; Boundary Cases
(ert-deftest test-wttrin--set-favorite-location-boundary-mode-line-off-no-fetch ()
"Boundary: with the mode-line inactive, changing the favorite does not fetch."
(let ((wttrin-favorite-location "Oslo, NO")
(wttrin--favorite-override nil)
(wttrin-mode-line-mode nil)
(fetched nil))
(cl-letf (((symbol-function 'wttrin--mode-line-fetch-weather)
(lambda () (setq fetched t))))
(wttrin--set-favorite-location "Paris, FR")
(should-not fetched))))
(ert-deftest test-wttrin--set-favorite-location-boundary-unchanged-no-fetch ()
"Boundary: re-promoting the current favorite does not refetch the mode-line."
(let ((wttrin-favorite-location nil)
(wttrin--favorite-override "Paris, FR")
(wttrin-mode-line-mode t)
(fetched nil))
(cl-letf (((symbol-function 'wttrin--mode-line-fetch-weather)
(lambda () (setq fetched t)))
((symbol-function 'wttrin--mode-line-set-placeholder)
(lambda () nil)))
(wttrin--set-favorite-location "Paris, FR")
(should-not fetched))))
;;; --------------------------------------------------------------------------
;;; wttrin-make-default
;;; --------------------------------------------------------------------------
;;; Normal Cases
(ert-deftest test-wttrin-make-default-normal-sets-favorite-from-current ()
"Normal: promotes the buffer's current location to the favorite."
(let ((wttrin-favorite-location nil)
(wttrin--favorite-override nil)
(wttrin-saved-locations nil)
(wttrin--saved-locations-runtime nil)
(savehist-additional-variables nil))
(with-temp-buffer
(setq-local wttrin--current-location "Tokyo, JP")
(wttrin-make-default)
(should (equal "Tokyo, JP" (wttrin--favorite-location))))))
;;; Boundary Cases
(ert-deftest test-wttrin-make-default-boundary-nil-current-leaves-favorite ()
"Boundary: no current location is a no-op that leaves the favorite intact."
(let ((wttrin-favorite-location "Berkeley, CA")
(wttrin--favorite-override nil)
(savehist-additional-variables nil))
(with-temp-buffer
(setq-local wttrin--current-location nil)
(wttrin-make-default)
(should (equal "Berkeley, CA" (wttrin--favorite-location))))))
;;; --------------------------------------------------------------------------
;;; favorite in completion candidates
;;; --------------------------------------------------------------------------
;;; Normal Cases
(ert-deftest test-wttrin-make-default-normal-favorite-prepended-to-candidates ()
"Normal: a typed-in favorite is offered in the picker, at the front."
(let ((wttrin-default-locations '("Honolulu, HI" "Berkeley, CA"))
(wttrin--location-history nil)
(wttrin-favorite-location "Reykjavik")
(wttrin--favorite-override nil))
(should (equal (wttrin--completion-candidates)
(list wttrin--geolocation-sentinel
"Reykjavik" "Honolulu, HI" "Berkeley, CA")))))
;;; Boundary Cases
(ert-deftest test-wttrin-make-default-boundary-favorite-default-not-duplicated ()
"Boundary: a favorite that is already a default appears exactly once."
(require 'cl-lib)
(let ((wttrin-default-locations '("Honolulu, HI" "Berkeley, CA"))
(wttrin--location-history nil)
(wttrin-favorite-location "Berkeley, CA")
(wttrin--favorite-override nil))
(should (= 1 (cl-count "Berkeley, CA" (wttrin--completion-candidates) :test #'equal)))))
(ert-deftest test-wttrin-make-default-boundary-nil-favorite-candidates-unchanged ()
"Boundary: nil favorite leaves the candidate list as defaults plus history."
(let ((wttrin-default-locations '("Honolulu, HI"))
(wttrin--location-history '("Oslo, NO"))
(wttrin-favorite-location nil)
(wttrin--favorite-override nil))
(should (equal (wttrin--completion-candidates)
(list wttrin--geolocation-sentinel "Honolulu, HI" "Oslo, NO")))))
;;; --------------------------------------------------------------------------
;;; keymap binding
;;; --------------------------------------------------------------------------
(ert-deftest test-wttrin-make-default-normal-d-bound-in-mode-map ()
"Normal: the weather-buffer keymap binds \"d\" to the command."
(should (eq (lookup-key wttrin-mode-map (kbd "d")) 'wttrin-make-default)))
(provide 'test-wttrin-make-default)
;;; test-wttrin-make-default.el ends here
|