# Path: test/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2
# 
# Path: test/framework/api_select.py
# def api(file_name, api_type, login=None, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
#         username=None, password=None):
# 
# Path: test/framework/utils.py
# def lines_in(api_type, text, *expected_lines):
#     """Assert that `*expected_lines` occur in order in the lines of `text`.
# 
#     Args:
#         *expected_lines (str or RegexObject (hasattr `match`)): For each `expected_line` in expected_lines:
#             If `expected_line` has a match method it is called and must return True for a line in `text`.
#             Otherwise, if the `expected_line` starts with '^', a line in `text` must start with `expected_line[1:]`
#             Otherwise `expected line` must simply occur in a line in `text`
#     """
# 
#     assert isinstance(api_type, test_cfg.ApiType)
#     assert text
#     assert expected_lines
# 
#     return generic_lines_in(text, partial(replace_host_port, api_type), *expected_lines)
# 
# def build_started_msg(api, job_name, num, invocation_number=0):
#     return "Build started: " + repr(job_name) + ((' Invocation-' + repr(invocation_number)) if invocation_number else '') + " - " + console_url(api, job_name, num)
# 
# def build_queued_msg(api, job_name, num):
#     if api.api_type == ApiType.MOCK:
#         queued_why = r"Why am I queued\?"
#     else:
#         queued_why = r"Build #[0-9]+ is already in progress \(ETA: ?([0-9.]+ sec|N/A)\)"
#     return re.compile("^job: '" + job_name + "' Status QUEUED - " + queued_why)


from jenkinsflow.flow import serial
from .cfg import ApiType
from .framework import api_select
from .framework.utils import lines_in, build_started_msg, build_queued_msg
# Copyright (c) 2012 - 2015 Lars Hupfeldt Nielsen, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.



def test_reporting_queued(api_type, capsys):
    # TODO
    skip_apis = (ApiType.SCRIPT, ApiType.MOCK)

    with api_select.api(__file__, api_type, login=True) as api:
        api.flow_job()
        exp_invocations = 2 if api.api_type not in skip_apis else 1
        unknown_result = False if api.api_type not in skip_apis else True
        api.job('j1', max_fails=0, expect_invocations=exp_invocations, expect_order=None, exec_time=10, unknown_result=unknown_result)

        with serial(api, timeout=70, job_name_prefix=api.job_name_prefix) as ctrl1:
            ctrl1.invoke_unchecked('j1')

        print(api.api_type, type(api.api_type))
        if api.api_type in skip_apis:
            return

        with serial(api, timeout=70, job_name_prefix=api.job_name_prefix, require_idle=False) as ctrl1:
            ctrl1.invoke('j1')

        sout, _ = capsys.readouterr()
        assert lines_in(
            api_type, sout,
            "^Job Invocation (1/1,1/1): http://x.x/job/jenkinsflow_test__reporting_queued__j1",
            build_queued_msg(api, "jenkinsflow_test__reporting_queued__j1", 1),

# Path: test/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2
# 
# Path: test/framework/api_select.py
# def api(file_name, api_type, login=None, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
#         username=None, password=None):
# 
# Path: test/framework/utils.py
# def lines_in(api_type, text, *expected_lines):
#     """Assert that `*expected_lines` occur in order in the lines of `text`.
# 
#     Args:
#         *expected_lines (str or RegexObject (hasattr `match`)): For each `expected_line` in expected_lines:
#             If `expected_line` has a match method it is called and must return True for a line in `text`.
#             Otherwise, if the `expected_line` starts with '^', a line in `text` must start with `expected_line[1:]`
#             Otherwise `expected line` must simply occur in a line in `text`
#     """
# 
#     assert isinstance(api_type, test_cfg.ApiType)
#     assert text
#     assert expected_lines
# 
#     return generic_lines_in(text, partial(replace_host_port, api_type), *expected_lines)
# 
# def build_started_msg(api, job_name, num, invocation_number=0):
#     return "Build started: " + repr(job_name) + ((' Invocation-' + repr(invocation_number)) if invocation_number else '') + " - " + console_url(api, job_name, num)
# 
# def build_queued_msg(api, job_name, num):
#     if api.api_type == ApiType.MOCK:
#         queued_why = r"Why am I queued\?"
#     else:
#         queued_why = r"Build #[0-9]+ is already in progress \(ETA: ?([0-9.]+ sec|N/A)\)"
#     return re.compile("^job: '" + job_name + "' Status QUEUED - " + queued_why)


# Path: test/reporting_queued_test.py
from jenkinsflow.flow import serial
from .cfg import ApiType
from .framework import api_select
from .framework.utils import lines_in, build_started_msg, build_queued_msg

# Copyright (c) 2012 - 2015 Lars Hupfeldt Nielsen, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.



def test_reporting_queued(api_type, capsys):
    # TODO
    skip_apis = (ApiType.SCRIPT, ApiType.MOCK)

    with api_select.api(__file__, api_type, login=True) as api:
        api.flow_job()
        exp_invocations = 2 if api.api_type not in skip_apis else 1
        unknown_result = False if api.api_type not in skip_apis else True
        api.job('j1', max_fails=0, expect_invocations=exp_invocations, expect_order=None, exec_time=10, unknown_result=unknown_result)

        with serial(api, timeout=70, job_name_prefix=api.job_name_prefix) as ctrl1:
            ctrl1.invoke_unchecked('j1')

        print(api.api_type, type(api.api_type))
        if api.api_type in skip_apis:
            return

        with serial(api, timeout=70, job_name_prefix=api.job_name_prefix, require_idle=False) as ctrl1:
            ctrl1.invoke('j1')

        sout, _ = capsys.readouterr()
        assert lines_in(
            api_type, sout,
            "^Job Invocation (1/1,1/1): http://x.x/job/jenkinsflow_test__reporting_queued__j1",
            build_queued_msg(api, "jenkinsflow_test__reporting_queued__j1", 1),

            build_started_msg(api, "jenkinsflow_test__reporting_queued__j1", 2),