-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstream_reader_test.go
173 lines (151 loc) · 3.98 KB
/
stream_reader_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package coze
import (
"bufio"
"bytes"
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func mockHTTPResponse() *httpResponse {
header := http.Header{}
header.Set(logIDHeader, "test_log_id")
return &httpResponse{
Header: header,
}
}
// Mock event processor for testing
func mockEventProcessor(line []byte, reader *bufio.Reader) (*WorkflowEvent, bool, error) {
if len(line) == 0 {
return nil, false, nil
}
// Parse event data
event := &WorkflowEvent{
ID: 0,
Event: WorkflowEventTypeMessage,
Message: &WorkflowEventMessage{
Content: string(line),
},
}
// Check if this is the last event
isDone := string(line) == "done"
if isDone {
event.Event = WorkflowEventTypeDone
}
return event, isDone, nil
}
func TestStreamReader(t *testing.T) {
ctx := context.Background()
t.Run("successful event processing", func(t *testing.T) {
// Create mock response with multiple events
events := []string{
"first",
"second",
"done",
}
resp := createMockResponse(events)
// Create stream reader
reader := &streamReader[WorkflowEvent]{
ctx: ctx,
reader: bufio.NewReader(resp.Body),
response: resp,
processor: mockEventProcessor,
httpResponse: mockHTTPResponse(),
}
defer reader.Close()
// Read first event
event, err := reader.Recv()
require.NoError(t, err)
assert.Equal(t, WorkflowEventTypeMessage, event.Event)
assert.Equal(t, "first", event.Message.Content)
assert.False(t, reader.isFinished)
// Read second event
event, err = reader.Recv()
require.NoError(t, err)
assert.Equal(t, WorkflowEventTypeMessage, event.Event)
assert.Equal(t, "second", event.Message.Content)
assert.False(t, reader.isFinished)
// Read final event
event, err = reader.Recv()
require.NoError(t, err)
assert.Equal(t, WorkflowEventTypeDone, event.Event)
assert.True(t, reader.isFinished)
// Try reading after done
event, err = reader.Recv()
assert.Equal(t, io.EOF, err)
assert.Nil(t, event)
})
t.Run("empty lines are skipped", func(t *testing.T) {
events := []string{
"",
"test",
"",
"done",
}
resp := createMockResponse(events)
reader := &streamReader[WorkflowEvent]{
ctx: ctx,
reader: bufio.NewReader(resp.Body),
response: resp,
processor: mockEventProcessor,
httpResponse: mockHTTPResponse(),
}
defer reader.Close()
// First non-empty event
event, err := reader.Recv()
require.NoError(t, err)
assert.Equal(t, WorkflowEventTypeMessage, event.Event)
assert.Equal(t, "test", event.Message.Content)
// Second non-empty event
event, err = reader.Recv()
require.NoError(t, err)
assert.Equal(t, WorkflowEventTypeDone, event.Event)
})
t.Run("error response handling", func(t *testing.T) {
// Create mock error response
errorResp := &http.Response{
StatusCode: http.StatusBadRequest,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: io.NopCloser(strings.NewReader(`{
"log_id": "error_log_id",
"error": {
"code": 400,
"message": "Bad Request"
}
}`)),
}
reader := &streamReader[WorkflowEvent]{
ctx: ctx,
reader: bufio.NewReader(errorResp.Body),
response: errorResp,
processor: mockEventProcessor,
httpResponse: mockHTTPResponse(),
}
defer reader.Close()
// Attempt to read should return error
event, err := reader.Recv()
assert.Error(t, err)
assert.Nil(t, event)
})
t.Run("LogID method", func(t *testing.T) {
reader := &streamReader[WorkflowEvent]{
ctx: ctx,
httpResponse: mockHTTPResponse(),
}
assert.Equal(t, "test_log_id", reader.httpResponse.LogID())
})
}
// Helper function to create mock response with events
func createMockResponse(events []string) *http.Response {
// Join events with newlines
body := strings.Join(events, "\n")
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(body)),
}
}