diff options
| author | Craig Jennings <c@cjennings.net> | 2025-11-03 15:26:11 -0600 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2025-11-03 15:26:11 -0600 |
| commit | 0a69c5854378afcafc567d965f206cf6a0a984be (patch) | |
| tree | 923425a2aaf106c51cf5a3100cdf42f98e534da8 /tests/test-video-audio-recording-detect-mic-device.el | |
| parent | 9f8aec55033d46ca4f1cd78fbd315444a0c00bc6 (diff) | |
test: Add comprehensive test suite for video-audio-recording module
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 <noreply@anthropic.com>
Diffstat (limited to 'tests/test-video-audio-recording-detect-mic-device.el')
| -rw-r--r-- | tests/test-video-audio-recording-detect-mic-device.el | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/tests/test-video-audio-recording-detect-mic-device.el b/tests/test-video-audio-recording-detect-mic-device.el new file mode 100644 index 00000000..e95889e3 --- /dev/null +++ b/tests/test-video-audio-recording-detect-mic-device.el @@ -0,0 +1,152 @@ +;;; test-video-audio-recording-detect-mic-device.el --- Tests for cj/recording-detect-mic-device -*- lexical-binding: t; -*- + +;;; Commentary: +;; Unit tests for cj/recording-detect-mic-device function. +;; Tests auto-detection of microphone input device from pactl output. +;; Mocks shell-command-to-string to test regex matching logic. +;; +;; IMPORTANT: These tests document actual behavior, which appears to have a bug. +;; The function currently returns the pactl ID number (e.g., "50") instead of +;; the device name (e.g., "alsa_input.pci-0000_00_1f.3.analog-stereo"). +;; This is because the regex captures group 1 is \\([^\t\n]+\\) which stops +;; at the first tab, capturing only the ID. +;; +;; This function may not be actively used (parse-sources is preferred). +;; Tests document current behavior to catch regressions if function is fixed. + +;;; 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) + +;;; Normal Cases + +(ert-deftest test-video-audio-recording-detect-mic-device-normal-built-in-analog-stereo-found () + "Test detection of built-in analog stereo microphone. +Note: Returns first match which is the monitor (ID 49), not the input." + (let ((output "49\talsa_output.pci-0000_00_1f.3.analog-stereo.monitor\tPipeWire\ts32le 2ch 48000Hz\tSUSPENDED\n50\talsa_input.pci-0000_00_1f.3.analog-stereo\tPipeWire\ts32le 2ch 48000Hz\tSUSPENDED\n")) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + (should (stringp result)) + ;; BUG: Returns first match "49" (monitor), not input "50" + (should (equal "49" result)))))) + +(ert-deftest test-video-audio-recording-detect-mic-device-normal-usb-analog-stereo-found () + "Test detection of USB analog stereo microphone. +Note: Returns ID '100', not device name." + (let ((output "100\talsa_input.usb-0b0e_Jabra_SPEAK_510_USB_1C48F9C067D5020A00-00.analog-stereo\tPipeWire\ts16le 2ch 16000Hz\tSUSPENDED\n")) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + (should (stringp result)) + ;; Current behavior: returns ID "100" + (should (equal "100" result)))))) + +(ert-deftest test-video-audio-recording-detect-mic-device-normal-first-match-returned () + "Test that first matching device is returned when multiple exist. +Note: Returns first ID, not device name." + (let ((output (concat "50\talsa_input.pci-0000_00_1f.3.analog-stereo\tPipeWire\ts32le 2ch 48000Hz\tSUSPENDED\n" + "100\talsa_input.usb-device.analog-stereo\tPipeWire\ts16le 2ch 16000Hz\tSUSPENDED\n"))) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + ;; Current behavior: returns first ID "50" + (should (equal "50" result)))))) + +;;; Boundary Cases + +(ert-deftest test-video-audio-recording-detect-mic-device-boundary-empty-output-returns-nil () + "Test that empty output returns nil." + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) ""))) + (let ((result (cj/recording-detect-mic-device))) + (should (null result))))) + +(ert-deftest test-video-audio-recording-detect-mic-device-boundary-only-monitors-returns-nil () + "Test that output with only monitor devices still matches (documents bug). +Current regex doesn't exclude monitors, so this returns ID '49'." + (let ((output "49\talsa_output.pci-0000_00_1f.3.analog-stereo.monitor\tPipeWire\ts32le 2ch 48000Hz\tSUSPENDED\n")) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + ;; BUG: Should return nil for monitors, but regex doesn't exclude them + (should (equal "49" result)))))) + +(ert-deftest test-video-audio-recording-detect-mic-device-boundary-mono-fallback-no-match () + "Test that mono-fallback device doesn't match (not stereo)." + (let ((output "100\talsa_input.usb-0b0e_Jabra_SPEAK_510_USB_1C48F9C067D5020A00-00.mono-fallback\tPipeWire\ts16le 1ch 16000Hz\tSUSPENDED\n")) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + (should (null result)))))) + +(ert-deftest test-video-audio-recording-detect-mic-device-boundary-bluetooth-no-match () + "Test that Bluetooth devices without 'analog stereo' don't match." + (let ((output "79\tbluez_input.00:1B:66:C0:91:6D\tPipeWire\tfloat32le 1ch 48000Hz\tSUSPENDED\n")) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + (should (null result)))))) + +(ert-deftest test-video-audio-recording-detect-mic-device-boundary-whitespace-only-returns-nil () + "Test that whitespace-only output returns nil." + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) " \n\t\n "))) + (let ((result (cj/recording-detect-mic-device))) + (should (null result))))) + +(ert-deftest test-video-audio-recording-detect-mic-device-boundary-case-insensitive-analog () + "Test that 'ANALOG' (uppercase) matches (case-insensitive regex). +Documents that regex is actually case-insensitive." + (let ((output "50\talsa_input.pci-0000_00_1f.3.ANALOG-STEREO\tPipeWire\ts32le 2ch 48000Hz\tSUSPENDED\n")) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + ;; Regex is case-insensitive, matches uppercase + (should (equal "50" result)))))) + +;;; Error Cases + +(ert-deftest test-video-audio-recording-detect-mic-device-error-malformed-output-returns-nil () + "Test that malformed output returns nil." + (let ((output "This is not valid pactl output\nRandom text here\n")) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + (should (null result)))))) + +(ert-deftest test-video-audio-recording-detect-mic-device-error-partial-match-analog-only () + "Test that 'analog' without 'stereo' doesn't match." + (let ((output "50\talsa_input.pci-0000_00_1f.3.analog-mono\tPipeWire\ts32le 1ch 48000Hz\tSUSPENDED\n")) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + (should (null result)))))) + +(ert-deftest test-video-audio-recording-detect-mic-device-error-partial-match-stereo-only () + "Test that 'stereo' without 'analog' doesn't match." + (let ((output "50\talsa_input.pci-0000_00_1f.3.digital-stereo\tPipeWire\ts32le 2ch 48000Hz\tSUSPENDED\n")) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + (should (null result)))))) + +(ert-deftest test-video-audio-recording-detect-mic-device-error-monitor-with-analog-stereo-matches-bug () + "Test that monitor device with 'analog stereo' incorrectly matches (documents bug). +Should return nil for monitors, but current regex doesn't filter them." + (let ((output "49\talsa_output.pci-0000_00_1f.3.analog-stereo.monitor\tPipeWire\ts32le 2ch 48000Hz\tSUSPENDED\n")) + (cl-letf (((symbol-function 'shell-command-to-string) + (lambda (_cmd) output))) + (let ((result (cj/recording-detect-mic-device))) + ;; BUG: Returns ID "49" even though this is a monitor (output device) + (should (equal "49" result)))))) + +(provide 'test-video-audio-recording-detect-mic-device) +;;; test-video-audio-recording-detect-mic-device.el ends here |
