summaryrefslogtreecommitdiff
path: root/internal/api/dashboard_test.go
blob: 2b80de3b29d0cf4a41c2b8c24a92b1db75108afb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package api

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/thepeterstone/claudomator/internal/storage"
	"github.com/thepeterstone/claudomator/internal/task"
)

func TestHandleGetEscalationFunnel_AggregatesWithinDefaultWindow(t *testing.T) {
	srv, store := testServer(t)
	tk := createTaskWithState(t, store, "funnel-task", task.StateCompleted)

	now := time.Now().UTC()
	mustCreateAPIExecution(t, store, "f1", tk.ID, "local", 0, 0, now.Add(-1*time.Hour))
	mustCreateAPIExecution(t, store, "f2", tk.ID, "local", 0, 0, now.Add(-2*time.Hour))
	mustCreateAPIExecution(t, store, "f3", tk.ID, "anthropic", 1, 0.10, now.Add(-3*time.Hour))
	// Outside the default 24h window.
	mustCreateAPIExecution(t, store, "f4", tk.ID, "local", 0, 0, now.Add(-48*time.Hour))

	req := httptest.NewRequest("GET", "/api/escalation-funnel", nil)
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)

	if w.Code != http.StatusOK {
		t.Fatalf("status: want 200, got %d: %s", w.Code, w.Body.String())
	}
	var buckets []storage.EscalationBucket
	if err := json.Unmarshal(w.Body.Bytes(), &buckets); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if len(buckets) != 2 {
		t.Fatalf("expected 2 buckets (rung 0/local, rung 1/anthropic), got %d: %+v", len(buckets), buckets)
	}
	for _, b := range buckets {
		if b.Rung == 0 && b.Agent == "local" && b.Count != 2 {
			t.Errorf("rung0/local count = %d, want 2", b.Count)
		}
		if b.Rung == 1 && b.Agent == "anthropic" && (b.Count != 1 || b.CostUSD != 0.10) {
			t.Errorf("rung1/anthropic = %+v, want count 1 cost 0.10", b)
		}
	}
}

func TestHandleGetEscalationFunnel_WindowParam_Narrows(t *testing.T) {
	srv, store := testServer(t)
	tk := createTaskWithState(t, store, "funnel-window-task", task.StateCompleted)
	now := time.Now().UTC()
	mustCreateAPIExecution(t, store, "fw1", tk.ID, "local", 0, 0, now.Add(-10*time.Hour))

	req := httptest.NewRequest("GET", "/api/escalation-funnel?window=5h", nil)
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)

	var buckets []storage.EscalationBucket
	if err := json.Unmarshal(w.Body.Bytes(), &buckets); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if len(buckets) != 0 {
		t.Errorf("expected no buckets within a 5h window, got %+v", buckets)
	}
}

func TestHandleGetSpendTimeseries_ReturnsPerProviderPoints(t *testing.T) {
	srv, store := testServer(t)
	tk := createTaskWithState(t, store, "spend-task", task.StateCompleted)
	now := time.Now().UTC()
	mustCreateAPIExecution(t, store, "s1", tk.ID, "local", 0, 0.01, now.Add(-1*time.Hour))
	mustCreateAPIExecution(t, store, "s2", tk.ID, "anthropic", 0, 0.20, now.Add(-1*time.Hour))

	req := httptest.NewRequest("GET", "/api/spend-timeseries", nil)
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)

	if w.Code != http.StatusOK {
		t.Fatalf("status: want 200, got %d: %s", w.Code, w.Body.String())
	}
	var points []storage.SpendPoint
	if err := json.Unmarshal(w.Body.Bytes(), &points); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	agents := map[string]bool{}
	for _, p := range points {
		agents[p.Agent] = true
	}
	if !agents["local"] || !agents["anthropic"] {
		t.Errorf("expected points for both local and anthropic, got %+v", points)
	}
}

func TestHandleGetSpendTimeseries_EmptyDB_ReturnsEmptyArray(t *testing.T) {
	srv, _ := testServer(t)
	req := httptest.NewRequest("GET", "/api/spend-timeseries", nil)
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)

	if w.Code != http.StatusOK {
		t.Fatalf("status: want 200, got %d", w.Code)
	}
	var points []storage.SpendPoint
	if err := json.Unmarshal(w.Body.Bytes(), &points); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if len(points) != 0 {
		t.Errorf("expected empty slice, got %+v", points)
	}
}

// mustCreateAPIExecution mirrors createExecution (executions_test.go) but
// also sets Agent/EscalationRung, which the funnel/spend-timeseries
// endpoints aggregate on.
func mustCreateAPIExecution(t *testing.T, store *storage.DB, id, taskID, agent string, rung int, cost float64, start time.Time) {
	t.Helper()
	exec := &storage.Execution{
		ID:             id,
		TaskID:         taskID,
		StartTime:      start,
		EndTime:        start.Add(time.Minute),
		Status:         "COMPLETED",
		CostUSD:        cost,
		Agent:          agent,
		EscalationRung: rung,
	}
	if err := store.CreateExecution(exec); err != nil {
		t.Fatalf("createExecution(%s): %v", id, err)
	}
}