diff options
| author | Craig Jennings <c@cjennings.net> | 2026-04-22 00:07:51 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-04-22 00:07:51 -0500 |
| commit | 9958ec4c4396ae8435f7e1818ff383c05df47a14 (patch) | |
| tree | 9835229246368cca582f669837cd2859a79c8862 /tests/test-wttrin-geolocation-detect.el | |
| parent | 603f70d78f771c0a14c7f312aee6da68060b5d8b (diff) | |
| download | emacs-wttrin-9958ec4c4396ae8435f7e1818ff383c05df47a14.tar.gz emacs-wttrin-9958ec4c4396ae8435f7e1818ff383c05df47a14.zip | |
feat: add IP geolocation command for setting wttrin-favorite-location
Lets users set `wttrin-favorite-location` by IP lookup instead of typing a city by hand. `M-x wttrin-set-location-from-geolocation` runs the lookup, shows the detected "City, Region" in a yes/no prompt, and on confirmation sets the variable for the session. The docstring points at `M-x customize-save-variable` for persistence across restarts.
The new `wttrin-geolocation.el` module provides the provider layer. Three providers come built in: ipapi.co (the default), ipinfo.io, and ipwho.is. All three are HTTPS, need no API key, and have free tiers large enough for interactive use. The module has three layers. Pure JSON parsers handle the per-provider quirks: ipapi's `error: true` flag, ipwho.is's `success: false` flag, ipinfo's HTTP-status-only signalling. A small fetch helper extracts the HTTP body. `wttrin-geolocation-detect` wires them together and calls back with "City, Region" on success, or nil on any failure (network error, HTTP 4xx or 5xx, malformed response, rate-limit signal).
Providers live in an alist keyed by symbol, with plist values for :name, :url, and :parser. To use a different provider, push an entry onto `wttrin-geolocation--providers` and select it via `wttrin-geolocation-provider`. No code change needed.
README gains a subsection under Mode-line Weather Display covering the command, how to persist the result, provider selection with free-tier limits, and the accuracy caveat for VPN or mobile-hotspot users.
39 new tests across the parser layer (10 ipapi, 6 ipinfo, 6 ipwhois), fetch-and-dispatch (11), and interactive command (6). Each suite covers Normal, Boundary, and Error categories. Tests mock `url-retrieve` and `yes-or-no-p` at their boundaries and run the real extract-and-parse pipeline underneath. Test suite: 333 → 373 passing.
Diffstat (limited to 'tests/test-wttrin-geolocation-detect.el')
| -rw-r--r-- | tests/test-wttrin-geolocation-detect.el | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/tests/test-wttrin-geolocation-detect.el b/tests/test-wttrin-geolocation-detect.el new file mode 100644 index 0000000..ccd113f --- /dev/null +++ b/tests/test-wttrin-geolocation-detect.el @@ -0,0 +1,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 |
