summaryrefslogtreecommitdiff
path: root/tests/test-wttrin--format-staleness-header.el
blob: ef8882f515d201b877a6898c0f16768caa5eaf43 (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
;;; test-wttrin--format-staleness-header.el --- Tests for staleness header -*- lexical-binding: t; -*-

;; Copyright (C) 2025 Craig Jennings

;;; Commentary:

;; Unit tests for wttrin--format-staleness-header function.
;; Tests that buffer staleness information is formatted correctly.

;;; Code:

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

;;; Setup and Teardown

(defun test-wttrin--format-staleness-header-setup ()
  "Setup for staleness header tests."
  (testutil-wttrin-setup))

(defun test-wttrin--format-staleness-header-teardown ()
  "Teardown for staleness header tests."
  (testutil-wttrin-teardown))

;;; Normal Cases

(ert-deftest test-wttrin--format-staleness-header-normal-returns-formatted-string ()
  "Staleness header includes time and age when cache exists."
  (test-wttrin--format-staleness-header-setup)
  (unwind-protect
      (let ((now 1000000.0))
        (cl-letf (((symbol-function 'float-time) (lambda () now)))
          ;; Add data cached 5 minutes ago
          (testutil-wttrin-add-to-cache "Paris" "weather data" 300)
          (let ((header (wttrin--format-staleness-header "Paris")))
            (should header)
            (should (string-match-p "Last updated:" header))
            (should (string-match-p "5 minutes ago" header)))))
    (test-wttrin--format-staleness-header-teardown)))

(ert-deftest test-wttrin--format-staleness-header-normal-just-now ()
  "Staleness header shows 'just now' for very recent data."
  (test-wttrin--format-staleness-header-setup)
  (unwind-protect
      (let ((now 1000000.0))
        (cl-letf (((symbol-function 'float-time) (lambda () now)))
          (testutil-wttrin-add-to-cache "Paris" "weather data" 10)
          (let ((header (wttrin--format-staleness-header "Paris")))
            (should (string-match-p "just now" header)))))
    (test-wttrin--format-staleness-header-teardown)))

;;; Boundary Cases

(ert-deftest test-wttrin--format-staleness-header-boundary-no-cache-returns-nil ()
  "Returns nil when no cache entry exists for location."
  (test-wttrin--format-staleness-header-setup)
  (unwind-protect
      (should-not (wttrin--format-staleness-header "Unknown City"))
    (test-wttrin--format-staleness-header-teardown)))

(ert-deftest test-wttrin--format-staleness-header-boundary-different-unit-system ()
  "Cache lookup respects current unit system setting."
  (test-wttrin--format-staleness-header-setup)
  (unwind-protect
      (let ((now 1000000.0))
        (cl-letf (((symbol-function 'float-time) (lambda () now)))
          ;; Add data under metric unit system
          (let ((wttrin-unit-system "m"))
            (testutil-wttrin-add-to-cache "Paris" "metric data" 300))
          ;; Query under imperial unit system — should find nothing
          (let ((wttrin-unit-system "u"))
            (should-not (wttrin--format-staleness-header "Paris")))
          ;; Query under metric — should find it
          (let ((wttrin-unit-system "m"))
            (should (wttrin--format-staleness-header "Paris")))))
    (test-wttrin--format-staleness-header-teardown)))

(provide 'test-wttrin--format-staleness-header)
;;; test-wttrin--format-staleness-header.el ends here