Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/api/client/history_test.go
3429 views
1
// history_test.go
2
package client_test
3
4
import (
5
"testing"
6
"time"
7
8
"github.com/kardolus/chatgpt-cli/api"
9
"github.com/kardolus/chatgpt-cli/api/client"
10
"github.com/kardolus/chatgpt-cli/history"
11
12
. "github.com/onsi/gomega"
13
"github.com/sclevine/spec"
14
)
15
16
func testHistory(t *testing.T, when spec.G, it spec.S) {
17
when("History()", func() {
18
when("ProvideContext()", func() {
19
it("updates the history with the provided context", func() {
20
subject := factory.buildClientWithoutConfig()
21
22
chatContext := "This is a story about a dog named Kya. Kya loves to play fetch and swim in the lake."
23
mockHistoryStore.EXPECT().Read().Return(nil, nil).Times(1)
24
25
mockTimer.EXPECT().Now().Return(time.Time{}).AnyTimes()
26
27
subject.ProvideContext(chatContext)
28
29
Expect(len(subject.History)).To(Equal(2)) // system message + provided context
30
31
systemMessage := subject.History[0]
32
Expect(systemMessage.Role).To(Equal(client.SystemRole))
33
Expect(systemMessage.Content).To(Equal(config.Role))
34
35
contextMessage := subject.History[1]
36
Expect(contextMessage.Role).To(Equal(client.UserRole))
37
Expect(contextMessage.Content).To(Equal(chatContext))
38
})
39
40
it("behaves as expected with a non empty initial history", func() {
41
subject := factory.buildClientWithoutConfig()
42
43
subject.History = []history.History{
44
{
45
Message: api.Message{
46
Role: client.SystemRole,
47
Content: "system message",
48
},
49
},
50
{
51
Message: api.Message{
52
Role: client.UserRole,
53
},
54
},
55
}
56
57
mockTimer.EXPECT().Now().Return(time.Time{}).AnyTimes()
58
59
chatContext := "test context"
60
subject.ProvideContext(chatContext)
61
62
Expect(len(subject.History)).To(Equal(3))
63
64
contextMessage := subject.History[2]
65
Expect(contextMessage.Role).To(Equal(client.UserRole))
66
Expect(contextMessage.Content).To(Equal(chatContext))
67
})
68
})
69
})
70
}
71
72