diff --git a/.gitignore b/.gitignore index fc6c141..81393e6 100644 --- a/.gitignore +++ b/.gitignore @@ -131,6 +131,7 @@ ENV/ env.bak/ venv.bak/ .hatch +.test.env # Spyder project settings .spyderproject diff --git a/pyproject.toml b/pyproject.toml index dce1bcc..83221d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,10 +40,9 @@ path = "obsws_cli/__about__.py" [tool.hatch.envs.default.scripts] cli = "obsws-cli {args:}" -test = "pytest {args:obsws_cli tests}" [tool.hatch.envs.hatch-test] -dependencies = ["pytest>=8.3.5", "pytest-dotenv"] +dependencies = ["pytest>=8.3.5"] [tool.hatch.envs.types] extra-dependencies = ["mypy>=1.0.0"] diff --git a/tests/conftest.py b/tests/conftest.py index 36bcf74..29a480a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,6 +3,7 @@ import os import obsws_python as obsws +from dotenv import find_dotenv, load_dotenv def pytest_configure(config): @@ -19,9 +20,9 @@ def pytest_sessionstart(session): """ # Initialize the OBS WebSocket client session.obsws = obsws.ReqClient( - host=os.environ['OBSWS_HOST'], - port=os.environ['OBSWS_PORT'], - password=os.environ['OBSWS_PASSWORD'], + host=os.environ['OBS_HOST'], + port=os.environ['OBS_PORT'], + password=os.environ['OBS_PASSWORD'], timeout=5, ) resp = session.obsws.get_version() @@ -32,6 +33,17 @@ def pytest_sessionstart(session): ) print(' '.join(out)) + load_dotenv(find_dotenv('.test.env')) + + session.obsws.set_stream_service_settings( + 'rtmp_common', + { + 'service': 'Twitch', + 'server': 'auto', + 'key': os.environ['OBS_STREAM_KEY'], + }, + ) + session.obsws.set_current_scene_collection('test-collection') session.obsws.create_scene('pytest') @@ -68,6 +80,10 @@ def pytest_sessionfinish(session, exitstatus): """ session.obsws.remove_scene('pytest') + resp = session.obsws.get_stream_status() + if resp.output_active: + session.obsws.stop_stream() + # Close the OBS WebSocket client connection session.obsws.disconnect() diff --git a/tests/test_group.py b/tests/test_group.py index d16e5e6..4f640b6 100644 --- a/tests/test_group.py +++ b/tests/test_group.py @@ -9,35 +9,35 @@ runner = CliRunner() def test_group_list(): """Test the group list command.""" - result = runner.invoke(app, ['group', 'list', 'pytest00']) + result = runner.invoke(app, ['group', 'list', 'Scene']) assert result.exit_code == 0 assert 'test_group' in result.stdout def test_group_show(): """Test the group show command.""" - result = runner.invoke(app, ['group', 'show', 'pytest00', 'test_group']) + result = runner.invoke(app, ['group', 'show', 'Scene', 'test_group']) assert result.exit_code == 0 assert "Group 'test_group' is now visible." in result.stdout def test_group_toggle(): """Test the group toggle command.""" - result = runner.invoke(app, ['group', 'hide', 'pytest00', 'test_group']) + result = runner.invoke(app, ['group', 'hide', 'Scene', 'test_group']) assert result.exit_code == 0 assert "Group 'test_group' is now hidden." in result.stdout - result = runner.invoke(app, ['group', 'toggle', 'pytest00', 'test_group']) + result = runner.invoke(app, ['group', 'toggle', 'Scene', 'test_group']) assert result.exit_code == 0 assert "Group 'test_group' is now visible." in result.stdout def test_group_status(): """Test the group status command.""" - result = runner.invoke(app, ['group', 'show', 'pytest00', 'test_group']) + result = runner.invoke(app, ['group', 'show', 'Scene', 'test_group']) assert result.exit_code == 0 assert "Group 'test_group' is now visible." in result.stdout - result = runner.invoke(app, ['group', 'status', 'pytest00', 'test_group']) + result = runner.invoke(app, ['group', 'status', 'Scene', 'test_group']) assert result.exit_code == 0 assert "Group 'test_group' is now visible." in result.stdout diff --git a/tests/test_stream.py b/tests/test_stream.py new file mode 100644 index 0000000..6bf2c83 --- /dev/null +++ b/tests/test_stream.py @@ -0,0 +1,43 @@ +"""Unit tests for the stream commands in the OBS WebSocket CLI.""" + +import time + +from typer.testing import CliRunner + +from obsws_cli.app import app + +runner = CliRunner() + + +def test_stream_start(): + """Test the stream start command.""" + result = runner.invoke(app, ['stream', 'status']) + assert result.exit_code == 0 + active = 'Streaming is in progress' in result.stdout + + result = runner.invoke(app, ['stream', 'start']) + assert result.exit_code == 0 + + time.sleep(1) # Wait for the stream to start + + if active: + assert 'Streaming is already in progress, cannot start.' in result.stdout + else: + assert 'Streaming started successfully.' in result.stdout + + +def test_stream_stop(): + """Test the stream stop command.""" + result = runner.invoke(app, ['stream', 'status']) + assert result.exit_code == 0 + active = 'Streaming is in progress' in result.stdout + + result = runner.invoke(app, ['stream', 'stop']) + assert result.exit_code == 0 + + time.sleep(1) # Wait for the stream to stop + + if active: + assert 'Streaming stopped successfully.' in result.stdout + else: + assert 'Streaming is not in progress, cannot stop.' in result.stdout diff --git a/tests/test_studiomode.py b/tests/test_studiomode.py new file mode 100644 index 0000000..94fa1fc --- /dev/null +++ b/tests/test_studiomode.py @@ -0,0 +1,27 @@ +"""Unit tests for the studio mode command in the OBS WebSocket CLI.""" + +from typer.testing import CliRunner + +from obsws_cli.app import app + +runner = CliRunner() + + +def test_studio_enable(): + """Test the studio enable command.""" + result = runner.invoke(app, ['studiomode', 'enable']) + assert result.exit_code == 0 + + result = runner.invoke(app, ['studiomode', 'status']) + assert result.exit_code == 0 + assert 'Studio mode is enabled.' in result.stdout + + +def test_studio_disable(): + """Test the studio disable command.""" + result = runner.invoke(app, ['studiomode', 'disable']) + assert result.exit_code == 0 + + result = runner.invoke(app, ['studiomode', 'status']) + assert result.exit_code == 0 + assert 'Studio mode is disabled.' in result.stdout