import struct
import asyncio
from unittest.mock import AsyncMock
from pm3py.protocol import Cmd
from pm3py.transport import PM3Response
from pm3py.lf import LFCommands

def make_response(cmd, status, data):
    return PM3Response(cmd=cmd, status=status, reason=0, ng=True, data=data)

def test_lf_tune():
    t = AsyncMock()
    lf = LFCommands(t)
    # First call: init, second call: poll returns data
    t.send_ng.return_value = make_response(Cmd.MEASURE_ANTENNA_TUNING_LF, 0,
                                           struct.pack("<I", 42000))
    result = asyncio.get_event_loop().run_until_complete(lf.tune(divisor=95))
    assert "voltage_mV" in result

def test_lf_config_get():
    t = AsyncMock()
    lf = LFCommands(t)
    payload = struct.pack("<bbbhhib", 1, 8, 1, 95, 128, 0, 0)
    t.send_ng.return_value = make_response(Cmd.LF_SAMPLING_GET_CONFIG, 0, payload)
    result = asyncio.get_event_loop().run_until_complete(lf.config())
    assert "decimation" in result
    assert "bits_per_sample" in result
