summaryrefslogtreecommitdiff
path: root/tests/test-video-audio-recording-quick-setup.el
blob: 8b46b9b02350abca325846ee666631e18e28c095 (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
;;; 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 shows available mics and auto-selects the default
;; sink's monitor for system audio capture.

;;; 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")
                              ("builtin-input" . "Built-in Analog"))))
                ((symbol-function 'cj/recording--get-default-sink-monitor)
                 (lambda () "jds-labs.monitor"))
                ((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-default-monitor ()
  "Test that system device is set to the default 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"))))
                ((symbol-function 'cj/recording--get-default-sink-monitor)
                 (lambda () "alsa_output.usb-JDS_Labs-00.analog-stereo.monitor"))
                ((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-presents-descriptions ()
  "Test that completing-read receives friendly descriptions and Cancel option."
  (test-quick-setup-setup)
  (unwind-protect
      (let ((presented-candidates nil))
        (cl-letf (((symbol-function 'cj/recording--get-available-mics)
                   (lambda () '(("raw-device-1" . "Jabra SPEAK 510 Mono")
                                ("raw-device-2" . "Built-in Analog"))))
                  ((symbol-function 'cj/recording--get-default-sink-monitor)
                   (lambda () "default.monitor"))
                  ((symbol-function 'completing-read)
                   (lambda (_prompt table &rest _args)
                     (setq presented-candidates (all-completions "" table))
                     (car presented-candidates))))
          (cj/recording-quick-setup)
          ;; Candidates should have friendly descriptions
          (should (member "Jabra SPEAK 510 Mono" presented-candidates))
          (should (member "Built-in Analog" presented-candidates))
          ;; Cancel option should be present
          (should (member "Cancel" presented-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"))))
                  ((symbol-function 'cj/recording--get-default-sink-monitor)
                   (lambda () "jds-labs.analog-stereo.monitor"))
                  ((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 "Jabra SPEAK 510 Mono" message-text))
          (should (string-match-p "default output 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 nil))
        (cl-letf (((symbol-function 'cj/recording--get-available-mics)
                   (lambda () '(("sole-mic" . "Only Mic Available"))))
                  ((symbol-function 'cj/recording--get-default-sink-monitor)
                   (lambda () "default.monitor"))
                  ((symbol-function 'completing-read)
                   (lambda (_prompt table &rest _args)
                     (setq read-called t)
                     (car (all-completions "" table)))))
          (cj/recording-quick-setup)
          (should 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-selected ()
  "Test that selecting Cancel 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"))))
                ((symbol-function 'cj/recording--get-default-sink-monitor)
                 (lambda () "default.monitor"))
                ((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-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))
                ((symbol-function 'cj/recording--get-default-sink-monitor)
                 (lambda () "default.monitor")))
        (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 and unmuted."
  (test-quick-setup-setup)
  (unwind-protect
      (cl-letf (((symbol-function 'cj/recording--get-available-mics)
                 (lambda () nil))
                ((symbol-function 'cj/recording--get-default-sink-monitor)
                 (lambda () "default.monitor")))
        (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