"""
Tests for token ingestion from various sources.
"""

import pytest
import json
from tools.ingest.css import CSSTokenSource
from tools.ingest.scss import SCSSTokenSource
from tools.ingest.json_tokens import JSONTokenSource


@pytest.mark.asyncio
async def test_css_ingestion(sample_css):
    """Test CSS custom property extraction."""
    parser = CSSTokenSource()
    result = await parser.extract(sample_css)

    assert len(result.tokens) >= 5
    assert any(t.name == "color.primary" for t in result.tokens)
    assert any(t.value == "#3B82F6" for t in result.tokens)


@pytest.mark.asyncio
async def test_scss_ingestion(sample_scss):
    """Test SCSS variable extraction."""
    parser = SCSSTokenSource()
    result = await parser.extract(sample_scss)

    assert len(result.tokens) >= 4
    # SCSS converts $primary-color to primary.color (dashes to dots)
    assert any(t.name == "primary.color" for t in result.tokens)


@pytest.mark.asyncio
async def test_json_ingestion(sample_json_tokens):
    """Test JSON token extraction (W3C format)."""
    parser = JSONTokenSource()
    result = await parser.extract(json.dumps(sample_json_tokens))

    assert len(result.tokens) >= 6
    # Check color tokens
    primary_tokens = [t for t in result.tokens if "primary" in t.name]
    assert len(primary_tokens) >= 2

    # Check spacing tokens
    spacing_tokens = [t for t in result.tokens if "spacing" in t.name]
    assert len(spacing_tokens) == 3


@pytest.mark.asyncio
async def test_empty_css():
    """Test handling of empty CSS."""
    parser = CSSTokenSource()
    # Use CSS syntax marker so parser detects as content, not file path
    result = await parser.extract(":root {}")

    assert len(result.tokens) == 0
    assert result.name


@pytest.mark.asyncio
async def test_invalid_json():
    """Test handling of invalid JSON."""
    parser = JSONTokenSource()

    # Parser wraps JSONDecodeError in ValueError
    with pytest.raises(ValueError, match="Invalid JSON"):
        await parser.extract("invalid json{")
