plan/10: Add /api/stats endpoint and dashboard stats badge (#2)
Adds GET /api/stats endpoint with agent/plan/task/queue counts, stats widget in dashboard header, and Go test coverage.
This commit was merged in pull request #2.
This commit is contained in:
39
api/handlers/stats_test.go
Normal file
39
api/handlers/stats_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user