summaryrefslogtreecommitdiff
path: root/tests/test-video-audio-recording-quick-setup-for-calls.el
blob: 0d3fe53ab5eb780dd1b080c804bc37eef1f0379a (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
;;; test-video-audio-recording-quick-setup-for-calls.el --- Tests for cj/recording-quick-setup-for-calls -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for cj/recording-quick-setup-for-calls function.
;; Tests quick device setup workflow for call recording.

;;; 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-for-calls-normal-sets-both-devices ()
  "Test that function sets both mic and system device variables."
  (test-quick-setup-setup)
  (unwind-protect
      (let ((grouped-devices '(("Bluetooth Headset" . ("bluez_input.00:1B:66" . "bluez_output.00_1B_66.monitor")))))
        (cl-letf (((symbol-function 'cj/recording-group-devices-by-hardware)
                   (lambda () grouped-devices))
                  ((symbol-function 'completing-read)
                   (lambda (_prompt _choices &rest _args) "Bluetooth Headset")))
          (cj/recording-quick-setup-for-calls)
          (should (equal "bluez_input.00:1B:66" cj/recording-mic-device))
          (should (equal "bluez_output.00_1B_66.monitor" cj/recording-system-device))))
    (test-quick-setup-teardown)))

(ert-deftest test-video-audio-recording-quick-setup-for-calls-normal-presents-friendly-names ()
  "Test that function presents friendly device names to user."
  (test-quick-setup-setup)
  (unwind-protect
      (let ((grouped-devices '(("Jabra SPEAK 510 USB" . ("usb-input" . "usb-monitor"))
                              ("Built-in Laptop Audio" . ("pci-input" . "pci-monitor"))))
            (presented-choices nil))
        (cl-letf (((symbol-function 'cj/recording-group-devices-by-hardware)
                   (lambda () grouped-devices))
                  ((symbol-function 'completing-read)
                   (lambda (_prompt choices &rest _args)
                     (setq presented-choices choices)
                     (car choices))))
          (cj/recording-quick-setup-for-calls)
          (should (member "Jabra SPEAK 510 USB" presented-choices))
          (should (member "Built-in Laptop Audio" presented-choices))))
    (test-quick-setup-teardown)))

(ert-deftest test-video-audio-recording-quick-setup-for-calls-normal-displays-confirmation ()
  "Test that function displays confirmation message with device details."
  (test-quick-setup-setup)
  (unwind-protect
      (let ((grouped-devices '(("Bluetooth Headset" . ("bluez_input.00:1B:66" . "bluez_output.00_1B_66.monitor"))))
            (message-text nil))
        (cl-letf (((symbol-function 'cj/recording-group-devices-by-hardware)
                   (lambda () grouped-devices))
                  ((symbol-function 'completing-read)
                   (lambda (_prompt _choices &rest _args) "Bluetooth Headset"))
                  ((symbol-function 'message)
                   (lambda (fmt &rest args) (setq message-text (apply #'format fmt args)))))
          (cj/recording-quick-setup-for-calls)
          (should (string-match-p "Call recording ready" message-text))
          (should (string-match-p "Bluetooth Headset" message-text))))
    (test-quick-setup-teardown)))

;;; Boundary Cases

(ert-deftest test-video-audio-recording-quick-setup-for-calls-boundary-single-device-no-prompt ()
  "Test that with single device, selection still happens."
  (test-quick-setup-setup)
  (unwind-protect
      (let ((grouped-devices '(("Built-in Laptop Audio" . ("pci-input" . "pci-monitor")))))
        (cl-letf (((symbol-function 'cj/recording-group-devices-by-hardware)
                   (lambda () grouped-devices))
                  ((symbol-function 'completing-read)
                   (lambda (_prompt _choices &rest _args) "Built-in Laptop Audio")))
          (cj/recording-quick-setup-for-calls)
          (should (equal "pci-input" cj/recording-mic-device))
          (should (equal "pci-monitor" cj/recording-system-device))))
    (test-quick-setup-teardown)))

(ert-deftest test-video-audio-recording-quick-setup-for-calls-boundary-device-name-with-special-chars ()
  "Test that device names with special characters are handled correctly."
  (test-quick-setup-setup)
  (unwind-protect
      (let ((grouped-devices '(("Device (USB-C)" . ("special-input" . "special-monitor")))))
        (cl-letf (((symbol-function 'cj/recording-group-devices-by-hardware)
                   (lambda () grouped-devices))
                  ((symbol-function 'completing-read)
                   (lambda (_prompt _choices &rest _args) "Device (USB-C)")))
          (cj/recording-quick-setup-for-calls)
          (should (equal "special-input" cj/recording-mic-device))
          (should (equal "special-monitor" cj/recording-system-device))))
    (test-quick-setup-teardown)))

;;; Error Cases

(ert-deftest test-video-audio-recording-quick-setup-for-calls-error-no-devices-signals-error ()
  "Test that function signals user-error when no complete devices are found."
  (test-quick-setup-setup)
  (unwind-protect
      (cl-letf (((symbol-function 'cj/recording-group-devices-by-hardware)
                 (lambda () nil)))
        (should-error (cj/recording-quick-setup-for-calls) :type 'user-error))
    (test-quick-setup-teardown)))

(ert-deftest test-video-audio-recording-quick-setup-for-calls-error-message-mentions-both-devices ()
  "Test that error message mentions need for both mic and monitor."
  (test-quick-setup-setup)
  (unwind-protect
      (cl-letf (((symbol-function 'cj/recording-group-devices-by-hardware)
                 (lambda () nil)))
        (condition-case err
            (cj/recording-quick-setup-for-calls)
          (user-error
           (should (string-match-p "both mic and monitor" (error-message-string err))))))
    (test-quick-setup-teardown)))

(ert-deftest test-video-audio-recording-quick-setup-for-calls-error-empty-device-list ()
  "Test that empty device list from grouping is handled gracefully."
  (test-quick-setup-setup)
  (unwind-protect
      (cl-letf (((symbol-function 'cj/recording-group-devices-by-hardware)
                 (lambda () '())))
        (should-error (cj/recording-quick-setup-for-calls) :type 'user-error))
    (test-quick-setup-teardown)))

(provide 'test-video-audio-recording-quick-setup-for-calls)
;;; test-video-audio-recording-quick-setup-for-calls.el ends here