import pytest
from src.reflection import resolve_variable, resolve_class
from langchain.chat_models import BaseChatModel
def test_resolve_variable_success():
func = resolve_variable("os.path:join")
assert callable(func)
assert func("a", "b") == "a/b" # or "a\\b" on Windows
def test_resolve_variable_with_type():
from langchain_core.tools import BaseTool
from src.sandbox.tools import bash_tool
tool = resolve_variable(
"src.sandbox.tools:bash_tool",
expected_type=BaseTool
)
assert tool == bash_tool
def test_resolve_class_success():
from langchain_openai import ChatOpenAI
model_class = resolve_class(
"langchain_openai:ChatOpenAI",
base_class=BaseChatModel
)
assert model_class == ChatOpenAI
assert issubclass(model_class, BaseChatModel)
def test_resolve_missing_module():
with pytest.raises(ImportError, match="Install it with `uv add"):
resolve_variable("nonexistent.module:variable")
def test_resolve_missing_attribute():
with pytest.raises(ImportError, match="does not define"):
resolve_variable("os:nonexistent_function")
def test_resolve_wrong_type():
with pytest.raises(ValueError, match="not an instance of"):
resolve_variable(
"langchain_openai:ChatOpenAI",
expected_type=int # ChatOpenAI is a class, not an int
)
def test_resolve_class_not_subclass():
from langchain_core.tools import BaseTool
with pytest.raises(ValueError, match="not a subclass of"):
resolve_class(
"langchain_core.tools:BaseTool",
base_class=BaseChatModel # BaseTool is not a chat model
)