summaryrefslogtreecommitdiff
path: root/tests/test-wttrin--save-debug-data.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-04-04 12:26:16 -0500
committerCraig Jennings <c@cjennings.net>2026-04-04 12:26:16 -0500
commitea5636c51361b86d132d647ee3548d208394878e (patch)
treea60ab847c7324417ca7de46e73d370ba2af90d05 /tests/test-wttrin--save-debug-data.el
parentb74b98f177d92d50ddbede900ba41212e07c5f63 (diff)
downloademacs-wttrin-ea5636c51361b86d132d647ee3548d208394878e.tar.gz
emacs-wttrin-ea5636c51361b86d132d647ee3548d208394878e.zip
test: add 50 tests for untested functions; fix nil crash in save-debug-data
Add 11 new test files covering wttrin--save-debug-data, wttrin--buffer-cache-refresh, wttrin--mode-line-stop, wttrin--mode-line-start, wttrin-query, wttrin-requery-force, wttrin-mode-line-click, wttrin-mode-line-force-refresh, wttrin-fetch-raw-string, wttrin-clear-cache, and wttrin-requery. Fix bug in wttrin--save-debug-data where nil raw-string caused (insert nil) crash — reachable when debug mode is on and fetch fails. Refactor wttrin-requery: extract wttrin--requery-location so the core kill-buffer-and-query logic is testable without mocking completing-read. 267 tests total (was 217), all passing.
Diffstat (limited to 'tests/test-wttrin--save-debug-data.el')
-rw-r--r--tests/test-wttrin--save-debug-data.el144
1 files changed, 144 insertions, 0 deletions
diff --git a/tests/test-wttrin--save-debug-data.el b/tests/test-wttrin--save-debug-data.el
new file mode 100644
index 0000000..b480329
--- /dev/null
+++ b/tests/test-wttrin--save-debug-data.el
@@ -0,0 +1,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