import pytest
from savcfg.edit import ConfigEditor, VarsEditor


class InMemVarsEditor(VarsEditor):
    def __init__(self):
        self._data = {}

    def list_vars(self) -> list[str]:
        return list(self._data.keys())

    def read(self, key: str) -> str:
        return self._data.get(key, "")

    def write(self, key: str, value: str):
        self._data[key] = value

    def remove(self, key: str):
        self._data.pop(key)


@pytest.fixture
def var_editor():
    return InMemVarsEditor()


@pytest.fixture
def editor(var_editor):
    return ConfigEditor(var_editor)


class A_ConfigEditor:
    def should_set_a_primitive(self, editor):
        value = 123
        editor.set("test.set_primitive", value)

        assert value == editor.get("test.set_primitive")

    def should_set_group(self, editor):
        value = {"token": "value"}
        editor.set("test.set_group", value)

        assert value == editor.get("test.set_group")
        assert ["test.set_group"] == editor.list()
