blob: 203a2328c09b77135e1baa39af3b30daada4880e (
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
|
;;; test-wttrin--handle-fetch-callback.el --- Tests for wttrin--handle-fetch-callback -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Craig Jennings
;;; Commentary:
;; Unit tests for wttrin--handle-fetch-callback function.
;; Tests callback coordination and error handling logic in isolation.
;;; Code:
(require 'ert)
(require 'wttrin)
;;; Normal Cases
(ert-deftest test-wttrin--handle-fetch-callback-normal-successful-response ()
"Test handling successful response with callback invocation."
(let ((callback-called nil)
(callback-data nil))
;; Mock wttrin--extract-response-body to return test data
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () "Weather: ☀️ Sunny")))
(wttrin--handle-fetch-callback
nil ;; status with no error
(lambda (data)
(setq callback-called t)
(setq callback-data data)))
(should callback-called)
(should (string= "Weather: ☀️ Sunny" callback-data)))))
(ert-deftest test-wttrin--handle-fetch-callback-normal-empty-response ()
"Test handling empty but successful response."
(let ((callback-called nil)
(callback-data 'not-nil))
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () "")))
(wttrin--handle-fetch-callback
nil
(lambda (data)
(setq callback-called t)
(setq callback-data data)))
(should callback-called)
(should (string= "" callback-data)))))
(ert-deftest test-wttrin--handle-fetch-callback-normal-large-response ()
"Test handling large response data."
(let ((callback-called nil)
(callback-data nil)
(large-data (make-string 10000 ?x)))
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () large-data)))
(wttrin--handle-fetch-callback
nil
(lambda (data)
(setq callback-called t)
(setq callback-data data)))
(should callback-called)
(should (= 10000 (length callback-data)))
(should (string= large-data callback-data)))))
;;; Boundary Cases
(ert-deftest test-wttrin--handle-fetch-callback-boundary-nil-status ()
"Test handling nil status (successful response)."
(let ((callback-called nil))
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () "data")))
(wttrin--handle-fetch-callback
nil ;; nil status means success
(lambda (data)
(setq callback-called t)))
(should callback-called))))
(ert-deftest test-wttrin--handle-fetch-callback-boundary-empty-status-plist ()
"Test handling empty status plist."
(let ((callback-called nil)
(callback-data nil))
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () "data")))
(wttrin--handle-fetch-callback
'() ;; empty plist, no error key
(lambda (data)
(setq callback-called t)
(setq callback-data data)))
(should callback-called)
(should (string= "data" callback-data)))))
(ert-deftest test-wttrin--handle-fetch-callback-boundary-status-with-other-keys ()
"Test handling status with various keys but no error."
(let ((callback-called nil))
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () "data")))
(wttrin--handle-fetch-callback
'(:peer "example.com" :redirect nil) ;; status with other keys
(lambda (data)
(setq callback-called t)))
(should callback-called))))
;;; Error Cases
(ert-deftest test-wttrin--handle-fetch-callback-error-network-error ()
"Test handling network error in status."
(let ((callback-called nil)
(callback-data 'not-nil))
(wttrin--handle-fetch-callback
'(:error (error "Network unreachable"))
(lambda (data)
(setq callback-called t)
(setq callback-data data)))
(should callback-called)
(should (null callback-data))))
(ert-deftest test-wttrin--handle-fetch-callback-error-http-404 ()
"Test handling HTTP error status."
(let ((callback-called nil)
(callback-data 'not-nil))
(wttrin--handle-fetch-callback
'(:error (error "HTTP 404"))
(lambda (data)
(setq callback-called t)
(setq callback-data data)))
(should callback-called)
(should (null callback-data))))
(ert-deftest test-wttrin--handle-fetch-callback-error-timeout ()
"Test handling timeout error."
(let ((callback-called nil)
(callback-data 'not-nil))
(wttrin--handle-fetch-callback
'(:error (error "Request timed out"))
(lambda (data)
(setq callback-called t)
(setq callback-data data)))
(should callback-called)
(should (null callback-data))))
(ert-deftest test-wttrin--handle-fetch-callback-error-callback-throws ()
"Test handling errors thrown by user callback."
(let ((callback-called nil)
(error-caught nil))
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () "data")))
;; Should not propagate error from callback
(condition-case err
(wttrin--handle-fetch-callback
nil
(lambda (data)
(setq callback-called t)
(error "User callback error")))
(error
(setq error-caught (error-message-string err))))
(should callback-called)
;; Error should be caught and handled, not propagated
(should-not error-caught))))
(ert-deftest test-wttrin--handle-fetch-callback-error-extract-body-throws ()
"Test that errors during body extraction are not caught and propagate."
(let ((callback-called nil)
(error-caught nil))
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () (error "Extraction error"))))
(condition-case err
(wttrin--handle-fetch-callback
nil
(lambda (data)
(setq callback-called t)))
(error
(setq error-caught t)))
;; Extraction errors propagate, callback should not be called
(should-not callback-called)
(should error-caught))))
(ert-deftest test-wttrin--handle-fetch-callback-error-nil-data-from-extract ()
"Test handling nil data returned from extraction."
(let ((callback-called nil)
(callback-data 'not-nil))
(cl-letf (((symbol-function 'wttrin--extract-response-body)
(lambda () nil)))
(wttrin--handle-fetch-callback
nil
(lambda (data)
(setq callback-called t)
(setq callback-data data)))
(should callback-called)
(should (null callback-data)))))
(ert-deftest test-wttrin--handle-fetch-callback-error-multiple-error-keys ()
"Test handling status with multiple error indicators."
(let ((callback-called nil)
(callback-data 'not-nil))
(wttrin--handle-fetch-callback
'(:error (error "First error") :another-error "Second error")
(lambda (data)
(setq callback-called t)
(setq callback-data data)))
(should callback-called)
;; Should return nil when :error key is present
(should (null callback-data))))
(provide 'test-wttrin--handle-fetch-callback)
;;; test-wttrin--handle-fetch-callback.el ends here
|