-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi_context_cache_test.go
More file actions
183 lines (162 loc) · 4.08 KB
/
Copy pathapi_context_cache_test.go
File metadata and controls
183 lines (162 loc) · 4.08 KB
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
package moonshot_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/northes/go-moonshot"
"github.com/northes/go-moonshot/test"
)
// Due to some problems, this test is temporarily shielded in the GitHub Actions environment.
// waiting for the official Mock Server.
// https://github.com/MoonshotAI/moonpalace
func TestContextCache(t *testing.T) {
if test.IsGithubActions() {
return
}
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
// Create
var createResponse *moonshot.ContextCacheCreateResponse
createResponse, err = cli.ContextCache().Create(ctx, &moonshot.ContextCacheCreateRequest{
Model: moonshot.ModelFamilyMoonshotV1,
Messages: []moonshot.ChatCompletionsMessage{
{
Role: moonshot.RoleSystem,
Content: "你是一个翻译机器人,我会告诉你一些英文,请帮我翻译成中文。",
},
},
ExpiredAt: -1,
TTL: 60,
})
if err != nil {
t.Fatal(err)
}
id := createResponse.Id
assert.Equal(t, time.Now().Unix()+60, createResponse.ExpiredAt)
// Get
var getResponse *moonshot.ContextCacheGetResponse
getResponse, err = cli.ContextCache().Get(ctx, &moonshot.ContextCacheGetRequest{
Id: id,
})
if err != nil {
t.Fatal(err)
}
assert.Equal(t, id, getResponse.Id)
// Update
var updateResponse *moonshot.ContextCacheUpdateResponse
updateResponse, err = cli.ContextCache().Update(ctx, &moonshot.ContextCacheUpdateRequest{
Id: id,
ExpiredAt: -1,
TTL: 120,
})
if err != nil {
t.Fatal(err)
}
_ = updateResponse
// assert.Equal(t, time.Now().Unix()+120, updateResponse.ExpiredAt)
// List
var listResponse *moonshot.ContextCacheListResponse
listResponse, err = cli.ContextCache().List(ctx, &moonshot.ContextCacheListRequest{
// Limit: 10,
// Order: moonshot.ContextCacheOrderAsc,
// After: "",
// Before: "",
})
if err != nil {
t.Fatal(err)
}
assert.GreaterOrEqual(t, len(listResponse.Data), 1)
// Delete
var deleteResponse *moonshot.ContextCacheDeleteResponse
deleteResponse, err = cli.ContextCache().Delete(ctx, &moonshot.ContextCacheDeleteRequest{
Id: id,
})
if err != nil {
t.Fatal(err)
}
assert.Equal(t, id, deleteResponse.Id)
}
func TestContextCache_Create(t *testing.T) {
if test.IsGithubActions() {
return
}
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
response, err := cli.ContextCache().Create(ctx, &moonshot.ContextCacheCreateRequest{
Model: moonshot.ModelFamilyMoonshotV1,
Messages: []moonshot.ChatCompletionsMessage{
{
Role: moonshot.RoleSystem,
Content: "你是一个翻译机器人,我会告诉你一些英文,请帮我翻译成中文。",
},
},
Tools: nil,
Description: "这是一个测试的描述",
Metadata: nil,
ExpiredAt: -1,
TTL: 60,
})
if err != nil {
t.Fatal(err)
}
t.Log(test.MarshalJsonToStringX(response))
}
func TestContextCache_Delete(t *testing.T) {
if test.IsGithubActions() {
return
}
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
response, err := cli.ContextCache().Delete(ctx, &moonshot.ContextCacheDeleteRequest{
Id: "cache-xxxx",
})
if err != nil {
t.Fatal(err)
}
t.Log(test.MarshalJsonToStringX(response))
}
func TestContextCache_List(t *testing.T) {
if test.IsGithubActions() {
return
}
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
response, err := cli.ContextCache().List(ctx, &moonshot.ContextCacheListRequest{})
if err != nil {
t.Fatal(err)
}
t.Log(test.MarshalJsonToStringX(response))
}
func TestContextCache_CreateTag(t *testing.T) {
if test.IsGithubActions() {
return
}
cli, err := NewTestClient()
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
// Create
var createResponse *moonshot.ContextCacheCreateTagResponse
createResponse, err = cli.ContextCache().CreateTag(ctx, &moonshot.ContextCacheCreateTagRequest{
Tag: "MyCacheTag",
CacheId: "cache-",
})
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "MyCacheTag", createResponse.Tag)
}