package api import ( "context" "net/http" "net/http/httptest" "strings" "testing" "time" "github.com/go-chi/chi/v5" "github.com/will/k8s-agent-dashboard/internal/claude" ) func TestClaudeStream_SendsEvent(t *testing.T) { hub := claude.NewEventHub(10) r := chi.NewRouter() r.Get("/api/claude/stream", GetClaudeStream(hub)) ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) defer cancel() req := httptest.NewRequest(http.MethodGet, "/api/claude/stream", nil).WithContext(ctx) w := httptest.NewRecorder() go func() { time.Sleep(10 * time.Millisecond) hub.Publish(claude.Event{Type: claude.EventTypeServerNotice, Data: map[string]any{"msg": "hi"}}) }() r.ServeHTTP(w, req) if ct := w.Header().Get("Content-Type"); !strings.Contains(ct, "text/event-stream") { t.Fatalf("content-type=%q", ct) } if !strings.Contains(w.Body.String(), "event:") || !strings.Contains(w.Body.String(), "data:") { t.Fatalf("body=%s", w.Body.String()) } }