-
Notifications
You must be signed in to change notification settings - Fork 3
/
completion_sse_decoder_test.go
111 lines (104 loc) · 2.15 KB
/
completion_sse_decoder_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
package anthrogo
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDecode(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
wantEv *CompletionEvent
}{
{
name: "has : prefix",
input: ":\n",
wantErr: false,
wantEv: nil,
},
{
name: "carriage w/ no event",
input: "\r\n",
wantErr: false,
wantEv: nil,
},
{
name: "empty line",
input: "\n",
wantErr: false,
wantEv: nil,
},
{
name: "invalid field",
input: "some junk data\n",
wantErr: false,
wantEv: nil,
},
{
name: "id field",
input: "id: testID\n\r",
wantErr: false,
wantEv: &CompletionEvent{ID: "testID"},
},
{
name: "event field",
input: "event: testEvent\n\r",
wantErr: false,
wantEv: &CompletionEvent{Event: "testEvent"},
},
{
name: "retry field",
input: "retry: 5\n\r",
wantErr: false,
wantEv: &CompletionEvent{Retry: 5},
},
{
name: "data field",
input: "data: {\"completion\":\"testCompletion\",\"stop_reason\":\"testReason\",\"model\":\"testModel\",\"stop\":\"testStop\",\"log_id\":\"testLogId\"}\n\r",
wantErr: false,
wantEv: &CompletionEvent{
Data: &CompletionEventData{
Completion: "testCompletion",
StopReason: "testReason",
Model: "testModel",
Stop: "testStop",
LogID: "testLogId",
},
},
},
{
name: "invalid json in data field",
input: "data: {\"completion\":\"testCompletion\",}",
wantErr: true,
},
{
name: "invalid integer in retry field",
input: "retry: invalid\n",
wantErr: false,
wantEv: nil,
},
{
name: "id field with null byte",
input: "id: test\000ID\n",
wantErr: false,
wantEv: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := strings.NewReader(tt.input + "\n")
dec := NewCompletionSSEDecoder(r)
ev, err := dec.Decode()
if !tt.wantErr {
ev, err = dec.Decode()
}
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.wantEv, ev, "they should be equal")
})
}
}