blob: 1b2e6e53ace265592d4efce6d0ea0fe68d13eba9 (
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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
;;; test-video-audio-recording-quick-setup.el --- Tests for cj/recording-quick-setup -*- lexical-binding: t; -*-
;;; Commentary:
;; Unit tests for cj/recording-quick-setup function.
;; The quick setup is a two-step flow:
;; Step 1: Pick a microphone
;; Step 2: Pick an audio output (sink) with active/inactive indicators
;; The chosen sink's .monitor source is set as the system audio device.
;;; Code:
(require 'ert)
;; Stub dependencies before loading the module
(defvar cj/custom-keymap (make-sparse-keymap)
"Stub keymap for testing.")
;; Now load the actual production module
(require 'video-audio-recording)
;;; Setup and Teardown
(defun test-quick-setup-setup ()
"Reset device variables before each test."
(setq cj/recording-mic-device nil)
(setq cj/recording-system-device nil))
(defun test-quick-setup-teardown ()
"Clean up device variables after each test."
(setq cj/recording-mic-device nil)
(setq cj/recording-system-device nil))
;;; Normal Cases
(ert-deftest test-video-audio-recording-quick-setup-normal-sets-mic-device ()
"Test that selecting a mic sets cj/recording-mic-device."
(test-quick-setup-setup)
(unwind-protect
(let ((call-count 0))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono")
("builtin-input" . "Built-in Analog"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("jds-labs" . "JDS Labs Element IV"))))
((symbol-function 'cj/recording--sink-active-p)
(lambda (_name) nil))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(setq call-count (1+ call-count))
(car (all-completions "" table)))))
(cj/recording-quick-setup)
(should (equal "jabra-input" cj/recording-mic-device))))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-normal-sets-system-to-sink-monitor ()
"Test that system device is set to the chosen sink's .monitor."
(test-quick-setup-setup)
(unwind-protect
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("alsa_output.usb-JDS_Labs-00.analog-stereo" . "JDS Labs Element IV"))))
((symbol-function 'cj/recording--sink-active-p)
(lambda (_name) nil))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(car (all-completions "" table)))))
(cj/recording-quick-setup)
(should (equal "alsa_output.usb-JDS_Labs-00.analog-stereo.monitor"
cj/recording-system-device)))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-normal-two-completing-reads ()
"Test that completing-read is called twice (mic + sink)."
(test-quick-setup-setup)
(unwind-protect
(let ((call-count 0))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("mic-1" . "Mic One"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sink-1" . "Sink One"))))
((symbol-function 'cj/recording--sink-active-p)
(lambda (_name) nil))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(setq call-count (1+ call-count))
(car (all-completions "" table)))))
(cj/recording-quick-setup)
(should (= 2 call-count))))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-normal-active-sink-indicator ()
"Test that active sinks get the active icon in their label."
(test-quick-setup-setup)
(unwind-protect
(let ((sink-candidates nil)
(call-count 0))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("mic-1" . "Mic One"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("active-sink" . "Active Sink")
("inactive-sink" . "Inactive Sink"))))
((symbol-function 'cj/recording--sink-active-p)
(lambda (name) (equal name "active-sink")))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(setq call-count (1+ call-count))
(let ((candidates (all-completions "" table)))
(when (= call-count 2)
(setq sink-candidates candidates))
(car candidates)))))
(cj/recording-quick-setup)
;; Active sink should have icon (substring-no-properties strips faces but keeps text)
(should (cl-some (lambda (c) (and (string-match-p "Active Sink" c)
(string-match-p "" c)))
sink-candidates))
;; Inactive sink should have icon
(should (cl-some (lambda (c) (and (string-match-p "Inactive Sink" c)
(string-match-p "" c)))
sink-candidates))))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-normal-active-sorted-first ()
"Test that active sinks are sorted to the top of the list."
(test-quick-setup-setup)
(unwind-protect
(let ((sink-candidates nil))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("mic-1" . "Mic One"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("inactive-sink" . "Inactive Sink")
("active-sink" . "Active Sink"))))
((symbol-function 'cj/recording--sink-active-p)
(lambda (name) (equal name "active-sink")))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(let ((candidates (all-completions "" table)))
(when (cl-some (lambda (c) (string-match-p "Sink" c)) candidates)
(setq sink-candidates candidates))
(car candidates)))))
(cj/recording-quick-setup)
;; First non-Cancel candidate should be the active sink
(let ((first-sink (car sink-candidates)))
(should (string-match-p "Active Sink" first-sink)))))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-normal-confirmation-message ()
"Test that confirmation message mentions the selected mic and monitor."
(test-quick-setup-setup)
(unwind-protect
(let ((message-text nil))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("jds-labs.analog-stereo" . "JDS Labs Element IV"))))
((symbol-function 'cj/recording--sink-active-p)
(lambda (_name) nil))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(car (all-completions "" table))))
((symbol-function 'message)
(lambda (fmt &rest args)
(setq message-text (apply #'format fmt args)))))
(cj/recording-quick-setup)
(should (string-match-p "Recording ready" message-text))
(should (string-match-p ".monitor" message-text))))
(test-quick-setup-teardown)))
;;; Boundary Cases
(ert-deftest test-video-audio-recording-quick-setup-boundary-single-mic ()
"Test that with only one mic, it still presents selection."
(test-quick-setup-setup)
(unwind-protect
(let ((read-called 0))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("sole-mic" . "Only Mic Available"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sole-sink" . "Only Sink"))))
((symbol-function 'cj/recording--sink-active-p)
(lambda (_name) nil))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(setq read-called (1+ read-called))
(car (all-completions "" table)))))
(cj/recording-quick-setup)
(should (= 2 read-called))
(should (equal "sole-mic" cj/recording-mic-device))))
(test-quick-setup-teardown)))
;;; Error Cases
(ert-deftest test-video-audio-recording-quick-setup-error-cancel-mic ()
"Test that cancelling mic selection signals user-error and does not set devices."
(test-quick-setup-setup)
(unwind-protect
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sink-1" . "Sink One"))))
((symbol-function 'cj/recording--sink-active-p)
(lambda (_name) nil))
((symbol-function 'completing-read)
(lambda (_prompt _choices &rest _args)
"Cancel")))
(should-error (cj/recording-quick-setup) :type 'user-error)
(should (null cj/recording-mic-device))
(should (null cj/recording-system-device)))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-error-cancel-sink ()
"Test that cancelling sink selection signals user-error."
(test-quick-setup-setup)
(unwind-protect
(let ((call-count 0))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sink-1" . "Sink One"))))
((symbol-function 'cj/recording--sink-active-p)
(lambda (_name) nil))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(setq call-count (1+ call-count))
(if (= call-count 1)
;; First call: select mic
(car (all-completions "" table))
;; Second call: cancel sink
"Cancel"))))
(should-error (cj/recording-quick-setup) :type 'user-error)
(should (null cj/recording-system-device))))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-error-no-mics ()
"Test that function signals error when no mics are found."
(test-quick-setup-setup)
(unwind-protect
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () nil)))
(should-error (cj/recording-quick-setup) :type 'user-error))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-error-no-mics-message ()
"Test that error message mentions mic and unmuted."
(test-quick-setup-setup)
(unwind-protect
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () nil)))
(condition-case err
(cj/recording-quick-setup)
(user-error
(should (string-match-p "mic" (error-message-string err))))))
(test-quick-setup-teardown)))
(provide 'test-video-audio-recording-quick-setup)
;;; test-video-audio-recording-quick-setup.el ends here
|