Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/agent/core/transcript_buffer_test.go
3433 views
1
package core_test
2
3
import (
4
"strings"
5
"testing"
6
7
"github.com/kardolus/chatgpt-cli/agent/core"
8
. "github.com/onsi/gomega"
9
"github.com/sclevine/spec"
10
"github.com/sclevine/spec/report"
11
)
12
13
func TestTranscriptBuffer(t *testing.T) {
14
spec.Run(t, "TranscriptBuffer", testTranscriptBuffer, spec.Report(report.Terminal{}))
15
}
16
17
func testTranscriptBuffer(t *testing.T, when spec.G, it spec.S) {
18
it.Before(func() {
19
RegisterTestingT(t)
20
})
21
22
when("NewTranscriptBuffer(maxBytes)", func() {
23
it("clamps negative maxBytes to 0 (buffer stays empty)", func() {
24
tb := core.NewTranscriptBuffer(-1)
25
tb.AppendString("hello")
26
Expect(tb.Len()).To(Equal(0))
27
Expect(tb.String()).To(Equal(""))
28
})
29
30
it("uses 0 maxBytes to discard all content", func() {
31
tb := core.NewTranscriptBuffer(0)
32
tb.AppendString("hello")
33
Expect(tb.Len()).To(Equal(0))
34
Expect(tb.String()).To(Equal(""))
35
})
36
})
37
38
when("AppendString()", func() {
39
it("normalizes entries to end with newline", func() {
40
tb := core.NewTranscriptBuffer(1024)
41
tb.AppendString("hello")
42
Expect(tb.String()).To(Equal("hello\n"))
43
})
44
45
it("does not double-add newline if already present", func() {
46
tb := core.NewTranscriptBuffer(1024)
47
tb.AppendString("hello\n")
48
Expect(tb.String()).To(Equal("hello\n"))
49
})
50
51
it("is a no-op for empty string", func() {
52
tb := core.NewTranscriptBuffer(1024)
53
tb.AppendString("")
54
Expect(tb.Len()).To(Equal(0))
55
})
56
})
57
58
when("Appendf()", func() {
59
it("formats and appends (with newline normalization)", func() {
60
tb := core.NewTranscriptBuffer(1024)
61
tb.Appendf("x=%d", 7)
62
Expect(tb.String()).To(Equal("x=7\n"))
63
})
64
})
65
66
when("truncation / cap behavior", func() {
67
it("caps total length and prepends the truncation banner when there is room", func() {
68
// Make max large enough to include the banner + some content.
69
tb := core.NewTranscriptBuffer(40)
70
71
// Force overflow: two appends produce more than 40 bytes.
72
tb.AppendString("0123456789") // 11 incl newline
73
tb.AppendString("ABCDEFGHIJKLMN") // 15 incl newline => total 26, still under
74
tb.AppendString(strings.Repeat("Z", 50))
75
76
s := tb.String()
77
78
// Must be capped
79
Expect(tb.Len()).To(BeNumerically("<=", 40))
80
81
// Banner should appear and should be at the beginning (it’s prepended)
82
Expect(s).To(HavePrefix("\n…(truncated)\n"))
83
84
// Only once as prefix
85
Expect(strings.Count(s, "…(truncated)")).To(Equal(1))
86
})
87
88
it("does not add the banner when maxBytes is too small to fit it", func() {
89
// Banner length is > 5, so condition len(banner) < max is false.
90
tb := core.NewTranscriptBuffer(5)
91
92
tb.AppendString("hello")
93
tb.AppendString("world") // overflow
94
95
s := tb.String()
96
Expect(tb.Len()).To(BeNumerically("<=", 5))
97
Expect(s).NotTo(ContainSubstring("…(truncated)"))
98
})
99
100
it("does not repeatedly prepend the banner across multiple truncations", func() {
101
tb := core.NewTranscriptBuffer(50)
102
103
// First truncation
104
tb.AppendString(strings.Repeat("A", 200))
105
s1 := tb.String()
106
Expect(s1).To(HavePrefix("\n…(truncated)\n"))
107
Expect(strings.Count(s1, "…(truncated)")).To(Equal(1))
108
109
// Second truncation (append more, should still be exactly one banner prefix)
110
tb.AppendString(strings.Repeat("B", 200))
111
s2 := tb.String()
112
Expect(tb.Len()).To(BeNumerically("<=", 50))
113
Expect(s2).To(HavePrefix("\n…(truncated)\n"))
114
Expect(strings.Count(s2, "…(truncated)")).To(Equal(1))
115
})
116
})
117
118
when("Reset()", func() {
119
it("clears the buffer", func() {
120
tb := core.NewTranscriptBuffer(1024)
121
tb.AppendString("hello")
122
Expect(tb.Len()).To(BeNumerically(">", 0))
123
124
tb.Reset()
125
Expect(tb.Len()).To(Equal(0))
126
Expect(tb.String()).To(Equal(""))
127
})
128
})
129
130
when("nil receiver safety", func() {
131
it("does not panic and returns empty/zero values", func() {
132
var tb *core.TranscriptBuffer
133
134
Expect(func() { tb.AppendString("hello") }).NotTo(Panic())
135
Expect(func() { tb.Appendf("x=%d", 1) }).NotTo(Panic())
136
Expect(func() { _ = tb.String() }).NotTo(Panic())
137
Expect(func() { _ = tb.Len() }).NotTo(Panic())
138
Expect(func() { tb.Reset() }).NotTo(Panic())
139
140
Expect(tb.String()).To(Equal(""))
141
Expect(tb.Len()).To(Equal(0))
142
})
143
})
144
}
145
146