"""Tests for backend.main_headless ready-event JSON format.""" import sys from pathlib import Path import pytest # Ensure project root is on path project_root = Path(__file__).resolve().parent.parent.parent sys.path.insert(0, str(project_root)) def test_ready_event_reports_api_port_not_obs_port(): """The ready JSON printed by main_headless must set ``port`` to ``obs_port + 1`` (the API port), not the OBS display port. From main_headless.py:: obs_port = controller.actual_web_port or args.port api_port = obs_port + 1 print(json.dumps({ "event": "ready", "port": api_port, "obs_port": obs_port, }), flush=True) We verify this contract by reading the source and checking the structure directly (running main() would start a real server). """ import ast import textwrap source_path = project_root / "backend" / "main_headless.py" source = source_path.read_text() # Verify the key relationships exist in the source: # 1. api_port = obs_port + 1 assert "api_port = obs_port + 1" in source, ( "Expected `api_port = obs_port + 1` in main_headless.py" ) # 2. The ready event JSON uses api_port for "port", not obs_port assert '"port": api_port' in source or "'port': api_port" in source, ( "The ready event should report api_port as 'port'" ) # 3. obs_port is also included separately assert '"obs_port": obs_port' in source or "'obs_port': obs_port" in source, ( "The ready event should also include 'obs_port'" ) # 4. Verify the event name assert '"event": "ready"' in source or "'event': 'ready'" in source, ( "The ready event should have event='ready'" )