-
Notifications
You must be signed in to change notification settings - Fork 123
/
mods_test.go
275 lines (253 loc) · 7.23 KB
/
mods_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"fmt"
"testing"
"github.com/sashabaranov/go-openai"
"github.com/stretchr/testify/require"
)
func TestFindCacheOpsDetails(t *testing.T) {
newMods := func(t *testing.T) *Mods {
db := testDB(t)
return &Mods{
db: db,
Config: &Config{},
}
}
t.Run("all empty", func(t *testing.T) {
msg := newMods(t).findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Empty(t, dets.ReadID)
require.NotEmpty(t, dets.WriteID)
require.Empty(t, dets.Title)
})
t.Run("show id", func(t *testing.T) {
mods := newMods(t)
id := newConversationID()
require.NoError(t, mods.db.Save(id, "message", "gpt-4"))
mods.Config.Show = id[:8]
msg := mods.findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Equal(t, id, dets.ReadID)
})
t.Run("show title", func(t *testing.T) {
mods := newMods(t)
id := newConversationID()
require.NoError(t, mods.db.Save(id, "message 1", "gpt-4"))
mods.Config.Show = "message 1"
msg := mods.findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Equal(t, id, dets.ReadID)
})
t.Run("continue id", func(t *testing.T) {
mods := newMods(t)
id := newConversationID()
require.NoError(t, mods.db.Save(id, "message", "gpt-4"))
mods.Config.Continue = id[:5]
mods.Config.Prefix = "prompt"
msg := mods.findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Equal(t, id, dets.ReadID)
require.Equal(t, id, dets.WriteID)
})
t.Run("continue with no prompt", func(t *testing.T) {
mods := newMods(t)
id := newConversationID()
require.NoError(t, mods.db.Save(id, "message 1", "gpt-4"))
mods.Config.ContinueLast = true
msg := mods.findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Equal(t, id, dets.ReadID)
require.Equal(t, id, dets.WriteID)
require.Empty(t, dets.Title)
})
t.Run("continue title", func(t *testing.T) {
mods := newMods(t)
id := newConversationID()
require.NoError(t, mods.db.Save(id, "message 1", "gpt-4"))
mods.Config.Continue = "message 1"
mods.Config.Prefix = "prompt"
msg := mods.findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Equal(t, id, dets.ReadID)
require.Equal(t, id, dets.WriteID)
})
t.Run("continue last", func(t *testing.T) {
mods := newMods(t)
id := newConversationID()
require.NoError(t, mods.db.Save(id, "message 1", "gpt-4"))
mods.Config.ContinueLast = true
mods.Config.Prefix = "prompt"
msg := mods.findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Equal(t, id, dets.ReadID)
require.Equal(t, id, dets.WriteID)
require.Empty(t, dets.Title)
})
t.Run("continue last with name", func(t *testing.T) {
mods := newMods(t)
id := newConversationID()
require.NoError(t, mods.db.Save(id, "message 1", "gpt-4"))
mods.Config.Continue = "message 2"
mods.Config.Prefix = "prompt"
msg := mods.findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Equal(t, id, dets.ReadID)
require.Equal(t, "message 2", dets.Title)
require.NotEmpty(t, dets.WriteID)
require.Equal(t, id, dets.WriteID)
})
t.Run("write", func(t *testing.T) {
mods := newMods(t)
mods.Config.Title = "some title"
msg := mods.findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Empty(t, dets.ReadID)
require.NotEmpty(t, dets.WriteID)
require.NotEqual(t, "some title", dets.WriteID)
require.Equal(t, "some title", dets.Title)
})
t.Run("continue id and write with title", func(t *testing.T) {
mods := newMods(t)
id := newConversationID()
require.NoError(t, mods.db.Save(id, "message 1", "gpt-4"))
mods.Config.Title = "some title"
mods.Config.Continue = id[:10]
msg := mods.findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Equal(t, id, dets.ReadID)
require.NotEmpty(t, dets.WriteID)
require.NotEqual(t, id, dets.WriteID)
require.NotEqual(t, "some title", dets.WriteID)
require.Equal(t, "some title", dets.Title)
})
t.Run("continue title and write with title", func(t *testing.T) {
mods := newMods(t)
id := newConversationID()
require.NoError(t, mods.db.Save(id, "message 1", "gpt-4"))
mods.Config.Title = "some title"
mods.Config.Continue = "message 1"
msg := mods.findCacheOpsDetails()()
dets := msg.(cacheDetailsMsg)
require.Equal(t, id, dets.ReadID)
require.NotEmpty(t, dets.WriteID)
require.NotEqual(t, id, dets.WriteID)
require.NotEqual(t, "some title", dets.WriteID)
require.Equal(t, "some title", dets.Title)
})
t.Run("show invalid", func(t *testing.T) {
mods := newMods(t)
mods.Config.Show = "aaa"
msg := mods.findCacheOpsDetails()()
err := msg.(modsError)
require.Equal(t, "Could not find the conversation.", err.reason)
require.EqualError(t, err, errNoMatches.Error())
})
}
func TestRemoveWhitespace(t *testing.T) {
t.Run("only whitespaces", func(t *testing.T) {
require.Equal(t, "", removeWhitespace(" \n"))
})
t.Run("regular text", func(t *testing.T) {
require.Equal(t, " regular\n ", removeWhitespace(" regular\n "))
})
}
var responseTypeCases = map[string]struct {
config Config
expect openai.ChatCompletionResponseFormatType
}{
"no format": {
Config{},
openai.ChatCompletionResponseFormatTypeText,
},
"format markdown": {
Config{
Format: true,
FormatAs: "markdown",
Model: "gpt-4",
},
openai.ChatCompletionResponseFormatTypeText,
},
"format json with unsupported model": {
Config{
Format: true,
FormatAs: "json",
Model: "gpt-4",
},
openai.ChatCompletionResponseFormatTypeText,
},
"format markdown with gpt-4-1106-preview": {
Config{
Format: true,
FormatAs: "markdown",
Model: "gpt-4-1106-preview",
},
openai.ChatCompletionResponseFormatTypeText,
},
"format markdown with gpt-3.5-turbo-1106": {
Config{
Format: true,
FormatAs: "markdown",
Model: "gpt-3.5-turbo-1106",
},
openai.ChatCompletionResponseFormatTypeText,
},
"format json with gpt-4-1106-preview": {
Config{
Format: true,
FormatAs: "json",
Model: "gpt-4-1106-preview",
},
openai.ChatCompletionResponseFormatTypeJSONObject,
},
"format json with gpt-3.5-turbo-1106": {
Config{
Format: true,
FormatAs: "json",
Model: "gpt-3.5-turbo-1106",
},
openai.ChatCompletionResponseFormatTypeJSONObject,
},
}
func TestResponseType(t *testing.T) {
for k, tc := range responseTypeCases {
t.Run(k, func(t *testing.T) {
require.Equal(t, tc.expect, responseType(&tc.config))
})
}
}
var cutPromptTests = map[string]struct {
msg string
prompt string
expected string
}{
"bad error": {
msg: "nope",
prompt: "the prompt",
expected: "the prompt",
},
"crazy error": {
msg: tokenErrMsg(10, 93),
prompt: "the prompt",
expected: "the prompt",
},
"cut prompt": {
msg: tokenErrMsg(10, 3),
prompt: "this is a long prompt I have no idea if its really 10 tokens",
expected: "this is a long prompt ",
},
"missmatch of token estimation vs api result": {
msg: tokenErrMsg(30000, 100),
prompt: "tell me a joke",
expected: "tell me a joke",
},
}
func tokenErrMsg(l, ml int) string {
return fmt.Sprintf(`This model's maximum context length is %d tokens. However, your messages resulted in %d tokens`, ml, l)
}
func TestCutPrompt(t *testing.T) {
for name, tc := range cutPromptTests {
t.Run(name, func(t *testing.T) {
require.Equal(t, tc.expected, cutPrompt(tc.msg, tc.prompt))
})
}
}