mv experimental apps

This commit is contained in:
Ramnique Singh 2025-04-07 23:53:17 +05:30
parent 7f6ece90f8
commit f722591ccd
53 changed files with 31 additions and 31 deletions

View file

@ -0,0 +1,95 @@
# tests/test_app.py
import json
import pytest
from tools_webhook.app import app # If "sidecar" is recognized as a package
@pytest.fixture
def client():
"""
A pytest fixture that provides a Flask test client.
The `app.test_client()` allows us to make requests to our Flask app
without running the server.
"""
with app.test_client() as client:
yield client
def test_tool_call_greet(client):
# This matches the structure of the request in our code:
# {
# "content": "...a JSON string..."
# }
# The content we pass is another JSON, so we have to double-escape quotes.
request_data = {
"content": json.dumps({
"toolCall": {
"function": {
"name": "greet",
"arguments": json.dumps({
"name": "Alice",
"message": "Hello"
})
}
}
})
}
response = client.post(
"/tool_call",
data=json.dumps(request_data),
content_type="application/json"
)
assert response.status_code == 200
data = response.get_json()
assert data["result"] == "Hello, Alice!"
def test_tool_call_missing_params(client):
request_data = {
"content": json.dumps({
"toolCall": {
"function": {
"name": "greet",
"arguments": json.dumps({
"name": "Alice"
# Missing "message"
})
}
}
})
}
response = client.post(
"/tool_call",
data=json.dumps(request_data),
content_type="application/json"
)
assert response.status_code == 400
data = response.get_json()
assert "Missing required parameter: message" in data["error"]
def test_tool_call_invalid_func(client):
request_data = {
"content": json.dumps({
"toolCall": {
"function": {
"name": "does_not_exist",
"arguments": json.dumps({})
}
}
})
}
response = client.post(
"/tool_call",
data=json.dumps(request_data),
content_type="application/json"
)
assert response.status_code == 400
data = response.get_json()
assert "Function 'does_not_exist' not found" in data["error"]

View file

@ -0,0 +1,40 @@
# tests/test_tool_caller.py
import pytest
from tools_webhook.tool_caller import call_tool
from tools_webhook.function_map import FUNCTIONS_MAP
def test_call_tool_greet():
# Normal case
result = call_tool("greet", {"name": "Alice", "message": "Hello"}, FUNCTIONS_MAP)
assert result == "Hello, Alice!"
def test_call_tool_add():
# Normal case
result = call_tool("add", {"a": 2, "b": 5}, FUNCTIONS_MAP)
assert result == 7
def test_call_tool_missing_func():
# Should raise ValueError if function is not in FUNCTIONS_MAP
with pytest.raises(ValueError) as exc_info:
call_tool("non_existent_func", {}, FUNCTIONS_MAP)
assert "Function 'non_existent_func' not found" in str(exc_info.value)
def test_call_tool_missing_param():
# greet requires `name` and `message`
with pytest.raises(ValueError) as exc_info:
call_tool("greet", {"name": "Alice"}, FUNCTIONS_MAP)
assert "Missing required parameter: message" in str(exc_info.value)
def test_call_tool_unexpected_param():
# `greet` only expects name and message
with pytest.raises(ValueError) as exc_info:
call_tool("greet", {"name": "Alice", "message": "Hello", "extra": "???"},
FUNCTIONS_MAP)
assert "Unexpected parameter: extra" in str(exc_info.value)
def test_call_tool_type_conversion_error():
# `add` expects integers `a` and `b`, so passing a string should fail
with pytest.raises(ValueError) as exc_info:
call_tool("add", {"a": "not_an_int", "b": 3}, FUNCTIONS_MAP)
assert "Parameter 'a' must be of type int" in str(exc_info.value)