summaryrefslogtreecommitdiff
path: root/tests/test-wttrin--replace-response-location.el
blob: 3412ae9cd4c95d88053d09c485faf1da6accbc19 (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
;;; test-wttrin--replace-response-location.el --- Tests for wttrin--replace-response-location -*- lexical-binding: t; -*-

;; Copyright (C) 2025-2026 Craig Jennings

;;; Commentary:

;; Unit tests for wttrin--replace-response-location function.
;; Tests that the API's lowercase location prefix gets replaced with the
;; user's original casing while preserving the weather data.

;;; Code:

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

;;; Setup and Teardown

(defun test-wttrin--replace-response-location-setup ()
  "Setup for replace-response-location tests."
  (testutil-wttrin-setup))

(defun test-wttrin--replace-response-location-teardown ()
  "Teardown for replace-response-location tests."
  (testutil-wttrin-teardown))

;;; Normal Cases

(ert-deftest test-wttrin--replace-response-location-normal-replaces-lowercase ()
  "API's lowercase location should be replaced with user's original casing."
  (should (equal (wttrin--replace-response-location
                  "new orleans, la: ☀️ +72°F Clear"
                  "New Orleans, LA")
                 "New Orleans, LA: ☀️ +72°F Clear")))

(ert-deftest test-wttrin--replace-response-location-normal-preserves-weather-data ()
  "Everything after the colon should be identical to the original response."
  (let* ((response "berlin: 🌧 +48°F Light rain")
         (result (wttrin--replace-response-location response "Berlin")))
    ;; Weather data portion should be untouched
    (should (string-suffix-p ": 🌧 +48°F Light rain" result))))

;;; Boundary Cases

(ert-deftest test-wttrin--replace-response-location-boundary-already-correct ()
  "When API casing already matches user's, the string should be unchanged."
  (should (equal (wttrin--replace-response-location
                  "Paris: ☀️ +61°F Clear"
                  "Paris")
                 "Paris: ☀️ +61°F Clear")))

(ert-deftest test-wttrin--replace-response-location-boundary-unicode ()
  "Unicode location should be preserved correctly in the replacement."
  (should (equal (wttrin--replace-response-location
                  "são paulo: 🌤 +77°F Partly cloudy"
                  "São Paulo")
                 "São Paulo: 🌤 +77°F Partly cloudy")))

(ert-deftest test-wttrin--replace-response-location-boundary-no-colon ()
  "Response with no colon should be returned unchanged."
  (should (equal (wttrin--replace-response-location
                  "Unknown location"
                  "Paris")
                 "Unknown location")))

;;; Integration: fetch populates cache with corrected casing

(ert-deftest test-wttrin--mode-line-fetch-caches-user-location-casing ()
  "After a successful fetch, the cached string should use the user's
location casing, not the API's lowercase version."
  (test-wttrin--replace-response-location-setup)
  (unwind-protect
      (let ((wttrin-favorite-location "New Orleans, LA")
            (wttrin--mode-line-cache nil))
        (testutil-wttrin-mock-http-response
            "new orleans, la: ☀️ +72°F Clear"
          (wttrin--mode-line-fetch-weather)
          (should wttrin--mode-line-cache)
          (let ((cached-string (cdr wttrin--mode-line-cache)))
            (should (string-prefix-p "New Orleans, LA:" cached-string)))))
    (test-wttrin--replace-response-location-teardown)))

(provide 'test-wttrin--replace-response-location)
;;; test-wttrin--replace-response-location.el ends here