blob: 154ad26ec8aa8b4cea68ea4f9ca8ecfb0e9fb789 (
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
;;; 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 (with status labels)
;; Step 2: Pick an audio output (with status labels + app names)
;; Status labels: [in use], [ready], [available], [muted]
;; Sorted: in use → ready → available → muted.
;; 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
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("jabra-input" "Jabra SPEAK 510 Mono" "SUSPENDED" "no")
("builtin-input" "Built-in Analog" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("jds-labs" "JDS Labs Element IV" "RUNNING" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () nil))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) ""))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(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" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("alsa_output.usb-JDS_Labs-00.analog-stereo" "JDS Labs Element IV" "RUNNING" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () nil))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) ""))
((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" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sink-1" "Sink One" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () nil))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) ""))
((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-in-use-label ()
"Test that RUNNING devices show [in use] label."
(test-quick-setup-setup)
(unwind-protect
(let ((mic-candidates nil)
(call-count 0))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("mic-1" "Running Mic" "RUNNING" "no"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sink-1" "Sink One" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () nil))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) ""))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(setq call-count (1+ call-count))
(let ((candidates (all-completions "" table)))
(when (= call-count 1)
(setq mic-candidates candidates))
(car candidates)))))
(cj/recording-quick-setup)
(should (cl-some (lambda (c) (string-match-p "\\[in use\\]" c))
mic-candidates))))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-normal-ready-label ()
"Test that IDLE devices show [ready] label."
(test-quick-setup-setup)
(unwind-protect
(let ((mic-candidates nil)
(call-count 0))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("mic-1" "Idle Mic" "IDLE" "no"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sink-1" "Sink One" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () nil))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) ""))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(setq call-count (1+ call-count))
(let ((candidates (all-completions "" table)))
(when (= call-count 1)
(setq mic-candidates candidates))
(car candidates)))))
(cj/recording-quick-setup)
(should (cl-some (lambda (c) (string-match-p "\\[ready\\]" c))
mic-candidates))))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-normal-muted-label ()
"Test that muted devices show [muted] label."
(test-quick-setup-setup)
(unwind-protect
(let ((mic-candidates nil)
(call-count 0))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("mic-1" "Muted Mic" "SUSPENDED" "yes"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sink-1" "Sink One" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () nil))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) ""))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(setq call-count (1+ call-count))
(let ((candidates (all-completions "" table)))
(when (= call-count 1)
(setq mic-candidates candidates))
(car candidates)))))
(cj/recording-quick-setup)
(should (cl-some (lambda (c) (string-match-p "\\[muted\\]" c))
mic-candidates))))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-normal-sorted-by-status ()
"Test that devices are sorted: in use → ready → available → muted."
(test-quick-setup-setup)
(unwind-protect
(let ((mic-candidates nil)
(call-count 0))
(cl-letf (((symbol-function 'cj/recording--get-available-mics)
(lambda () '(("muted-mic" "Muted Mic" "SUSPENDED" "yes")
("running-mic" "Running Mic" "RUNNING" "no")
("suspended-mic" "Suspended Mic" "SUSPENDED" "no")
("idle-mic" "Idle Mic" "IDLE" "no"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sink-1" "Sink One" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () nil))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) ""))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(setq call-count (1+ call-count))
(let ((candidates (all-completions "" table)))
(when (= call-count 1)
(setq mic-candidates candidates))
(car candidates)))))
(cj/recording-quick-setup)
;; in use → ready → available → muted
(should (string-match-p "Running Mic" (nth 0 mic-candidates)))
(should (string-match-p "Idle Mic" (nth 1 mic-candidates)))
(should (string-match-p "Suspended Mic" (nth 2 mic-candidates)))
(should (string-match-p "Muted Mic" (nth 3 mic-candidates)))))
(test-quick-setup-teardown)))
(ert-deftest test-video-audio-recording-quick-setup-normal-sink-shows-apps ()
"Test that sink labels include application names."
(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" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sink-with-apps" "JDS Labs" "RUNNING" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () '(("65" "Firefox" "Spotify"))))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) "65\tsink-with-apps\tPipeWire\n"))
((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)
(should (cl-some (lambda (c) (string-match-p "Firefox" c))
sink-candidates))
(should (cl-some (lambda (c) (string-match-p "Spotify" c))
sink-candidates))))
(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" "RUNNING" "no"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("jds-labs.analog-stereo" "JDS Labs Element IV" "RUNNING" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () nil))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) ""))
((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" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sole-sink" "Only Sink" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () nil))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) ""))
((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" "SUSPENDED" "no"))))
((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" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-available-sinks)
(lambda () '(("sink-1" "Sink One" "SUSPENDED" "no"))))
((symbol-function 'cj/recording--get-sink-apps)
(lambda () nil))
((symbol-function 'shell-command-to-string)
(lambda (_cmd) ""))
((symbol-function 'completing-read)
(lambda (_prompt table &rest _args)
(setq call-count (1+ call-count))
(if (= call-count 1)
(car (all-completions "" table))
"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."
(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
|