blob: b4803297b54e32110b487008ebb0e566364d6efa (
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
|
;;; test-wttrin--save-debug-data.el --- Tests for wttrin--save-debug-data -*- lexical-binding: t; -*-
;; Copyright (C) 2025 Craig Jennings
;;; Commentary:
;; Unit tests for wttrin--save-debug-data function.
;; Tests that debug data files are created correctly with the expected contents.
;;; Code:
(require 'ert)
(require 'wttrin)
(require 'testutil-wttrin)
;;; Setup and Teardown
(defun test-wttrin--save-debug-data-setup ()
"Setup for save-debug-data tests."
(testutil-wttrin-setup))
(defun test-wttrin--save-debug-data-teardown ()
"Teardown for save-debug-data tests."
(testutil-wttrin-teardown))
;;; Normal Cases
(ert-deftest test-wttrin--save-debug-data-normal-creates-file-that-exists ()
"Returned filepath should point to a file that actually exists on disk."
(test-wttrin--save-debug-data-setup)
(unwind-protect
(let ((filepath (wttrin--save-debug-data "Paris" "some weather data")))
(unwind-protect
(should (file-exists-p filepath))
(when (file-exists-p filepath) (delete-file filepath))))
(test-wttrin--save-debug-data-teardown)))
(ert-deftest test-wttrin--save-debug-data-normal-file-contains-location ()
"File should contain a Location header with the queried location."
(test-wttrin--save-debug-data-setup)
(unwind-protect
(let ((filepath (wttrin--save-debug-data "New Orleans, LA" "weather")))
(unwind-protect
(let ((contents (with-temp-buffer
(insert-file-contents filepath)
(buffer-string))))
(should (string-match-p "^Location: New Orleans, LA$" contents)))
(when (file-exists-p filepath) (delete-file filepath))))
(test-wttrin--save-debug-data-teardown)))
(ert-deftest test-wttrin--save-debug-data-normal-file-contains-unit-system ()
"File should record the wttrin-unit-system value at the time of capture."
(test-wttrin--save-debug-data-setup)
(unwind-protect
(let* ((wttrin-unit-system "m")
(filepath (wttrin--save-debug-data "Paris" "weather data")))
(unwind-protect
(let ((contents (with-temp-buffer
(insert-file-contents filepath)
(buffer-string))))
(should (string-match-p "wttrin-unit-system: m" contents)))
(when (file-exists-p filepath) (delete-file filepath))))
(test-wttrin--save-debug-data-teardown)))
(ert-deftest test-wttrin--save-debug-data-normal-file-contains-raw-response ()
"File should contain the raw weather response body after the separator."
(test-wttrin--save-debug-data-setup)
(unwind-protect
(let ((filepath (wttrin--save-debug-data "Berlin" "Clear skies 72°F")))
(unwind-protect
(let ((contents (with-temp-buffer
(insert-file-contents filepath)
(buffer-string))))
(should (string-match-p "--- Raw Response ---" contents))
(should (string-match-p "Clear skies 72°F" contents)))
(when (file-exists-p filepath) (delete-file filepath))))
(test-wttrin--save-debug-data-teardown)))
(ert-deftest test-wttrin--save-debug-data-normal-file-contains-timestamp ()
"File should contain a human-readable timestamp."
(test-wttrin--save-debug-data-setup)
(unwind-protect
(let ((filepath (wttrin--save-debug-data "Tokyo" "data")))
(unwind-protect
(let ((contents (with-temp-buffer
(insert-file-contents filepath)
(buffer-string))))
;; Timestamp should look like YYYY-MM-DD HH:MM:SS
(should (string-match-p "^Timestamp: [0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}$"
contents)))
(when (file-exists-p filepath) (delete-file filepath))))
(test-wttrin--save-debug-data-teardown)))
;;; Boundary Cases
(ert-deftest test-wttrin--save-debug-data-boundary-unicode-location ()
"File should preserve unicode characters in the location name."
(test-wttrin--save-debug-data-setup)
(unwind-protect
(let ((filepath (wttrin--save-debug-data "São Paulo, BR" "data")))
(unwind-protect
(let ((contents (with-temp-buffer
(insert-file-contents filepath)
(buffer-string))))
(should (string-match-p "São Paulo" contents)))
(when (file-exists-p filepath) (delete-file filepath))))
(test-wttrin--save-debug-data-teardown)))
(ert-deftest test-wttrin--save-debug-data-boundary-empty-raw-string ()
"Empty raw-string should still produce a valid file (just no response body)."
(test-wttrin--save-debug-data-setup)
(unwind-protect
(let ((filepath (wttrin--save-debug-data "Paris" "")))
(unwind-protect
(progn
(should (file-exists-p filepath))
(let ((contents (with-temp-buffer
(insert-file-contents filepath)
(buffer-string))))
;; Should still have the headers and separator
(should (string-match-p "Location: Paris" contents))
(should (string-match-p "--- Raw Response ---" contents))))
(when (file-exists-p filepath) (delete-file filepath))))
(test-wttrin--save-debug-data-teardown)))
;;; Error Cases
(ert-deftest test-wttrin--save-debug-data-error-nil-raw-string-should-not-crash ()
"Nil raw-string should be handled gracefully, not cause an insert error.
This can happen when wttrin--display-weather is called with nil data
and debug mode is on — save-debug-data is called before validation."
(test-wttrin--save-debug-data-setup)
(unwind-protect
;; wttrin--save-debug-data calls (insert raw-string) which errors on nil.
;; This test documents the bug: a nil raw-string should produce a file
;; with an empty or placeholder response body, not crash.
(let ((filepath (wttrin--save-debug-data "Paris" nil)))
(unwind-protect
(should (file-exists-p filepath))
(when (and filepath (file-exists-p filepath)) (delete-file filepath))))
(test-wttrin--save-debug-data-teardown)))
(provide 'test-wttrin--save-debug-data)
;;; test-wttrin--save-debug-data.el ends here
|