blob: e82614e296584edb50b0090d6377a91e38b59b26 (
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
;;; test-video-audio-recording-ffmpeg-functions.el --- Tests for ffmpeg recording functions -*- lexical-binding: t; -*-
;;; Commentary:
;; Unit tests for cj/ffmpeg-record-video, cj/ffmpeg-record-audio,
;; cj/video-recording-stop, and cj/audio-recording-stop functions.
;; Tests process creation, sentinel attachment, and cleanup.
;;; Code:
(require 'ert)
;; Stub dependencies before loading the module
(defvar cj/custom-keymap (make-sparse-keymap)
"Stub keymap for testing.")
;; Stub directory variables
(defvar video-recordings-dir "/tmp/video-recordings/")
(defvar audio-recordings-dir "/tmp/audio-recordings/")
;; Now load the actual production module
(require 'video-audio-recording)
;;; Setup and Teardown
(defun test-ffmpeg-setup ()
"Reset all variables before each test."
(setq cj/video-recording-ffmpeg-process nil)
(setq cj/audio-recording-ffmpeg-process nil)
(setq cj/recording-mic-device "test-mic-device")
(setq cj/recording-system-device "test-monitor-device")
(setq cj/recording-mic-boost 2.0)
(setq cj/recording-system-volume 0.5))
(defun test-ffmpeg-teardown ()
"Clean up after each test."
(when cj/video-recording-ffmpeg-process
(ignore-errors (delete-process cj/video-recording-ffmpeg-process)))
(when cj/audio-recording-ffmpeg-process
(ignore-errors (delete-process cj/audio-recording-ffmpeg-process)))
(setq cj/video-recording-ffmpeg-process nil)
(setq cj/audio-recording-ffmpeg-process nil)
(setq cj/recording-mic-device nil)
(setq cj/recording-system-device nil))
;;; Video Recording - Normal Cases
(ert-deftest test-video-audio-recording-ffmpeg-record-video-normal-creates-process ()
"Test that video recording creates a process."
(test-ffmpeg-setup)
(unwind-protect
(let ((process-created nil))
(cl-letf (((symbol-function 'start-process-shell-command)
(lambda (_name _buffer _command)
(setq process-created t)
(make-process :name "fake-video" :command '("sleep" "1000")))))
(cj/ffmpeg-record-video video-recordings-dir)
(should process-created)
(should cj/video-recording-ffmpeg-process)))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-ffmpeg-record-video-normal-attaches-sentinel ()
"Test that video recording attaches sentinel to process."
(test-ffmpeg-setup)
(unwind-protect
(let ((sentinel-attached nil))
(cl-letf (((symbol-function 'start-process-shell-command)
(lambda (_name _buffer _command)
(make-process :name "fake-video" :command '("sleep" "1000"))))
((symbol-function 'set-process-sentinel)
(lambda (_proc sentinel)
(should (eq sentinel #'cj/recording-process-sentinel))
(setq sentinel-attached t))))
(cj/ffmpeg-record-video video-recordings-dir)
(should sentinel-attached)))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-ffmpeg-record-video-normal-updates-modeline ()
"Test that video recording triggers modeline update."
(test-ffmpeg-setup)
(unwind-protect
(let ((update-called nil))
(cl-letf (((symbol-function 'start-process-shell-command)
(lambda (_name _buffer _command)
(make-process :name "fake-video" :command '("sleep" "1000"))))
((symbol-function 'force-mode-line-update)
(lambda (&optional _all) (setq update-called t))))
(cj/ffmpeg-record-video video-recordings-dir)
(should update-called)))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-ffmpeg-record-video-normal-uses-device-settings ()
"Test that video recording uses configured devices and volume settings."
(test-ffmpeg-setup)
(unwind-protect
(let ((command nil))
(cl-letf (((symbol-function 'start-process-shell-command)
(lambda (_name _buffer cmd)
(setq command cmd)
(make-process :name "fake-video" :command '("sleep" "1000")))))
(cj/ffmpeg-record-video video-recordings-dir)
(should (string-match-p "test-mic-device" command))
(should (string-match-p "test-monitor-device" command))
(should (string-match-p "2\\.0" command)) ; mic boost
(should (string-match-p "0\\.5" command)))) ; system volume
(test-ffmpeg-teardown)))
;;; Audio Recording - Normal Cases
(ert-deftest test-video-audio-recording-ffmpeg-record-audio-normal-creates-process ()
"Test that audio recording creates a process."
(test-ffmpeg-setup)
(unwind-protect
(let ((process-created nil))
(cl-letf (((symbol-function 'start-process-shell-command)
(lambda (_name _buffer _command)
(setq process-created t)
(make-process :name "fake-audio" :command '("sleep" "1000")))))
(cj/ffmpeg-record-audio audio-recordings-dir)
(should process-created)
(should cj/audio-recording-ffmpeg-process)))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-ffmpeg-record-audio-normal-attaches-sentinel ()
"Test that audio recording attaches sentinel to process."
(test-ffmpeg-setup)
(unwind-protect
(let ((sentinel-attached nil))
(cl-letf (((symbol-function 'start-process-shell-command)
(lambda (_name _buffer _command)
(make-process :name "fake-audio" :command '("sleep" "1000"))))
((symbol-function 'set-process-sentinel)
(lambda (_proc sentinel)
(should (eq sentinel #'cj/recording-process-sentinel))
(setq sentinel-attached t))))
(cj/ffmpeg-record-audio audio-recordings-dir)
(should sentinel-attached)))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-ffmpeg-record-audio-normal-updates-modeline ()
"Test that audio recording triggers modeline update."
(test-ffmpeg-setup)
(unwind-protect
(let ((update-called nil))
(cl-letf (((symbol-function 'start-process-shell-command)
(lambda (_name _buffer _command)
(make-process :name "fake-audio" :command '("sleep" "1000"))))
((symbol-function 'force-mode-line-update)
(lambda (&optional _all) (setq update-called t))))
(cj/ffmpeg-record-audio audio-recordings-dir)
(should update-called)))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-ffmpeg-record-audio-normal-creates-m4a-file ()
"Test that audio recording creates .m4a file."
(test-ffmpeg-setup)
(unwind-protect
(let ((command nil))
(cl-letf (((symbol-function 'start-process-shell-command)
(lambda (_name _buffer cmd)
(setq command cmd)
(make-process :name "fake-audio" :command '("sleep" "1000")))))
(cj/ffmpeg-record-audio audio-recordings-dir)
(should (string-match-p "\\.m4a" command))))
(test-ffmpeg-teardown)))
;;; Stop Functions - Normal Cases
(ert-deftest test-video-audio-recording-video-stop-normal-interrupts-process ()
"Test that stopping video recording interrupts the process."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-video" :command '("sleep" "1000")))
(interrupt-called nil))
(setq cj/video-recording-ffmpeg-process fake-process)
(cl-letf (((symbol-function 'interrupt-process)
(lambda (_proc) (setq interrupt-called t))))
(cj/video-recording-stop)
(should interrupt-called))
(delete-process fake-process))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-video-stop-normal-clears-variable ()
"Test that stopping video recording clears the process variable."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-video" :command '("sleep" "1000"))))
(setq cj/video-recording-ffmpeg-process fake-process)
(cj/video-recording-stop)
(should (null cj/video-recording-ffmpeg-process))
(delete-process fake-process))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-video-stop-normal-updates-modeline ()
"Test that stopping video recording updates modeline."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-video" :command '("sleep" "1000")))
(update-called nil))
(setq cj/video-recording-ffmpeg-process fake-process)
(cl-letf (((symbol-function 'force-mode-line-update)
(lambda (&optional _all) (setq update-called t))))
(cj/video-recording-stop)
(should update-called))
(delete-process fake-process))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-audio-stop-normal-interrupts-process ()
"Test that stopping audio recording interrupts the process."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-audio" :command '("sleep" "1000")))
(interrupt-called nil))
(setq cj/audio-recording-ffmpeg-process fake-process)
(cl-letf (((symbol-function 'interrupt-process)
(lambda (_proc) (setq interrupt-called t))))
(cj/audio-recording-stop)
(should interrupt-called))
(delete-process fake-process))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-audio-stop-normal-clears-variable ()
"Test that stopping audio recording clears the process variable."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-audio" :command '("sleep" "1000"))))
(setq cj/audio-recording-ffmpeg-process fake-process)
(cj/audio-recording-stop)
(should (null cj/audio-recording-ffmpeg-process))
(delete-process fake-process))
(test-ffmpeg-teardown)))
;;; Boundary Cases
(ert-deftest test-video-audio-recording-video-stop-boundary-no-process-displays-message ()
"Test that stopping when no video recording shows message."
(test-ffmpeg-setup)
(unwind-protect
(let ((message-text nil))
(setq cj/video-recording-ffmpeg-process nil)
(cl-letf (((symbol-function 'message)
(lambda (fmt &rest args) (setq message-text (apply #'format fmt args)))))
(cj/video-recording-stop)
(should (string-match-p "No video recording" message-text))))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-audio-stop-boundary-no-process-displays-message ()
"Test that stopping when no audio recording shows message."
(test-ffmpeg-setup)
(unwind-protect
(let ((message-text nil))
(setq cj/audio-recording-ffmpeg-process nil)
(cl-letf (((symbol-function 'message)
(lambda (fmt &rest args) (setq message-text (apply #'format fmt args)))))
(cj/audio-recording-stop)
(should (string-match-p "No audio recording" message-text))))
(test-ffmpeg-teardown)))
;;; Error Cases
(ert-deftest test-video-audio-recording-video-stop-error-interrupt-process-fails ()
"Test that video stop handles interrupt-process failure gracefully."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-video" :command '("sleep" "1000")))
(error-raised nil))
(setq cj/video-recording-ffmpeg-process fake-process)
(cl-letf (((symbol-function 'interrupt-process)
(lambda (_proc) (error "Interrupt failed"))))
;; Should handle the error without crashing
(condition-case err
(cj/video-recording-stop)
(error (setq error-raised t)))
;; Error should propagate (function doesn't catch it)
(should error-raised))
(delete-process fake-process))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-audio-stop-error-interrupt-process-fails ()
"Test that audio stop handles interrupt-process failure gracefully."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-audio" :command '("sleep" "1000")))
(error-raised nil))
(setq cj/audio-recording-ffmpeg-process fake-process)
(cl-letf (((symbol-function 'interrupt-process)
(lambda (_proc) (error "Interrupt failed"))))
;; Should handle the error without crashing
(condition-case err
(cj/audio-recording-stop)
(error (setq error-raised t)))
;; Error should propagate (function doesn't catch it)
(should error-raised))
(delete-process fake-process))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-video-stop-error-dead-process-raises-error ()
"Test that video stop raises error if process is already dead.
This documents current behavior - interrupt-process on dead process errors.
The sentinel should clear the variable before this happens in practice."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-video" :command '("sleep" "1000"))))
(setq cj/video-recording-ffmpeg-process fake-process)
;; Kill process before calling stop
(delete-process fake-process)
(sit-for 0.1)
;; Calling stop on dead process raises error
(should-error (cj/video-recording-stop)))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-audio-stop-error-dead-process-raises-error ()
"Test that audio stop raises error if process is already dead.
This documents current behavior - interrupt-process on dead process errors.
The sentinel should clear the variable before this happens in practice."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-audio" :command '("sleep" "1000"))))
(setq cj/audio-recording-ffmpeg-process fake-process)
;; Kill process before calling stop
(delete-process fake-process)
(sit-for 0.1)
;; Calling stop on dead process raises error
(should-error (cj/audio-recording-stop)))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-ffmpeg-record-video-boundary-skips-if-already-recording ()
"Test that video recording skips if already in progress."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-video" :command '("sleep" "1000")))
(start-called nil))
(setq cj/video-recording-ffmpeg-process fake-process)
(cl-letf (((symbol-function 'start-process-shell-command)
(lambda (_name _buffer _command)
(setq start-called t)
(make-process :name "fake-video2" :command '("sleep" "1000")))))
(cj/ffmpeg-record-video video-recordings-dir)
;; Should NOT start a new process
(should-not start-called))
(delete-process fake-process))
(test-ffmpeg-teardown)))
(ert-deftest test-video-audio-recording-ffmpeg-record-audio-boundary-skips-if-already-recording ()
"Test that audio recording skips if already in progress."
(test-ffmpeg-setup)
(unwind-protect
(let ((fake-process (make-process :name "test-audio" :command '("sleep" "1000")))
(start-called nil))
(setq cj/audio-recording-ffmpeg-process fake-process)
(cl-letf (((symbol-function 'start-process-shell-command)
(lambda (_name _buffer _command)
(setq start-called t)
(make-process :name "fake-audio2" :command '("sleep" "1000")))))
(cj/ffmpeg-record-audio audio-recordings-dir)
;; Should NOT start a new process
(should-not start-called))
(delete-process fake-process))
(test-ffmpeg-teardown)))
(provide 'test-video-audio-recording-ffmpeg-functions)
;;; test-video-audio-recording-ffmpeg-functions.el ends here
|