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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
;;; test-chime-async-helpers.el --- Tests for async result helpers -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Craig Jennings
;; Author: Craig Jennings <c@cjennings.net>
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Unit tests for the async-result helpers used inside
;; chime--fetch-and-process:
;; - chime--record-async-failure
;; - chime--handle-async-success
;;; Code:
(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))
(require 'testutil-time (expand-file-name "testutil-time.el"))
;;; Setup and Teardown
(defun test-chime-async-helpers-setup ()
"Reset counters, modeline state, and the deprecation-warning guard before each test."
(setq chime--consecutive-async-failures 0)
(setq chime-max-consecutive-failures 5)
(setq chime-modeline-no-events-text "*")
(setq chime-modeline-string nil)
(setq chime--deprecated-property-warned nil))
(defun test-chime-async-helpers-teardown ()
"Restore default state after each test, including the deprecation-warning guard."
(setq chime--consecutive-async-failures 0)
(setq chime-max-consecutive-failures 5)
(setq chime-modeline-string nil)
(setq chime--deprecated-property-warned nil))
(defun test-chime-async-helpers--event (title &optional deprecated-property)
"Build a minimal valid Chime event alist with TITLE.
DEPRECATED-PROPERTY, when given, marks the event as having used a
deprecated per-event property of that name."
(let ((time (test-time-tomorrow-at 14 0)))
(chime--make-event (list (cons (test-timestamp-string time) time))
title '((10 . medium)) nil nil deprecated-property)))
;;;; Tests for chime--record-async-failure
(ert-deftest test-chime-record-async-failure-normal-increments-counter ()
"Normal: each call increments the consecutive-failure counter by one."
(test-chime-async-helpers-setup)
(unwind-protect
(let ((err '(error "boom")))
(chime--record-async-failure err "Async error")
(should (= 1 chime--consecutive-async-failures))
(chime--record-async-failure err "Async error")
(should (= 2 chime--consecutive-async-failures)))
(test-chime-async-helpers-teardown)))
(ert-deftest test-chime-record-async-failure-normal-sets-modeline-error-state ()
"Normal: sets chime-modeline-string with the standard error tooltip."
(test-chime-async-helpers-setup)
(unwind-protect
(cl-letf (((symbol-function 'force-mode-line-update) (lambda (&optional _))))
(chime--record-async-failure '(error "boom") "Async error")
(should chime-modeline-string)
(should (string-match-p "Event check failed"
(get-text-property 0 'help-echo chime-modeline-string))))
(test-chime-async-helpers-teardown)))
(ert-deftest test-chime-record-async-failure-normal-warns-at-threshold ()
"Normal: triggers display-warning when the counter reaches the threshold."
(test-chime-async-helpers-setup)
(unwind-protect
(let ((warned nil))
(setq chime--consecutive-async-failures 4)
(setq chime-max-consecutive-failures 5)
(cl-letf (((symbol-function 'display-warning)
(lambda (_type _msg &rest _args) (setq warned t)))
((symbol-function 'force-mode-line-update) (lambda (&optional _))))
(chime--record-async-failure '(error "boom") "Async error")
(should warned)
(should (= 5 chime--consecutive-async-failures))))
(test-chime-async-helpers-teardown)))
(ert-deftest test-chime-record-async-failure-boundary-no-modeline-text-skips-modeline ()
"Boundary: when chime-modeline-no-events-text is nil, modeline string stays nil."
(test-chime-async-helpers-setup)
(unwind-protect
(let ((chime-modeline-no-events-text nil))
(setq chime-modeline-string nil)
(chime--record-async-failure '(error "boom") "Async error")
(should (= 1 chime--consecutive-async-failures))
(should (null chime-modeline-string)))
(test-chime-async-helpers-teardown)))
;;;; Tests for chime--handle-async-success
(ert-deftest test-chime-handle-async-success-normal-resets-counter ()
"Normal: resets the consecutive-failure counter from a positive value to zero."
(test-chime-async-helpers-setup)
(unwind-protect
(let ((called-with nil))
(setq chime--consecutive-async-failures 3)
(chime--handle-async-success
(lambda (events) (setq called-with events))
(list (test-chime-async-helpers--event "A")
(test-chime-async-helpers--event "B")))
(should (= 0 chime--consecutive-async-failures)))
(test-chime-async-helpers-teardown)))
(ert-deftest test-chime-handle-async-success-normal-invokes-callback-with-events ()
"Normal: calls the supplied callback with the events list verbatim."
(test-chime-async-helpers-setup)
(unwind-protect
(let* ((called-with 'unset)
(events (list (test-chime-async-helpers--event "A")
(test-chime-async-helpers--event "B"))))
(chime--handle-async-success
(lambda (e) (setq called-with e))
events)
(should (eq events called-with)))
(test-chime-async-helpers-teardown)))
(ert-deftest test-chime-handle-async-success-boundary-empty-events ()
"Boundary: works with an empty events list."
(test-chime-async-helpers-setup)
(unwind-protect
(let ((called-with 'unset))
(chime--handle-async-success
(lambda (events) (setq called-with events))
'())
(should (null called-with))
(should (= 0 chime--consecutive-async-failures)))
(test-chime-async-helpers-teardown)))
(ert-deftest test-chime-handle-async-success-boundary-counter-already-zero ()
"Boundary: counter starts at zero, stays at zero, callback still fires."
(test-chime-async-helpers-setup)
(unwind-protect
(let* ((called-with 'unset)
(events (list (test-chime-async-helpers--event "X"))))
(setq chime--consecutive-async-failures 0)
(chime--handle-async-success
(lambda (e) (setq called-with e))
events)
(should (= 0 chime--consecutive-async-failures))
(should (eq events called-with)))
(test-chime-async-helpers-teardown)))
(ert-deftest test-chime-handle-async-success-normal-warns-on-deprecated-property ()
"Normal: warns once when an event used a deprecated per-event property."
(test-chime-async-helpers-setup)
(unwind-protect
(let ((warned nil))
(cl-letf (((symbol-function 'display-warning)
(lambda (_type msg &rest _) (push msg warned))))
(chime--handle-async-success
#'ignore
(list (test-chime-async-helpers--event "A")
(test-chime-async-helpers--event "B" "WILD_NOTIFIER_NOTIFY_BEFORE"))))
(should (= 1 (length warned)))
(should (string-match-p "WILD_NOTIFIER_NOTIFY_BEFORE" (car warned)))
(should chime--deprecated-property-warned))
(test-chime-async-helpers-teardown)))
(provide 'test-chime-async-helpers)
;;; test-chime-async-helpers.el ends here
|