diff --git a/internal/tui/model_test.go b/internal/tui/model_test.go new file mode 100644 index 0000000..cf7e218 --- /dev/null +++ b/internal/tui/model_test.go @@ -0,0 +1,76 @@ +package tui + +import ( + "testing" + "time" +) + +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") + } +}