;;; 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.") (defvar test-wttrin-use-current-location--saved-override nil "Snapshot of `wttrin--favorite-override' restored in teardown.") (defun test-wttrin-use-current-location-setup () "Snapshot the configured favorite and the runtime override, clearing both." (setq test-wttrin-use-current-location--saved wttrin-favorite-location) (setq test-wttrin-use-current-location--saved-override wttrin--favorite-override) (setq wttrin-favorite-location nil) (setq wttrin--favorite-override nil)) (defun test-wttrin-use-current-location-teardown () "Restore the configured favorite and the runtime override." (setq wttrin-favorite-location test-wttrin-use-current-location--saved) (setq wttrin--favorite-override test-wttrin-use-current-location--saved-override)) ;;; 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))) (ert-deftest test-wttrin-use-current-location-normal-confirm-refreshes-mode-line () "Normal: confirming refreshes the mode-line so it switches to the current location immediately rather than at the next scheduled fetch." (test-wttrin-use-current-location-setup) (setq wttrin-favorite-location "Berkeley, CA") (unwind-protect (let ((wttrin-geolocation-enabled t) (wttrin-mode-line-mode t) (wttrin--location-history nil) (fetched nil)) (cl-letf (((symbol-function 'yes-or-no-p) (lambda (&rest _) t)) ((symbol-function 'message) (lambda (&rest _) nil)) ((symbol-function 'wttrin--mode-line-fetch-weather) (lambda () (setq fetched t))) ((symbol-function 'wttrin--mode-line-set-placeholder) (lambda () nil))) (wttrin-use-current-location)) (should (eq t (wttrin--favorite-location))) (should fetched)) (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