import pytest

from src.py_cdll import CDLL, ValueNotFoundError, MultipleValuesFoundError
from src.py_cdll.node import is_consistent, is_value_at_index


def test_replace_unique_present_success():
    # Setup
    data: str = "data"
    data0: str = "data0"
    data1: str = "data1"
    data2: str = "data2"
    values0: list[str] = [data0, data1, data2]
    values1: list[str] = [data0, data, data2]
    cdll0: CDLL = CDLL(values=values0)

    # Execution
    cdll0.replace_unique(value=data1, replacement=data)

    # Validation
    assert cdll0[1] is data
    assert cdll0._head is cdll0._head.head
    assert is_consistent(node=cdll0._head)
    assert is_value_at_index(node=cdll0._head, reference=values1)


def test_replace_unique_not_present_failure():
    # Setup
    data_find: str = "data_find"
    data_new: str = "data_new"
    data0: str = "data0"
    data1: str = "data1"
    data2: str = "data2"
    values0: list[str] = [data0, data1, data2]
    cdll0: CDLL = CDLL(values=values0)

    # Validation
    with pytest.raises(ValueNotFoundError):
        cdll0.replace_unique(value=data_find, replacement=data_new)


def test_replace_unique_multiple_options_failure():
    # Setup
    data: str = "data"
    data0: str = "data0"
    data1: str = "data1"
    data2: str = "data2"
    values0: list[str] = [data0, data1, data1, data2]
    cdll0: CDLL = CDLL(values=values0)

    # Validation
    with pytest.raises(MultipleValuesFoundError):
        cdll0.replace_unique(value=data1, replacement=data)
