1
1
import httpx
2
2
import pytest
3
3
4
-
5
4
from unittest .mock import AsyncMock
6
- from hive_agent_client .chat import send_chat_message
5
+ from hive_agent_client .chat import send_chat_message , get_chat_history
7
6
8
7
9
8
@pytest .mark .asyncio
@@ -15,25 +14,33 @@ async def test_send_chat_message_success():
15
14
mock_client .post .return_value = mock_response
16
15
17
16
base_url = "http://example.com/api/v1"
17
+ user_id = "user123"
18
+ session_id = "session123"
18
19
content = "Hello, how are you?"
19
20
20
- result = await send_chat_message (mock_client , base_url , content )
21
+ result = await send_chat_message (mock_client , base_url , user_id , session_id , content )
21
22
22
23
assert result == "Hello, world!"
23
24
mock_client .post .assert_called_once_with (
24
25
"http://example.com/api/v1/chat" ,
25
- json = {"messages" : [{"role" : "user" , "content" : content }]},
26
+ json = {
27
+ "user_id" : user_id ,
28
+ "session_id" : session_id ,
29
+ "chat_data" : {"messages" : [{"role" : "user" , "content" : content }]}
30
+ },
26
31
)
27
32
28
33
29
34
@pytest .mark .asyncio
30
35
async def test_send_chat_message_empty_content ():
31
36
mock_client = AsyncMock (spec = httpx .AsyncClient )
32
37
base_url = "http://example.com"
38
+ user_id = "user123"
39
+ session_id = "session123"
33
40
content = ""
34
41
35
42
with pytest .raises (ValueError , match = "Content must not be empty" ):
36
- await send_chat_message (mock_client , base_url , content )
43
+ await send_chat_message (mock_client , base_url , user_id , session_id , content )
37
44
38
45
39
46
@pytest .mark .asyncio
@@ -42,34 +49,67 @@ async def test_send_chat_message_http_error():
42
49
mock_response = AsyncMock (spec = httpx .Response )
43
50
mock_response .status_code = 400
44
51
mock_response .text = "Bad request"
52
+ mock_response .raise_for_status .side_effect = httpx .HTTPStatusError (
53
+ message = "Bad request" , request = mock_response .request , response = mock_response
54
+ )
45
55
mock_client .post .return_value = mock_response
46
56
47
57
base_url = "http://example.com"
58
+ user_id = "user123"
59
+ session_id = "session123"
48
60
content = "Hello, how are you?"
49
61
50
62
with pytest .raises (
51
- Exception ,
52
- match = "HTTP error occurred when sending message to the chat API: 400 - Bad request" ,
63
+ Exception ,
64
+ match = "HTTP error occurred when sending message to the chat API: 400 - Bad request" ,
53
65
):
54
- await send_chat_message (mock_client , base_url , content )
66
+ await send_chat_message (mock_client , base_url , user_id , session_id , content )
55
67
56
68
57
69
@pytest .mark .asyncio
58
- async def test_send_chat_message_http_error ():
70
+ async def test_get_chat_history_success ():
71
+ mock_client = AsyncMock (spec = httpx .AsyncClient )
72
+ mock_response = AsyncMock (spec = httpx .Response )
73
+ mock_response .status_code = 200
74
+ expected_history = [
75
+ {"user_id" : "user123" , "session_id" : "session123" , "message" : "Hello" , "role" : "user" ,
76
+ "timestamp" : "2023-01-01T00:00:00Z" },
77
+ {"user_id" : "user123" , "session_id" : "session123" , "message" : "Hi there" , "role" : "assistant" ,
78
+ "timestamp" : "2023-01-01T00:00:01Z" }
79
+ ]
80
+ mock_response .json .return_value = expected_history
81
+ mock_client .get .return_value = mock_response
82
+
83
+ base_url = "http://example.com/api/v1"
84
+ user_id = "user123"
85
+ session_id = "session123"
86
+
87
+ result = await get_chat_history (mock_client , base_url , user_id , session_id )
88
+
89
+ assert result == expected_history
90
+ mock_client .get .assert_called_once_with (
91
+ f"http://example.com/api/v1/chat_history" ,
92
+ params = {"user_id" : user_id , "session_id" : session_id },
93
+ )
94
+
95
+
96
+ @pytest .mark .asyncio
97
+ async def test_get_chat_history_failure ():
59
98
mock_client = AsyncMock (spec = httpx .AsyncClient )
60
99
mock_response = AsyncMock (spec = httpx .Response )
61
100
mock_response .status_code = 400
62
101
mock_response .text = "Bad request"
63
102
mock_response .raise_for_status .side_effect = httpx .HTTPStatusError (
64
103
message = "Bad request" , request = mock_response .request , response = mock_response
65
104
)
66
- mock_client .post .return_value = mock_response
105
+ mock_client .get .return_value = mock_response
67
106
68
- base_url = "http://example.com"
69
- content = "Hello, how are you?"
107
+ base_url = "http://example.com/api/v1"
108
+ user_id = "user123"
109
+ session_id = "session123"
70
110
71
111
with pytest .raises (
72
- Exception ,
73
- match = "HTTP error occurred when sending message to the chat API: 400 - Bad request" ,
112
+ Exception ,
113
+ match = "HTTP error occurred when fetching chat history from the chat API: 400 - Bad request" ,
74
114
):
75
- await send_chat_message (mock_client , base_url , content )
115
+ await get_chat_history (mock_client , base_url , user_id , session_id )
0 commit comments