diff options
| author | Craig Jennings <c@cjennings.net> | 2026-06-25 16:03:18 -0400 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-06-25 16:03:18 -0400 |
| commit | 7027cccea9eb7170ea0f08e1def3f979f2e59932 (patch) | |
| tree | 51752f8cac0d92b4442048d913272e923211b619 /tests/test-wttrin-use-current-location.el | |
| parent | 33621b5a6e5407da190767b89756e287698ef234 (diff) | |
| download | emacs-wttrin-7027cccea9eb7170ea0f08e1def3f979f2e59932.tar.gz emacs-wttrin-7027cccea9eb7170ea0f08e1def3f979f2e59932.zip | |
feat: add external-command geolocation, opt-out, and example adapters
These build on the current-location picker with the rest of the geolocation work.
wttrin-geolocation-command runs a command that prints {lat,lng} (and optionally an address) and queries wttr.in by those coordinates. IP geolocation only finds the network's exit point, which is wrong on a VPN or hotspot. A command that scans nearby WiFi resolves to street level. It runs asynchronously, falls back to the IP provider when unset or failing, and assumes nothing about the OS, so it's inert until set. When the command returns an address, wttrin shows it on a "Location:" line, so the resolved place is readable even though the fetch is by raw coordinates.
wttrin-use-current-location is a labeled command that sets the favorite to auto-detect, so the bare t value never has to be typed into init by hand.
wttrin-geolocation-enabled (default t) turns every geolocation surface off for anyone who wants that: the picker entry, the auto-detect favorite, and the command.
examples/geolocation/ ships two reference adapters for the command: google-geolocate.py (Google API, key via the environment or ~/.authinfo.gpg) and apple-wps.py (Apple's keyless WiFi positioning, which uses an undocumented endpoint, so read its caveat). Both are Python 3 standard library and scan via nmcli, with notes on adapting the scan to other systems.
Diffstat (limited to 'tests/test-wttrin-use-current-location.el')
| -rw-r--r-- | tests/test-wttrin-use-current-location.el | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/test-wttrin-use-current-location.el b/tests/test-wttrin-use-current-location.el new file mode 100644 index 0000000..2b08448 --- /dev/null +++ b/tests/test-wttrin-use-current-location.el @@ -0,0 +1,73 @@ +;;; test-wttrin-use-current-location.el --- Tests for wttrin-use-current-location -*- lexical-binding: t; -*- + +;; Copyright (C) 2024-2026 Craig Jennings + +;;; Commentary: +;; Unit tests for the `wttrin-use-current-location' command, the labeled way to +;; set `wttrin-favorite-location' to t (always auto-detect) instead of typing +;; the bare symbol. Mocks `yes-or-no-p' and `message'; touches no network. + +;;; Code: + +(require 'ert) +(require 'cl-lib) +(require 'wttrin) + +;;; Setup and Teardown + +(defvar test-wttrin-use-current-location--saved nil + "Snapshot of `wttrin-favorite-location' restored in teardown.") + +(defun test-wttrin-use-current-location-setup () + "Snapshot `wttrin-favorite-location' and clear it." + (setq test-wttrin-use-current-location--saved wttrin-favorite-location) + (setq wttrin-favorite-location nil)) + +(defun test-wttrin-use-current-location-teardown () + "Restore `wttrin-favorite-location'." + (setq wttrin-favorite-location test-wttrin-use-current-location--saved)) + +;;; Normal Cases + +(ert-deftest test-wttrin-use-current-location-normal-confirm-sets-t () + "Normal: confirming sets the favorite to t (auto-detect)." + (test-wttrin-use-current-location-setup) + (unwind-protect + (let ((wttrin-geolocation-enabled t)) + (cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest _) t)) + ((symbol-function 'message) (lambda (&rest _) nil))) + (wttrin-use-current-location)) + (should (eq t wttrin-favorite-location))) + (test-wttrin-use-current-location-teardown))) + +(ert-deftest test-wttrin-use-current-location-normal-decline-leaves-unchanged () + "Normal: declining leaves the favorite untouched." + (test-wttrin-use-current-location-setup) + (setq wttrin-favorite-location "Berkeley, CA") + (unwind-protect + (let ((wttrin-geolocation-enabled t)) + (cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest _) nil)) + ((symbol-function 'message) (lambda (&rest _) nil))) + (wttrin-use-current-location)) + (should (equal "Berkeley, CA" wttrin-favorite-location))) + (test-wttrin-use-current-location-teardown))) + +;;; Boundary / Error Cases + +(ert-deftest test-wttrin-use-current-location-boundary-disabled-no-prompt-no-set () + "Boundary: with geolocation disabled, no prompt and the favorite is unchanged." + (test-wttrin-use-current-location-setup) + (setq wttrin-favorite-location "Berkeley, CA") + (unwind-protect + (let ((prompted nil) + (wttrin-geolocation-enabled nil)) + (cl-letf (((symbol-function 'yes-or-no-p) + (lambda (&rest _) (setq prompted t) t)) + ((symbol-function 'message) (lambda (&rest _) nil))) + (wttrin-use-current-location)) + (should-not prompted) + (should (equal "Berkeley, CA" wttrin-favorite-location))) + (test-wttrin-use-current-location-teardown))) + +(provide 'test-wttrin-use-current-location) +;;; test-wttrin-use-current-location.el ends here |
