From bc43c8483ace38a72a1ab6e02df248d2594d708c Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 12 Jun 2025 23:47:07 +0100 Subject: [PATCH] add audio unit tests add audio status command patch bump --- src/slobs_cli/__about__.py | 2 +- src/slobs_cli/audio.py | 45 ++++++++++++++++++++++++++++++++------ src/slobs_cli/scene.py | 2 +- tests/test_audio.py | 43 ++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 tests/test_audio.py diff --git a/src/slobs_cli/__about__.py b/src/slobs_cli/__about__.py index a14b0d1..6a31305 100644 --- a/src/slobs_cli/__about__.py +++ b/src/slobs_cli/__about__.py @@ -1,3 +1,3 @@ """module for package metadata.""" -__version__ = '0.9.1' +__version__ = '0.9.2' diff --git a/src/slobs_cli/audio.py b/src/slobs_cli/audio.py index b50d369..d37274d 100644 --- a/src/slobs_cli/audio.py +++ b/src/slobs_cli/audio.py @@ -75,10 +75,10 @@ async def mute(ctx: click.Context, source_name: str): break else: # If no source by the given name was found conn.close() - raise SlobsCliError(f"Source '{source_name}' not found.") + raise SlobsCliError(f'Audio source "{source_name}" not found.') await source.set_muted(True) - click.echo(f'Muted audio source: {source_name}') + click.echo(f'{source_name} muted successfully.') conn.close() try: @@ -106,10 +106,10 @@ async def unmute(ctx: click.Context, source_name: str): break else: # If no source by the given name was found conn.close() - raise SlobsCliError(f"Source '{source_name}' not found.") + raise SlobsCliError(f'Audio source "{source_name}" not found.') await source.set_muted(False) - click.echo(f'Unmuted audio source: {source_name}') + click.echo(f'{source_name} unmuted successfully.') conn.close() try: @@ -136,15 +136,46 @@ async def toggle(ctx: click.Context, source_name: str): if model.name.lower() == source_name.lower(): if model.muted: await source.set_muted(False) - click.echo(f'Unmuted audio source: {source_name}') + click.echo(f'{source_name} unmuted successfully.') else: await source.set_muted(True) - click.echo(f'Muted audio source: {source_name}') + click.echo(f'{source_name} muted successfully.') conn.close() break else: # If no source by the given name was found conn.close() - raise SlobsCliError(f"Source '{source_name}' not found.") + raise SlobsCliError(f'Audio source "{source_name}" not found.') + + try: + async with create_task_group() as tg: + tg.start_soon(conn.background_processing) + tg.start_soon(_run) + except* SlobsCliError as excgroup: + for e in excgroup.exceptions: + raise e + + +@audio.command() +@click.argument('source_name') +@click.pass_context +async def status(ctx: click.Context, source_name: str): + """Get the mute status of an audio source by name.""" + conn = ctx.obj['connection'] + as_ = AudioService(conn) + + async def _run(): + sources = await as_.get_sources() + for source in sources: + model = await source.get_model() + if model.name.lower() == source_name.lower(): + click.echo( + f'"{source_name}" is {"muted" if model.muted else "unmuted"}.' + ) + conn.close() + return + else: + conn.close() + raise SlobsCliError(f'Audio source "{source_name}" not found.') try: async with create_task_group() as tg: diff --git a/src/slobs_cli/scene.py b/src/slobs_cli/scene.py index c180253..385be14 100644 --- a/src/slobs_cli/scene.py +++ b/src/slobs_cli/scene.py @@ -84,7 +84,7 @@ async def current(ctx: click.Context, id: bool = False): @scene.command() @click.option('--id', is_flag=True, help='Include scene IDs in the output.') -@click.argument('scene_name', type=str) +@click.argument('scene_name') @click.option( '--preview', is_flag=True, diff --git a/tests/test_audio.py b/tests/test_audio.py new file mode 100644 index 0000000..4a19ff1 --- /dev/null +++ b/tests/test_audio.py @@ -0,0 +1,43 @@ +"""Test cases for audio commands in slobs_cli.""" + +import pytest +from asyncclick.testing import CliRunner + +from slobs_cli import cli + + +@pytest.mark.anyio +async def test_audio_list(): + """Test the list audio sources command.""" + runner = CliRunner() + result = await runner.invoke(cli, ['audio', 'list']) + assert result.exit_code == 0 + assert 'Desktop Audio' in result.output + assert 'Mic/Aux' in result.output + + +@pytest.mark.anyio +async def test_audio_mute(): + """Test the mute audio source command.""" + runner = CliRunner() + result = await runner.invoke(cli, ['audio', 'mute', 'Mic/Aux']) + assert result.exit_code == 0 + assert 'Mic/Aux muted successfully' in result.output + + +@pytest.mark.anyio +async def test_audio_unmute(): + """Test the unmute audio source command.""" + runner = CliRunner() + result = await runner.invoke(cli, ['audio', 'unmute', 'Mic/Aux']) + assert result.exit_code == 0 + assert 'Mic/Aux unmuted successfully' in result.output + + +@pytest.mark.anyio +async def test_audio_invalid_source(): + """Test handling of invalid audio source.""" + runner = CliRunner() + result = await runner.invoke(cli, ['audio', 'mute', 'InvalidSource']) + assert result.exit_code != 0 + assert 'Audio source "InvalidSource" not found' in result.output