from pathlib import Path

from clarabot_nano_tests.core.utility.common import start_core_expect_error
from clarabot_nano_tests.core.utility.config import check_configs_empty, get_configs
from clarabot_nano_tests.testing.file import write_yaml, temp_path


def test_core_bootstrap_config_error_by_gui(tmpdir, reset_user_app_config_dir, reset_user_app_data_dir):
    _r = Path(temp_path(tmpdir))
    _r.mkdir()
    feedback_path = _r / "fb.txt"
    check_configs_empty(reset_user_app_config_dir)
    reset_user_app_data_dir.mkdir(parents=True, exist_ok=True)
    write_yaml(str(reset_user_app_data_dir / "logs"), {"test": "me"})
    start_core_expect_error(
        [],
        exc := "Exception: Could not create default configuration",
        env={"CBNANO_TEST_GUI_ERROR_FEEDBACK_PATH": str(feedback_path)},
        start_module="clarabot_nano_tests.core.instance.err_main",
    )
    assert get_configs(reset_user_app_config_dir) == {"local": None}
    assert feedback_path.read_text("utf-8").find(exc) >= 0
