-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_test.go
More file actions
177 lines (144 loc) · 3.72 KB
/
chat_test.go
File metadata and controls
177 lines (144 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package ai
import (
"testing"
)
func TestConversationCreation(t *testing.T) {
b := New(ModelGPT5).System("You are helpful")
chat := b.Chat()
if chat.builder != b {
t.Error("conversation should reference builder")
}
if len(chat.history) != 0 {
t.Error("conversation should start with empty history")
}
}
func TestConversationHistory(t *testing.T) {
chat := &Conversation{
builder: New(ModelGPT5),
history: []Message{},
}
// Manually add to history for testing
chat.history = append(chat.history, Message{Role: "user", Content: "Hello"})
chat.history = append(chat.history, Message{Role: "assistant", Content: "Hi!"})
history := chat.History()
if len(history) != 2 {
t.Fatalf("expected 2 messages, got %d", len(history))
}
if history[0].Role != "user" {
t.Error("first message should be user")
}
if history[1].Role != "assistant" {
t.Error("second message should be assistant")
}
}
func TestConversationClear(t *testing.T) {
// Disable pretty for test
oldPretty := Pretty
Pretty = false
defer func() { Pretty = oldPretty }()
chat := &Conversation{
builder: New(ModelGPT5),
history: []Message{
{Role: "user", Content: "Hello"},
{Role: "assistant", Content: "Hi!"},
},
}
chat.Clear()
if len(chat.history) != 0 {
t.Error("history should be empty after clear")
}
}
func TestConversationLastResponse(t *testing.T) {
chat := &Conversation{
builder: New(ModelGPT5),
history: []Message{
{Role: "user", Content: "Hello"},
{Role: "assistant", Content: "First response"},
{Role: "user", Content: "Follow up"},
{Role: "assistant", Content: "Second response"},
},
}
last := chat.LastResponse()
if last != "Second response" {
t.Errorf("expected 'Second response', got %q", last)
}
}
func TestConversationLastResponseEmpty(t *testing.T) {
chat := &Conversation{
builder: New(ModelGPT5),
history: []Message{},
}
last := chat.LastResponse()
if last != "" {
t.Error("should return empty string when no responses")
}
}
func TestConversationLastResponseNoAssistant(t *testing.T) {
chat := &Conversation{
builder: New(ModelGPT5),
history: []Message{
{Role: "user", Content: "Hello"},
},
}
last := chat.LastResponse()
if last != "" {
t.Error("should return empty string when no assistant messages")
}
}
func TestConversationSummarize(t *testing.T) {
chat := &Conversation{
builder: New(ModelGPT5),
history: []Message{
{Role: "user", Content: "Hello"},
{Role: "assistant", Content: "Hi!"},
{Role: "user", Content: "How are you?"},
{Role: "assistant", Content: "Good!"},
},
}
summary := chat.Summarize()
if summary == "" {
t.Error("summary should not be empty")
}
}
func TestConversationBuildMessages(t *testing.T) {
b := New(ModelGPT5).
System("You are {{role}}").
With(Vars{"role": "helpful"})
chat := &Conversation{
builder: b,
history: []Message{
{Role: "user", Content: "Hello"},
{Role: "assistant", Content: "Hi!"},
},
}
msgs := chat.buildMessages()
// Should have system + history
if len(msgs) != 3 {
t.Fatalf("expected 3 messages, got %d", len(msgs))
}
// System should have vars applied
if msgs[0].Content != "You are helpful" {
t.Errorf("expected 'You are helpful', got %q", msgs[0].Content)
}
// History should be intact
if msgs[1].Content != "Hello" {
t.Errorf("expected 'Hello', got %q", msgs[1].Content)
}
}
func TestConversationBuildMessagesNoSystem(t *testing.T) {
b := New(ModelGPT5)
chat := &Conversation{
builder: b,
history: []Message{
{Role: "user", Content: "Hello"},
},
}
msgs := chat.buildMessages()
// Should only have history (no system)
if len(msgs) != 1 {
t.Fatalf("expected 1 message, got %d", len(msgs))
}
if msgs[0].Role != "user" {
t.Error("should only have user message")
}
}