# CLI Tests Comprehensive test suite for the utils CLI commands. ## Test Structure - `test_common.py` - Tests for common CLI utilities (YAML loading, metadata parsing, frequency formatting) - `test_discover.py` - Tests for device discovery functionality - `test_capture.py` - Tests for the capture command - `test_transmit.py` - Tests for the transmit command ## Running Tests ### Run all CLI tests: ```bash poetry run pytest tests/utils_cli/ -v ``` ### Run specific test file: ```bash poetry run pytest tests/utils_cli/test_common.py -v poetry run pytest tests/utils_cli/test_discover.py -v poetry run pytest tests/utils_cli/test_capture.py -v ``` ### Run specific test class or function: ```bash poetry run pytest tests/utils_cli/test_capture.py::TestCaptureCommand::test_capture_basic -v poetry run pytest tests/utils_cli/test_common.py::test_parse_frequency -v ``` ### Run with coverage: ```bash poetry run pytest tests/utils_cli/ --cov=utils_cli --cov-report=html ``` ## Test Coverage ### test_common.py - YAML configuration file loading - Metadata KEY=VALUE parsing - Frequency parsing (scientific notation, suffixes) - Frequency and sample rate formatting ### test_discover.py - Device discovery for all supported SDR types (PlutoSDR, HackRF, BladeRF, USRP, RTL-SDR, ThinkRF) - Device auto-selection logic - Device connection testing - CLI command options (--verbose, --json-output, --test, --type) - Error handling for missing devices and multiple devices ### test_capture.py - Basic capture functionality - Parameter validation (sample rate, center frequency, duration/num-samples) - Device auto-selection - Multiple output formats (SigMF, NPY, WAV, Blue) - Format auto-detection from file extension - YAML configuration file loading - Custom metadata handling - Gain and bandwidth configuration - Visualization saving (--save-image) - Chunked capture for large recordings - Verbose and quiet output modes - Proper device cleanup on errors ### test_transmit.py - TX device initialization (PlutoSDR, HackRF, BladeRF, USRP only) - RX-only device rejection (RTL-SDR, ThinkRF) - TX device auto-selection - Input file loading (SigMF, NPY, WAV, Blue) - Legacy NPY format support - TX gain validation and range checking - Sample rate mismatch warnings - Transmission modes: - Single transmission - Repeat mode with delays - Continuous transmission with safety warnings - YAML configuration file loading - Safety confirmations for continuous mode - Proper device cleanup on errors - Verbose and quiet output modes ## Mock Strategy Tests use `unittest.mock` to: - Mock SDR device instances to avoid requiring actual hardware - Mock file I/O operations - Mock discovery functions to simulate different device scenarios - Verify proper function calls and parameters ## Adding New Tests When adding new CLI commands, follow this pattern: 1. Create `test_.py` in this directory 2. Use Click's `CliRunner` for testing CLI commands 3. Mock external dependencies (SDR devices, file I/O) 4. Test both success and error cases 5. Verify proper resource cleanup (device.close(), file handles, etc.) Example: ```python from click.testing import CliRunner from unittest.mock import patch, MagicMock def test_new_command(): runner = CliRunner() with patch('module.dependency') as mock_dep: mock_dep.return_value = expected_value result = runner.invoke(command, ['--option', 'value']) assert result.exit_code == 0 assert 'expected output' in result.output ``` ## CI/CD Integration These tests are designed to run in CI/CD pipelines without requiring actual SDR hardware. All hardware interactions are mocked. ## Notes - Tests use temporary directories for file operations (cleaned up automatically) - Device mocks simulate real SDR behavior without hardware dependencies - Tests verify both command-line interface and underlying function behavior