Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 7194ca6

Browse files
LeVinhGithubHarry Le
andauthored
task: Expand e2e test for section "message" #2064
Co-authored-by: Harry Le <[email protected]>
1 parent b74e4d4 commit 7194ca6

File tree

6 files changed

+586
-0
lines changed

6 files changed

+586
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import pytest
2+
import requests
3+
from utils.test_runner import start_server, stop_server
4+
import jsonschema
5+
from utils.logger import log_response
6+
from utils.assertion import assert_equal
7+
8+
9+
class TestApiCreateMessage:
10+
11+
@pytest.fixture(autouse=True)
12+
def setup_and_teardown(self):
13+
# Setup
14+
success = start_server()
15+
if not success:
16+
raise Exception("Failed to start server")
17+
18+
yield
19+
20+
# Teardown
21+
stop_server()
22+
23+
def test_api_create_message_successfully(self):
24+
title = "New Thread"
25+
26+
data = {
27+
"metadata": {
28+
"title": title
29+
}
30+
}
31+
32+
post_thread_url = f"http://localhost:3928/v1/threads"
33+
response = requests.post(
34+
post_thread_url, json=data
35+
)
36+
json_data = response.json()
37+
log_response(json_data, "test_api_create_message_successfully")
38+
assert_equal(response.status_code,200)
39+
40+
thread_id = json_data["id"]
41+
42+
post_message_data = {
43+
"role": "user",
44+
"content": "Hello, world!"
45+
}
46+
47+
post_message_url = f"http://localhost:3928/v1/threads/{thread_id}/messages"
48+
new_message_response = requests.post(post_message_url, json=post_message_data)
49+
json_data_new_message = new_message_response.json()
50+
log_response(json_data_new_message, "test_api_create_message_successfully")
51+
assert_equal(new_message_response.status_code,200)
52+
53+
schema = {
54+
"type": "object",
55+
"properties": {
56+
"id": {
57+
"type": "string",
58+
"description": "Unique identifier for the message"
59+
},
60+
"object": {
61+
"type": "string",
62+
"description": "Type of object, always 'thread.message'"
63+
},
64+
"created_at": {
65+
"type": "integer",
66+
"description": "Unix timestamp of when the message was created"
67+
},
68+
"completed_at": {
69+
"type": "integer",
70+
"description": "Unix timestamp of when the message was completed"
71+
},
72+
"thread_id": {
73+
"type": "string",
74+
"description": "ID of the thread this message belongs to"
75+
},
76+
"role": {
77+
"type": "string",
78+
"description": "Role of the message sender",
79+
"enum": [
80+
"user",
81+
"assistant"
82+
]
83+
},
84+
"status": {
85+
"type": "string",
86+
"description": "Status of the message",
87+
"enum": [
88+
"completed"
89+
]
90+
},
91+
"content": {
92+
"type": "array",
93+
"items": {
94+
"type": "object",
95+
"properties": {
96+
"type": {
97+
"type": "string",
98+
"description": "Type of content",
99+
"enum": [
100+
"text"
101+
]
102+
},
103+
"text": {
104+
"type": "object",
105+
"properties": {
106+
"value": {
107+
"type": "string",
108+
"description": "The message text"
109+
},
110+
"annotations": {
111+
"type": "array",
112+
"description": "Array of annotations for the text"
113+
}
114+
}
115+
}
116+
}
117+
}
118+
},
119+
"metadata": {
120+
"type": "object",
121+
"description": "Additional metadata for the message"
122+
}
123+
},
124+
"required": [
125+
"id",
126+
"object",
127+
"created_at",
128+
"completed_at",
129+
"thread_id",
130+
"role",
131+
"status",
132+
"content"
133+
]
134+
}
135+
136+
# Validate response schema
137+
jsonschema.validate(instance=json_data_new_message, schema=schema)
138+
139+
assert_equal(json_data_new_message["content"][0]['text']["value"], "Hello, world!")
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import pytest
2+
import requests
3+
from utils.test_runner import start_server, stop_server
4+
import jsonschema
5+
from utils.logger import log_response
6+
from utils.assertion import assert_equal
7+
8+
9+
class TestApiDeleteMessage:
10+
11+
@pytest.fixture(autouse=True)
12+
def setup_and_teardown(self):
13+
# Setup
14+
success = start_server()
15+
if not success:
16+
raise Exception("Failed to start server")
17+
18+
yield
19+
20+
# Teardown
21+
stop_server()
22+
23+
def test_api_delete_message_successfully(self):
24+
title = "New Thread"
25+
26+
data = {
27+
"metadata": {
28+
"title": title
29+
}
30+
}
31+
# Create new thread
32+
post_thread_url = f"http://localhost:3928/v1/threads"
33+
response = requests.post(
34+
post_thread_url, json=data
35+
)
36+
json_data = response.json()
37+
log_response(json_data, "test_api_delete_message_successfully")
38+
assert_equal(response.status_code,200)
39+
40+
thread_id = json_data["id"]
41+
42+
post_message_data = {
43+
"role": "user",
44+
"content": "Hello, world!"
45+
}
46+
47+
# Create new message
48+
message_url = f"http://localhost:3928/v1/threads/{thread_id}/messages"
49+
new_message_response = requests.post(message_url, json=post_message_data)
50+
json_data_new_message = new_message_response.json()
51+
log_response(json_data_new_message, "test_api_delete_message_successfully")
52+
assert_equal(new_message_response.status_code,200)
53+
54+
message_id = json_data_new_message["id"]
55+
56+
# Delete message with id
57+
del_message_url = f"http://localhost:3928/v1/threads/{thread_id}/messages/{message_id}"
58+
del_message_response = requests.delete(del_message_url)
59+
json_data_del_message = del_message_response.json()
60+
log_response(json_data_del_message, "test_api_delete_message_successfully")
61+
assert_equal(del_message_response.status_code,200)
62+
63+
schema = {
64+
"type": "object",
65+
"properties": {
66+
"deleted": {
67+
"type": "boolean",
68+
"description": "Indicates if the message was successfully deleted"
69+
},
70+
"id": {
71+
"type": "string",
72+
"description": "ID of the deleted message"
73+
},
74+
"object": {
75+
"type": "string",
76+
"description": "Type of object, always 'thread.message.deleted'"
77+
}
78+
},
79+
"required": [
80+
"deleted",
81+
"id",
82+
"object"
83+
]
84+
}
85+
86+
# Validate response schema
87+
jsonschema.validate(instance=json_data_del_message, schema=schema)
88+
89+
assert_equal(json_data_del_message["deleted"], True)
90+
assert_equal(json_data_del_message["id"], message_id)
91+
assert_equal(json_data_del_message["object"], "thread.message.deleted")

0 commit comments

Comments
 (0)