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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
package api
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/thepeterstone/claudomator/internal/storage"
"github.com/thepeterstone/claudomator/internal/task"
)
// createExecution inserts a test execution directly into the store.
func createExecution(t *testing.T, store *storage.DB, id, taskID string, start, end time.Time, status string) *storage.Execution {
t.Helper()
exec := &storage.Execution{
ID: id,
TaskID: taskID,
StartTime: start,
EndTime: end,
ExitCode: 0,
Status: status,
CostUSD: 0.001,
}
if err := store.CreateExecution(exec); err != nil {
t.Fatalf("createExecution: %v", err)
}
return exec
}
func TestListRecentExecutions_Empty(t *testing.T) {
srv, _ := testServer(t)
req := httptest.NewRequest("GET", "/api/executions", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: want 200, got %d; body: %s", w.Code, w.Body.String())
}
var execs []storage.RecentExecution
if err := json.NewDecoder(w.Body).Decode(&execs); err != nil {
t.Fatalf("decoding response: %v", err)
}
if len(execs) != 0 {
t.Errorf("want 0 executions, got %d", len(execs))
}
}
func TestListRecentExecutions_ReturnsCorrectShape(t *testing.T) {
srv, store := testServer(t)
tk := createTaskWithState(t, store, "exec-shape", task.StateCompleted)
now := time.Now().UTC().Truncate(time.Second)
end := now.Add(5 * time.Second)
exec := &storage.Execution{
ID: "e-shape",
TaskID: tk.ID,
StartTime: now,
EndTime: end,
ExitCode: 0,
Status: "COMPLETED",
CostUSD: 0.042,
}
if err := store.CreateExecution(exec); err != nil {
t.Fatalf("creating execution: %v", err)
}
req := httptest.NewRequest("GET", "/api/executions", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: want 200, got %d; body: %s", w.Code, w.Body.String())
}
var execs []storage.RecentExecution
if err := json.NewDecoder(w.Body).Decode(&execs); err != nil {
t.Fatalf("decoding response: %v", err)
}
if len(execs) != 1 {
t.Fatalf("want 1 execution, got %d", len(execs))
}
e := execs[0]
if e.ID != "e-shape" {
t.Errorf("id: want e-shape, got %q", e.ID)
}
if e.TaskID != tk.ID {
t.Errorf("task_id: want %q, got %q", tk.ID, e.TaskID)
}
if e.TaskName != tk.Name {
t.Errorf("task_name: want %q, got %q", tk.Name, e.TaskName)
}
if e.State != "COMPLETED" {
t.Errorf("state: want COMPLETED, got %q", e.State)
}
if e.CostUSD != 0.042 {
t.Errorf("cost_usd: want 0.042, got %f", e.CostUSD)
}
if e.DurationMS == nil {
t.Error("duration_ms: want non-nil for completed execution")
} else if *e.DurationMS != 5000 {
t.Errorf("duration_ms: want 5000, got %d", *e.DurationMS)
}
}
func TestListRecentExecutions_SinceFilter(t *testing.T) {
srv, store := testServer(t)
tk := createTaskWithState(t, store, "exec-since", task.StateCompleted)
oldStart := time.Now().UTC().Add(-48 * time.Hour)
recentStart := time.Now().UTC()
for i, start := range []time.Time{oldStart, recentStart} {
createExecution(t, store, fmt.Sprintf("e-since-%d", i), tk.ID, start, start.Add(time.Minute), "COMPLETED")
}
since := time.Now().UTC().Add(-25 * time.Hour).Format(time.RFC3339)
req := httptest.NewRequest("GET", "/api/executions?since="+since, nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: want 200, got %d; body: %s", w.Code, w.Body.String())
}
var execs []storage.RecentExecution
json.NewDecoder(w.Body).Decode(&execs)
if len(execs) != 1 {
t.Errorf("since filter: want 1 execution, got %d", len(execs))
}
}
func TestListRecentExecutions_TaskIDFilter(t *testing.T) {
srv, store := testServer(t)
tk1 := createTaskWithState(t, store, "filter-t1", task.StateCompleted)
tk2 := createTaskWithState(t, store, "filter-t2", task.StateCompleted)
now := time.Now().UTC()
createExecution(t, store, "e-filter-0", tk1.ID, now, now.Add(time.Minute), "COMPLETED")
createExecution(t, store, "e-filter-1", tk2.ID, now, now.Add(time.Minute), "COMPLETED")
req := httptest.NewRequest("GET", "/api/executions?task_id="+tk1.ID, nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: want 200, got %d; body: %s", w.Code, w.Body.String())
}
var execs []storage.RecentExecution
json.NewDecoder(w.Body).Decode(&execs)
if len(execs) != 1 {
t.Errorf("task_id filter: want 1 execution, got %d", len(execs))
}
if len(execs) > 0 && execs[0].TaskID != tk1.ID {
t.Errorf("task_id filter: want task_id=%q, got %q", tk1.ID, execs[0].TaskID)
}
}
func TestGetExecutionLog_NotFound(t *testing.T) {
srv, _ := testServer(t)
req := httptest.NewRequest("GET", "/api/executions/nonexistent/log", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Errorf("status: want 404, got %d", w.Code)
}
}
func TestGetExecutionLog_TailLines(t *testing.T) {
srv, store := testServer(t)
dir := t.TempDir()
logPath := filepath.Join(dir, "stdout.log")
var lines []string
for i := 0; i < 10; i++ {
lines = append(lines, fmt.Sprintf(`{"type":"text","line":%d}`, i))
}
if err := os.WriteFile(logPath, []byte(strings.Join(lines, "\n")+"\n"), 0600); err != nil {
t.Fatal(err)
}
tk := createTaskWithState(t, store, "log-tail", task.StateCompleted)
exec := &storage.Execution{
ID: "e-tail",
TaskID: tk.ID,
StartTime: time.Now().UTC(),
EndTime: time.Now().UTC().Add(time.Minute),
Status: "COMPLETED",
StdoutPath: logPath,
}
if err := store.CreateExecution(exec); err != nil {
t.Fatalf("creating execution: %v", err)
}
req := httptest.NewRequest("GET", "/api/executions/e-tail/log?tail=3", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status: want 200, got %d; body: %s", w.Code, w.Body.String())
}
if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/plain") {
t.Errorf("content-type: want text/plain, got %q", ct)
}
scanner := bufio.NewScanner(w.Body)
var got []string
for scanner.Scan() {
if line := scanner.Text(); line != "" {
got = append(got, line)
}
}
if len(got) != 3 {
t.Errorf("tail=3: want 3 lines, got %d: %v", len(got), got)
}
if len(got) > 0 && !strings.Contains(got[0], `"line":7`) {
t.Errorf("first tail line: want line 7, got %q", got[0])
}
}
func TestGetExecutionLog_FollowSSEHeaders(t *testing.T) {
srv, store := testServer(t)
dir := t.TempDir()
logPath := filepath.Join(dir, "stdout.log")
os.WriteFile(logPath, []byte(`{"type":"result","cost_usd":0.001}`+"\n"), 0600)
tk := createTaskWithState(t, store, "log-sse", task.StateCompleted)
exec := &storage.Execution{
ID: "e-sse",
TaskID: tk.ID,
StartTime: time.Now().UTC(),
EndTime: time.Now().UTC().Add(time.Minute),
Status: "COMPLETED",
StdoutPath: logPath,
}
if err := store.CreateExecution(exec); err != nil {
t.Fatalf("creating execution: %v", err)
}
req := httptest.NewRequest("GET", "/api/executions/e-sse/log?follow=true", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if ct := w.Header().Get("Content-Type"); ct != "text/event-stream" {
t.Errorf("content-type: want text/event-stream, got %q", ct)
}
if cc := w.Header().Get("Cache-Control"); cc != "no-cache" {
t.Errorf("cache-control: want no-cache, got %q", cc)
}
}
func TestListTasks_ReturnsStateField(t *testing.T) {
srv, store := testServer(t)
createTaskWithState(t, store, "state-check", task.StateRunning)
req := httptest.NewRequest("GET", "/api/tasks", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
var tasks []map[string]interface{}
json.NewDecoder(w.Body).Decode(&tasks)
if len(tasks) != 1 {
t.Fatalf("want 1 task, got %d", len(tasks))
}
if state, ok := tasks[0]["state"]; !ok || state == nil {
t.Error("task response missing 'state' field")
}
if tasks[0]["state"] != string(task.StateRunning) {
t.Errorf("state: want %q, got %q", task.StateRunning, tasks[0]["state"])
}
}
|