- Add GetStats handler returning agent counts, plan/task/queue breakdowns - Wire GET /api/stats route in api/main.go - Add stats_test.go with handler unit tests - Add StatsWidget.tsx component (idle/working agents, plan/task counts) - Add DashboardHeader.tsx displaying stats badge in the dashboard header
40 lines
933 B
Go
40 lines
933 B
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestGetStats(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest("GET", "/api/stats", nil)
|
|
|
|
// Create a mock handler with nil DB and PubSub (since we're not testing the actual DB queries)
|
|
h := &Handler{db: nil, ps: nil}
|
|
|
|
h.GetStats(c)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("expected 200, got %d", w.Code)
|
|
}
|
|
|
|
var body map[string]any
|
|
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("invalid json: %v", err)
|
|
}
|
|
|
|
// Check that all expected top-level fields are present
|
|
expectedFields := []string{"agent_count", "plan_counts", "task_counts", "queue_counts"}
|
|
for _, field := range expectedFields {
|
|
if _, ok := body[field]; !ok {
|
|
t.Errorf("missing expected field: %s", field)
|
|
}
|
|
}
|
|
}
|