Path: blob/main/agent/core/transcript_buffer_test.go
3433 views
package core_test12import (3"strings"4"testing"56"github.com/kardolus/chatgpt-cli/agent/core"7. "github.com/onsi/gomega"8"github.com/sclevine/spec"9"github.com/sclevine/spec/report"10)1112func TestTranscriptBuffer(t *testing.T) {13spec.Run(t, "TranscriptBuffer", testTranscriptBuffer, spec.Report(report.Terminal{}))14}1516func testTranscriptBuffer(t *testing.T, when spec.G, it spec.S) {17it.Before(func() {18RegisterTestingT(t)19})2021when("NewTranscriptBuffer(maxBytes)", func() {22it("clamps negative maxBytes to 0 (buffer stays empty)", func() {23tb := core.NewTranscriptBuffer(-1)24tb.AppendString("hello")25Expect(tb.Len()).To(Equal(0))26Expect(tb.String()).To(Equal(""))27})2829it("uses 0 maxBytes to discard all content", func() {30tb := core.NewTranscriptBuffer(0)31tb.AppendString("hello")32Expect(tb.Len()).To(Equal(0))33Expect(tb.String()).To(Equal(""))34})35})3637when("AppendString()", func() {38it("normalizes entries to end with newline", func() {39tb := core.NewTranscriptBuffer(1024)40tb.AppendString("hello")41Expect(tb.String()).To(Equal("hello\n"))42})4344it("does not double-add newline if already present", func() {45tb := core.NewTranscriptBuffer(1024)46tb.AppendString("hello\n")47Expect(tb.String()).To(Equal("hello\n"))48})4950it("is a no-op for empty string", func() {51tb := core.NewTranscriptBuffer(1024)52tb.AppendString("")53Expect(tb.Len()).To(Equal(0))54})55})5657when("Appendf()", func() {58it("formats and appends (with newline normalization)", func() {59tb := core.NewTranscriptBuffer(1024)60tb.Appendf("x=%d", 7)61Expect(tb.String()).To(Equal("x=7\n"))62})63})6465when("truncation / cap behavior", func() {66it("caps total length and prepends the truncation banner when there is room", func() {67// Make max large enough to include the banner + some content.68tb := core.NewTranscriptBuffer(40)6970// Force overflow: two appends produce more than 40 bytes.71tb.AppendString("0123456789") // 11 incl newline72tb.AppendString("ABCDEFGHIJKLMN") // 15 incl newline => total 26, still under73tb.AppendString(strings.Repeat("Z", 50))7475s := tb.String()7677// Must be capped78Expect(tb.Len()).To(BeNumerically("<=", 40))7980// Banner should appear and should be at the beginning (it’s prepended)81Expect(s).To(HavePrefix("\n…(truncated)\n"))8283// Only once as prefix84Expect(strings.Count(s, "…(truncated)")).To(Equal(1))85})8687it("does not add the banner when maxBytes is too small to fit it", func() {88// Banner length is > 5, so condition len(banner) < max is false.89tb := core.NewTranscriptBuffer(5)9091tb.AppendString("hello")92tb.AppendString("world") // overflow9394s := tb.String()95Expect(tb.Len()).To(BeNumerically("<=", 5))96Expect(s).NotTo(ContainSubstring("…(truncated)"))97})9899it("does not repeatedly prepend the banner across multiple truncations", func() {100tb := core.NewTranscriptBuffer(50)101102// First truncation103tb.AppendString(strings.Repeat("A", 200))104s1 := tb.String()105Expect(s1).To(HavePrefix("\n…(truncated)\n"))106Expect(strings.Count(s1, "…(truncated)")).To(Equal(1))107108// Second truncation (append more, should still be exactly one banner prefix)109tb.AppendString(strings.Repeat("B", 200))110s2 := tb.String()111Expect(tb.Len()).To(BeNumerically("<=", 50))112Expect(s2).To(HavePrefix("\n…(truncated)\n"))113Expect(strings.Count(s2, "…(truncated)")).To(Equal(1))114})115})116117when("Reset()", func() {118it("clears the buffer", func() {119tb := core.NewTranscriptBuffer(1024)120tb.AppendString("hello")121Expect(tb.Len()).To(BeNumerically(">", 0))122123tb.Reset()124Expect(tb.Len()).To(Equal(0))125Expect(tb.String()).To(Equal(""))126})127})128129when("nil receiver safety", func() {130it("does not panic and returns empty/zero values", func() {131var tb *core.TranscriptBuffer132133Expect(func() { tb.AppendString("hello") }).NotTo(Panic())134Expect(func() { tb.Appendf("x=%d", 1) }).NotTo(Panic())135Expect(func() { _ = tb.String() }).NotTo(Panic())136Expect(func() { _ = tb.Len() }).NotTo(Panic())137Expect(func() { tb.Reset() }).NotTo(Panic())138139Expect(tb.String()).To(Equal(""))140Expect(tb.Len()).To(Equal(0))141})142})143}144145146