From 6cf0aac96ae0abb455ee8525e0ede9a63f5974f3 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Tue, 4 Nov 2025 07:56:31 -0600 Subject: Add comprehensive ERT test suite and fix critical bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Session 1: Testing infrastructure and initial test coverage Bug fixes in wttrin.el: - Fix wttrin-additional-url-params to handle nil unit system - Remove incorrect callback parameter to url-retrieve-synchronously - Add nil buffer check for network failures - Strip HTTP headers before decoding response - Kill buffer after fetch to prevent memory leaks - Fix double concatenation of URL params in cache function - Add proper URL encoding via new wttrin--build-url function Refactoring: - Extract wttrin--build-url as pure, testable function - Separate URL building logic from network I/O Test infrastructure (33 tests, 100% passing): - tests/testutil-wttrin.el: Shared test utilities - tests/test-wttrin-additional-url-params.el: 7 tests - tests/test-wttrin--make-cache-key.el: 9 tests - tests/test-wttrin--build-url.el: 10 tests - tests/test-wttrin--cleanup-cache-if-needed.el: 7 tests Documentation: - docs/testing-plan.org: Comprehensive testing roadmap - docs/bugs.org: Bug analysis from code review - docs/NOTES.org: Session tracking and guidelines - docs/session-1-summary.org: Detailed session summary Next session: Cache workflow tests, parsing logic extraction, integration tests 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- tests/testutil-wttrin.el | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tests/testutil-wttrin.el (limited to 'tests/testutil-wttrin.el') diff --git a/tests/testutil-wttrin.el b/tests/testutil-wttrin.el new file mode 100644 index 0000000..0e109ad --- /dev/null +++ b/tests/testutil-wttrin.el @@ -0,0 +1,83 @@ +;;; testutil-wttrin.el --- Test utilities for wttrin -*- lexical-binding: t; -*- + +;; Copyright (C) 2025 Craig Jennings + +;;; Commentary: + +;; Shared test utilities for wttrin test suite. +;; Provides helper functions, fixtures, and common setup/teardown functionality. + +;;; Code: + +(require 'ert) + +;;; Test Data Fixtures + +(defconst testutil-wttrin-sample-weather-response + "Weather report: Paris, France + + \\ / Partly cloudy + _ /\"\".-. 22 °C + \\_( ). ↓ 15 km/h + /(___(__) 10 km + 0.0 mm" + "Sample weather response for testing parsing logic.") + +(defconst testutil-wttrin-sample-error-response + "ERROR: Unknown location; please try ~curl wttr.in/:help" + "Sample error response from wttr.in service.") + +;;; Cache Testing Helpers + +(defun testutil-wttrin-clear-cache () + "Clear the wttrin cache for test isolation." + (clrhash wttrin--cache)) + +(defun testutil-wttrin-add-to-cache (location data &optional age-seconds) + "Add DATA to cache for LOCATION, optionally aged by AGE-SECONDS." + (let* ((cache-key (wttrin--make-cache-key location)) + (timestamp (if age-seconds + (- (float-time) age-seconds) + (float-time)))) + (puthash cache-key (cons timestamp data) wttrin--cache))) + +(defun testutil-wttrin-cache-size () + "Return the current number of entries in the cache." + (hash-table-count wttrin--cache)) + +;;; Custom Variable Management + +(defmacro testutil-wttrin-with-unit-system (unit-system &rest body) + "Execute BODY with wttrin-unit-system temporarily set to UNIT-SYSTEM." + (declare (indent 1)) + `(let ((wttrin-unit-system ,unit-system)) + ,@body)) + +(defmacro testutil-wttrin-with-cache-ttl (ttl &rest body) + "Execute BODY with wttrin-cache-ttl temporarily set to TTL." + (declare (indent 1)) + `(let ((wttrin-cache-ttl ,ttl)) + ,@body)) + +(defmacro testutil-wttrin-with-cache-max (max-entries &rest body) + "Execute BODY with wttrin-cache-max-entries temporarily set to MAX-ENTRIES." + (declare (indent 1)) + `(let ((wttrin-cache-max-entries ,max-entries)) + ,@body)) + +;;; Test Setup and Teardown + +(defun testutil-wttrin-setup () + "Common setup for wttrin tests. +Call this at the beginning of each test." + (testutil-wttrin-clear-cache) + (setq wttrin--force-refresh nil)) + +(defun testutil-wttrin-teardown () + "Common teardown for wttrin tests. +Call this at the end of each test." + (testutil-wttrin-clear-cache) + (setq wttrin--force-refresh nil)) + +(provide 'testutil-wttrin) +;;; testutil-wttrin.el ends here -- cgit v1.2.3