blob: 86c5ed8a6c0c4d6403f265487d6bbf173c21ea57 (
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
145
146
147
|
;;; 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"))
;;; Setup and Teardown
(defun test-chime-async-helpers-setup ()
"Reset counters and modeline state 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))
(defun test-chime-async-helpers-teardown ()
"Restore default state after each test."
(setq chime--consecutive-async-failures 0)
(setq chime-max-consecutive-failures 5)
(setq chime-modeline-string nil))
;;;; 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))
'(event1 event2))
(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."
(test-chime-async-helpers-setup)
(unwind-protect
(let ((called-with 'unset))
(chime--handle-async-success
(lambda (events) (setq called-with events))
'(a b c))
(should (equal '(a b c) 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))
(setq chime--consecutive-async-failures 0)
(chime--handle-async-success
(lambda (events) (setq called-with events))
'(x))
(should (= 0 chime--consecutive-async-failures))
(should (equal '(x) called-with)))
(test-chime-async-helpers-teardown)))
(provide 'test-chime-async-helpers)
;;; test-chime-async-helpers.el ends here
|