refactor(chat_manager.py): move process_graph function outside of ChatManager class
test(websocket.py): add tests for websocket connection, chat history, and sending messages
This commit is contained in:
parent
7d183ff57e
commit
0a630cd70d
2 changed files with 74 additions and 35 deletions
|
|
@ -1,30 +1,51 @@
|
|||
import json
|
||||
from unittest.mock import patch
|
||||
from langflow.api.schemas import ChatMessage
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
def test_websocket_connection(client):
|
||||
with client.websocket_connect("/ws") as websocket:
|
||||
assert websocket.client == client
|
||||
assert websocket.url.path == "/ws"
|
||||
def test_websocket_connection(client: TestClient):
|
||||
with client.websocket_connect("/ws/test_client") as websocket:
|
||||
assert websocket.scope["client"] == ["testclient", 50000]
|
||||
assert websocket.scope["path"] == "/ws/test_client"
|
||||
|
||||
|
||||
def test_chat_history(client):
|
||||
chat_history = ["Test message 1", "Test message 2"]
|
||||
def test_chat_history(client: TestClient):
|
||||
chat_history = []
|
||||
|
||||
with client.websocket_connect("/ws") as websocket:
|
||||
received_history = websocket.receive_text()
|
||||
received_history = json.loads(received_history)
|
||||
# Mock the process_graph function to return a specific value
|
||||
with patch("langflow.api.chat_manager.process_graph") as mock_process_graph:
|
||||
mock_process_graph.return_value = ("Hello, I'm a mock response!", "")
|
||||
|
||||
assert received_history == chat_history
|
||||
with client.websocket_connect("/ws/test_client") as websocket:
|
||||
# First message should be the history
|
||||
history = websocket.receive_json()
|
||||
assert json.loads(history) == [] # Empty history
|
||||
# Send a message
|
||||
payload = {"message": "Hello"}
|
||||
websocket.send_json(json.dumps(payload))
|
||||
|
||||
# Receive the response from the server
|
||||
response = websocket.receive_json()
|
||||
assert json.loads(response) == {
|
||||
"sender": "bot",
|
||||
"message": None,
|
||||
"intermediate_steps": "",
|
||||
"type": "start",
|
||||
"data": None,
|
||||
"data_type": "",
|
||||
}
|
||||
# Send another message
|
||||
payload = {"message": "How are you?"}
|
||||
websocket.send_json(json.dumps(payload))
|
||||
|
||||
def test_send_message(client, basic_graph):
|
||||
with client.websocket_connect("/ws") as websocket:
|
||||
# Send the JSON payload through the WebSocket connection
|
||||
websocket.send_text(basic_graph)
|
||||
|
||||
# Receive and parse the response from the server
|
||||
response = websocket.receive_text()
|
||||
response = json.loads(response)
|
||||
|
||||
# Test that the response is as expected
|
||||
assert response == "Your response message here"
|
||||
# Receive the response from the server
|
||||
response = websocket.receive_json()
|
||||
assert json.loads(response) == {
|
||||
"sender": "bot",
|
||||
"message": "Hello, I'm a mock response!",
|
||||
"intermediate_steps": "",
|
||||
"type": "end",
|
||||
"data": None,
|
||||
"data_type": "",
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue