summaryrefslogtreecommitdiff
path: root/tests/test-wttrin-requery-force.el
blob: d57234880696ad8f2d32fb0002af876d961e0d0b (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
;;; test-wttrin-requery-force.el --- Tests for wttrin-requery-force -*- lexical-binding: t; -*-

;; Copyright (C) 2025-2026 Craig Jennings

;;; Commentary:

;; Unit tests for wttrin-requery-force function.
;; Tests the force-refresh behavior that bypasses cache for the current location.

;;; Code:

(require 'ert)
(require 'wttrin)
(require 'testutil-wttrin)

;;; Setup and Teardown

(defun test-wttrin-requery-force-setup ()
  "Setup for requery-force tests."
  (testutil-wttrin-setup)
  (when (get-buffer "*wttr.in*")
    (kill-buffer "*wttr.in*")))

(defun test-wttrin-requery-force-teardown ()
  "Teardown for requery-force tests."
  (testutil-wttrin-teardown)
  (when (get-buffer "*wttr.in*")
    (kill-buffer "*wttr.in*")))

;;; Normal Cases

(ert-deftest test-wttrin-requery-force-normal-queries-current-location ()
  "Force refresh should query weather for the buffer's current location."
  (test-wttrin-requery-force-setup)
  (unwind-protect
      (let ((queried-location nil))
        (cl-letf (((symbol-function 'wttrin-query)
                   (lambda (location) (setq queried-location location))))
          ;; Set up a weather buffer with a known location
          (with-current-buffer (get-buffer-create "*wttr.in*")
            (setq-local wttrin--current-location "Berlin, DE")
            (wttrin-requery-force)
            (should (equal queried-location "Berlin, DE")))))
    (test-wttrin-requery-force-teardown)))

(ert-deftest test-wttrin-requery-force-normal-sets-force-refresh-flag ()
  "The force-refresh flag should be true when the query executes."
  (test-wttrin-requery-force-setup)
  (unwind-protect
      (let ((force-refresh-was-set nil))
        (cl-letf (((symbol-function 'wttrin-query)
                   (lambda (_location)
                     (setq force-refresh-was-set wttrin--force-refresh))))
          (with-current-buffer (get-buffer-create "*wttr.in*")
            (setq-local wttrin--current-location "Paris")
            (wttrin-requery-force)
            (should force-refresh-was-set))))
    (test-wttrin-requery-force-teardown)))

;;; Boundary Cases

(ert-deftest test-wttrin-requery-force-boundary-no-location-shows-message ()
  "When no current location is set, user should be told there's nothing to refresh."
  (test-wttrin-requery-force-setup)
  (unwind-protect
      (let ((displayed-message nil))
        (cl-letf (((symbol-function 'message)
                   (lambda (fmt &rest args)
                     (setq displayed-message (apply #'format fmt args)))))
          (with-current-buffer (get-buffer-create "*wttr.in*")
            ;; wttrin--current-location is nil (buffer-local default)
            (wttrin-requery-force)
            (should displayed-message)
            (should (string-match-p "No location" displayed-message)))))
    (test-wttrin-requery-force-teardown)))

(ert-deftest test-wttrin-requery-force-boundary-force-flag-does-not-leak ()
  "The force-refresh flag should not persist after the requery completes."
  (test-wttrin-requery-force-setup)
  (unwind-protect
      (progn
        (cl-letf (((symbol-function 'wttrin-query) (lambda (_location) nil)))
          (with-current-buffer (get-buffer-create "*wttr.in*")
            (setq-local wttrin--current-location "Paris")
            (wttrin-requery-force)))
        ;; After the let-binding unwinds, force-refresh should be back to nil
        (should-not wttrin--force-refresh))
    (test-wttrin-requery-force-teardown)))

(provide 'test-wttrin-requery-force)
;;; test-wttrin-requery-force.el ends here