import pytest

from src.ccltt.ccl_manager import CclManager, build_tree, parse_config_to_dict


def _build_manager_from_config_text(config_text: str) -> CclManager:
    data = parse_config_to_dict(config_text)
    manager = CclManager()
    root = manager.trees["source"].ROOT()
    for child_data in data.get("children", []):
        build_tree(root, child_data)
    return manager


def test_parse_config_formula_with_equals_no_spaces_is_string_value():
    manager = _build_manager_from_config_text('tem="a=b+c"\n')
    root = manager.trees["source"].ROOT()
    tem = root.get("tem")
    assert tem is not None
    assert tem.type() == "str"
    assert tem.value() == "a=b+c"


def test_parse_config_formula_with_equals_with_spaces_is_string_value():
    manager = _build_manager_from_config_text('tem="a = b + c"\n')
    root = manager.trees["source"].ROOT()
    tem = root.get("tem")
    assert tem is not None
    assert tem.type() == "str"
    assert tem.value() == "a = b + c"


def test_parse_config_semicolon_assignment_list_becomes_children():
    manager = _build_manager_from_config_text("tem=a=1;b=2;\n")
    root = manager.trees["source"].ROOT()
    assert root.get("tem") is not None
    assert root.get("tem").value() is None
    assert root.get("tem/a").type() == "int"
    assert root.get("tem/a").value() == "1"
    assert root.get("tem/b").type() == "int"
    assert root.get("tem/b").value() == "2"

