aboutsummaryrefslogtreecommitdiff
path: root/tests/test-wttrin--mode-line-timer-lifecycle.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-06-28 03:56:24 -0400
committerCraig Jennings <c@cjennings.net>2026-06-28 04:01:06 -0400
commit19e103fea2e55a3e3bc3a777b4c618e644b57760 (patch)
tree6af33c4c7bf146ace78db9feec09a02f5885f23b /tests/test-wttrin--mode-line-timer-lifecycle.el
parentec38418d6d3eb4342363af1714f0073a74d52fbc (diff)
downloademacs-wttrin-19e103fea2e55a3e3bc3a777b4c618e644b57760.tar.gz
emacs-wttrin-19e103fea2e55a3e3bc3a777b4c618e644b57760.zip
fix: stop the mode-line cleanly when disabled before startup fires
The delayed initial fetch was scheduled with run-at-time but the one-shot timer was discarded, and when the mode is enabled before after-init the start is queued on after-init-hook. Disabling the mode before either fired could still start timers, hit the network, or mutate mode-line state after the user turned it off. Stop now cancels the stored startup timer and removes the after-init hook, and the startup callback no-ops when the mode is off.
Diffstat (limited to 'tests/test-wttrin--mode-line-timer-lifecycle.el')
-rw-r--r--tests/test-wttrin--mode-line-timer-lifecycle.el55
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/test-wttrin--mode-line-timer-lifecycle.el b/tests/test-wttrin--mode-line-timer-lifecycle.el
new file mode 100644
index 0000000..2cd5b9f
--- /dev/null
+++ b/tests/test-wttrin--mode-line-timer-lifecycle.el
@@ -0,0 +1,55 @@
+;;; test-wttrin--mode-line-timer-lifecycle.el --- Mode-line timer lifecycle -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2024-2026 Craig Jennings
+
+;;; Commentary:
+;; Tests that disabling wttrin-mode-line-mode before the delayed startup fetch
+;; fires cannot start the mode-line: the one-shot startup timer is cancelled and
+;; the deferred after-init hook is removed, and the startup callback no-ops when
+;; the mode is off.
+
+;;; Code:
+
+(require 'ert)
+(require 'cl-lib)
+(require 'wttrin)
+(require 'testutil-wttrin)
+
+(ert-deftest test-wttrin--mode-line-fetch-weather-if-enabled-normal-runs-when-enabled ()
+ "Normal: the delayed startup fetch runs when the mode is on."
+ (let ((called nil))
+ (cl-letf (((symbol-function 'wttrin--mode-line-fetch-weather)
+ (lambda (&rest _) (setq called t))))
+ (let ((wttrin-mode-line-mode t))
+ (wttrin--mode-line-fetch-weather-if-enabled)
+ (should called)))))
+
+(ert-deftest test-wttrin--mode-line-fetch-weather-if-enabled-error-skips-when-disabled ()
+ "Error: the delayed startup fetch is skipped when the mode was turned off
+before it fired, so a disabled mode never hits the network or mutates state."
+ (let ((called nil))
+ (cl-letf (((symbol-function 'wttrin--mode-line-fetch-weather)
+ (lambda (&rest _) (setq called t))))
+ (let ((wttrin-mode-line-mode nil))
+ (wttrin--mode-line-fetch-weather-if-enabled)
+ (should-not called)))))
+
+(ert-deftest test-wttrin--mode-line-stop-boundary-cancels-startup-timer-and-hook ()
+ "Boundary: stop cancels a pending startup timer and removes the after-init
+hook, so neither can start the mode-line after the user disabled it."
+ (let ((wttrin--mode-line-timer nil)
+ (wttrin--buffer-refresh-timer nil)
+ (wttrin--mode-line-startup-timer (run-at-time 9999 nil #'ignore))
+ (wttrin-mode-line-string nil)
+ (wttrin--mode-line-cache nil)
+ (wttrin--mode-line-rendered-stale nil))
+ (add-hook 'after-init-hook #'wttrin--mode-line-start)
+ (unwind-protect
+ (progn
+ (wttrin--mode-line-stop)
+ (should (null wttrin--mode-line-startup-timer))
+ (should-not (memq #'wttrin--mode-line-start after-init-hook)))
+ (remove-hook 'after-init-hook #'wttrin--mode-line-start))))
+
+(provide 'test-wttrin--mode-line-timer-lifecycle)
+;;; test-wttrin--mode-line-timer-lifecycle.el ends here