-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathworkflows_chat_test.go
120 lines (101 loc) · 3.03 KB
/
workflows_chat_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
package coze
import (
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWorkflowsChat(t *testing.T) {
t.Run("Stream chat success", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// Verify request method and path
assert.Equal(t, http.MethodPost, req.Method)
assert.Equal(t, "/v1/workflows/chat", req.URL.Path)
// Return mock response with chat events
events := []string{
`event: conversation.chat.created
data: {"id":"chat1","conversation_id":"test_conversation_id","bot_id":"bot1","status":"created"}
event: conversation.message.delta
data: {"id":"msg1","conversation_id":"test_conversation_id","role":"assistant","content":"Hello"}
event: done
data: {}
`,
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(strings.Join(events, "\n"))),
Header: make(http.Header),
}, nil
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
chat := newWorkflowsChat(core)
// Create test request
req := &WorkflowsChatStreamReq{
WorkflowID: "test_workflow",
AdditionalMessages: []*Message{
{
Role: MessageRoleUser,
Content: "Hello",
},
},
Parameters: map[string]any{
"test": "value",
},
}
// Test streaming
stream, err := chat.Stream(context.Background(), req)
require.NoError(t, err)
defer stream.Close()
// Verify first event
event1, err := stream.Recv()
require.NoError(t, err)
assert.Equal(t, ChatEventConversationChatCreated, event1.Event)
// Verify second event
event2, err := stream.Recv()
require.NoError(t, err)
assert.Equal(t, ChatEventConversationMessageDelta, event2.Event)
// Verify completion event
event3, err := stream.Recv()
require.NoError(t, err)
assert.Equal(t, ChatEventDone, event3.Event)
// Verify stream end
_, err = stream.Recv()
assert.Equal(t, io.EOF, err)
})
t.Run("Stream chat with error response", func(t *testing.T) {
mockTransport := &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// Return error response
mockResp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(`{
"code": 100,
"msg": "Invalid workflow ID"
}`)),
Header: make(http.Header),
}
mockResp.Header.Set("Content-Type", "application/json")
mockResp.Header.Set("X-Tt-Logid", "test_log_id")
return mockResp, nil
},
}
core := newCore(&http.Client{Transport: mockTransport}, ComBaseURL)
chat := newWorkflowsChat(core)
req := &WorkflowsChatStreamReq{
WorkflowID: "invalid_workflow",
}
_, err := chat.Stream(context.Background(), req)
require.Error(t, err)
// Verify error details
cozeErr, ok := AsCozeError(err)
require.True(t, ok)
assert.Equal(t, 100, cozeErr.Code)
assert.Equal(t, "Invalid workflow ID", cozeErr.Message)
assert.Equal(t, "test_log_id", cozeErr.LogID)
})
}