This repository has been archived on 2026-03-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Timmy-time-dashboard/tests/timmy/test_timmy_serve_app.py
Claude 4e11dd2490 refactor: Phase 3 — reorganize tests into module-mirroring subdirectories
Move 97 test files from flat tests/ into 13 subdirectories:
  tests/dashboard/   (8 files — routes, mobile, mission control)
  tests/swarm/       (17 files — coordinator, docker, routing, tasks)
  tests/timmy/       (12 files — agent, backends, CLI, tools)
  tests/self_coding/  (14 files — git safety, indexer, self-modify)
  tests/lightning/   (3 files — L402, LND, interface)
  tests/creative/    (8 files — assembler, director, image/music/video)
  tests/integrations/ (10 files — chat bridge, telegram, voice, websocket)
  tests/mcp/         (4 files — bootstrap, discovery, executor)
  tests/spark/       (3 files — engine, tools, events)
  tests/hands/       (3 files — registry, oracle, phase5)
  tests/scripture/   (1 file)
  tests/infrastructure/ (3 files — router cascade, API)
  tests/security/    (3 files — XSS, regression)

Fix Path(__file__) reference in test_mobile_scenarios.py for new depth.
Add __init__.py to all test subdirectories.

Tests: 1503 passed, 9 failed (pre-existing), 53 errors (pre-existing)

https://claude.ai/code/session_019oMFNvD8uSGSSmBMGkBfQN
2026-02-26 21:21:28 +00:00

98 lines
3.5 KiB
Python

"""Tests for timmy_serve/app.py — Serve FastAPI app and endpoints."""
from unittest.mock import MagicMock, patch
import pytest
from fastapi.testclient import TestClient
@pytest.fixture
def serve_client():
"""Create a TestClient for the timmy-serve app."""
from timmy_serve.app import create_timmy_serve_app
app = create_timmy_serve_app(price_sats=100)
return TestClient(app)
class TestHealthEndpoint:
def test_health_returns_ok(self, serve_client):
resp = serve_client.get("/health")
assert resp.status_code == 200
data = resp.json()
assert data["status"] == "healthy"
assert data["service"] == "timmy-serve"
class TestServeStatus:
def test_status_returns_pricing(self, serve_client):
resp = serve_client.get("/serve/status")
assert resp.status_code == 200
data = resp.json()
assert data["price_sats"] == 100
assert "total_invoices" in data
assert "total_earned_sats" in data
class TestServeChatEndpoint:
"""Regression tests for /serve/chat.
The original implementation declared ``async def serve_chat(request: ChatRequest)``
which shadowed FastAPI's ``Request`` object. Calling ``request.headers`` on a
Pydantic model raised ``AttributeError``. The fix splits the parameters into
``request: Request`` (FastAPI) and ``body: ChatRequest`` (Pydantic).
"""
def test_chat_without_auth_returns_402(self, serve_client):
"""Unauthenticated request should get a 402 challenge."""
resp = serve_client.post(
"/serve/chat",
json={"message": "Hello"},
)
assert resp.status_code == 402
data = resp.json()
assert data["error"] == "Payment Required"
assert "macaroon" in data
assert "invoice" in data
@patch("timmy_serve.app.create_timmy")
@patch("timmy_serve.app.verify_l402_token", return_value=True)
def test_chat_with_valid_l402_token(self, mock_verify, mock_create, serve_client):
"""Authenticated request should reach the chat handler without AttributeError."""
mock_agent = MagicMock()
mock_result = MagicMock()
mock_result.content = "I am Timmy."
mock_agent.run.return_value = mock_result
mock_create.return_value = mock_agent
resp = serve_client.post(
"/serve/chat",
json={"message": "Who are you?"},
headers={"Authorization": "L402 fake-macaroon:fake-preimage"},
)
# The key assertion: we must NOT get a 500 from AttributeError
assert resp.status_code == 200
data = resp.json()
assert data["response"] == "I am Timmy."
mock_agent.run.assert_called_once_with("Who are you?", stream=False)
@patch("timmy_serve.app.create_timmy")
@patch("timmy_serve.app.verify_l402_token", return_value=True)
def test_chat_reads_auth_header_from_request(
self, mock_verify, mock_create, serve_client
):
"""Ensure auth header is read from the HTTP Request, not the JSON body."""
mock_agent = MagicMock()
mock_result = MagicMock()
mock_result.content = "ok"
mock_agent.run.return_value = mock_result
mock_create.return_value = mock_agent
resp = serve_client.post(
"/serve/chat",
json={"message": "test"},
headers={"Authorization": "L402 abc:def"},
)
assert resp.status_code == 200
# Should not raise AttributeError on request.headers