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

;;; Commentary:
;; Unit tests for cj/recording-process-sentinel function.
;; Tests process cleanup and modeline update when recording processes exit.

;;; 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-sentinel-setup ()
  "Reset process variables before each test."
  (setq cj/audio-recording-ffmpeg-process nil)
  (setq cj/video-recording-ffmpeg-process nil))

(defun test-sentinel-teardown ()
  "Clean up process variables after each test."
  (setq cj/audio-recording-ffmpeg-process nil)
  (setq cj/video-recording-ffmpeg-process nil))

;;; Normal Cases

(ert-deftest test-video-audio-recording-process-sentinel-normal-audio-exit-clears-variable ()
  "Test that sentinel clears audio process variable when process exits."
  (test-sentinel-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-audio" :command '("sh" "-c" "exit 0"))))
        (setq cj/audio-recording-ffmpeg-process fake-process)
        ;; Mock process-status to return 'exit
        (cl-letf (((symbol-function 'process-status)
                   (lambda (_proc) 'exit)))
          ;; Call sentinel with exit status
          (cj/recording-process-sentinel fake-process "finished\n")
          ;; Variable should be cleared
          (should (null cj/audio-recording-ffmpeg-process))))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-normal-video-exit-clears-variable ()
  "Test that sentinel clears video process variable when process exits."
  (test-sentinel-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-video" :command '("sh" "-c" "exit 0"))))
        (setq cj/video-recording-ffmpeg-process fake-process)
        ;; Mock process-status to return 'exit
        (cl-letf (((symbol-function 'process-status)
                   (lambda (_proc) 'exit)))
          ;; Call sentinel with exit status
          (cj/recording-process-sentinel fake-process "finished\n")
          ;; Variable should be cleared
          (should (null cj/video-recording-ffmpeg-process))))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-normal-signal-status-clears-variable ()
  "Test that sentinel clears variable on signal status (killed)."
  (test-sentinel-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-audio" :command '("sleep" "1000"))))
        (setq cj/audio-recording-ffmpeg-process fake-process)
        (delete-process fake-process)
        ;; Call sentinel with signal status
        (cj/recording-process-sentinel fake-process "killed\n")
        ;; Variable should be cleared
        (should (null cj/audio-recording-ffmpeg-process)))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-normal-modeline-update-called ()
  "Test that sentinel triggers modeline update."
  (test-sentinel-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-audio" :command '("sh" "-c" "exit 0")))
            (update-called nil))
        (setq cj/audio-recording-ffmpeg-process fake-process)
        ;; Mock process-status to return 'exit and force-mode-line-update to track call
        (cl-letf (((symbol-function 'process-status)
                   (lambda (_proc) 'exit))
                  ((symbol-function 'force-mode-line-update)
                   (lambda (&optional _all) (setq update-called t))))
          (cj/recording-process-sentinel fake-process "finished\n")
          (should update-called)))
    (test-sentinel-teardown)))

;;; Boundary Cases

(ert-deftest test-video-audio-recording-process-sentinel-boundary-run-status-ignored ()
  "Test that sentinel ignores processes in 'run status (still running)."
  (test-sentinel-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-audio" :command '("sleep" "1000"))))
        (setq cj/audio-recording-ffmpeg-process fake-process)
        ;; Mock process-status to return 'run
        (cl-letf (((symbol-function 'process-status)
                   (lambda (_proc) 'run)))
          (cj/recording-process-sentinel fake-process "run")
          ;; Variable should NOT be cleared
          (should (eq fake-process cj/audio-recording-ffmpeg-process)))
        (delete-process fake-process))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-boundary-open-status-ignored ()
  "Test that sentinel ignores processes in 'open status."
  (test-sentinel-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-audio" :command '("sleep" "1000"))))
        (setq cj/audio-recording-ffmpeg-process fake-process)
        (cl-letf (((symbol-function 'process-status)
                   (lambda (_proc) 'open)))
          (cj/recording-process-sentinel fake-process "open")
          ;; Variable should NOT be cleared
          (should (eq fake-process cj/audio-recording-ffmpeg-process)))
        (delete-process fake-process))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-boundary-event-trimmed ()
  "Test that event string is trimmed in message."
  (test-sentinel-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-audio" :command '("sh" "-c" "exit 0")))
            (message-text nil))
        (setq cj/audio-recording-ffmpeg-process fake-process)
        ;; Mock process-status to return 'exit and message to capture output
        (cl-letf (((symbol-function 'process-status)
                   (lambda (_proc) 'exit))
                  ((symbol-function 'message)
                   (lambda (fmt &rest args) (setq message-text (apply #'format fmt args)))))
          (cj/recording-process-sentinel fake-process "  finished  \n")
          ;; Message should contain trimmed event
          (should (string-match-p "finished" message-text))
          ;; Should not have extra whitespace
          (should-not (string-match-p "  finished  " message-text))))
    (test-sentinel-teardown)))

;;; Error Cases

(ert-deftest test-video-audio-recording-process-sentinel-error-unknown-process-ignored ()
  "Test that sentinel handles unknown process (not audio or video) gracefully."
  (test-sentinel-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-unknown" :command '("sh" "-c" "exit 0")))
            (audio-proc (make-process :name "test-audio" :command '("sleep" "1000")))
            (video-proc (make-process :name "test-video" :command '("sleep" "1000"))))
        (setq cj/audio-recording-ffmpeg-process audio-proc)
        (setq cj/video-recording-ffmpeg-process video-proc)
        ;; Call sentinel with unknown process
        (cj/recording-process-sentinel fake-process "finished\n")
        ;; Audio and video variables should NOT be cleared
        (should (eq audio-proc cj/audio-recording-ffmpeg-process))
        (should (eq video-proc cj/video-recording-ffmpeg-process))
        (delete-process audio-proc)
        (delete-process video-proc))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-error-nil-event-handled ()
  "Test that sentinel handles nil event string gracefully."
  (test-sentinel-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-audio" :command '("sh" "-c" "exit 0"))))
        (setq cj/audio-recording-ffmpeg-process fake-process)
        ;; Mock process-status to return 'exit
        (cl-letf (((symbol-function 'process-status)
                   (lambda (_proc) 'exit)))
          ;; Should not crash with nil event (string-trim will error, but that's caught)
          ;; The function uses string-trim without protection, so this will error
          ;; Testing that it doesn't crash means we expect an error
          (should-error
            (cj/recording-process-sentinel fake-process nil))))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-error-empty-event-handled ()
  "Test that sentinel handles empty event string gracefully."
  (test-sentinel-setup)
  (unwind-protect
      (let ((fake-process (make-process :name "test-audio" :command '("sh" "-c" "exit 0"))))
        (setq cj/audio-recording-ffmpeg-process fake-process)
        ;; Mock process-status to return 'exit
        (cl-letf (((symbol-function 'process-status)
                   (lambda (_proc) 'exit)))
          ;; Empty string is fine - string-trim handles it
          ;; No error should be raised
          (cj/recording-process-sentinel fake-process "")
          ;; Variable should be cleared
          (should (null cj/audio-recording-ffmpeg-process))))
    (test-sentinel-teardown)))

;;; Failed-Start Stub Deletion
;;
;; On Wayland a wf-recorder that fails to grab the compositor capture
;; still writes a ~500KB, ~0.5s stub .mkv before dying.  The sentinel
;; detects the failed start (exit sooner than
;; `cj/recording-start-fail-threshold' without a user stop); these tests
;; pin that it also deletes the stub file stamped on the process as the
;; `cj-output-file' property — and that normal stops and user stops
;; never delete anything.

(defun test-sentinel--make-exited-process ()
  "Return a real process that has already exited.
Drives the sentinel with a genuinely dead process so `process-status'
and the process plist behave for real instead of through mocks."
  (let ((proc (make-process :name "test-sentinel-exited"
                            :command '("true")
                            :sentinel #'ignore)))
    (while (process-live-p proc)
      (accept-process-output proc 0.05))
    proc))

(defun test-sentinel--make-stub-file ()
  "Create and return a temp file standing in for the stub .mkv."
  (make-temp-file "test-sentinel-stub-" nil ".mkv" "stub-content"))

(ert-deftest test-video-audio-recording-process-sentinel-normal-failed-start-deletes-stub ()
  "Normal: a failed video start deletes the stub output file."
  (test-sentinel-setup)
  (unwind-protect
      (let ((proc (test-sentinel--make-exited-process))
            (stub (test-sentinel--make-stub-file)))
        (unwind-protect
            (progn
              (setq cj/video-recording-ffmpeg-process proc)
              ;; Exited immediately after its start time — a failed start.
              (process-put proc 'cj-start-time (float-time))
              (process-put proc 'cj-output-file stub)
              (cj/recording-process-sentinel proc "exited abnormally\n")
              (should-not (file-exists-p stub)))
          (when (file-exists-p stub) (delete-file stub))))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-normal-failed-start-still-messages ()
  "Normal: the failed-start branch still reports the failure to the user."
  (test-sentinel-setup)
  (unwind-protect
      (let ((proc (test-sentinel--make-exited-process))
            (stub (test-sentinel--make-stub-file))
            (failure-messaged nil))
        (unwind-protect
            (progn
              (setq cj/video-recording-ffmpeg-process proc)
              (process-put proc 'cj-start-time (float-time))
              (process-put proc 'cj-output-file stub)
              (cl-letf (((symbol-function 'message)
                         (lambda (fmt &rest args)
                           (let ((msg (apply #'format fmt args)))
                             (when (string-match-p "failed to start" msg)
                               (setq failure-messaged t))))))
                (cj/recording-process-sentinel proc "exited abnormally\n"))
              (should failure-messaged))
          (when (file-exists-p stub) (delete-file stub))))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-normal-long-run-keeps-file ()
  "Normal: a recording that ran past the threshold keeps its output file."
  (test-sentinel-setup)
  (unwind-protect
      (let ((proc (test-sentinel--make-exited-process))
            (stub (test-sentinel--make-stub-file)))
        (unwind-protect
            (progn
              (setq cj/video-recording-ffmpeg-process proc)
              ;; Ran well past the fail threshold — a real recording.
              (process-put proc 'cj-start-time
                           (- (float-time)
                              (* 10 cj/recording-start-fail-threshold)))
              (process-put proc 'cj-output-file stub)
              (cj/recording-process-sentinel proc "finished\n")
              (should (file-exists-p stub)))
          (when (file-exists-p stub) (delete-file stub))))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-boundary-user-stop-keeps-file ()
  "Boundary: a quick user stop (cj-stopping) never deletes the file."
  (test-sentinel-setup)
  (unwind-protect
      (let ((proc (test-sentinel--make-exited-process))
            (stub (test-sentinel--make-stub-file)))
        (unwind-protect
            (progn
              (setq cj/video-recording-ffmpeg-process proc)
              ;; Quick exit, but the user asked for it.
              (process-put proc 'cj-start-time (float-time))
              (process-put proc 'cj-stopping t)
              (process-put proc 'cj-output-file stub)
              (cj/recording-process-sentinel proc "finished\n")
              (should (file-exists-p stub)))
          (when (file-exists-p stub) (delete-file stub))))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-boundary-missing-stub-no-error ()
  "Boundary: failed start whose stub never hit disk signals no error."
  (test-sentinel-setup)
  (unwind-protect
      (let ((proc (test-sentinel--make-exited-process)))
        (setq cj/video-recording-ffmpeg-process proc)
        (process-put proc 'cj-start-time (float-time))
        (process-put proc 'cj-output-file "/nonexistent/dir/never-written.mkv")
        ;; Must not signal even though the file is absent.
        (cj/recording-process-sentinel proc "exited abnormally\n")
        (should (null cj/video-recording-ffmpeg-process)))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-error-nil-output-property-no-error ()
  "Error: failed start with no cj-output-file property signals no error.
Covers processes started before the property existed (a live daemon
mid-upgrade) — the sentinel degrades to the old message-only path."
  (test-sentinel-setup)
  (unwind-protect
      (let ((proc (test-sentinel--make-exited-process)))
        (setq cj/video-recording-ffmpeg-process proc)
        (process-put proc 'cj-start-time (float-time))
        ;; No cj-output-file property at all.
        (cj/recording-process-sentinel proc "exited abnormally\n")
        (should (null cj/video-recording-ffmpeg-process)))
    (test-sentinel-teardown)))

(ert-deftest test-video-audio-recording-process-sentinel-normal-start-stamps-output-file ()
  "Normal: `cj/ffmpeg-record-video' stamps cj-output-file on the process."
  (test-sentinel-setup)
  (unwind-protect
      (let ((cj/recording-mic-device "test-mic-device")
            (cj/recording-system-device "test-monitor-device")
            (cj/recording-mic-boost 2.0)
            (cj/recording-system-volume 1.0))
        (cl-letf (((symbol-function 'cj/recording--wayland-p) (lambda () nil))
                  ((symbol-function 'cj/recording--validate-system-audio)
                   (lambda () nil))
                  ((symbol-function 'start-process-shell-command)
                   (lambda (_name _buffer _command)
                     (make-process :name "fake-video" :command '("sleep" "1000")))))
          (cj/ffmpeg-record-video "/tmp/video-recordings/")
          (let ((output-file (process-get cj/video-recording-ffmpeg-process
                                          'cj-output-file)))
            (should (stringp output-file))
            (should (string-suffix-p ".mkv" output-file))
            (should (string-prefix-p "/tmp/video-recordings/" output-file))))
        (when cj/video-recording-ffmpeg-process
          (ignore-errors (delete-process cj/video-recording-ffmpeg-process))))
    (test-sentinel-teardown)))

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