-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchats_messages_test.go
134 lines (111 loc) · 3.93 KB
/
chats_messages_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package coze
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestChatMessages(t *testing.T) {
t.Run("List messages success", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// 验证请求方法和路径
assert.Equal(t, http.MethodGet, req.Method)
assert.Equal(t, "/v3/chat/message/list", req.URL.Path)
// 验证查询参数
assert.Equal(t, "test_conversation_id", req.URL.Query().Get("conversation_id"))
assert.Equal(t, "test_chat_id", req.URL.Query().Get("chat_id"))
// 返回模拟响应
return mockResponse(http.StatusOK, &listChatsMessagesResp{
ListChatsMessagesResp: &ListChatsMessagesResp{
Messages: []*Message{
{
ID: "msg1",
ConversationID: "test_conversation_id",
Role: "user",
Content: "Hello",
},
{
ID: "msg2",
ConversationID: "test_conversation_id",
Role: "assistant",
Content: "Hi there!",
},
},
},
})
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
messages := newChatMessages(core)
resp, err := messages.List(context.Background(), &ListChatsMessagesReq{
ConversationID: "test_conversation_id",
ChatID: "test_chat_id",
})
require.NoError(t, err)
assert.Equal(t, "test_log_id", resp.LogID())
require.Len(t, resp.Messages, 2)
// 验证第一条消息
assert.Equal(t, "msg1", resp.Messages[0].ID)
assert.Equal(t, "test_conversation_id", resp.Messages[0].ConversationID)
assert.Equal(t, "user", resp.Messages[0].Role.String())
assert.Equal(t, "Hello", resp.Messages[0].Content)
// 验证第二条消息
assert.Equal(t, "msg2", resp.Messages[1].ID)
assert.Equal(t, "test_conversation_id", resp.Messages[1].ConversationID)
assert.Equal(t, "assistant", resp.Messages[1].Role.String())
assert.Equal(t, "Hi there!", resp.Messages[1].Content)
})
t.Run("List messages with error", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// 返回错误响应
return mockResponse(http.StatusBadRequest, &baseResponse{})
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
messages := newChatMessages(core)
resp, err := messages.List(context.Background(), &ListChatsMessagesReq{
ConversationID: "invalid_conversation_id",
ChatID: "invalid_chat_id",
})
require.Error(t, err)
assert.Nil(t, resp)
})
t.Run("List messages with empty response", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
return mockResponse(http.StatusOK, &listChatsMessagesResp{
ListChatsMessagesResp: &ListChatsMessagesResp{
Messages: []*Message{},
},
})
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
messages := newChatMessages(core)
resp, err := messages.List(context.Background(), &ListChatsMessagesReq{
ConversationID: "test_conversation_id",
ChatID: "test_chat_id",
})
require.NoError(t, err)
assert.Equal(t, "test_log_id", resp.LogID())
assert.Empty(t, resp.Messages)
})
t.Run("List messages with missing parameters", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// 验证缺失的参数
assert.Empty(t, req.URL.Query().Get("conversation_id"))
assert.Empty(t, req.URL.Query().Get("chat_id"))
return mockResponse(http.StatusBadRequest, &baseResponse{})
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
messages := newChatMessages(core)
resp, err := messages.List(context.Background(), &ListChatsMessagesReq{})
require.Error(t, err)
assert.Nil(t, resp)
})
}