#!/usr/bin/env python3
"""
#exonware/xwnode/tests/0.core/test_core_indexing.py
Tests for XWNode indexing support (int, slice, str).
Company: eXonware.com
Author: eXonware Backend Team
Email: connect@exonware.com
Version: 0.0.1
Generation Date: 15-Dec-2025
"""

import pytest
from exonware.xwnode import XWNode
@pytest.mark.xwnode_core

class TestXWNodeIndexing:
    """Test XWNode indexing with int, slice, and str keys."""

    def test_integer_indexing_list(self):
        """Test integer indexing for list data."""
        node = XWNode.from_native([{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}])
        # Integer indexing
        first = node[0]
        assert first == {"name": "Alice", "age": 30}
        second = node[1]
        assert second == {"name": "Bob", "age": 25}
        # Out of range
        with pytest.raises((IndexError, KeyError)):
            _ = node[2]

    def test_slice_indexing_list(self):
        """Test slice indexing for list data."""
        node = XWNode.from_native([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        # Slice indexing
        first_three = node[0:3]
        assert first_three == [1, 2, 3]
        middle = node[2:5]
        assert middle == [3, 4, 5]
        all_items = node[:]
        assert all_items == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        last_three = node[-3:]
        assert last_three == [8, 9, 10]

    def test_string_key_indexing_dict(self):
        """Test string key indexing for dict data."""
        node = XWNode.from_native({"name": "Alice", "age": 30, "city": "NYC"})
        # String key indexing
        assert node["name"] == "Alice"
        assert node["age"] == 30
        assert node["city"] == "NYC"
        # Missing key
        with pytest.raises(KeyError):
            _ = node["email"]

    def test_nested_path_indexing(self):
        """Test nested path indexing."""
        node = XWNode.from_native({
            "users": [
                {"name": "Alice", "age": 30},
                {"name": "Bob", "age": 25}
            ]
        })
        # Nested path
        assert node["users.0.name"] == "Alice"
        assert node["users.1.age"] == 25
        # Get list first, then index
        users = node["users"]
        assert users[0]["name"] == "Alice"
        assert users[1]["name"] == "Bob"

    def test_mixed_indexing(self):
        """Test mixing different indexing types."""
        node = XWNode.from_native({
            "items": [
                {"id": 1, "value": "a"},
                {"id": 2, "value": "b"},
                {"id": 3, "value": "c"}
            ]
        })
        # Get list via string key
        items = node["items"]
        assert isinstance(items, list)
        # Then index with int
        first_item = items[0]
        assert first_item == {"id": 1, "value": "a"}
        # Slice the list
        first_two = items[0:2]
        assert first_two == [{"id": 1, "value": "a"}, {"id": 2, "value": "b"}]

    def test_indexing_error_cases(self):
        """Test error cases for indexing."""
        # Try to index dict with int
        node = XWNode.from_native({"name": "Alice"})
        with pytest.raises((TypeError, KeyError)):
            _ = node[0]
        # Try to slice dict
        with pytest.raises(TypeError):
            _ = node[0:2]
        # Try to index list with string (not a path)
        node = XWNode.from_native([1, 2, 3])
        with pytest.raises((KeyError, TypeError)):
            _ = node["not_a_path"]
