import pytest
from slack_bot.tools.registry import TOOLS_SCHEMA, TOOL_FUNCTIONS

def test_tools_schema_structure():
    """Verify that TOOLS_SCHEMA is a valid list of OpenAI-style tool definitions."""
    assert isinstance(TOOLS_SCHEMA, list)
    assert len(TOOLS_SCHEMA) > 0
    
    for tool in TOOLS_SCHEMA:
        assert "type" in tool
        assert tool["type"] == "function"
        assert "function" in tool
        fn = tool["function"]
        assert "name" in fn
        assert "description" in fn
        assert "parameters" in fn

def test_registry_consistency():
    """Verify that every tool in the schema has a corresponding function implementation."""
    schema_names = {t["function"]["name"] for t in TOOLS_SCHEMA}
    impl_names = set(TOOL_FUNCTIONS.keys())
    
    # Check for missing implementations
    missing_impl = schema_names - impl_names
    assert not missing_impl, f"Tools defined in schema but missing implementation: {missing_impl}"
    
    # Check for extra implementations (optional, but good to know)
    extra_impl = impl_names - schema_names
    if extra_impl:
        print(f"Warning: Functions implemented but not exposed in schema: {extra_impl}")

def test_tool_parameters():
    """Spot check critical tools for correct parameter structure."""
    log_diet = next(t for t in TOOLS_SCHEMA if t["function"]["name"] == "log_diet")
    props = log_diet["function"]["parameters"]["properties"]
    assert "target_date" in props
    assert "description" in props
    assert "meal_type" in props
    
    required = log_diet["function"]["parameters"]["required"]
    assert "target_date" in required
    assert "description" in required
