From 0a69c5854378afcafc567d965f206cf6a0a984be Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Mon, 3 Nov 2025 15:26:11 -0600 Subject: test: Add comprehensive test suite for video-audio-recording module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added 83 test cases across 9 test files with 100% pass rate, covering device detection, parsing, grouping, and complete workflow integration. ## What Was Done ### Refactoring for Testability - Extracted `cj/recording--parse-pactl-output` from `cj/recording-parse-sources` - Separated parsing logic from shell command execution - Enables testing with fixture data instead of live system calls ### Test Fixtures Created - `pactl-output-normal.txt` - All device types (built-in, USB, Bluetooth) - `pactl-output-empty.txt` - Empty output - `pactl-output-single.txt` - Single device - `pactl-output-monitors-only.txt` - Only monitor devices - `pactl-output-inputs-only.txt` - Only input devices - `pactl-output-malformed.txt` - Invalid/malformed output ### Unit Tests (8 files, 78 test cases) 1. **test-video-audio-recording-friendly-state.el** (10 tests) - State name conversion: SUSPENDED→Ready, RUNNING→Active 2. **test-video-audio-recording-parse-pactl-output.el** (14 tests) - Parse raw pactl output into structured data - Handle empty, malformed, and mixed valid/invalid input 3. **test-video-audio-recording-parse-sources.el** (6 tests) - Shell command wrapper testing with mocked output 4. **test-video-audio-recording-detect-mic-device.el** (13 tests) - Documents bugs: Returns ID numbers instead of device names - Doesn't filter monitors (legacy function, not actively used) 5. **test-video-audio-recording-detect-system-device.el** (13 tests) - Works correctly: Returns full device names - Tests monitor detection with various device types 6. **test-video-audio-recording-group-devices-by-hardware.el** (12 tests) - CRITICAL: Bluetooth MAC address normalization (colons vs underscores) - Device pairing logic (mic + monitor from same hardware) - Friendly name assignment - Filters incomplete devices 7. **test-video-audio-recording-check-ffmpeg.el** (3 tests) - ffmpeg availability detection 8. **test-video-audio-recording-get-devices.el** (7 tests) - Auto-detection fallback logic - Error handling for incomplete detection ### Integration Tests (1 file, 5 test cases) 9. **test-integration-recording-device-workflow.el** (5 tests) - Complete workflow: parse → group → friendly names - Bluetooth MAC normalization end-to-end - Incomplete device filtering across components - Malformed data graceful handling ## Key Testing Insights ### Bugs Documented - `cj/recording-detect-mic-device` has bugs (returns IDs, doesn't filter monitors) - These functions appear to be legacy code not used by main workflow - Tests document current behavior to catch regressions if fixed ### Critical Features Validated - **Bluetooth MAC normalization**: Input uses colons (00:1B:66:C0:91:6D), output uses underscores (00_1B_66_C0_91_6D), grouping normalizes correctly - **Device pairing**: Only devices with BOTH mic and monitor are included - **Friendly names**: USB/PCI/Bluetooth patterns correctly identified ### Test Coverage - Normal cases: Valid inputs, typical workflows - Boundary cases: Empty, single device, incomplete pairs - Error cases: Malformed input, missing devices, partial detection ## Test Execution All tests pass: 9/9 files, 83/83 test cases (100% pass rate) ```bash make test-file FILE=test-video-audio-recording-*.el # All pass individually # Integration test also passes make test-file FILE=test-integration-recording-device-workflow.el ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- tests/test-video-audio-recording-get-devices.el | 142 ++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 tests/test-video-audio-recording-get-devices.el (limited to 'tests/test-video-audio-recording-get-devices.el') diff --git a/tests/test-video-audio-recording-get-devices.el b/tests/test-video-audio-recording-get-devices.el new file mode 100644 index 00000000..b1b8470b --- /dev/null +++ b/tests/test-video-audio-recording-get-devices.el @@ -0,0 +1,142 @@ +;;; test-video-audio-recording-get-devices.el --- Tests for cj/recording-get-devices -*- lexical-binding: t; -*- + +;;; Commentary: +;; Unit tests for cj/recording-get-devices function. +;; Tests device auto-detection fallback logic. +;; +;; Note: This function has interactive prompts, but we test the core logic paths +;; without mocking y-or-n-p. We focus on testing: +;; - Already-set devices (no auto-detection needed) +;; - Successful auto-detection +;; - Failed auto-detection → error + +;;; 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-get-devices-setup () + "Reset device variables before each test." + (setq cj/recording-mic-device nil) + (setq cj/recording-system-device nil)) + +(defun test-get-devices-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-get-devices-normal-already-set-returns-devices () + "Test that already-set devices are returned without auto-detection." + (test-get-devices-setup) + (unwind-protect + (progn + (setq cj/recording-mic-device "test-mic") + (setq cj/recording-system-device "test-monitor") + (let ((result (cj/recording-get-devices))) + (should (consp result)) + (should (equal "test-mic" (car result))) + (should (equal "test-monitor" (cdr result))))) + (test-get-devices-teardown))) + +(ert-deftest test-video-audio-recording-get-devices-normal-auto-detect-success () + "Test that auto-detection succeeds and returns devices." + (test-get-devices-setup) + (unwind-protect + (cl-letf (((symbol-function 'cj/recording-detect-mic-device) + (lambda () "auto-detected-mic")) + ((symbol-function 'cj/recording-detect-system-device) + (lambda () "auto-detected-monitor"))) + (let ((result (cj/recording-get-devices))) + (should (consp result)) + (should (equal "auto-detected-mic" (car result))) + (should (equal "auto-detected-monitor" (cdr result))) + ;; Verify variables were set + (should (equal "auto-detected-mic" cj/recording-mic-device)) + (should (equal "auto-detected-monitor" cj/recording-system-device)))) + (test-get-devices-teardown))) + +(ert-deftest test-video-audio-recording-get-devices-normal-partial-auto-detect () + "Test when only one device is already set, only the other is auto-detected." + (test-get-devices-setup) + (unwind-protect + (progn + (setq cj/recording-mic-device "preset-mic") + (cl-letf (((symbol-function 'cj/recording-detect-system-device) + (lambda () "auto-detected-monitor"))) + (let ((result (cj/recording-get-devices))) + (should (consp result)) + (should (equal "preset-mic" (car result))) + (should (equal "auto-detected-monitor" (cdr result)))))) + (test-get-devices-teardown))) + +;;; Error Cases + +(ert-deftest test-video-audio-recording-get-devices-error-auto-detect-fails-signals-error () + "Test that failed auto-detection signals user-error. +When auto-detection fails and user doesn't manually select, function errors." + (test-get-devices-setup) + (unwind-protect + (cl-letf (((symbol-function 'cj/recording-detect-mic-device) + (lambda () nil)) + ((symbol-function 'cj/recording-detect-system-device) + (lambda () nil)) + ;; Mock y-or-n-p to say no to manual selection + ((symbol-function 'y-or-n-p) + (lambda (_prompt) nil))) + (should-error (cj/recording-get-devices) :type 'user-error)) + (test-get-devices-teardown))) + +(ert-deftest test-video-audio-recording-get-devices-error-only-mic-detected-signals-error () + "Test that detecting only mic (no monitor) signals error." + (test-get-devices-setup) + (unwind-protect + (cl-letf (((symbol-function 'cj/recording-detect-mic-device) + (lambda () "detected-mic")) + ((symbol-function 'cj/recording-detect-system-device) + (lambda () nil)) + ((symbol-function 'y-or-n-p) + (lambda (_prompt) nil))) + (should-error (cj/recording-get-devices) :type 'user-error)) + (test-get-devices-teardown))) + +(ert-deftest test-video-audio-recording-get-devices-error-only-monitor-detected-signals-error () + "Test that detecting only monitor (no mic) signals error." + (test-get-devices-setup) + (unwind-protect + (cl-letf (((symbol-function 'cj/recording-detect-mic-device) + (lambda () nil)) + ((symbol-function 'cj/recording-detect-system-device) + (lambda () "detected-monitor")) + ((symbol-function 'y-or-n-p) + (lambda (_prompt) nil))) + (should-error (cj/recording-get-devices) :type 'user-error)) + (test-get-devices-teardown))) + +(ert-deftest test-video-audio-recording-get-devices-error-message-mentions-select-devices () + "Test that error message guides user to manual selection command." + (test-get-devices-setup) + (unwind-protect + (cl-letf (((symbol-function 'cj/recording-detect-mic-device) + (lambda () nil)) + ((symbol-function 'cj/recording-detect-system-device) + (lambda () nil)) + ((symbol-function 'y-or-n-p) + (lambda (_prompt) nil))) + (condition-case err + (cj/recording-get-devices) + (user-error + (should (string-match-p "cj/recording-select-devices" (error-message-string err)))))) + (test-get-devices-teardown))) + +(provide 'test-video-audio-recording-get-devices) +;;; test-video-audio-recording-get-devices.el ends here -- cgit v1.2.3