import pytest
from target_simulator.core.serial_communicator import SerialCommunicator
from target_simulator.core.models import Scenario


@pytest.fixture
def serial_config():
    return {
        "port": "COM1",
        "baudrate": 9600,
        "bytesize": "8",
        "parity": "N",
        "stopbits": "1",
    }


def test_serial_communicator_init():
    comm = SerialCommunicator()
    assert comm.serial_instance is None
    assert comm.config is None


def test_serial_communicator_connect_disconnect(serial_config):
    comm = SerialCommunicator()
    # connect should fail (no real port), but should not raise
    result = comm.connect(serial_config)
    assert result is False or result is True
    comm.disconnect()


def test_serial_communicator_is_open_property():
    comm = SerialCommunicator()
    assert comm.is_open is False


def test_serial_communicator_send_scenario(serial_config):
    comm = SerialCommunicator()
    comm.connect(serial_config)
    scenario = Scenario(name="TestScenario")
    # send_scenario should not raise, even if not connected to real device
    result = comm.send_scenario(scenario)
    assert result is False or result is True


def test_serial_communicator_list_available_ports():
    ports = SerialCommunicator.list_available_ports()
    assert isinstance(ports, list)


def test_serial_communicator_test_connection(serial_config):
    result = SerialCommunicator.test_connection(serial_config)
    assert result is False or result is True
