Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion pkgs/chat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const (
host = "localhost"
port = 23234
recipientQueueDepth = 16

// maxHistory is the maximum number of chat messages retained per session.
// The viewport is 5 lines tall; capping history at 100 provides generous
// scrollback (20 full viewports) while bounding slice growth and preventing
// quadratic strings.Join re-render costs in long-lived sessions (Fixes #3268).
maxHistory = 100
)

// sender is the subset of tea.Program used for broadcast. Keeping the
Expand Down Expand Up @@ -335,7 +341,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

case chatMsg:
m.messages = append(m.messages, m.senderStyle.Render(msg.id)+": "+msg.text)
formatted := m.senderStyle.Render(msg.id) + ": " + msg.text
// Maintain a fixed-size history sliding window once maxHistory is reached.
// Avoids unbounded slice allocations and keeps strings.Join work bounded at O(maxHistory).
if len(m.messages) >= maxHistory {
copy(m.messages, m.messages[1:])
m.messages[len(m.messages)-1] = formatted
} else {
m.messages = append(m.messages, formatted)
}
m.viewport.SetContent(strings.Join(m.messages, "\n"))
m.viewport.GotoBottom()

Expand Down
74 changes: 74 additions & 0 deletions pkgs/chat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"runtime"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -445,3 +446,76 @@ func TestSelfSendFromUpdateIsDeliveredOnceInOrderWithoutBlocking(t *testing.T) {
t.Fatalf("self received %d Send calls for two updates and one barrier, want exactly 3", got)
}
}

func TestChatHistoryCap(t *testing.T) {
m := initialModel()

// Initial model starts with 0 messages
if len(m.messages) != 0 {
t.Fatalf("expected initial messages length 0, got %d", len(m.messages))
}

totalMessages := maxHistory + 50
for i := 1; i <= totalMessages; i++ {
text := fmt.Sprintf("message %d", i)
updatedModel, _ := m.Update(chatMsg{
id: "alice",
text: text,
})
m = updatedModel.(model)
}

// Verify messages slice is capped at maxHistory
if len(m.messages) != maxHistory {
t.Errorf("expected len(m.messages) == %d, got %d", maxHistory, len(m.messages))
}

// Oldest retained message should be totalMessages - maxHistory + 1
oldestExpected := fmt.Sprintf("message %d", totalMessages-maxHistory+1)
if !strings.Contains(m.messages[0], oldestExpected) {
t.Errorf("expected oldest message to contain %q, got %q", oldestExpected, m.messages[0])
}

// Newest message should be totalMessages
newestExpected := fmt.Sprintf("message %d", totalMessages)
if !strings.Contains(m.messages[len(m.messages)-1], newestExpected) {
t.Errorf("expected newest message to contain %q, got %q", newestExpected, m.messages[len(m.messages)-1])
}

// Verify the newest message is visible in viewport view
viewportContent := m.viewport.View()
if !strings.Contains(viewportContent, newestExpected) {
t.Errorf("expected viewport to show newest message %q, got viewport view:\n%s", newestExpected, viewportContent)
}

// Verify earlier dropped message is not in retained messages
droppedMsg := "message 1"
if strings.Contains(m.messages[0], droppedMsg) {
t.Errorf("expected dropped message %q to not be in m.messages[0], got %q", droppedMsg, m.messages[0])
}
}

func BenchmarkChatMsgUpdate(b *testing.B) {
m := initialModel()
// Pre-fill history to maxHistory
for i := 0; i < maxHistory; i++ {
updatedModel, _ := m.Update(chatMsg{
id: "benchuser",
text: fmt.Sprintf("setup message %d", i),
})
m = updatedModel.(model)
}

b.ResetTimer()
b.ReportAllocs()

msg := chatMsg{
id: "benchuser",
text: "streaming bench message content",
}

for i := 0; i < b.N; i++ {
updatedModel, _ := m.Update(msg)
m = updatedModel.(model)
}
}
Loading