import pytest

from clarabot_nano.utility import parse_environment_flag
from clarabot_nano_tests.testing.control import run_nano_control_tester
from clarabot_nano_tests.testing.core.common import clear_nano_slots
from clarabot_nano_tests.testing.file import temp_dir
from clarabot_nano_tests.testing.server.manager import DevServerManager


async def _run_control_basic_tests(
    tmpdir,
    reset_user_app_dirs,
    dev_nano_root_dir,
    testing_server_manager: DevServerManager,
    testing_server_user0,
):
    """
    Test:
        - logging in
        - setup wizard (basic config)
        - drive manager
        - reset
    """
    await clear_nano_slots(testing_server_manager, [testing_server_user0["account_id"]])
    test_log = await run_nano_control_tester(
        tmpdir,
        testing_server_manager,
        reset_user_app_dirs,
        dev_nano_root_dir,
        "clarabot_nano_tests.control.cases._init_login:_InitLoginControlApplicationTester",
        {
            "email": testing_server_user0["email"],
            "password": testing_server_user0["password"],
            "tmp_dir": str(temp_dir(tmpdir)),
        },
        timeout=60.0,
    )
    assert test_log == [
        "Starting _InitLoginControlApplicationTester",
        "Systray available: True",
        "Phase 0 passed.",
        "Phase 1 passed.",
        "Phase 2 passed.",
        "Phase 3 passed.",
        "Phase 4 passed.",
        "Phase 5 passed.",
        "Phase 6 passed.",
        "Phase 7 passed.",
        "Phase 8 passed.",
        "Phase 9 passed.",
        "Phase 10 passed.",
        "Phase 11 passed.",
        "Phase 12 passed.",
        "Phase 13 passed.",
        "Phase 14 passed.",
        "Phase 15 passed.",
        "Phase 16 passed.",
        "Phase 17 passed.",
        "Phase 18 passed.",
        "Phase 19 passed.",
        "Phase 20 passed.",
        "Phase 21 passed.",
        "Phase 22 passed.",
        "Phase 23 passed.",
        "Phase 24 passed.",
        "Phase 25 passed.",
        "Phase 26 passed.",
        "Phase 27 passed.",
        "Phase 28 passed.",
        "Phase 29 passed.",
        "Phase 30 passed.",
        "Phase 31 passed.",
        "Phase 32 passed.",
        "Phase 33 passed.",
        "Test phases passed.",
        "Exiting",
        "",
    ], test_log


@pytest.mark.skipif(parse_environment_flag("CBNANO_TEST_WITHOUT_SERVER"), reason="server-dependant tests disabled")
@pytest.mark.asyncio
async def test_control_init_login(
    tmpdir,
    reset_user_app_dirs,
    dev_nano_root_dir,
    testing_server_manager: DevServerManager,
    testing_server_user0,
):
    await _run_control_basic_tests(
        tmpdir, reset_user_app_dirs, dev_nano_root_dir, testing_server_manager, testing_server_user0
    )
