blob: 5aa794bb3b3490ef5c8bdcc678f33d044fc138d2 (
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
|
;;; test-video-audio-recording-test-mic.el --- Tests for cj/recording-test-mic -*- lexical-binding: t; -*-
;;; Commentary:
;; Unit tests for cj/recording-test-mic function.
;; Tests microphone testing functionality.
;;; 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-mic-setup ()
"Reset device variables before each test."
(setq cj/recording-mic-device nil))
(defun test-mic-teardown ()
"Clean up device variables after each test."
(setq cj/recording-mic-device nil))
;;; Normal Cases
(ert-deftest test-video-audio-recording-test-mic-normal-creates-temp-wav-file ()
"Test that function creates temp file with .wav extension."
(test-mic-setup)
(unwind-protect
(progn
(setq cj/recording-mic-device "test-mic-device")
(let ((temp-file nil))
;; Mock make-temp-file to capture filename
(cl-letf (((symbol-function 'make-temp-file)
(lambda (prefix _dir-flag suffix)
(setq temp-file (concat prefix "12345" suffix))
temp-file))
((symbol-function 'shell-command)
(lambda (_cmd) 0)))
(cj/recording-test-mic)
(should (string-match-p "\\.wav$" temp-file)))))
(test-mic-teardown)))
(ert-deftest test-video-audio-recording-test-mic-normal-runs-ffmpeg-command ()
"Test that function runs ffmpeg command with configured mic device."
(test-mic-setup)
(unwind-protect
(progn
(setq cj/recording-mic-device "test-mic-device")
(let ((commands nil))
;; Mock shell-command to capture all commands
(cl-letf (((symbol-function 'shell-command)
(lambda (cmd) (push cmd commands) 0)))
(cj/recording-test-mic)
(should (= 2 (length commands)))
;; First command should be ffmpeg (stored last in list due to push)
(let ((ffmpeg-cmd (cadr commands)))
(should (stringp ffmpeg-cmd))
(should (string-match-p "ffmpeg" ffmpeg-cmd))
(should (string-match-p "test-mic-device" ffmpeg-cmd))
(should (string-match-p "-t 5" ffmpeg-cmd))))))
(test-mic-teardown)))
(ert-deftest test-video-audio-recording-test-mic-normal-runs-ffplay-for-playback ()
"Test that function runs ffplay for playback."
(test-mic-setup)
(unwind-protect
(progn
(setq cj/recording-mic-device "test-mic-device")
(let ((commands nil))
;; Capture all shell commands
(cl-letf (((symbol-function 'shell-command)
(lambda (cmd) (push cmd commands) 0)))
(cj/recording-test-mic)
(should (= 2 (length commands)))
;; Second command should be ffplay
(should (string-match-p "ffplay" (car commands)))
(should (string-match-p "-autoexit" (car commands))))))
(test-mic-teardown)))
(ert-deftest test-video-audio-recording-test-mic-normal-displays-messages ()
"Test that function displays appropriate messages to user."
(test-mic-setup)
(unwind-protect
(progn
(setq cj/recording-mic-device "test-mic-device")
(let ((messages nil))
;; Capture messages
(cl-letf (((symbol-function 'message)
(lambda (fmt &rest args) (push (apply #'format fmt args) messages)))
((symbol-function 'shell-command)
(lambda (_cmd) 0)))
(cj/recording-test-mic)
(should (>= (length messages) 3))
;; Check for recording message
(should (cl-some (lambda (msg) (string-match-p "Recording.*SPEAK NOW" msg)) messages))
;; Check for playback message
(should (cl-some (lambda (msg) (string-match-p "Playing back" msg)) messages))
;; Check for complete message
(should (cl-some (lambda (msg) (string-match-p "complete" msg)) messages)))))
(test-mic-teardown)))
;;; Error Cases
(ert-deftest test-video-audio-recording-test-mic-error-no-mic-configured-signals-error ()
"Test that function signals user-error when mic device is not configured."
(test-mic-setup)
(unwind-protect
(progn
(setq cj/recording-mic-device nil)
(should-error (cj/recording-test-mic) :type 'user-error))
(test-mic-teardown)))
(ert-deftest test-video-audio-recording-test-mic-error-message-mentions-setup ()
"Test that error message guides user to run setup."
(test-mic-setup)
(unwind-protect
(progn
(setq cj/recording-mic-device nil)
(condition-case err
(cj/recording-test-mic)
(user-error
(should (string-match-p "C-; r c" (error-message-string err))))))
(test-mic-teardown)))
(ert-deftest test-video-audio-recording-test-mic-error-ffmpeg-failure-handled ()
"Test that ffmpeg command failure is handled gracefully."
(test-mic-setup)
(unwind-protect
(progn
(setq cj/recording-mic-device "test-mic-device")
;; Mock shell-command to fail
(cl-letf (((symbol-function 'shell-command)
(lambda (_cmd) 1))) ;; Non-zero exit code
;; Should complete without crashing (ffmpeg errors are ignored)
;; No error is raised - function just completes
(cj/recording-test-mic)
;; Test passes if we get here
(should t)))
(test-mic-teardown)))
(provide 'test-video-audio-recording-test-mic)
;;; test-video-audio-recording-test-mic.el ends here
|