103 lines
2.4 KiB
Go
103 lines
2.4 KiB
Go
package tui
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/charmbracelet/bubbles/textarea"
|
|
)
|
|
|
|
func TestEventWriter(t *testing.T) {
|
|
ch := make(chan Event, 10)
|
|
w := NewEventWriter(ch)
|
|
|
|
w.Write([]byte("hello"))
|
|
w.Write([]byte(" world\n"))
|
|
|
|
select {
|
|
case ev := <-ch:
|
|
if ev.Type != "stream" {
|
|
t.Errorf("expected stream event, got %s", ev.Type)
|
|
}
|
|
if ev.Content != "hello world\n" {
|
|
t.Errorf("expected 'hello world\\n', got %s", ev.Content)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Error("timeout waiting for event")
|
|
}
|
|
}
|
|
|
|
func TestModelHandleEvent(t *testing.T) {
|
|
m := Model{
|
|
messages: []ChatMessage{},
|
|
events: make(chan Event, 10),
|
|
agentRuns: make(map[string]bool),
|
|
}
|
|
|
|
m.handleEvent(Event{Type: "stream", Content: "Hello"})
|
|
if len(m.messages) != 1 {
|
|
t.Fatalf("expected 1 message, got %d", len(m.messages))
|
|
}
|
|
if m.messages[0].Content != "Hello" {
|
|
t.Errorf("expected 'Hello', got %s", m.messages[0].Content)
|
|
}
|
|
|
|
m.handleEvent(Event{Type: "stream", Content: " World"})
|
|
if m.messages[0].Content != "Hello World" {
|
|
t.Errorf("expected 'Hello World', got %s", m.messages[0].Content)
|
|
}
|
|
|
|
m.handleEvent(Event{Type: "agent_start", Agent: "calculator"})
|
|
if !m.agentRuns["calculator"] {
|
|
t.Error("expected calculator to be running")
|
|
}
|
|
|
|
m.handleEvent(Event{Type: "agent_end", Agent: "calculator"})
|
|
if m.agentRuns["calculator"] {
|
|
t.Error("expected calculator to not be running")
|
|
}
|
|
}
|
|
|
|
func TestModelFormatMessage(t *testing.T) {
|
|
m := Model{}
|
|
|
|
user := m.formatMessage(ChatMessage{Role: "user", Content: "hi"})
|
|
if user == "" {
|
|
t.Error("user message should not be empty")
|
|
}
|
|
|
|
assistant := m.formatMessage(ChatMessage{Role: "assistant", Content: "hello"})
|
|
if assistant == "" {
|
|
t.Error("assistant message should not be empty")
|
|
}
|
|
|
|
system := m.formatMessage(ChatMessage{Role: "system", Content: "error"})
|
|
if system == "" {
|
|
t.Error("system message should not be empty")
|
|
}
|
|
}
|
|
|
|
func TestViewContainsHeader(t *testing.T) {
|
|
ta := textarea.New()
|
|
ta.ShowLineNumbers = false
|
|
ta.KeyMap.InsertNewline.SetEnabled(false)
|
|
|
|
m := Model{
|
|
width: 120,
|
|
height: 40,
|
|
textarea: ta,
|
|
messages: []ChatMessage{},
|
|
events: make(chan Event, 10),
|
|
agentRuns: make(map[string]bool),
|
|
}
|
|
m.updateLayout()
|
|
view := m.View()
|
|
if view == "Initializing..." {
|
|
t.Fatal("view should not be initializing")
|
|
}
|
|
if !strings.Contains(view, "orca.agent") {
|
|
t.Errorf("view should contain 'orca.agent', got:\n%s", view[:min(200, len(view))])
|
|
}
|
|
}
|