from matterlab_relays import (
    CE221ARelay,
    JYdaqRelay,
    R421B16Relay,
    create_ce221a_sim_transport_factory,
    create_jydaq_sim_rtu_master_factory,
    create_jydaq_sim_transport_factory,
    create_r421b16_sim_transport_factory,
)


def test_ce221a_transport_sim_uses_public_api():
    transport_factory = create_ce221a_sim_transport_factory()
    relay = CE221ARelay(com_port="SIM::ce221a", channel=1, transport_factory=transport_factory)

    relay.on = True

    assert relay.on is True
    assert transport_factory.state.get_channel(1) is True


def test_r421b16_transport_sim_uses_public_api():
    transport_factory = create_r421b16_sim_transport_factory(address=1)
    relay = R421B16Relay(com_port="SIM::r421", address=1, channel=1, transport_factory=transport_factory)

    assert relay.on is False
    relay.on = True

    assert relay.on is True
    assert transport_factory.state.get_channel(1) is True


def test_jydaq_rtu_sim_uses_public_api():
    transport_factory = create_jydaq_sim_transport_factory()
    rtu_master_factory = create_jydaq_sim_rtu_master_factory()
    relay = JYdaqRelay(
        com_port="SIM::jydaq",
        address=1,
        channel=1,
        transport_factory=transport_factory,
        rtu_master_factory=rtu_master_factory,
    )
    relay._add_rtu_master()

    assert relay.on is False
    relay.on = True

    assert relay.on is True
    assert rtu_master_factory.state.get_channel(1, one_based=False) is True
