aboutsummaryrefslogtreecommitdiff
path: root/tests/test-wttrin-favorite-override.el
blob: 95916c74cdf0ab0a0c172a740e6eed0b1c39353d (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
;;; test-wttrin-favorite-override.el --- Tests for the runtime favorite override -*- lexical-binding: t; -*-

;; Copyright (C) 2026 Craig Jennings

;;; Commentary:
;; Unit tests for the split between the configured favorite
;; (`wttrin-favorite-location', a defcustom set in init) and the runtime
;; favorite (`wttrin--favorite-override', set by `d'/the geolocation commands
;; and persisted by savehist).  The effective favorite resolves via
;; `wttrin--favorite-location': the override wins over the config, so a favorite
;; chosen at runtime is not clobbered by an init that also sets the option.

;;; Code:

(require 'ert)
(require 'cl-lib)
(require 'wttrin)

;;; wttrin--favorite-location (resolver)

(ert-deftest test-wttrin-favorite-override-normal-config-when-no-override ()
  "Normal: with no override, the effective favorite is the configured one."
  (let ((wttrin-favorite-location "New Orleans, LA")
        (wttrin--favorite-override nil))
    (should (equal "New Orleans, LA" (wttrin--favorite-location)))))

(ert-deftest test-wttrin-favorite-override-normal-override-wins ()
  "Normal: a runtime override shadows the configured favorite."
  (let ((wttrin-favorite-location "New Orleans, LA")
        (wttrin--favorite-override "Hyatt Place Warwick, RI"))
    (should (equal "Hyatt Place Warwick, RI" (wttrin--favorite-location)))))

(ert-deftest test-wttrin-favorite-override-boundary-both-nil ()
  "Boundary: no config and no override yields nil (favorite disabled)."
  (let ((wttrin-favorite-location nil)
        (wttrin--favorite-override nil))
    (should (null (wttrin--favorite-location)))))

(ert-deftest test-wttrin-favorite-override-boundary-auto-detect-override ()
  "Boundary: the override carries the tri-state t (auto-detect)."
  (let ((wttrin-favorite-location "New Orleans, LA")
        (wttrin--favorite-override t))
    (should (eq t (wttrin--favorite-location)))))

(ert-deftest test-wttrin-favorite-override-regression-init-does-not-clobber ()
  "Regression: setting the config after an override (as an init `setopt' would)
does not change the effective favorite.  This is the bug where a favorite set
with `d' reverted to the init value on restart."
  (let ((wttrin--favorite-override "Hyatt Place Warwick, RI")
        (wttrin-favorite-location nil))
    ;; Simulate the init running its (setopt wttrin-favorite-location ...).
    (setq wttrin-favorite-location "New Orleans, LA")
    (should (equal "Hyatt Place Warwick, RI" (wttrin--favorite-location)))))

;;; wttrin--set-favorite-location writes the override, not the config

(ert-deftest test-wttrin-favorite-override-normal-setter-writes-override ()
  "Normal: the setter writes the runtime override and leaves the config intact."
  (let ((wttrin-favorite-location "New Orleans, LA")
        (wttrin--favorite-override nil)
        (wttrin--location-history nil))
    (wttrin--set-favorite-location "Paris, FR")
    (should (equal "Paris, FR" wttrin--favorite-override))
    (should (equal "New Orleans, LA" wttrin-favorite-location))
    (should (equal "Paris, FR" (wttrin--favorite-location)))))

;;; savehist registration covers only the scrub-tolerant search history;
;;; the runtime vars persist in `wttrin-state-file' (see test-wttrin-state-file.el)

(ert-deftest test-wttrin-favorite-override-normal-savehist-excludes-runtime-vars ()
  "Normal: savehist persists neither the runtime vars nor the config defcustoms.
The runtime favorite and directory live in `wttrin-state-file' — a savehist
entry would be scrubbed by any session that saves savehist without wttrin."
  (require 'savehist)
  (let ((savehist-additional-variables '(kill-ring)))
    (wttrin--savehist-register)
    (should-not (memq 'wttrin--favorite-override savehist-additional-variables))
    (should-not (memq 'wttrin--saved-locations-runtime savehist-additional-variables))
    (should-not (memq 'wttrin-favorite-location savehist-additional-variables))
    (should-not (memq 'wttrin-saved-locations savehist-additional-variables))))

(provide 'test-wttrin-favorite-override)
;;; test-wttrin-favorite-override.el ends here