-
Notifications
You must be signed in to change notification settings - Fork 11
/
stream_test.go
152 lines (117 loc) · 3.12 KB
/
stream_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
package replicate_test
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/replicate/replicate-go"
)
func TestStreamText(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, `event: output
data: foo
event: done
`)
}))
t.Cleanup(ts.Close)
p := &replicate.Prediction{
URLs: map[string]string{
"stream": ts.URL,
},
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
c, err := replicate.NewClient(replicate.WithToken("test-token"))
require.NoError(t, err)
r, err := c.StreamPredictionText(ctx, p)
t.Cleanup(func() { r.Close() })
require.NoError(t, err)
text, err := io.ReadAll(r)
require.NoError(t, err)
assert.Equal(t, "foo", string(text))
}
func TestStreamTextWithComment(t *testing.T) {
// nchan seems to put a `: hi` empty event at the start of each stream.
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, `: hi
event: output
data: foo
event: done
`)
}))
t.Cleanup(ts.Close)
p := &replicate.Prediction{
URLs: map[string]string{
"stream": ts.URL,
},
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
c, err := replicate.NewClient(replicate.WithToken("test-token"))
require.NoError(t, err)
r, err := c.StreamPredictionText(ctx, p)
t.Cleanup(func() { r.Close() })
require.NoError(t, err)
text, err := io.ReadAll(r)
require.NoError(t, err)
assert.Equal(t, "foo", string(text))
}
func TestStreamFiles(t *testing.T) {
var baseURL string
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/file" {
fmt.Fprintln(w, "mango")
return
}
fmt.Fprint(w, `event: output
data: data:text/plain,banana
event: output
data: data:text/plain;base64,YXBwbGU=
event: output
data: `+baseURL+`/file
event: done
`)
}))
t.Cleanup(ts.Close)
baseURL = ts.URL
p := &replicate.Prediction{
URLs: map[string]string{
"stream": ts.URL,
},
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
c, err := replicate.NewClient(replicate.WithToken("test-token"))
require.NoError(t, err)
files, err := c.StreamPredictionFiles(p)
require.NoError(t, err)
// first file is a data URI
file, err := files.NextFile(ctx)
require.NoError(t, err)
body, err := file.Body(ctx)
require.NoError(t, err)
content1, err := io.ReadAll(body)
require.NoError(t, err)
assert.Equal(t, "banana", string(content1))
// second file is a base64'd data URI
file, err = files.NextFile(ctx)
require.NoError(t, err)
body, err = file.Body(ctx)
require.NoError(t, err)
content2, err := io.ReadAll(body)
require.NoError(t, err)
assert.Equal(t, "apple", string(content2))
// third file is an http URI
file, err = files.NextFile(ctx)
require.NoError(t, err)
body, err = file.Body(ctx)
require.NoError(t, err)
content3, err := io.ReadAll(body)
require.NoError(t, err)
assert.Equal(t, "mango\n", string(content3))
}