-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathsession_test.go
More file actions
70 lines (63 loc) · 1.69 KB
/
Copy pathsession_test.go
File metadata and controls
70 lines (63 loc) · 1.69 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
package blades
import (
"context"
"testing"
)
func TestSession_SetState(t *testing.T) {
s := NewSession()
s.SetState("key", "value")
if s.State()["key"] != "value" {
t.Errorf("state[key] = %v, want 'value'", s.State()["key"])
}
}
func TestSession_History(t *testing.T) {
s := NewSession()
for i := 0; i < 3; i++ {
s.Append(context.Background(), UserMessage("msg"))
}
history, err := s.History(context.Background())
if err != nil {
t.Fatal(err)
}
if len(history) != 3 {
t.Errorf("history len = %d, want 3", len(history))
}
}
func TestSession_History_NoCompressor(t *testing.T) {
s := NewSession()
msg := UserMessage("a")
s.Append(context.Background(), msg)
got, err := s.History(context.Background())
if err != nil {
t.Fatal(err)
}
if len(got) != 1 {
t.Errorf("expected raw history, got %v", got)
}
}
func TestSession_History_WithContextCompressor(t *testing.T) {
// A context compressor that always returns only the last message.
limiter := &limitCompressor{max: 1}
s := NewSession(WithContextCompressor(limiter))
for _, text := range []string{"a", "b", "c"} {
s.Append(context.Background(), UserMessage(text))
}
got, err := s.History(context.Background())
if err != nil {
t.Fatal(err)
}
if len(got) != 1 {
t.Errorf("len = %d, want 1 (compressed)", len(got))
}
if got[0].Text() != "c" {
t.Errorf("text = %q, want %q", got[0].Text(), "c")
}
}
// limitCompressor is a test ContextCompressor that keeps only the last max messages.
type limitCompressor struct{ max int }
func (l *limitCompressor) Compress(_ context.Context, messages []*Message) ([]*Message, error) {
if len(messages) <= l.max {
return messages, nil
}
return messages[len(messages)-l.max:], nil
}