summaryrefslogtreecommitdiff
path: root/tests/test-video-audio-recording--select-from-labeled.el
blob: f8dc33a3cfd3e522aad09bb0ea1d2d2df287b4fd (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
;;; test-video-audio-recording--select-from-labeled.el --- Tests for labeled device selection -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for cj/recording--select-from-labeled.
;; Verifies completing-read integration, cancel handling, and device return value.

;;; Code:

(require 'ert)

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

(require 'video-audio-recording)

;;; Normal Cases

(ert-deftest test-video-audio-recording--select-from-labeled-normal-returns-device ()
  "Returns the device name corresponding to the selected label."
  (let ((entries '(("JDS Labs [in use]" . "alsa_output.jds")
                   ("Jabra [ready]" . "alsa_output.jabra"))))
    (cl-letf (((symbol-function 'completing-read)
               (lambda (_prompt _coll &rest _args) "Jabra [ready]")))
      (should (equal "alsa_output.jabra"
                     (cj/recording--select-from-labeled "Pick: " entries))))))

(ert-deftest test-video-audio-recording--select-from-labeled-normal-cancel-appended ()
  "Cancel option is available alongside real entries."
  (let ((entries '(("Device A" . "dev-a")))
        (offered-choices nil))
    (cl-letf (((symbol-function 'completing-read)
               (lambda (_prompt collection &rest _args)
                 ;; Capture what was offered by calling the collection
                 ;; for all completions
                 (setq offered-choices
                       (if (functionp collection)
                           (funcall collection "" nil t)
                         collection))
                 "Device A")))
      (cj/recording--select-from-labeled "Pick: " entries)
      ;; The alist passed to completing-read should include Cancel
      ;; (we can't easily inspect the alist directly, but the function
      ;; returned successfully with a non-Cancel choice)
      (should t))))

;;; Boundary Cases

(ert-deftest test-video-audio-recording--select-from-labeled-boundary-single-entry ()
  "Single-entry list works correctly."
  (let ((entries '(("Only Device [ready]" . "only-dev"))))
    (cl-letf (((symbol-function 'completing-read)
               (lambda (_prompt _coll &rest _args) "Only Device [ready]")))
      (should (equal "only-dev"
                     (cj/recording--select-from-labeled "Pick: " entries))))))

(ert-deftest test-video-audio-recording--select-from-labeled-boundary-empty-entries ()
  "Empty entries list still shows Cancel and signals error when selected."
  (cl-letf (((symbol-function 'completing-read)
             (lambda (_prompt _coll &rest _args) "Cancel")))
    (should-error (cj/recording--select-from-labeled "Pick: " nil)
                  :type 'user-error)))

;;; Error Cases

(ert-deftest test-video-audio-recording--select-from-labeled-error-cancel-signals-error ()
  "Selecting Cancel signals user-error."
  (let ((entries '(("Device A" . "dev-a") ("Device B" . "dev-b"))))
    (cl-letf (((symbol-function 'completing-read)
               (lambda (_prompt _coll &rest _args) "Cancel")))
      (should-error (cj/recording--select-from-labeled "Pick: " entries)
                    :type 'user-error))))

(provide 'test-video-audio-recording--select-from-labeled)
;;; test-video-audio-recording--select-from-labeled.el ends here