summaryrefslogtreecommitdiff
path: root/tests/test-video-audio-recording-process-cleanup.el
blob: d1cd442c5abb68e1a58f6bad08f5514bf15ffd83 (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
;;; test-video-audio-recording-process-cleanup.el --- Tests for recording process cleanup -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests that verify proper cleanup of recording processes.
;;
;; Key behaviors tested:
;; - SIGINT is sent to the process group (negative PID), not just the shell
;; - Video stop waits for the process to actually exit before declaring done
;; - On Wayland, wf-recorder is killed AFTER ffmpeg exits (safety net)
;; - On X11, pkill is not called (no wf-recorder involved)
;; - Starting a recording cleans up orphan wf-recorder from previous crashes

;;; Code:

(require 'ert)

;; Stub dependencies before loading the module
(defvar cj/custom-keymap (make-sparse-keymap)
  "Stub keymap for testing.")

(defvar video-recordings-dir "/tmp/video-recordings/")

;; Now load the actual production module
(require 'video-audio-recording)

;;; Setup and Teardown

(defun test-cleanup-setup ()
  "Reset all variables before each test."
  (setq cj/video-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 1.0))

(defun test-cleanup-teardown ()
  "Clean up after each test."
  (when cj/video-recording-ffmpeg-process
    (ignore-errors (delete-process cj/video-recording-ffmpeg-process)))
  (setq cj/video-recording-ffmpeg-process nil)
  (setq cj/recording-mic-device nil)
  (setq cj/recording-system-device nil))

;;; Unit Tests - Signal delivery

(ert-deftest test-video-recording-stop-sends-sigint-to-process-group ()
  "Test that stopping sends SIGINT to the process group via negative PID."
  (test-cleanup-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-video" :command '("sleep" "1000")))
            (signaled-pid nil)
            (signaled-sig nil))
        (setq cj/video-recording-ffmpeg-process fake-process)
        (cl-letf (((symbol-function 'cj/recording--wayland-p) (lambda () nil))
                  ((symbol-function 'signal-process)
                   (lambda (pid sig)
                     (setq signaled-pid pid)
                     (setq signaled-sig sig)
                     0))
                  ((symbol-function 'cj/recording--wait-for-exit)
                   (lambda (_proc _timeout) t)))
          (cj/video-recording-stop)
          ;; Should send signal 2 (SIGINT) to negative PID (process group)
          (should signaled-pid)
          (should (< signaled-pid 0))
          (should (= signaled-sig 2)))
        (ignore-errors (delete-process fake-process)))
    (test-cleanup-teardown)))

;;; Unit Tests - Wayland wf-recorder cleanup

(ert-deftest test-video-recording-stop-wayland-kills-wf-recorder-before-signal ()
  "Test that on Wayland, pkill wf-recorder is called BEFORE signal-process.
Orderly pipeline shutdown requires killing the producer (wf-recorder) first
so ffmpeg sees EOF on its video input pipe and starts finalizing the file."
  (test-cleanup-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-video" :command '("sleep" "1000")))
            (call-order nil))
        (setq cj/video-recording-ffmpeg-process fake-process)
        (cl-letf (((symbol-function 'cj/recording--wayland-p) (lambda () t))
                  ((symbol-function 'call-process)
                   (lambda (program &rest args)
                     (when (equal program "pkill")
                       (push (cons 'pkill args) call-order))
                     0))
                  ((symbol-function 'signal-process)
                   (lambda (_pid _sig)
                     (push 'signal call-order)
                     0))
                  ((symbol-function 'cj/recording--wait-for-exit)
                   (lambda (_proc _timeout) t)))
          (cj/video-recording-stop)
          ;; call-order is reversed (push adds to front), so last element is first call
          (let ((order (reverse call-order)))
            ;; First call should be pkill (kill producer first)
            (should (consp (car order)))
            (should (eq 'pkill (caar order)))
            ;; signal-process should come after pkill
            (should (eq 'signal (cadr order)))))
        (ignore-errors (delete-process fake-process)))
    (test-cleanup-teardown)))

(ert-deftest test-video-recording-stop-wayland-calls-pkill-with-correct-args ()
  "Test that Wayland stop calls pkill -INT wf-recorder."
  (test-cleanup-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-video" :command '("sleep" "1000")))
            (pkill-args-list nil))
        (setq cj/video-recording-ffmpeg-process fake-process)
        (cl-letf (((symbol-function 'cj/recording--wayland-p) (lambda () t))
                  ((symbol-function 'call-process)
                   (lambda (program &rest args)
                     (when (equal program "pkill")
                       (push args pkill-args-list))
                     0))
                  ((symbol-function 'signal-process) (lambda (_pid _sig) 0))
                  ((symbol-function 'cj/recording--wait-for-exit)
                   (lambda (_proc _timeout) t)))
          (cj/video-recording-stop)
          ;; Should call pkill at least once with -INT wf-recorder
          (should pkill-args-list)
          (should (cl-some (lambda (args)
                             (and (member "-INT" args)
                                  (member "wf-recorder" args)))
                           pkill-args-list)))
        (ignore-errors (delete-process fake-process)))
    (test-cleanup-teardown)))

(ert-deftest test-video-recording-stop-x11-does-not-call-pkill ()
  "Test that stopping video on X11 does NOT call pkill wf-recorder."
  (test-cleanup-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-video" :command '("sleep" "1000")))
            (pkill-called nil))
        (setq cj/video-recording-ffmpeg-process fake-process)
        (cl-letf (((symbol-function 'cj/recording--wayland-p) (lambda () nil))
                  ((symbol-function 'call-process)
                   (lambda (program &rest _args)
                     (when (equal program "pkill")
                       (setq pkill-called t))
                     0))
                  ((symbol-function 'signal-process) (lambda (_pid _sig) 0))
                  ((symbol-function 'cj/recording--wait-for-exit)
                   (lambda (_proc _timeout) t)))
          (cj/video-recording-stop)
          (should-not pkill-called))
        (ignore-errors (delete-process fake-process)))
    (test-cleanup-teardown)))

;;; Unit Tests - Start cleans orphans

(ert-deftest test-video-recording-start-wayland-kills-orphans ()
  "Test that starting video on Wayland kills orphan wf-recorder processes."
  (test-cleanup-setup)
  (unwind-protect
      (let ((pkill-called nil)
            (pkill-args nil))
        (cl-letf (((symbol-function 'cj/recording--wayland-p) (lambda () t))
                  ((symbol-function 'cj/recording--check-wf-recorder) (lambda () t))
                  ((symbol-function 'call-process)
                   (lambda (program &rest args)
                     (when (equal program "pkill")
                       (setq pkill-called t)
                       (setq pkill-args args))
                     0))
                  ((symbol-function 'start-process-shell-command)
                   (lambda (_name _buffer _command)
                     (make-process :name "fake-video" :command '("sleep" "1000")))))
          (cj/ffmpeg-record-video video-recordings-dir)
          (should pkill-called)
          (should (member "-INT" pkill-args))
          (should (member "wf-recorder" pkill-args))))
    (test-cleanup-teardown)))

(ert-deftest test-video-recording-start-x11-does-not-kill-orphans ()
  "Test that starting video on X11 does NOT call pkill."
  (test-cleanup-setup)
  (unwind-protect
      (let ((pkill-called nil))
        (cl-letf (((symbol-function 'cj/recording--wayland-p) (lambda () nil))
                  ((symbol-function 'call-process)
                   (lambda (program &rest _args)
                     (when (equal program "pkill")
                       (setq pkill-called t))
                     0))
                  ((symbol-function 'start-process-shell-command)
                   (lambda (_name _buffer _command)
                     (make-process :name "fake-video" :command '("sleep" "1000")))))
          (cj/ffmpeg-record-video video-recordings-dir)
          (should-not pkill-called)))
    (test-cleanup-teardown)))

;;; Unit Tests - Wait for exit

(ert-deftest test-video-recording-stop-waits-for-process-exit ()
  "Test that stop waits for the process to exit instead of blind sit-for."
  (test-cleanup-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-video" :command '("sleep" "1000")))
            (wait-called nil)
            (wait-timeout nil))
        (setq cj/video-recording-ffmpeg-process fake-process)
        (cl-letf (((symbol-function 'cj/recording--wayland-p) (lambda () nil))
                  ((symbol-function 'signal-process) (lambda (_pid _sig) 0))
                  ((symbol-function 'cj/recording--wait-for-exit)
                   (lambda (_proc timeout)
                     (setq wait-called t)
                     (setq wait-timeout timeout)
                     t)))
          (cj/video-recording-stop)
          (should wait-called)
          ;; Video uses 5 second timeout for MKV finalization
          (should (= wait-timeout 5)))
        (ignore-errors (delete-process fake-process)))
    (test-cleanup-teardown)))

(ert-deftest test-video-recording-stop-timeout-shows-warning ()
  "Test that video stop shows warning when process doesn't exit in time."
  (test-cleanup-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-video" :command '("sleep" "1000")))
            (warning-shown nil))
        (setq cj/video-recording-ffmpeg-process fake-process)
        (cl-letf (((symbol-function 'cj/recording--wayland-p) (lambda () nil))
                  ((symbol-function 'signal-process) (lambda (_pid _sig) 0))
                  ((symbol-function 'cj/recording--wait-for-exit)
                   (lambda (_proc _timeout) nil))  ; Simulate timeout
                  ((symbol-function 'message)
                   (lambda (fmt &rest args)
                     (let ((msg (apply #'format fmt args)))
                       (when (string-match-p "did not exit" msg)
                         (setq warning-shown t))))))
          (cj/video-recording-stop)
          (should warning-shown))
        (ignore-errors (delete-process fake-process)))
    (test-cleanup-teardown)))

(ert-deftest test-audio-recording-stop-timeout-shows-warning ()
  "Test that audio stop shows warning when process doesn't exit in time."
  (test-cleanup-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-audio" :command '("sleep" "1000")))
            (warning-shown nil))
        (setq cj/audio-recording-ffmpeg-process fake-process)
        (cl-letf (((symbol-function 'signal-process) (lambda (_pid _sig) 0))
                  ((symbol-function 'cj/recording--wait-for-exit)
                   (lambda (_proc _timeout) nil))  ; Simulate timeout
                  ((symbol-function 'message)
                   (lambda (fmt &rest args)
                     (let ((msg (apply #'format fmt args)))
                       (when (string-match-p "did not exit" msg)
                         (setq warning-shown t))))))
          (cj/audio-recording-stop)
          (should warning-shown))
        (ignore-errors (delete-process fake-process)))
    (test-cleanup-teardown)))

(ert-deftest test-audio-recording-stop-waits-for-process-exit ()
  "Test that audio stop waits for the process to exit."
  (test-cleanup-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-audio" :command '("sleep" "1000")))
            (wait-called nil)
            (wait-timeout nil))
        (setq cj/audio-recording-ffmpeg-process fake-process)
        (cl-letf (((symbol-function 'signal-process) (lambda (_pid _sig) 0))
                  ((symbol-function 'cj/recording--wait-for-exit)
                   (lambda (_proc timeout)
                     (setq wait-called t)
                     (setq wait-timeout timeout)
                     t)))
          (cj/audio-recording-stop)
          (should wait-called)
          ;; Audio uses 3 second timeout for M4A finalization
          (should (= wait-timeout 3)))
        (ignore-errors (delete-process fake-process)))
    (test-cleanup-teardown)))

;;; Integration Tests - Verify actual process cleanup
;;; These tests require wf-recorder to be installed and a Wayland session

(defun test-cleanup--count-wf-recorder-processes ()
  "Count running wf-recorder processes."
  (let ((output (shell-command-to-string "pgrep -c wf-recorder 2>/dev/null || echo 0")))
    (string-to-number (string-trim output))))

(defun test-cleanup--get-wf-recorder-pids ()
  "Get list of wf-recorder PIDs."
  (let ((output (shell-command-to-string "pgrep wf-recorder 2>/dev/null")))
    (when (and output (not (string-empty-p (string-trim output))))
      (mapcar #'string-to-number (split-string (string-trim output) "\n")))))

(ert-deftest test-integration-video-recording-no-orphan-wf-recorder-after-stop ()
  "Test that no wf-recorder processes remain after stopping recording.
This is an integration test that requires wf-recorder and Wayland."
  :tags '(:integration :wayland)
  (skip-unless (executable-find "wf-recorder"))
  (skip-unless (cj/recording--wayland-p))
  (test-cleanup-setup)
  (unwind-protect
      (let ((initial-count (test-cleanup--count-wf-recorder-processes)))
        (cj/ffmpeg-record-video video-recordings-dir)
        (sit-for 1.0)
        (should (> (test-cleanup--count-wf-recorder-processes) initial-count))
        (cj/video-recording-stop)
        (sit-for 1.0)
        (should (= (test-cleanup--count-wf-recorder-processes) initial-count)))
    (test-cleanup-teardown)
    (ignore-errors (call-process "pkill" nil nil nil "-INT" "wf-recorder"))))

(ert-deftest test-integration-video-recording-start-cleans-orphans ()
  "Test that starting a recording cleans up any orphan wf-recorder processes.
This is an integration test that requires wf-recorder and Wayland."
  :tags '(:integration :wayland)
  (skip-unless (executable-find "wf-recorder"))
  (skip-unless (cj/recording--wayland-p))
  (test-cleanup-setup)
  (unwind-protect
      (progn
        ;; Create an orphan wf-recorder (simulating a crash)
        (start-process "orphan-wf" nil "wf-recorder" "-c" "libx264" "-m" "matroska" "-f" "/dev/null")
        (sit-for 0.5)
        (let ((orphan-pids (test-cleanup--get-wf-recorder-pids)))
          (should orphan-pids)
          (cj/ffmpeg-record-video video-recordings-dir)
          (sit-for 1.0)
          (let ((current-pids (test-cleanup--get-wf-recorder-pids)))
            (dolist (orphan-pid orphan-pids)
              (let ((still-running (member orphan-pid current-pids)))
                (should-not still-running))))
          (cj/video-recording-stop)
          (sit-for 0.5)))
    (test-cleanup-teardown)
    (ignore-errors (call-process "pkill" nil nil nil "-INT" "wf-recorder"))))

(ert-deftest test-integration-video-recording-multiple-start-stop-cycles ()
  "Test that multiple recording cycles don't accumulate orphan processes.
This is an integration test that requires wf-recorder and Wayland."
  :tags '(:integration :wayland)
  (skip-unless (executable-find "wf-recorder"))
  (skip-unless (cj/recording--wayland-p))
  (test-cleanup-setup)
  (unwind-protect
      (let ((initial-count (test-cleanup--count-wf-recorder-processes)))
        (dotimes (_ 3)
          (cj/ffmpeg-record-video video-recordings-dir)
          (sit-for 0.5)
          (cj/video-recording-stop)
          (sit-for 0.5))
        (should (= (test-cleanup--count-wf-recorder-processes) initial-count)))
    (test-cleanup-teardown)
    (ignore-errors (call-process "pkill" nil nil nil "-INT" "wf-recorder"))))

(provide 'test-video-audio-recording-process-cleanup)
;;; test-video-audio-recording-process-cleanup.el ends here