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
|
;;; test-wttrin-geolocation-detect.el --- Tests for wttrin-geolocation-detect -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Craig Jennings
;;; Commentary:
;; Unit tests for `wttrin-geolocation-detect'. Mocks `url-retrieve' at
;; the boundary — exercises the real extract/parse logic via the selected
;; provider. No network, no async timing.
;;; Code:
(require 'ert)
(require 'cl-lib)
(require 'wttrin-geolocation)
;;; Setup and Teardown
(defun test-wttrin-geolocation-detect-setup ()
"Setup for detect tests — pin provider to ipapi for determinism."
(setq wttrin-geolocation-provider 'ipapi))
(defun test-wttrin-geolocation-detect-teardown ()
"Teardown for detect tests — restore default provider."
(setq wttrin-geolocation-provider 'ipapi))
;;; Helpers
(defmacro test-wttrin-geolocation-detect--with-http (status body-string &rest body)
"Run BODY with `url-retrieve' mocked to return HTTP STATUS and BODY-STRING.
The mock writes a full HTTP response into a temp buffer and invokes the
retrieval callback with a nil status plist (success)."
(declare (indent 2))
`(cl-letf (((symbol-function 'url-retrieve)
(lambda (_url callback)
(with-temp-buffer
(insert (format "HTTP/1.1 %d OK\r\n" ,status))
(insert "Content-Type: application/json\r\n\r\n")
(insert ,body-string)
(funcall callback nil)))))
,@body))
(defmacro test-wttrin-geolocation-detect--with-network-error (&rest body)
"Run BODY with `url-retrieve' mocked to report a network-level error."
(declare (indent 0))
`(cl-letf (((symbol-function 'url-retrieve)
(lambda (_url callback)
(with-temp-buffer
(funcall callback '(:error (error "Network unreachable")))))))
,@body))
;;; Normal Cases
(ert-deftest test-wttrin-geolocation-detect-normal-ipapi-success-callback-receives-location ()
"A successful ipapi response leads to the callback receiving \"City, Region\"."
(test-wttrin-geolocation-detect-setup)
(unwind-protect
(let ((result 'unset))
(test-wttrin-geolocation-detect--with-http 200
"{\"city\":\"Berkeley\",\"region\":\"California\"}"
(wttrin-geolocation-detect (lambda (loc) (setq result loc))))
(should (string= "Berkeley, California" result)))
(test-wttrin-geolocation-detect-teardown)))
(ert-deftest test-wttrin-geolocation-detect-normal-selected-provider-used ()
"Switching `wttrin-geolocation-provider' routes through that provider's URL and parser."
(let ((wttrin-geolocation-provider 'ipinfo)
(requested-url nil)
(result nil))
(cl-letf (((symbol-function 'url-retrieve)
(lambda (url callback)
(setq requested-url url)
(with-temp-buffer
(insert "HTTP/1.1 200 OK\r\n\r\n")
(insert "{\"city\":\"Mountain View\",\"region\":\"California\",\"loc\":\"37.4,-122.0\"}")
(funcall callback nil)))))
(wttrin-geolocation-detect (lambda (loc) (setq result loc))))
(should (string= "https://ipinfo.io/json" requested-url))
(should (string= "Mountain View, California" result))))
;;; Boundary Cases
(ert-deftest test-wttrin-geolocation-detect-boundary-parser-returns-nil-callback-gets-nil ()
"If the parser rejects the response (missing fields), the callback receives nil."
(test-wttrin-geolocation-detect-setup)
(unwind-protect
(let ((result 'unset))
(test-wttrin-geolocation-detect--with-http 200
"{\"city\":\"Berkeley\"}"
(wttrin-geolocation-detect (lambda (loc) (setq result loc))))
(should-not result))
(test-wttrin-geolocation-detect-teardown)))
(ert-deftest test-wttrin-geolocation-detect-boundary-ipapi-rate-limit-error-flag-returns-nil ()
"An ipapi rate-limit response (HTTP 200 with error flag) yields nil."
(test-wttrin-geolocation-detect-setup)
(unwind-protect
(let ((result 'unset))
(test-wttrin-geolocation-detect--with-http 200
"{\"error\":true,\"reason\":\"RateLimited\"}"
(wttrin-geolocation-detect (lambda (loc) (setq result loc))))
(should-not result))
(test-wttrin-geolocation-detect-teardown)))
;;; Error Cases
(ert-deftest test-wttrin-geolocation-detect-error-network-failure-calls-callback-with-nil ()
"A network-level error surfaces as nil to the callback."
(test-wttrin-geolocation-detect-setup)
(unwind-protect
(let ((called nil)
(result 'unset))
(test-wttrin-geolocation-detect--with-network-error
(wttrin-geolocation-detect (lambda (loc)
(setq called t)
(setq result loc))))
(should called)
(should-not result))
(test-wttrin-geolocation-detect-teardown)))
(ert-deftest test-wttrin-geolocation-detect-error-http-429-returns-nil ()
"An HTTP 429 rate-limit response yields nil."
(test-wttrin-geolocation-detect-setup)
(unwind-protect
(let ((result 'unset))
(test-wttrin-geolocation-detect--with-http 429
"{\"error\":\"rate limit\"}"
(wttrin-geolocation-detect (lambda (loc) (setq result loc))))
(should-not result))
(test-wttrin-geolocation-detect-teardown)))
(ert-deftest test-wttrin-geolocation-detect-error-http-500-returns-nil ()
"An HTTP 500 server error yields nil."
(test-wttrin-geolocation-detect-setup)
(unwind-protect
(let ((result 'unset))
(test-wttrin-geolocation-detect--with-http 500
"Internal Server Error"
(wttrin-geolocation-detect (lambda (loc) (setq result loc))))
(should-not result))
(test-wttrin-geolocation-detect-teardown)))
(ert-deftest test-wttrin-geolocation-detect-error-empty-body-returns-nil ()
"An empty response body yields nil."
(test-wttrin-geolocation-detect-setup)
(unwind-protect
(let ((result 'unset))
(test-wttrin-geolocation-detect--with-http 200 ""
(wttrin-geolocation-detect (lambda (loc) (setq result loc))))
(should-not result))
(test-wttrin-geolocation-detect-teardown)))
(ert-deftest test-wttrin-geolocation-detect-error-malformed-json-returns-nil ()
"Malformed JSON in the response body yields nil."
(test-wttrin-geolocation-detect-setup)
(unwind-protect
(let ((result 'unset))
(test-wttrin-geolocation-detect--with-http 200 "{not valid json"
(wttrin-geolocation-detect (lambda (loc) (setq result loc))))
(should-not result))
(test-wttrin-geolocation-detect-teardown)))
(ert-deftest test-wttrin-geolocation-detect-error-buffer-cleanup-after-success ()
"The response buffer is killed after a successful fetch (no leaks)."
(test-wttrin-geolocation-detect-setup)
(unwind-protect
(let ((buffers-before (length (buffer-list))))
(test-wttrin-geolocation-detect--with-http 200
"{\"city\":\"Paris\",\"region\":\"IDF\"}"
(wttrin-geolocation-detect #'ignore))
(should (= buffers-before (length (buffer-list)))))
(test-wttrin-geolocation-detect-teardown)))
(ert-deftest test-wttrin-geolocation-detect-error-unknown-provider-signals-error ()
"Selecting an unknown provider signals a clear error."
(let ((wttrin-geolocation-provider 'nonexistent-provider))
(should-error (wttrin-geolocation-detect #'ignore) :type 'error)))
(provide 'test-wttrin-geolocation-detect)
;;; test-wttrin-geolocation-detect.el ends here
|