Test empty and non-empty history

This commit is contained in:
kardolus
2023-05-03 10:18:50 -04:00
parent 5be0af6cb0
commit eae488c3b1
3 changed files with 67 additions and 31 deletions

View File

@@ -40,7 +40,7 @@ building the application:
4. To enable history tracking across CLI calls, create a ~/.chatgpt-cli directory using the command: 4. To enable history tracking across CLI calls, create a ~/.chatgpt-cli directory using the command:
```shell ```shell
mkdir ~/.chatgpt-cli mkdir -p ~/.chatgpt-cli
``` ```
With this directory in place, the CLI will automatically manage message history for seamless conversations with the GPT With this directory in place, the CLI will automatically manage message history for seamless conversations with the GPT

View File

@@ -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 Test the string returned from Stream
// TODO Write delete-specific tests (on store)
// TODO implement sliding window // TODO implement sliding window
c.history = append(c.history, message) c.history = append(c.history, message)

View File

@@ -34,8 +34,6 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
mockCtrl = gomock.NewController(t) mockCtrl = gomock.NewController(t)
mockCaller = NewMockCaller(mockCtrl) mockCaller = NewMockCaller(mockCtrl)
mockStore = NewMockStore(mockCtrl) mockStore = NewMockStore(mockCtrl)
mockStore.EXPECT().Read().Return(nil, nil).Times(1)
subject = client.New(mockCaller, mockStore) 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() { it("throws an error when the http callout fails", func() {
mockStore.EXPECT().Read().Return(nil, nil).Times(1)
errorMsg := "error message" errorMsg := "error message"
mockCaller.EXPECT().Post(client.URL, body, false).Return(nil, errors.New(errorMsg)) 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)) Expect(err.Error()).To(Equal(errorMsg))
}) })
it("throws an error when the response is empty", func() { 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) mockCaller.EXPECT().Post(client.URL, body, false).Return(nil, nil)
_, err := subject.Query(query) _, 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")) Expect(err.Error()).To(Equal("empty response"))
}) })
it("throws an error when the response is a malformed json", func() { 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 malformed := `{"invalid":"json"` // missing closing brace
mockCaller.EXPECT().Post(client.URL, body, false).Return([]byte(malformed), nil) 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:")) Expect(err.Error()).Should(HavePrefix("failed to decode response:"))
}) })
it("throws an error when the response is missing Choices", func() { it("throws an error when the response is missing Choices", func() {
mockStore.EXPECT().Read().Return(nil, nil).Times(1)
response := &types.Response{ response := &types.Response{
ID: "id", ID: "id",
Object: "object", Object: "object",
@@ -98,37 +103,67 @@ func testClient(t *testing.T, when spec.G, it spec.S) {
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("no responses returned")) Expect(err.Error()).To(Equal("no responses returned"))
}) })
it("parses a valid http response as expected", func() { when("a valid http response is received", func() {
const answer = "content" testValidHTTPResponse := func(history []types.Message, expectedBody []byte) {
mockStore.EXPECT().Read().Return(history, nil).Times(1)
choice := types.Choice{ const answer = "content"
Message: types.Message{
Role: "role", 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, Content: answer,
}, }))
FinishReason: "",
Index: 0, result, err := subject.Query(query)
} Expect(err).NotTo(HaveOccurred())
response := &types.Response{ Expect(result).To(Equal(answer))
ID: "id",
Object: "object",
Created: 0,
Model: "model",
Choices: []types.Choice{choice},
} }
respBytes, err := json.Marshal(response) it("returns the expected result for an empty history", func() {
Expect(err).NotTo(HaveOccurred()) testValidHTTPResponse(nil, body)
mockCaller.EXPECT().Post(client.URL, body, false).Return(respBytes, nil) })
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{ testValidHTTPResponse(history, body)
Role: client.AssistantRole, })
Content: answer,
}))
result, err := subject.Query(query)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(answer))
}) })
}) })
} }
@@ -151,6 +186,8 @@ func createMessages(history []types.Message, query string) []types.Message {
Role: client.SystemRole, Role: client.SystemRole,
Content: client.AssistantContent, Content: client.AssistantContent,
}) })
} else {
messages = history
} }
messages = append(messages, types.Message{ messages = append(messages, types.Message{