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

;; Copyright (C) 2025-2026 Craig Jennings

;;; Commentary:

;; Unit tests for wttrin--display-weather function.
;; Tests the extracted display logic that formats and shows weather data.

;;; Code:

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

;;; Test Data Fixtures

(defconst test-wttrin--display-weather-sample-raw-data
  "
Weather report: Paris, France

                                                       ┌─────────────┐
                                                  ┌──────────────────────────┐
   ┌──────────────────────────────┬───────────────────────────────────────────┴───────────────────────────────────────┐
   │            Monday             └──────────────────────────────────────────────────────────────────────────────────────┤
   │                                         2025-11-04 08:00:00 CST
   │
   │        Coordinates: 48.8566, 2.3522
   │
     \\  /       Partly cloudy
   _ /\".-. 22 °C
     \\_(   ).  ↓ 15 km/h
     /(___(__)  10 km
                0.0 mm"
  "Sample raw weather data with realistic wttr.in structure for testing.")

;;; Test Setup and Teardown

(defun test-wttrin--display-weather-setup ()
  "Setup for display weather tests."
  (testutil-wttrin-setup)
  (when (get-buffer "*wttr.in*")
    (kill-buffer "*wttr.in*")))

(defun test-wttrin--display-weather-teardown ()
  "Teardown for display weather tests."
  (testutil-wttrin-teardown)
  (when (get-buffer "*wttr.in*")
    (kill-buffer "*wttr.in*")))

;;; Normal Cases

(ert-deftest test-wttrin--display-weather-normal-valid-data-creates-buffer ()
  "Test that valid weather data creates and displays buffer correctly."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (testutil-wttrin-with-clean-weather-buffer
        (wttrin--display-weather "Paris, France" test-wttrin--display-weather-sample-raw-data)

        ;; Buffer should exist
        (should (get-buffer "*wttr.in*"))

        ;; Buffer should be displayed
        (should (get-buffer-window "*wttr.in*"))

        ;; Buffer should have content
        (with-current-buffer "*wttr.in*"
          (should (> (buffer-size) 0))

          ;; Buffer should be read-only
          (should buffer-read-only)

          ;; Location should be set
          (should (equal wttrin--current-location "Paris, France"))))
    (test-wttrin--display-weather-teardown)))

(ert-deftest test-wttrin--display-weather-normal-valid-data-sets-keybindings ()
  "Test that keybindings are properly set up in weather buffer."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (testutil-wttrin-with-clean-weather-buffer
        (wttrin--display-weather "London" test-wttrin--display-weather-sample-raw-data)

        (with-current-buffer "*wttr.in*"
          ;; Check that keybindings are set (they should be in the local map)
          (should (keymapp (current-local-map)))
          (should (commandp (lookup-key (current-local-map) "q")))
          (should (commandp (lookup-key (current-local-map) "a")))
          (should (commandp (lookup-key (current-local-map) "g")))))
    (test-wttrin--display-weather-teardown)))

(ert-deftest test-wttrin--display-weather-normal-valid-data-contains-instructions ()
  "Test that help instructions are displayed at bottom of buffer."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (testutil-wttrin-with-clean-weather-buffer
        (wttrin--display-weather "Tokyo" test-wttrin--display-weather-sample-raw-data)

        (with-current-buffer "*wttr.in*"
          (goto-char (point-max))
          (forward-line -2)
          ;; Should contain help text
          (should (search-forward "Press:" nil t))
          (should (search-forward "[a] for another location" nil t))
          (should (search-forward "[g] to refresh" nil t))
          (should (search-forward "[q] to quit" nil t))))
    (test-wttrin--display-weather-teardown)))

;;; Boundary Cases

(ert-deftest test-wttrin--display-weather-boundary-empty-location-name-creates-buffer ()
  "Test that empty location name still creates buffer."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (testutil-wttrin-with-clean-weather-buffer
        (wttrin--display-weather "" test-wttrin--display-weather-sample-raw-data)

        ;; Buffer should still be created
        (should (get-buffer "*wttr.in*"))

        (with-current-buffer "*wttr.in*"
          ;; Location should be set to empty string
          (should (equal wttrin--current-location ""))))
    (test-wttrin--display-weather-teardown)))

(ert-deftest test-wttrin--display-weather-boundary-location-with-special-chars-creates-buffer ()
  "Test that location with special characters creates buffer."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (testutil-wttrin-with-clean-weather-buffer
        (wttrin--display-weather "São Paulo, BR 🌆" test-wttrin--display-weather-sample-raw-data)

        (should (get-buffer "*wttr.in*"))

        (with-current-buffer "*wttr.in*"
          ;; Location with Unicode should be preserved
          (should (equal wttrin--current-location "São Paulo, BR 🌆"))))
    (test-wttrin--display-weather-teardown)))

(ert-deftest test-wttrin--display-weather-boundary-empty-string-creates-buffer ()
  "Test that empty weather string creates buffer without error.
Empty string does not match ERROR pattern, so it's processed as data."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (testutil-wttrin-with-clean-weather-buffer
        (wttrin--display-weather "Paris" "")

        ;; Empty string is not treated as error, buffer is created
        (should (get-buffer "*wttr.in*"))

        (with-current-buffer "*wttr.in*"
          (should buffer-read-only)))
    (test-wttrin--display-weather-teardown)))

;;; Error Cases

(ert-deftest test-wttrin--display-weather-error-nil-raw-string-shows-message ()
  "Test that nil raw-string displays error message."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (progn
        (let ((message-log-max t)
              (message-displayed nil))
          (cl-letf (((symbol-function 'message)
                     (lambda (format-string &rest args)
                       (setq message-displayed (apply #'format format-string args)))))
            (wttrin--display-weather "InvalidCity" nil)

            ;; Should display error message
            (should message-displayed)
            (should (string-match-p "Cannot retrieve" message-displayed)))))
    (test-wttrin--display-weather-teardown)))

(ert-deftest test-wttrin--display-weather-error-string-with-error-shows-message ()
  "Test that weather string containing ERROR shows error message."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (progn
        (let ((message-log-max t)
              (message-displayed nil))
          (cl-letf (((symbol-function 'message)
                     (lambda (format-string &rest args)
                       (setq message-displayed (apply #'format format-string args)))))
            (wttrin--display-weather "BadLocation" testutil-wttrin-sample-error-response)

            ;; Should display error message
            (should message-displayed)
            (should (string-match-p "Cannot retrieve" message-displayed)))))
    (test-wttrin--display-weather-teardown)))

(ert-deftest test-wttrin--display-weather-error-nil-raw-string-no-buffer-created ()
  "Test that nil raw-string does not create weather buffer."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (progn
        (cl-letf (((symbol-function 'message) (lambda (&rest _) nil)))
          (wttrin--display-weather "InvalidCity" nil)

          (should-not (string-match-p "wttr.in"
                                      (buffer-name (current-buffer))))))
    (test-wttrin--display-weather-teardown)))

;;; Location Casing

(ert-deftest test-wttrin--display-weather-normal-location-uses-user-casing ()
  "The 'Weather report:' header should show the user's original casing,
not the lowercase version that wttr.in returns."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (testutil-wttrin-with-clean-weather-buffer
        (wttrin--display-weather "New Orleans, LA"
                                 "Weather report: new orleans, la\n\nSunny 72°F")
        (with-current-buffer "*wttr.in*"
          (let ((case-fold-search nil))
            (goto-char (point-min))
            (should (search-forward "New Orleans, LA" nil t))
            (goto-char (point-min))
            (should-not (search-forward "new orleans, la" nil t)))))
    (test-wttrin--display-weather-teardown)))

(ert-deftest test-wttrin--display-weather-boundary-location-already-correct ()
  "When the API returns correct casing, nothing should break."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (testutil-wttrin-with-clean-weather-buffer
        (wttrin--display-weather "Paris"
                                 "Weather report: Paris\n\nSunny 22°C")
        (with-current-buffer "*wttr.in*"
          (goto-char (point-min))
          (should (search-forward "Paris" nil t))))
    (test-wttrin--display-weather-teardown)))

(ert-deftest test-wttrin--display-weather-normal-weather-data-preserved ()
  "Fixing the location casing should not alter the weather data itself."
  (test-wttrin--display-weather-setup)
  (unwind-protect
      (testutil-wttrin-with-clean-weather-buffer
        (wttrin--display-weather "Tokyo"
                                 "Weather report: tokyo\n\nCloudy 18°C\nWind: 5 km/h")
        (with-current-buffer "*wttr.in*"
          (let ((contents (buffer-string)))
            (should (string-match-p "Cloudy 18°C" contents))
            (should (string-match-p "Wind: 5 km/h" contents)))))
    (test-wttrin--display-weather-teardown)))

(provide 'test-wttrin--display-weather)
;;; test-wttrin--display-weather.el ends here