blob: c700f898c7575c79416134532d8e53ad35a5d84e (
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
148
149
150
151
152
|
;;; test-chime-warn-persistent-failures.el --- Tests for chime--maybe-warn-persistent-failures -*- 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 chime--maybe-warn-persistent-failures function.
;; This function warns the user when consecutive async failures reach
;; the configured threshold. It should warn exactly once at the threshold.
;;; Code:
(require 'test-bootstrap (expand-file-name "test-bootstrap.el"))
;;; Setup and Teardown
(defun test-warn-failures-setup ()
"Setup function."
(setq chime--consecutive-async-failures 0)
(setq chime-max-consecutive-failures 5))
(defun test-warn-failures-teardown ()
"Teardown function."
(setq chime--consecutive-async-failures 0)
(setq chime-max-consecutive-failures 5))
;;; Normal Cases
(ert-deftest test-chime-warn-failures-warns-at-threshold ()
"Should call display-warning when failures reach the threshold."
(test-warn-failures-setup)
(unwind-protect
(let ((warned nil))
(setq chime--consecutive-async-failures 5)
(setq chime-max-consecutive-failures 5)
(cl-letf (((symbol-function 'display-warning)
(lambda (_type _msg &rest _args) (setq warned t))))
(chime--maybe-warn-persistent-failures)
(should warned)))
(test-warn-failures-teardown)))
(ert-deftest test-chime-warn-failures-no-warn-below-threshold ()
"Should NOT warn when failures are below the threshold."
(test-warn-failures-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))))
(chime--maybe-warn-persistent-failures)
(should-not warned)))
(test-warn-failures-teardown)))
(ert-deftest test-chime-warn-failures-no-warn-above-threshold ()
"Should NOT warn again after exceeding the threshold (warns once)."
(test-warn-failures-setup)
(unwind-protect
(let ((warned nil))
(setq chime--consecutive-async-failures 6)
(setq chime-max-consecutive-failures 5)
(cl-letf (((symbol-function 'display-warning)
(lambda (_type _msg &rest _args) (setq warned t))))
(chime--maybe-warn-persistent-failures)
(should-not warned)))
(test-warn-failures-teardown)))
(ert-deftest test-chime-warn-failures-message-includes-count ()
"Warning message should include the failure count."
(test-warn-failures-setup)
(unwind-protect
(let ((warning-msg nil))
(setq chime--consecutive-async-failures 5)
(setq chime-max-consecutive-failures 5)
(cl-letf (((symbol-function 'display-warning)
(lambda (_type msg &rest _args) (setq warning-msg msg))))
(chime--maybe-warn-persistent-failures)
(should warning-msg)
(should (string-match-p "5" warning-msg))))
(test-warn-failures-teardown)))
(ert-deftest test-chime-warn-failures-severity-is-warning ()
"Warning should use :warning severity."
(test-warn-failures-setup)
(unwind-protect
(let ((warning-severity nil))
(setq chime--consecutive-async-failures 5)
(setq chime-max-consecutive-failures 5)
(cl-letf (((symbol-function 'display-warning)
(lambda (_type _msg &rest args) (setq warning-severity (car args)))))
(chime--maybe-warn-persistent-failures)
(should (eq :warning warning-severity))))
(test-warn-failures-teardown)))
;;; Boundary Cases
(ert-deftest test-chime-warn-failures-threshold-of-1 ()
"Threshold of 1 should warn on the very first failure."
(test-warn-failures-setup)
(unwind-protect
(let ((warned nil))
(setq chime--consecutive-async-failures 1)
(setq chime-max-consecutive-failures 1)
(cl-letf (((symbol-function 'display-warning)
(lambda (_type _msg &rest _args) (setq warned t))))
(chime--maybe-warn-persistent-failures)
(should warned)))
(test-warn-failures-teardown)))
(ert-deftest test-chime-warn-failures-threshold-of-0-disables ()
"Threshold of 0 should disable warnings entirely."
(test-warn-failures-setup)
(unwind-protect
(let ((warned nil))
(setq chime--consecutive-async-failures 0)
(setq chime-max-consecutive-failures 0)
(cl-letf (((symbol-function 'display-warning)
(lambda (_type _msg &rest _args) (setq warned t))))
(chime--maybe-warn-persistent-failures)
(should-not warned)))
(test-warn-failures-teardown)))
(ert-deftest test-chime-warn-failures-zero-failures-no-warn ()
"Zero failures should never warn regardless of threshold."
(test-warn-failures-setup)
(unwind-protect
(let ((warned nil))
(setq chime--consecutive-async-failures 0)
(setq chime-max-consecutive-failures 5)
(cl-letf (((symbol-function 'display-warning)
(lambda (_type _msg &rest _args) (setq warned t))))
(chime--maybe-warn-persistent-failures)
(should-not warned)))
(test-warn-failures-teardown)))
(provide 'test-chime-warn-persistent-failures)
;;; test-chime-warn-persistent-failures.el ends here
|