mirror of
https://github.com/kardolus/chatgpt-cli.git
synced 2024-09-08 23:15:00 +03:00
Refactor HTTP client initialization with factory pattern
This commit is contained in:
@@ -62,15 +62,3 @@ func (mr *MockCallerMockRecorder) Post(arg0, arg1, arg2 interface{}) *gomock.Cal
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Post", reflect.TypeOf((*MockCaller)(nil).Post), arg0, arg1, arg2)
|
||||
}
|
||||
|
||||
// SetAPIKey mocks base method.
|
||||
func (m *MockCaller) SetAPIKey(arg0 string) {
|
||||
m.ctrl.T.Helper()
|
||||
m.ctrl.Call(m, "SetAPIKey", arg0)
|
||||
}
|
||||
|
||||
// SetAPIKey indicates an expected call of SetAPIKey.
|
||||
func (mr *MockCallerMockRecorder) SetAPIKey(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetAPIKey", reflect.TypeOf((*MockCaller)(nil).SetAPIKey), arg0)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ type Client struct {
|
||||
historyStore history.HistoryStore
|
||||
}
|
||||
|
||||
func New(caller http.Caller, cs config.ConfigStore, hs history.HistoryStore) (*Client, error) {
|
||||
func New(callerFactory http.CallerFactory, cs config.ConfigStore, hs history.HistoryStore) (*Client, error) {
|
||||
cm := configmanager.New(cs).WithEnvironment()
|
||||
configuration := cm.Config
|
||||
|
||||
@@ -37,7 +37,8 @@ func New(caller http.Caller, cs config.ConfigStore, hs history.HistoryStore) (*C
|
||||
return nil, errors.New("missing environment variable: " + cm.APIKeyEnvVarName())
|
||||
}
|
||||
|
||||
caller.SetAPIKey(configuration.APIKey)
|
||||
caller := callerFactory(configuration)
|
||||
|
||||
hs.SetThread(configuration.Thread)
|
||||
|
||||
return &Client{
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/golang/mock/gomock"
|
||||
_ "github.com/golang/mock/mockgen/model"
|
||||
"github.com/kardolus/chatgpt-cli/client"
|
||||
"github.com/kardolus/chatgpt-cli/http"
|
||||
"github.com/kardolus/chatgpt-cli/types"
|
||||
"github.com/kardolus/chatgpt-cli/utils"
|
||||
"os"
|
||||
@@ -60,7 +61,7 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
|
||||
mockHistoryStore = NewMockHistoryStore(mockCtrl)
|
||||
mockConfigStore = NewMockConfigStore(mockCtrl)
|
||||
|
||||
factory = newClientFactory(mockCaller, mockConfigStore, mockHistoryStore)
|
||||
factory = newClientFactory(mockConfigStore, mockHistoryStore)
|
||||
|
||||
apiKeyEnvVar = strings.ToUpper(defaultName) + "_API_KEY"
|
||||
Expect(os.Setenv(apiKeyEnvVar, envApiKey)).To(Succeed())
|
||||
@@ -76,7 +77,7 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
|
||||
|
||||
mockConfigStore.EXPECT().Read().Return(types.Config{}, nil).Times(1)
|
||||
|
||||
_, err := client.New(mockCaller, mockConfigStore, mockHistoryStore)
|
||||
_, err := client.New(mockCallerFactory, mockConfigStore, mockHistoryStore)
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(ContainSubstring(apiKeyEnvVar))
|
||||
@@ -244,11 +245,10 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
|
||||
testValidHTTPResponse(subject, history, body, false)
|
||||
})
|
||||
it("ignores history when configured to do so", func() {
|
||||
mockCaller.EXPECT().SetAPIKey(envApiKey).Times(1)
|
||||
mockHistoryStore.EXPECT().SetThread(defaultThread).Times(1)
|
||||
mockConfigStore.EXPECT().Read().Return(types.Config{OmitHistory: true}, nil).Times(1)
|
||||
|
||||
subject, err := client.New(mockCaller, mockConfigStore, mockHistoryStore)
|
||||
subject, err := client.New(mockCallerFactory, mockConfigStore, mockHistoryStore)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// Read and Write are never called on the history store
|
||||
@@ -505,12 +505,11 @@ func createMessages(history []types.Message, query string) []types.Message {
|
||||
}
|
||||
|
||||
type clientFactory struct {
|
||||
mockCaller *MockCaller
|
||||
mockConfigStore *MockConfigStore
|
||||
mockHistoryStore *MockHistoryStore
|
||||
}
|
||||
|
||||
func newClientFactory(mc *MockCaller, mcs *MockConfigStore, mhs *MockHistoryStore) *clientFactory {
|
||||
func newClientFactory(mcs *MockConfigStore, mhs *MockHistoryStore) *clientFactory {
|
||||
mockConfigStore.EXPECT().ReadDefaults().Return(types.Config{
|
||||
Name: defaultName,
|
||||
Model: defaultModel,
|
||||
@@ -527,29 +526,26 @@ func newClientFactory(mc *MockCaller, mcs *MockConfigStore, mhs *MockHistoryStor
|
||||
}).Times(1)
|
||||
|
||||
return &clientFactory{
|
||||
mockCaller: mc,
|
||||
mockConfigStore: mcs,
|
||||
mockHistoryStore: mhs,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *clientFactory) buildClientWithoutConfig() *client.Client {
|
||||
f.mockCaller.EXPECT().SetAPIKey(envApiKey).Times(1)
|
||||
f.mockHistoryStore.EXPECT().SetThread(defaultThread).Times(1)
|
||||
f.mockConfigStore.EXPECT().Read().Return(types.Config{}, nil).Times(1)
|
||||
|
||||
c, err := client.New(f.mockCaller, f.mockConfigStore, f.mockHistoryStore)
|
||||
c, err := client.New(mockCallerFactory, f.mockConfigStore, f.mockHistoryStore)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
return c.WithCapacity(50)
|
||||
}
|
||||
|
||||
func (f *clientFactory) buildClientWithConfig(config types.Config) *client.Client {
|
||||
f.mockCaller.EXPECT().SetAPIKey(envApiKey).Times(1)
|
||||
f.mockHistoryStore.EXPECT().SetThread(defaultThread).Times(1)
|
||||
f.mockConfigStore.EXPECT().Read().Return(config, nil).Times(1)
|
||||
|
||||
c, err := client.New(f.mockCaller, f.mockConfigStore, f.mockHistoryStore)
|
||||
c, err := client.New(mockCallerFactory, f.mockConfigStore, f.mockHistoryStore)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
return c.WithCapacity(50)
|
||||
@@ -562,3 +558,7 @@ func (f *clientFactory) withoutHistory() {
|
||||
func (f *clientFactory) withHistory(history []types.Message) {
|
||||
f.mockHistoryStore.EXPECT().Read().Return(history, nil).Times(1)
|
||||
}
|
||||
|
||||
func mockCallerFactory(cfg types.Config) http.Caller {
|
||||
return mockCaller
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user