"""
Start ArduinoEmulator & hevserver background processes and assert expected
hevclient state/behaviour. Safely kills the background processes so you don't
have to use the CLI. Aim is to help integration tests to be more atomic and
easy to execute. Nothing wrong with using CLI, but managing these
processes from within test-code itself is useful for testing
can be more atomic and easier to manage: consider starting/killing
the background processes for each/every test individually and/or once
for a particular test-suite

Script is purposefully named without a leading or trailing 'test' in
the script name so that it doesn't get executed by pytest by default.

Make sure your PYTHONPATH var is set to the full path of:
'/<your_hev_root_dir>/hev/NativeUI'.
"""
import os
import tempfile
import time
import hevclient
from hevclient import HEVClient
from tests.integration.integration_utils import start_background_arduinosim_hevserver,\
    kill_process_group, assert_posix, assert_pythonpath

# Overwrite the mm file for OS agnostic testing
hevclient.mmFileName = tempfile.gettempdir() + os.path.sep + "HEVCLIENT_last_Data.mmap"

def test_hevclient(capsys, caplog):
    """
    Start the required background ArduinoSimulator and hevserver and
    assert the state of hevclient.

    IF the test fails unexpectedly due to e.g. raised exception,
    can't guarantee cleanup of the background processes so do check on the CLI!
    """
    assert_posix()
    assert_pythonpath()
    background_procs = None
    try:
        background_procs = start_background_arduinosim_hevserver()
        HEVClient(True)
        time.sleep(1) # give enough time for hevclient to log

        captured = capsys.readouterr()
        capsysout = captured.out
        capsyserr = captured.err
        assert "Err" not in capsyserr
        assert "Err" not in capsysout
        assert "is the microcontroller running" not in caplog.text
        assert "[Errno" not in caplog.text

    finally:
        if background_procs is not None:
            for proc in background_procs:
                kill_process_group(proc.pid)

def test_hevclient_alarms():
    """
    Start the required background ArduinoSimulator and hevserver and
    assert the state of hevclient alarms using B6-20201207.dump file.

    IF the test fails unexpectedly due to e.g. raised exception,
    can't guarantee cleanup of the background processes so do check on the CLI!
    """
    assert_posix()
    assert_pythonpath()
    background_procs = None
    try:
        background_procs = start_background_arduinosim_hevserver()
        myhevclient = HEVClient(True)
        time.sleep(2)
        alarms = myhevclient.get_alarms()
        alarms_as_str = ''.join(str(v) for v in alarms)
        # TODO meaningful assertions:
        assert 'PRIORITY_HIGH' in alarms_as_str
        assert 'PRIORITY_MEDIUM' in alarms_as_str
        assert '182' in alarms_as_str
        assert 'LOW_VTI' in alarms_as_str
        print(alarms)

    finally:
        if background_procs is not None:
            for proc in background_procs:
                kill_process_group(proc.pid)
