from typing import Dict

from clarabot_nano.common.spawn.constants import ShortNames
from clarabot_nano_tests.testing.gui import GUI_AVAILABLE
from clarabot_nano_tests.testing.process import pypopen_communicate


def start_core_expect_error(args, error, env=None, return_code=1, timeout=3, start_module="clarabot_nano"):
    try:
        with pypopen_communicate([start_module] + args, env=env) as pcom:
            assert pcom.subp.wait(timeout) == return_code
        assert pcom.err.find(error) >= 0, f"{error}\n{pcom.err}"
    except Exception:
        print("FAILED_SUBP_OUT", pcom.out)
        print("FAILED_SUBP_ERR", pcom.err)
        raise


def create_children_wait_map(
    general_server_number: int = 1, gui_available: bool = None, with_java_children: bool = True
) -> Dict[str, int]:
    if gui_available is None:
        gui_available = GUI_AVAILABLE
    children = {
        ShortNames.GENERAL_SERVER: general_server_number,
        ShortNames.FSTX_SERVER: 1,
        ShortNames.GATEWAY: 1,
        ShortNames.INDEXER: 1,
        ShortNames.CONTROL: int(gui_available),
    }
    if with_java_children:
        children.update(
            {
                ShortNames.SOLR: 1,
                ShortNames.TIKA: 1,
                ShortNames.PICOTURE: 1,
            }
        )
    return children
