mirror of
https://github.com/kardolus/chatgpt-cli.git
synced 2024-09-08 23:15:00 +03:00
Test empty and non-empty history
This commit is contained in:
@@ -115,9 +115,8 @@ func (c *Client) initHistory(query string) {
|
||||
}}
|
||||
}
|
||||
|
||||
// TODO Write history specific tests
|
||||
// TODO Write delete-specific tests (on store)
|
||||
// TODO Test the string returned from Stream
|
||||
// TODO Write delete-specific tests (on store)
|
||||
// TODO implement sliding window
|
||||
|
||||
c.history = append(c.history, message)
|
||||
|
||||
@@ -34,8 +34,6 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
|
||||
mockCtrl = gomock.NewController(t)
|
||||
mockCaller = NewMockCaller(mockCtrl)
|
||||
mockStore = NewMockStore(mockCtrl)
|
||||
|
||||
mockStore.EXPECT().Read().Return(nil, nil).Times(1)
|
||||
subject = client.New(mockCaller, mockStore)
|
||||
})
|
||||
|
||||
@@ -59,6 +57,8 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
|
||||
})
|
||||
|
||||
it("throws an error when the http callout fails", func() {
|
||||
mockStore.EXPECT().Read().Return(nil, nil).Times(1)
|
||||
|
||||
errorMsg := "error message"
|
||||
mockCaller.EXPECT().Post(client.URL, body, false).Return(nil, errors.New(errorMsg))
|
||||
|
||||
@@ -67,6 +67,7 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
|
||||
Expect(err.Error()).To(Equal(errorMsg))
|
||||
})
|
||||
it("throws an error when the response is empty", func() {
|
||||
mockStore.EXPECT().Read().Return(nil, nil).Times(1)
|
||||
mockCaller.EXPECT().Post(client.URL, body, false).Return(nil, nil)
|
||||
|
||||
_, err := subject.Query(query)
|
||||
@@ -74,6 +75,8 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
|
||||
Expect(err.Error()).To(Equal("empty response"))
|
||||
})
|
||||
it("throws an error when the response is a malformed json", func() {
|
||||
mockStore.EXPECT().Read().Return(nil, nil).Times(1)
|
||||
|
||||
malformed := `{"invalid":"json"` // missing closing brace
|
||||
mockCaller.EXPECT().Post(client.URL, body, false).Return([]byte(malformed), nil)
|
||||
|
||||
@@ -82,6 +85,8 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
|
||||
Expect(err.Error()).Should(HavePrefix("failed to decode response:"))
|
||||
})
|
||||
it("throws an error when the response is missing Choices", func() {
|
||||
mockStore.EXPECT().Read().Return(nil, nil).Times(1)
|
||||
|
||||
response := &types.Response{
|
||||
ID: "id",
|
||||
Object: "object",
|
||||
@@ -98,37 +103,67 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(err.Error()).To(Equal("no responses returned"))
|
||||
})
|
||||
it("parses a valid http response as expected", func() {
|
||||
const answer = "content"
|
||||
when("a valid http response is received", func() {
|
||||
testValidHTTPResponse := func(history []types.Message, expectedBody []byte) {
|
||||
mockStore.EXPECT().Read().Return(history, nil).Times(1)
|
||||
|
||||
choice := types.Choice{
|
||||
Message: types.Message{
|
||||
Role: "role",
|
||||
const answer = "content"
|
||||
|
||||
choice := types.Choice{
|
||||
Message: types.Message{
|
||||
Role: client.AssistantRole,
|
||||
Content: answer,
|
||||
},
|
||||
FinishReason: "",
|
||||
Index: 0,
|
||||
}
|
||||
response := &types.Response{
|
||||
ID: "id",
|
||||
Object: "object",
|
||||
Created: 0,
|
||||
Model: client.GPTModel,
|
||||
Choices: []types.Choice{choice},
|
||||
}
|
||||
|
||||
respBytes, err := json.Marshal(response)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
mockCaller.EXPECT().Post(client.URL, expectedBody, false).Return(respBytes, nil)
|
||||
|
||||
messages = createMessages(history, query)
|
||||
mockStore.EXPECT().Write(append(messages, types.Message{
|
||||
Role: client.AssistantRole,
|
||||
Content: answer,
|
||||
},
|
||||
FinishReason: "",
|
||||
Index: 0,
|
||||
}
|
||||
response := &types.Response{
|
||||
ID: "id",
|
||||
Object: "object",
|
||||
Created: 0,
|
||||
Model: "model",
|
||||
Choices: []types.Choice{choice},
|
||||
}))
|
||||
|
||||
result, err := subject.Query(query)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(result).To(Equal(answer))
|
||||
}
|
||||
|
||||
respBytes, err := json.Marshal(response)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
mockCaller.EXPECT().Post(client.URL, body, false).Return(respBytes, nil)
|
||||
it("returns the expected result for an empty history", func() {
|
||||
testValidHTTPResponse(nil, body)
|
||||
})
|
||||
it("returns the expected result for a non-empty history", func() {
|
||||
history := []types.Message{
|
||||
{
|
||||
Role: client.SystemRole,
|
||||
Content: client.AssistantContent,
|
||||
},
|
||||
{
|
||||
Role: client.UserRole,
|
||||
Content: "question 1",
|
||||
},
|
||||
{
|
||||
Role: client.AssistantRole,
|
||||
Content: "answer 1",
|
||||
},
|
||||
}
|
||||
messages = createMessages(history, query)
|
||||
body, err = createBody(messages)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
mockStore.EXPECT().Write(append(messages, types.Message{
|
||||
Role: client.AssistantRole,
|
||||
Content: answer,
|
||||
}))
|
||||
|
||||
result, err := subject.Query(query)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(result).To(Equal(answer))
|
||||
testValidHTTPResponse(history, body)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -151,6 +186,8 @@ func createMessages(history []types.Message, query string) []types.Message {
|
||||
Role: client.SystemRole,
|
||||
Content: client.AssistantContent,
|
||||
})
|
||||
} else {
|
||||
messages = history
|
||||
}
|
||||
|
||||
messages = append(messages, types.Message{
|
||||
|
||||
Reference in New Issue
Block a user