aboutsummaryrefslogtreecommitdiff
path: root/tests/test-wttrin-use-current-location.el
blob: d7d0ea6a2a3ab9584efc27f35c4af149129f2c36 (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
;;; 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