import copy
from pathlib import Path

import pytest
import schema

from clarabot_nano.core.config.schema import DEFAULT_CONFIG, LOCAL_CONFIG_SCHEMA
from clarabot_nano_tests.testing.file import temp_path


def test_core_config_schema_local(tmpdir):
    temp_dir = Path(str(temp_path(tmpdir)))
    temp_dir.mkdir()
    local_config = copy.deepcopy(DEFAULT_CONFIG["local"])
    local_config.update(
        {
            "source": "<memory>",
            "log_config": {},
            "config_dir": str(temp_dir),
            "data_dir": str(temp_dir),
            "pid_dir": "pid",
            "solr_dir": "",
            "solr_data_dir": "data",
            "solr_logs_dir": "logs",
            "tika_dir": "",
            "tika_logs_dir": "logs",
        }
    )
    for java_name in ["solr", "tika", "picoture"]:
        try:
            LOCAL_CONFIG_SCHEMA.validate(local_config)
        except Exception as e:
            assert False, str(e)

        local_config[f"{java_name}_heap_init"] = "513M"
        with pytest.raises(schema.SchemaError, match=f"Heap arguments are invalid for {java_name}"):
            LOCAL_CONFIG_SCHEMA.validate(local_config)

        local_config[f"{java_name}_heap_size"] = "513M"
        try:
            LOCAL_CONFIG_SCHEMA.validate(local_config)
        except Exception as e:
            assert False, str(e)
