summaryrefslogtreecommitdiff
path: root/tests/test-wttrin-requery.el
blob: e643a34719f35609adbec92a8b1cfcb682f04325 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
;;; test-wttrin-requery.el --- Tests for wttrin-requery -*- lexical-binding: t; -*-

;; Copyright (C) 2025 Craig Jennings

;;; Commentary:

;; Unit tests for wttrin--requery-location and wttrin-requery.
;; wttrin--requery-location holds the core logic (kill buffer, query new location).
;; wttrin-requery is the interactive wrapper that adds completing-read.

;;; Code:

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

;;; Setup and Teardown

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

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

;;; --------------------------------------------------------------------------
;;; wttrin--requery-location (core logic)
;;; --------------------------------------------------------------------------

;;; Normal Cases

(ert-deftest test-wttrin--requery-location-normal-kills-existing-buffer ()
  "Requerying should kill the existing *wttr.in* buffer before opening a new one."
  (test-wttrin-requery-setup)
  (unwind-protect
      (let ((old-buffer (get-buffer-create "*wttr.in*")))
        (cl-letf (((symbol-function 'wttrin-query) (lambda (_loc) nil)))
          (wttrin--requery-location "Tokyo")
          ;; Old buffer should be dead
          (should-not (buffer-live-p old-buffer))))
    (test-wttrin-requery-teardown)))

(ert-deftest test-wttrin--requery-location-normal-queries-new-location ()
  "Requerying should fetch weather for the newly specified location."
  (test-wttrin-requery-setup)
  (unwind-protect
      (let ((queried-location nil))
        (cl-letf (((symbol-function 'wttrin-query)
                   (lambda (loc) (setq queried-location loc))))
          (wttrin--requery-location "Berlin, DE")
          (should (equal queried-location "Berlin, DE"))))
    (test-wttrin-requery-teardown)))

;;; Boundary Cases

(ert-deftest test-wttrin--requery-location-boundary-no-existing-buffer ()
  "Requerying when no weather buffer exists should still query the new location."
  (test-wttrin-requery-setup)
  (unwind-protect
      (let ((queried-location nil))
        ;; Ensure no buffer exists
        (should-not (get-buffer "*wttr.in*"))
        (cl-letf (((symbol-function 'wttrin-query)
                   (lambda (loc) (setq queried-location loc))))
          (wttrin--requery-location "Paris")
          (should (equal queried-location "Paris"))))
    (test-wttrin-requery-teardown)))

(ert-deftest test-wttrin--requery-location-boundary-unicode-location ()
  "Requerying with unicode characters should pass them through unchanged."
  (test-wttrin-requery-setup)
  (unwind-protect
      (let ((queried-location nil))
        (cl-letf (((symbol-function 'wttrin-query)
                   (lambda (loc) (setq queried-location loc))))
          (wttrin--requery-location "Zürich, CH")
          (should (equal queried-location "Zürich, CH"))))
    (test-wttrin-requery-teardown)))

;;; --------------------------------------------------------------------------
;;; wttrin-requery (interactive wrapper)
;;; --------------------------------------------------------------------------

(ert-deftest test-wttrin-requery-normal-uses-selected-location ()
  "The interactive command should pass the user's completing-read selection
to the core requery function."
  (test-wttrin-requery-setup)
  (unwind-protect
      (let ((requeried-location nil))
        (cl-letf (((symbol-function 'completing-read)
                   (lambda (_prompt _collection &rest _args) "London, GB"))
                  ((symbol-function 'wttrin--requery-location)
                   (lambda (loc) (setq requeried-location loc))))
          (wttrin-requery)
          (should (equal requeried-location "London, GB"))))
    (test-wttrin-requery-teardown)))

(ert-deftest test-wttrin-requery-normal-offers-default-locations ()
  "Completing-read should be called with wttrin-default-locations as candidates."
  (test-wttrin-requery-setup)
  (unwind-protect
      (let ((offered-collection nil)
            (wttrin-default-locations '("Paris" "London" "Tokyo")))
        (cl-letf (((symbol-function 'completing-read)
                   (lambda (_prompt collection &rest _args)
                     (setq offered-collection collection)
                     "Paris"))
                  ((symbol-function 'wttrin--requery-location)
                   (lambda (_loc) nil)))
          (wttrin-requery)
          (should (equal offered-collection '("Paris" "London" "Tokyo")))))
    (test-wttrin-requery-teardown)))

(ert-deftest test-wttrin-requery-boundary-single-default-prefills ()
  "When only one default location exists, it should be pre-filled in the prompt."
  (test-wttrin-requery-setup)
  (unwind-protect
      (let ((initial-input nil)
            (wttrin-default-locations '("Solo City")))
        (cl-letf (((symbol-function 'completing-read)
                   (lambda (_prompt _collection _predicate _require-match init &rest _args)
                     (setq initial-input init)
                     "Solo City"))
                  ((symbol-function 'wttrin--requery-location)
                   (lambda (_loc) nil)))
          (wttrin-requery)
          (should (equal initial-input "Solo City"))))
    (test-wttrin-requery-teardown)))

(ert-deftest test-wttrin-requery-boundary-multiple-defaults-no-prefill ()
  "When multiple default locations exist, nothing should be pre-filled."
  (test-wttrin-requery-setup)
  (unwind-protect
      (let ((initial-input 'not-called)
            (wttrin-default-locations '("Paris" "London")))
        (cl-letf (((symbol-function 'completing-read)
                   (lambda (_prompt _collection _predicate _require-match init &rest _args)
                     (setq initial-input init)
                     "Paris"))
                  ((symbol-function 'wttrin--requery-location)
                   (lambda (_loc) nil)))
          (wttrin-requery)
          (should-not initial-input)))
    (test-wttrin-requery-teardown)))

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