summaryrefslogtreecommitdiff
path: root/tests/test-wttrin--mode-line-update-display.el
blob: 07ab73f689178f67381a324d53f0ce45e091ad85 (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
;;; test-wttrin--mode-line-update-display.el --- Tests for mode-line display update -*- lexical-binding: t; -*-

;; Copyright (C) 2025 Craig Jennings

;;; Commentary:
;; Unit tests for wttrin--mode-line-update-display and
;; wttrin--mode-line-valid-response-p.

;;; Code:

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

;;; Setup and Teardown

(defun test-wttrin--mode-line-update-display-setup ()
  "Setup for mode-line update display tests."
  (testutil-wttrin-setup)
  (setq wttrin-mode-line-string nil)
  (setq wttrin--mode-line-tooltip-data nil))

(defun test-wttrin--mode-line-update-display-teardown ()
  "Teardown for mode-line update display tests."
  (testutil-wttrin-teardown)
  (setq wttrin-mode-line-string nil)
  (setq wttrin--mode-line-tooltip-data nil))

;;; --------------------------------------------------------------------------
;;; wttrin--mode-line-valid-response-p
;;; --------------------------------------------------------------------------

;;; Normal Cases

(ert-deftest test-wttrin--mode-line-valid-response-p-normal-standard-response ()
  "Valid response with location, emoji, temp, and conditions."
  (should (wttrin--mode-line-valid-response-p "Paris: ☀️ +61°F Clear")))

(ert-deftest test-wttrin--mode-line-valid-response-p-normal-minimal-with-colon ()
  "Minimal valid response containing a colon."
  (should (wttrin--mode-line-valid-response-p "X: Y")))

;;; Boundary Cases

(ert-deftest test-wttrin--mode-line-valid-response-p-boundary-colon-only ()
  "Response that is just a colon is technically valid (has expected delimiter)."
  (should (wttrin--mode-line-valid-response-p ":")))

;;; Error Cases

(ert-deftest test-wttrin--mode-line-valid-response-p-error-nil ()
  "Nil input is invalid."
  (should-not (wttrin--mode-line-valid-response-p nil)))

(ert-deftest test-wttrin--mode-line-valid-response-p-error-empty-string ()
  "Empty string is invalid."
  (should-not (wttrin--mode-line-valid-response-p "")))

(ert-deftest test-wttrin--mode-line-valid-response-p-error-no-colon ()
  "String without colon is invalid (doesn't match expected format)."
  (should-not (wttrin--mode-line-valid-response-p "no colon here")))

(ert-deftest test-wttrin--mode-line-valid-response-p-error-not-a-string ()
  "Non-string input is invalid."
  (should-not (wttrin--mode-line-valid-response-p 42)))

;;; --------------------------------------------------------------------------
;;; wttrin--mode-line-update-display
;;; --------------------------------------------------------------------------

;;; Normal Cases

(ert-deftest test-wttrin--mode-line-update-display-normal-sets-mode-line-string ()
  "Display update sets wttrin-mode-line-string to non-nil."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (progn
        (wttrin--mode-line-update-display "Paris: ☀️ +61°F Clear")
        (should wttrin-mode-line-string))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-update-display-normal-stores-tooltip-data ()
  "Display update stores weather string as tooltip data."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (progn
        (wttrin--mode-line-update-display "Paris: ☀️ +61°F Clear")
        (should (equal wttrin--mode-line-tooltip-data "Paris: ☀️ +61°F Clear")))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-update-display-normal-extracts-emoji ()
  "Display update extracts emoji character into mode-line string."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-mode-line-emoji-font nil))
        (wttrin--mode-line-update-display "Paris: X +61°F Clear")
        ;; Mode-line string should contain the extracted character
        (should (string-match-p "X" (substring-no-properties wttrin-mode-line-string))))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-update-display-normal-has-help-echo ()
  "Display update sets help-echo property for tooltip."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (progn
        (wttrin--mode-line-update-display "Paris: ☀️ +61°F Clear")
        (should (get-text-property 0 'help-echo wttrin-mode-line-string)))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-update-display-normal-has-local-map ()
  "Display update sets local-map property for mouse interaction."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (progn
        (wttrin--mode-line-update-display "Paris: ☀️ +61°F Clear")
        (should (eq (get-text-property 0 'local-map wttrin-mode-line-string)
                    wttrin--mode-line-map)))
    (test-wttrin--mode-line-update-display-teardown)))

;;; Boundary Cases

(ert-deftest test-wttrin--mode-line-update-display-boundary-no-emoji-match-uses-fallback ()
  "When emoji regex doesn't match, fallback character '?' is used."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-mode-line-emoji-font nil))
        (wttrin--mode-line-update-display "no colon here")
        (should (string-match-p "\\?" (substring-no-properties wttrin-mode-line-string))))
    (test-wttrin--mode-line-update-display-teardown)))

;;; Tooltip Lambda Tests

(ert-deftest test-wttrin--mode-line-update-display-normal-tooltip-returns-weather-data ()
  "Tooltip lambda returns weather data when available."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (progn
        (wttrin--mode-line-update-display "Paris: ☀️ +61°F Clear")
        (let ((tooltip-fn (get-text-property 0 'help-echo wttrin-mode-line-string)))
          (should (equal (funcall tooltip-fn nil nil nil) "Paris: ☀️ +61°F Clear"))))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-update-display-boundary-tooltip-empty-string-uses-fallback ()
  "Tooltip lambda falls back when tooltip data is empty string."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris"))
        (wttrin--mode-line-update-display "Paris: ☀️ +61°F Clear")
        ;; Simulate empty tooltip data (as would happen with bad response)
        (setq wttrin--mode-line-tooltip-data "")
        (let ((tooltip-fn (get-text-property 0 'help-echo wttrin-mode-line-string)))
          (should (string-match-p "Weather for Paris" (funcall tooltip-fn nil nil nil)))))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-update-display-boundary-tooltip-nil-uses-fallback ()
  "Tooltip lambda falls back when tooltip data is nil."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris"))
        (wttrin--mode-line-update-display "Paris: ☀️ +61°F Clear")
        (setq wttrin--mode-line-tooltip-data nil)
        (let ((tooltip-fn (get-text-property 0 'help-echo wttrin-mode-line-string)))
          (should (string-match-p "Weather for Paris" (funcall tooltip-fn nil nil nil)))))
    (test-wttrin--mode-line-update-display-teardown)))

;;; --------------------------------------------------------------------------
;;; wttrin--mode-line-fetch-weather (validation integration)
;;; --------------------------------------------------------------------------

(ert-deftest test-wttrin--mode-line-fetch-weather-error-empty-response-keeps-previous ()
  "Empty API response does not overwrite previous valid display."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris"))
        ;; Set up a valid prior state
        (wttrin--mode-line-update-display "Paris: ☀️ +61°F Clear")
        (let ((previous-string wttrin-mode-line-string)
              (previous-tooltip wttrin--mode-line-tooltip-data))
          ;; Simulate fetch returning empty response
          (testutil-wttrin-mock-http-response ""
            (wttrin--mode-line-fetch-weather)
            ;; Previous state should be preserved
            (should (equal wttrin-mode-line-string previous-string))
            (should (equal wttrin--mode-line-tooltip-data previous-tooltip)))))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-fetch-weather-error-no-colon-response-keeps-previous ()
  "Malformed API response without colon does not overwrite previous valid display."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris"))
        (wttrin--mode-line-update-display "Paris: ☀️ +61°F Clear")
        (let ((previous-string wttrin-mode-line-string)
              (previous-tooltip wttrin--mode-line-tooltip-data))
          (testutil-wttrin-mock-http-response "Unknown location"
            (wttrin--mode-line-fetch-weather)
            (should (equal wttrin-mode-line-string previous-string))
            (should (equal wttrin--mode-line-tooltip-data previous-tooltip)))))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-fetch-weather-error-nil-location-does-not-fetch ()
  "No fetch is attempted when wttrin-favorite-location is nil."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location nil)
            (fetch-called nil))
        (cl-letf (((symbol-function 'wttrin--fetch-url)
                   (lambda (_url _callback) (setq fetch-called t))))
          (wttrin--mode-line-fetch-weather)
          (should-not fetch-called)))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-fetch-weather-normal-valid-response-updates-display ()
  "Valid API response updates the mode-line display."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris"))
        (testutil-wttrin-mock-http-response "Paris: ☀️ +61°F Clear"
          (wttrin--mode-line-fetch-weather)
          (should wttrin-mode-line-string)
          (should (equal wttrin--mode-line-tooltip-data "Paris: ☀️ +61°F Clear"))))
    (test-wttrin--mode-line-update-display-teardown)))

;;; --------------------------------------------------------------------------
;;; wttrin--mode-line-set-placeholder
;;; --------------------------------------------------------------------------

;;; Normal Cases

(ert-deftest test-wttrin--mode-line-set-placeholder-normal-sets-mode-line-string ()
  "Placeholder sets wttrin-mode-line-string to non-nil."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris"))
        (wttrin--mode-line-set-placeholder)
        (should wttrin-mode-line-string))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-set-placeholder-normal-contains-hourglass ()
  "Placeholder displays hourglass emoji."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris")
            (wttrin-mode-line-emoji-font nil))
        (wttrin--mode-line-set-placeholder)
        (should (string-match-p "⏳" (substring-no-properties wttrin-mode-line-string))))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-set-placeholder-normal-has-fetching-tooltip ()
  "Placeholder tooltip mentions fetching weather."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris"))
        (wttrin--mode-line-set-placeholder)
        (let ((tooltip (get-text-property 0 'help-echo wttrin-mode-line-string)))
          (should (string-match-p "Fetching" tooltip))
          (should (string-match-p "Paris" tooltip))))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-set-placeholder-normal-has-local-map ()
  "Placeholder has mouse interaction keymap."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris"))
        (wttrin--mode-line-set-placeholder)
        (should (eq (get-text-property 0 'local-map wttrin-mode-line-string)
                    wttrin--mode-line-map)))
    (test-wttrin--mode-line-update-display-teardown)))

(ert-deftest test-wttrin--mode-line-set-placeholder-normal-replaced-by-real-data ()
  "Placeholder is replaced when real weather data arrives."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris")
            (wttrin-mode-line-emoji-font nil))
        (wttrin--mode-line-set-placeholder)
        (should (string-match-p "⏳" (substring-no-properties wttrin-mode-line-string)))
        (wttrin--mode-line-update-display "Paris: ☀️ +61°F Clear")
        (should-not (string-match-p "⏳" (substring-no-properties wttrin-mode-line-string))))
    (test-wttrin--mode-line-update-display-teardown)))

;;; Boundary Cases

(ert-deftest test-wttrin--mode-line-set-placeholder-boundary-does-not-set-tooltip-data ()
  "Placeholder does not contaminate tooltip data variable."
  (test-wttrin--mode-line-update-display-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "Paris"))
        (wttrin--mode-line-set-placeholder)
        (should-not wttrin--mode-line-tooltip-data))
    (test-wttrin--mode-line-update-display-teardown)))

(provide 'test-wttrin--mode-line-update-display)
;;; test-wttrin--mode-line-update-display.el ends here