arcade-mcp/libs/tests/worker/test_worker_fastapi.py
Sam Partee b6b4cd0a4c
🏗️ Restructure: Multi-Package Architecture + uv Migration (#412)
### Overview
Major restructuring from monolithic `arcade-ai` package to modular
library architecture with standardized uv-based dependency management.

![arcade-ai Monorepo
(2)](https://github.com/user-attachments/assets/25f102b0-bb87-4a04-9701-d227d05664b1)

### New Package Structure
- **`arcade-tdk`** - Lightweight toolkit development kit (core
decorators, auth)
- **`arcade-core`** - Core execution engine and catalog functionality  
- **`arcade-serve`** - FastAPI/MCP server components
- **`arcade-ai`** - Meta package that includes CLI functionality.
Optionally include evals via the `evals` extra. Optionally include all
packages via the `all` extra.

### Key Benefits
- **Lighter Dependencies**: Toolkits now depend only on `arcade-tdk` (~2
deps) vs full `arcade-ai` (~30+ deps)
- **Faster Builds**: uv provides 10-100x faster dependency resolution
and installation
- **Better Modularity**: Clear separation of concerns, consumers import
only what they need
- **Standard Tooling**: Eliminates custom poetry scripts, uses standard
Python packaging

### Migration Impact
- All 20 toolkits converted from poetry → uv with `arcade-tdk`
dependencies plus `arcade-ai[evals]` and `arcade-serve` dev
dependencies. When developing locally, devs should install toolkits via
`make install-local`.
- Modern Python 3.10+ type hints throughout
- Standardized build system with hatchling backend
- Enhanced Makefile with robust toolkit management commands
- Removed `arcade dev` CLI command
- Reduce the number of files created by `arcade new` and add an option
to not generate a tests and evals folder.

This foundation enables faster development cycles and cleaner dependency
chains for the growing toolkit ecosystem.

### Todo After this PR is merged
- [ ] Post-merge workflow(s) (release & publish containers, etc)
- [ ] Release order plan. @EricGustin suggests releasing in the
following order:
    1. `arcade-core` version 0.1.0
    2. `arcade-serve` version 0.1.0 and `arcade-tdk` version 0.1.0
    3. `arcade-ai` version 2.0.0
4. Patch release for all toolkits (all changes in toolkits are internal
refactors)
- [ ] [Update docs](https://github.com/ArcadeAI/docs/pull/318)

---------

Co-authored-by: Eric Gustin <eric@arcade.dev>
Co-authored-by: Eric Gustin <34000337+EricGustin@users.noreply.github.com>
2025-06-11 16:48:17 -07:00

166 lines
5.3 KiB
Python

from typing import Annotated
import pytest
from arcade_core.schema import ToolCallRequest, ToolContext, ToolReference
from arcade_serve.fastapi.worker import FastAPIWorker
from arcade_tdk import tool
from fastapi import FastAPI
from fastapi.testclient import TestClient
@tool()
def sample_tool_fastapi(
context: ToolContext, x: Annotated[int, "x"], y: Annotated[str, "y"]
) -> Annotated[str, "output"]:
"""A sample tool for FastAPI tests."""
return f"{y}-{x}"
# Define tool at module level to avoid indentation issues with getsource
@tool()
def error_throwing_tool(
context: ToolContext,
a: Annotated[int, "a", "Input integer a"], # Added description for parameter
) -> int:
"""This tool throws a ValueError.""" # Added description for tool
raise ValueError("Test execution error")
@pytest.fixture
def test_app():
return FastAPI()
@pytest.fixture
def worker_secret():
return "test-secret-fastapi"
@pytest.fixture
def fastapi_worker(test_app, worker_secret):
worker = FastAPIWorker(app=test_app, secret=worker_secret)
worker.register_tool(sample_tool_fastapi, toolkit_name="fastapi_kit")
return worker
@pytest.fixture
def fastapi_worker_no_auth(test_app):
worker = FastAPIWorker(app=test_app, disable_auth=True)
worker.register_tool(sample_tool_fastapi, toolkit_name="fastapi_kit")
return worker
@pytest.fixture
def client(test_app, fastapi_worker): # Use the worker fixture to ensure routes are registered
return TestClient(test_app)
@pytest.fixture
def client_no_auth(test_app, fastapi_worker_no_auth):
return TestClient(test_app)
# --- FastAPIWorker Tests ---
def test_fastapi_worker_registers_routes(client, fastapi_worker):
# Check if routes exist by trying to access them (even if auth fails)
response = client.get(f"{fastapi_worker.base_path}/health")
assert response.status_code != 404 # Should be 200
response = client.get(f"{fastapi_worker.base_path}/tools")
assert response.status_code != 404 # Should be 403 without auth
# Prepare a dummy request body for invoke
tool_ref = ToolReference(toolkit="FastapiKit", name="SampleToolFastapi")
request_body = ToolCallRequest(
execution_id="test", tool=tool_ref, inputs={"x": 1, "y": "test"}
).model_dump()
response = client.post(f"{fastapi_worker.base_path}/tools/invoke", json=request_body)
assert response.status_code != 404 # Should be 403 without auth
# --- Route Tests (using TestClient) ---
# Health Check
def test_health_check_route(client, worker_secret):
response = client.get("/worker/health")
assert response.status_code == 200
assert response.json() == {"status": "ok", "tool_count": "1"}
def test_health_check_route_no_auth(client_no_auth):
response = client_no_auth.get("/worker/health")
assert response.status_code == 200
assert response.json() == {"status": "ok", "tool_count": "1"}
# Catalog
def test_get_catalog_route_no_auth_header(client):
response = client.get("/worker/tools")
assert response.status_code == 403
assert "Not authenticated" in response.text
def test_get_catalog_route_invalid_auth_header(client, worker_secret):
response = client.get("/worker/tools", headers={"Authorization": "Bearer invalid-token"})
assert response.status_code == 401 # Unauthorized
# Updated expected error message based on last run
assert "Invalid token. Error: Not enough segments" in response.text
def test_get_catalog_route_no_auth_worker(client_no_auth):
response = client_no_auth.get("/worker/tools")
assert response.status_code == 200
catalog = response.json()
assert isinstance(catalog, list)
assert len(catalog) == 1
assert catalog[0]["name"] == "SampleToolFastapi"
# Call Tool
@pytest.fixture
def call_tool_payload():
tool_ref = ToolReference(toolkit="FastapiKit", name="SampleToolFastapi")
return ToolCallRequest(
execution_id="fastapi-test-exec", tool=tool_ref, inputs={"x": 123, "y": "hello"}
).model_dump()
def test_call_tool_route_no_auth_header(client, call_tool_payload):
response = client.post("/worker/tools/invoke", json=call_tool_payload)
assert response.status_code == 403
def test_call_tool_route_invalid_auth_header(client, worker_secret, call_tool_payload):
response = client.post(
"/worker/tools/invoke",
json=call_tool_payload,
headers={"Authorization": "Bearer invalid-token"},
)
assert response.status_code == 401
def test_call_tool_route_no_auth_worker(client_no_auth, call_tool_payload):
response = client_no_auth.post("/worker/tools/invoke", json=call_tool_payload)
assert response.status_code == 200
result = response.json()
assert result["success"] is True
assert result["output"]["value"] == "hello-123"
def test_call_tool_route_tool_not_found(client_no_auth, call_tool_payload):
call_tool_payload["tool"]["name"] = "NonExistentTool"
call_tool_payload["tool"]["toolkit"] = "FastapiKit"
with pytest.raises(ValueError):
_ = client_no_auth.post(
"/worker/tools/invoke",
json=call_tool_payload,
)
# The handler catches the ValueError and returns a 500 internal server error
# Ideally, this might be a 404 or 400, but BaseWorker.call_tool raises ValueError
# which isn't automatically mapped to a 4xx by FastAPI unless handled explicitly.
# TODO fix this.