From ef5a7d0c73f2bfcc3cf3526ef5df33b3c2beb1cf Mon Sep 17 00:00:00 2001 From: 100x <119676922+Oyintarede@users.noreply.github.com> Date: Tue, 30 Jun 2026 07:59:03 +0000 Subject: [PATCH 1/2] Test:Unit tests for POST /chat --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 4897243..4c4d893 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ A decentralized, chat-first platform that combines **real-time messaging, token payments, and community-driven funding** into a single seamless experience. + + This project reimagines how people coordinate, transact, and build together online by embedding financial actions directly into conversations. Users can send tokens as easily as messages, contribute to shared group treasuries, and fund ideas through lightweight, on-chain proposals—all without leaving the chat interface. Built on blockchain infrastructure and modern messaging protocols, the platform bridges the gap between Web2 usability and Web3 ownership. From 24936ef11e82483129f662befccd28450c95454e Mon Sep 17 00:00:00 2001 From: 100x <119676922+Oyintarede@users.noreply.github.com> Date: Thu, 2 Jul 2026 10:35:45 +0000 Subject: [PATCH 2/2] Fix OpenAI client patching for chat tests --- apps/ai_agent/main.py | 8 +++- apps/ai_agent/tests/test_chat.py | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 apps/ai_agent/tests/test_chat.py diff --git a/apps/ai_agent/main.py b/apps/ai_agent/main.py index e11a925..2c48189 100644 --- a/apps/ai_agent/main.py +++ b/apps/ai_agent/main.py @@ -9,6 +9,11 @@ import weaviate from weaviate.classes.query import Filter +try: + from openai import OpenAI +except ImportError: # pragma: no cover - exercised when the dependency is absent + OpenAI = None + app = FastAPI(title="AI Agent API") _SYSTEM_PROMPT = ( @@ -73,7 +78,8 @@ def _openai_client(): api_key = os.environ.get("OPENAI_API_KEY") if not api_key: raise HTTPException(status_code=500, detail="OPENAI_API_KEY is not configured") - from openai import OpenAI # imported lazily so missing package gives a clear error + if OpenAI is None: + raise HTTPException(status_code=500, detail="OpenAI client is unavailable") return OpenAI(api_key=api_key) diff --git a/apps/ai_agent/tests/test_chat.py b/apps/ai_agent/tests/test_chat.py new file mode 100644 index 0000000..37054a0 --- /dev/null +++ b/apps/ai_agent/tests/test_chat.py @@ -0,0 +1,77 @@ +"""Unit tests for POST /chat (issue #144).""" + +import pytest +from unittest.mock import MagicMock + +_BASE_BODY = { + "message": "What is Clicked?", + "conversation_id": "conv-123", +} + + +def _fake_chat_reply(content: str = "This is a test reply."): + msg = MagicMock() + msg.content = content + choice = MagicMock() + choice.message = msg + resp = MagicMock() + resp.choices = [choice] + return resp + + +def test_missing_api_key_returns_500(monkeypatch, client): + monkeypatch.delenv("OPENAI_API_KEY", raising=False) + response = client.post("/chat", json=_BASE_BODY) + assert response.status_code == 500 + + +def test_valid_request_returns_reply(mock_openai, client): + mock_client = mock_openai.return_value + mock_client.chat.completions.create.return_value = _fake_chat_reply( + "Hello from Clicked AI!" + ) + + response = client.post("/chat", json=_BASE_BODY) + assert response.status_code == 200 + data = response.json() + assert "reply" in data + assert data["reply"] == "Hello from Clicked AI!" + + +def test_system_prompt_contains_context(mock_openai, client): + mock_client = mock_openai.return_value + mock_client.chat.completions.create.return_value = _fake_chat_reply("Sure, I can help!") + + response = client.post("/chat", json=_BASE_BODY) + assert response.status_code == 200 + + call_args = mock_client.chat.completions.create.call_args + messages = call_args[1]["messages"] + system_msg = messages[0] + assert system_msg["role"] == "system" + content = system_msg["content"] + assert "Clicked" in content + assert "Stellar" in content + assert "messaging" in content or "payment" in content + + +def test_missing_message_returns_422(client): + body = {"conversation_id": "conv-123"} + response = client.post("/chat", json=body) + assert response.status_code == 422 + + +def test_missing_conversation_id_returns_422(client): + body = {"message": "Hello"} + response = client.post("/chat", json=body) + assert response.status_code == 422 + + +def test_correct_model_used(mock_openai, client): + mock_client = mock_openai.return_value + mock_client.chat.completions.create.return_value = _fake_chat_reply("OK") + + client.post("/chat", json=_BASE_BODY) + + call_args = mock_client.chat.completions.create.call_args + assert call_args[1]["model"] == "gpt-4o-mini"