# coding=utf-8
"""
Tasks for running various tests for Rookeries

:copyright: Copyright 2013-2017, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""

import invoke as inv


@inv.task
def python_style(ctx):
    """
    Test the coding style using Flake8.

    :param ctx: Context of the invoke task.
    """
    inv.run('flake8 --exclude=docs,node_modules --max-line-length=120 --max-complexity=10', echo=True, pty=True)


def _prepare_py_test_command(test_path, verbosity=0, **extra_test_params):
    verbose_flag = ''
    if verbosity > 0:
        verbose_flag = '-{}'.format('v' * verbosity)

    if verbosity >= 3:
        verbose_flag = f'{verbose_flag} -r a'

    additional_test_params = ' '.join(
        ['--{}={}'.format(key.replace('_', '-'), value) for (key, value) in extra_test_params.items()])
    return f'py.test {test_path} --capture=no {verbose_flag} {additional_test_params}'


@inv.task
def server(ctx, server_host='localhost:5000', verbosity=0, test_tag='', randomize=False):
    """
    Test the API server using unit, and integration tests.

    :param ctx: Context of the invoke task.
    :param server_host: The host and port of the server to test.
    :param verbosity: The amount of logging from the test.  Default is 0 and is the lowest.  3 is the highest.
    :param test_tag: Optional tag (pytest mark) to run individually decorated tests in suite.
    :param randomize: Flag whether or not to randomize test order.
    """

    test_command = _prepare_py_test_command(
        'tests/server',
        verbosity=verbosity,
        server_host=server_host,
    )
    if test_tag:
        test_command = f'{test_command} -m {test_tag}'

    if randomize:
        test_command = f'{test_command} --random'
    inv.run(test_command, echo=True, pty=True)


@inv.task
def js_style(ctx):
    """
    Test the UI coding style using eslint.

    :param ctx: Context of the invoke task.
    """
    inv.run('npm run lint', echo=True, pty=True)


@inv.task
def js(ctx):
    inv.run('npm run test', echo=True, pty=True)


@inv.task
def ui(ctx, server_host='localhost:5000', verbosity=0, test_tag='', randomize=False):
    """
    Test the UI using unit, and integration tests.

    :param ctx: Context of the invoke task.
    :param server_host: The host and port of the server to run tests against.
    :param verbosity: The amount of logging from the test.  Default is 0 and is the lowest.  3 is the highest.
    :param test_tag: Optional tag (pytest mark) to run individually decorated tests in suite.
    :param randomize: Flag whether or not to randomize test order.
    """
    test_command = _prepare_py_test_command(
        'tests/feature',
        verbosity=verbosity,
        server_host=server_host,
        splinter_webdriver='remote',
        splinter_remote_url=f'http://testing:4444/wd/hub',
    )

    if test_tag:
        test_command = f'{test_command} -m {test_tag}'

    if randomize:
        test_command = f'{test_command} --random'
    inv.run(test_command, pty=True)
