Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/api/client/client_test.go
3434 views
1
package client_test
2
3
import (
4
"github.com/golang/mock/gomock"
5
_ "github.com/golang/mock/mockgen/model"
6
"github.com/kardolus/chatgpt-cli/api/client"
7
"github.com/kardolus/chatgpt-cli/api/http"
8
config2 "github.com/kardolus/chatgpt-cli/config"
9
"github.com/kardolus/chatgpt-cli/history"
10
. "github.com/onsi/gomega"
11
"github.com/sclevine/spec"
12
"github.com/sclevine/spec/report"
13
"os"
14
"strings"
15
"testing"
16
)
17
18
//go:generate mockgen -destination=callermocks_test.go -package=client_test github.com/kardolus/chatgpt-cli/api/http Caller
19
//go:generate mockgen -destination=historymocks_test.go -package=client_test github.com/kardolus/chatgpt-cli/history Store
20
//go:generate mockgen -destination=timermocks_test.go -package=client_test github.com/kardolus/chatgpt-cli/api/client Timer
21
//go:generate mockgen -destination=readermocks_test.go -package=client_test github.com/kardolus/chatgpt-cli/internal/fsio Reader
22
//go:generate mockgen -destination=writermocks_test.go -package=client_test github.com/kardolus/chatgpt-cli/internal/fsio Writer
23
//go:generate mockgen -destination=transportmocks_test.go -package=client_test github.com/kardolus/chatgpt-cli/api/client MCPTransport
24
25
const (
26
envApiKey = "api-key"
27
)
28
29
var (
30
mockCtrl *gomock.Controller
31
mockCaller *MockCaller
32
mockHistoryStore *MockStore
33
mockTimer *MockTimer
34
mockReader *MockReader
35
mockWriter *MockWriter
36
factory *clientFactory
37
apiKeyEnvVar string
38
config config2.Config
39
)
40
41
func TestUnitClient(t *testing.T) {
42
spec.Run(t, "Testing the client package", testClient, spec.Report(report.Terminal{}))
43
}
44
45
func testClient(t *testing.T, when spec.G, it spec.S) {
46
it.Before(func() {
47
RegisterTestingT(t)
48
mockCtrl = gomock.NewController(t)
49
mockCaller = NewMockCaller(mockCtrl)
50
mockHistoryStore = NewMockStore(mockCtrl)
51
mockTimer = NewMockTimer(mockCtrl)
52
mockReader = NewMockReader(mockCtrl)
53
mockWriter = NewMockWriter(mockCtrl)
54
config = MockConfig()
55
56
factory = newClientFactory(mockHistoryStore)
57
58
apiKeyEnvVar = strings.ToUpper(config.Name) + "_API_KEY"
59
Expect(os.Setenv(apiKeyEnvVar, envApiKey)).To(Succeed())
60
})
61
62
it.After(func() {
63
mockCtrl.Finish()
64
})
65
66
testMCP(t, when, it)
67
testSessionTransport(t, when, it)
68
testSessionTransportNonHTTP(t, when, it)
69
testNewMCPTransport(t, when, it)
70
testHistory(t, when, it)
71
testMedia(t, when, it)
72
testLLM(t, when, it)
73
testCapabilities(t, when, it)
74
}
75
76
func newClientFactory(mhs *MockStore) *clientFactory {
77
return &clientFactory{
78
mockHistoryStore: mhs,
79
}
80
}
81
82
type clientFactory struct {
83
mockHistoryStore *MockStore
84
}
85
86
func (f *clientFactory) buildClientWithoutConfig() *client.Client {
87
c := client.New(mockCallerFactory, f.mockHistoryStore, mockTimer, mockReader, mockWriter, MockConfig())
88
89
return c.WithContextWindow(config.ContextWindow)
90
}
91
92
func (f *clientFactory) withoutHistory() {
93
f.mockHistoryStore.EXPECT().Read().Return(nil, nil).Times(1)
94
}
95
96
func (f *clientFactory) withHistory(history []history.History) {
97
f.mockHistoryStore.EXPECT().Read().Return(history, nil).Times(1)
98
}
99
100
func mockCallerFactory(_ config2.Config) http.Caller {
101
return mockCaller
102
}
103
104
func MockConfig() config2.Config {
105
return config2.Config{
106
Name: "mock-openai",
107
APIKey: "mock-api-key",
108
Model: "gpt-3.5-turbo",
109
MaxTokens: 100,
110
ContextWindow: 50,
111
Role: "You are a test assistant.",
112
Temperature: 0.7,
113
TopP: 0.9,
114
FrequencyPenalty: 0.1,
115
PresencePenalty: 0.2,
116
Thread: "mock-thread",
117
OmitHistory: false,
118
URL: "https://api.mock-openai.com",
119
CompletionsPath: "/v1/test/completions",
120
ModelsPath: "/v1/test/models",
121
AuthHeader: "MockAuthorization",
122
AuthTokenPrefix: "MockBearer ",
123
CommandPrompt: "[mock-datetime] [Q%counter] [%usage]",
124
OutputPrompt: "[mock-output]",
125
AutoCreateNewThread: true,
126
TrackTokenUsage: true,
127
SkipTLSVerify: false,
128
Seed: 1,
129
Effort: "low",
130
ResponsesPath: "/v1/responses",
131
Voice: "mock-voice",
132
TranscriptionsPath: "/v1/test/transcriptions",
133
SpeechPath: "/v1/test/speech",
134
}
135
}
136
137