-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathcache_test.go
216 lines (190 loc) · 5.12 KB
/
cache_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"bytes"
"errors"
"flag"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/sashabaranov/go-openai"
"github.com/stretchr/testify/require"
)
var update = flag.Bool("update", false, "update .golden files")
func TestCache(t *testing.T) {
t.Run("read non-existent", func(t *testing.T) {
cache := newCache(t.TempDir())
err := cache.read("super-fake", &[]openai.ChatCompletionMessage{})
require.ErrorIs(t, err, os.ErrNotExist)
})
t.Run("write", func(t *testing.T) {
cache := newCache(t.TempDir())
messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "first 4 natural numbers",
},
{
Role: openai.ChatMessageRoleAssistant,
Content: "1, 2, 3, 4",
},
}
require.NoError(t, cache.write("fake", &messages))
result := []openai.ChatCompletionMessage{}
require.NoError(t, cache.read("fake", &result))
require.ElementsMatch(t, messages, result)
})
t.Run("delete", func(t *testing.T) {
cache := newCache(t.TempDir())
cache.write("fake", &[]openai.ChatCompletionMessage{})
require.NoError(t, cache.delete("fake"))
require.ErrorIs(t, cache.read("fake", nil), os.ErrNotExist)
})
t.Run("invalid id", func(t *testing.T) {
t.Run("write", func(t *testing.T) {
cache := newCache(t.TempDir())
require.ErrorIs(t, cache.write("", nil), errInvalidID)
})
t.Run("delete", func(t *testing.T) {
cache := newCache(t.TempDir())
require.ErrorIs(t, cache.delete(""), errInvalidID)
})
t.Run("read", func(t *testing.T) {
cache := newCache(t.TempDir())
require.ErrorIs(t, cache.read("", nil), errInvalidID)
})
})
}
func TestCachedCompletionStream(t *testing.T) {
stream := cachedCompletionStream{
messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "you are a medieval king",
},
{
Role: openai.ChatMessageRoleUser,
Content: "first 4 natural numbers",
},
{
Role: openai.ChatMessageRoleAssistant,
Content: "1, 2, 3, 4",
},
{
Role: openai.ChatMessageRoleUser,
Content: "as a json array",
},
{
Role: openai.ChatMessageRoleAssistant,
Content: "[ 1, 2, 3, 4 ]",
},
{
Role: openai.ChatMessageRoleAssistant,
Content: "something from an assistant",
},
{
Role: openai.ChatMessageRoleFunction,
Content: "something from a function",
},
},
}
t.Cleanup(func() { require.NoError(t, stream.Close()) })
var output []string
for {
resp, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
require.NoError(t, err)
output = append(output, resp.Choices[0].Delta.Content)
}
golden := filepath.Join("testdata", t.Name()+".md.golden")
content := strings.Join(output, "\n")
if *update {
require.NoError(t, os.WriteFile(golden, []byte(content), 0o644))
}
bts, err := os.ReadFile(golden)
require.NoError(t, err)
require.Equal(t, string(bytes.ReplaceAll(bts, []byte("\r\n"), []byte("\n"))), content)
}
func TestExpiringCache(t *testing.T) {
t.Run("write and read", func(t *testing.T) {
config.CachePath = t.TempDir()
cache, err := NewExpiringCache[string]()
require.NoError(t, err)
// Write a value with expiry
data := "test data"
expiresAt := time.Now().Add(time.Hour).Unix()
err = cache.Write("test", expiresAt, func(w io.Writer) error {
_, err := w.Write([]byte(data))
return err
})
require.NoError(t, err)
// Read it back
var result string
err = cache.Read("test", func(r io.Reader) error {
b, err := io.ReadAll(r)
if err != nil {
return err
}
result = string(b)
return nil
})
require.NoError(t, err)
require.Equal(t, data, result)
})
t.Run("expired token", func(t *testing.T) {
config.CachePath = t.TempDir()
cache, err := NewExpiringCache[string]()
require.NoError(t, err)
// Write a value that's already expired
data := "test data"
expiresAt := time.Now().Add(-time.Hour).Unix() // expired 1 hour ago
err = cache.Write("test", expiresAt, func(w io.Writer) error {
_, err := w.Write([]byte(data))
return err
})
require.NoError(t, err)
// Try to read it
err = cache.Read("test", func(r io.Reader) error {
return nil
})
require.Error(t, err)
require.True(t, os.IsNotExist(err))
})
t.Run("overwrite token", func(t *testing.T) {
config.CachePath = t.TempDir()
cache, err := NewExpiringCache[string]()
require.NoError(t, err)
// Write initial value
data1 := "test data 1"
expiresAt1 := time.Now().Add(time.Hour).Unix()
err = cache.Write("test", expiresAt1, func(w io.Writer) error {
_, err := w.Write([]byte(data1))
return err
})
require.NoError(t, err)
// Write new value
data2 := "test data 2"
expiresAt2 := time.Now().Add(2 * time.Hour).Unix()
err = cache.Write("test", expiresAt2, func(w io.Writer) error {
_, err := w.Write([]byte(data2))
return err
})
require.NoError(t, err)
// Read it back - should get the new value
var result string
err = cache.Read("test", func(r io.Reader) error {
b, err := io.ReadAll(r)
if err != nil {
return err
}
result = string(b)
return nil
})
require.NoError(t, err)
require.Equal(t, data2, result)
})
}