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
|
package api
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/thepeterstone/claudomator/internal/task"
)
func TestCreateStory_API(t *testing.T) {
srv, _ := testServer(t)
body := `{"name":"My Story","project_id":"proj-1"}`
req := httptest.NewRequest("POST", "/api/stories", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
if w.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d: %s", w.Code, w.Body.String())
}
var st task.Story
if err := json.NewDecoder(w.Body).Decode(&st); err != nil {
t.Fatalf("decode response: %v", err)
}
if st.Name != "My Story" {
t.Errorf("Name: want 'My Story', got %q", st.Name)
}
if st.ID == "" {
t.Error("ID should be auto-generated")
}
if st.Status != task.StoryPending {
t.Errorf("Status: want PENDING, got %q", st.Status)
}
}
func TestGetStory_API(t *testing.T) {
srv, _ := testServer(t)
// Create a story first.
body := `{"name":"Get Me"}`
req := httptest.NewRequest("POST", "/api/stories", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
if w.Code != http.StatusCreated {
t.Fatalf("create story: expected 201, got %d", w.Code)
}
var created task.Story
json.NewDecoder(w.Body).Decode(&created)
// Fetch it.
req2 := httptest.NewRequest("GET", "/api/stories/"+created.ID, nil)
w2 := httptest.NewRecorder()
srv.mux.ServeHTTP(w2, req2)
if w2.Code != http.StatusOK {
t.Fatalf("get story: expected 200, got %d: %s", w2.Code, w2.Body.String())
}
var got task.Story
if err := json.NewDecoder(w2.Body).Decode(&got); err != nil {
t.Fatalf("decode: %v", err)
}
if got.ID != created.ID {
t.Errorf("ID: want %q, got %q", created.ID, got.ID)
}
if got.Name != "Get Me" {
t.Errorf("Name: want 'Get Me', got %q", got.Name)
}
}
func TestAddTaskToStory_AutoWiresDependsOn(t *testing.T) {
srv, _ := testServer(t)
// Create a story.
storyBody := `{"name":"Story For Tasks"}`
req := httptest.NewRequest("POST", "/api/stories", bytes.NewBufferString(storyBody))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
if w.Code != http.StatusCreated {
t.Fatalf("create story: %d %s", w.Code, w.Body.String())
}
var story task.Story
json.NewDecoder(w.Body).Decode(&story)
addTask := func(name string) *task.Task {
body := `{"name":"` + name + `","agent":{"type":"claude","instructions":"do it"}}`
r := httptest.NewRequest("POST", "/api/stories/"+story.ID+"/tasks", bytes.NewBufferString(body))
r.Header.Set("Content-Type", "application/json")
wr := httptest.NewRecorder()
srv.mux.ServeHTTP(wr, r)
if wr.Code != http.StatusCreated {
t.Fatalf("add task %s: expected 201, got %d: %s", name, wr.Code, wr.Body.String())
}
var tk task.Task
json.NewDecoder(wr.Body).Decode(&tk)
return &tk
}
task1 := addTask("Task 1")
task2 := addTask("Task 2")
task3 := addTask("Task 3")
// task1 has no dependencies.
if len(task1.DependsOn) != 0 {
t.Errorf("task1.DependsOn: want [], got %v", task1.DependsOn)
}
// task2 depends on task1.
if len(task2.DependsOn) != 1 || task2.DependsOn[0] != task1.ID {
t.Errorf("task2.DependsOn: want [%s], got %v", task1.ID, task2.DependsOn)
}
// task3 depends on task2.
if len(task3.DependsOn) != 1 || task3.DependsOn[0] != task2.ID {
t.Errorf("task3.DependsOn: want [%s], got %v", task2.ID, task3.DependsOn)
}
}
func TestBuildStoryElaboratePrompt(t *testing.T) {
prompt := buildStoryElaboratePrompt()
checks := []struct {
label string
want string
}{
{"schema: name field", `"name"`},
{"schema: branch_name field", `"branch_name"`},
{"schema: tasks field", `"tasks"`},
{"schema: validation field", `"validation"`},
{"rule: git push", "git push origin"},
{"rule: sequential subtasks", "sequentially"},
{"rule: specific file paths", "file paths"},
}
for _, c := range checks {
if !strings.Contains(prompt, c.want) {
t.Errorf("%s: prompt should contain %q", c.label, c.want)
}
}
}
func TestHandleStoryApprove_WiresDepends(t *testing.T) {
srv, _ := testServer(t)
body := `{
"name": "My Story",
"branch_name": "story/my-story",
"tasks": [
{"name": "Task 1", "instructions": "do task 1", "subtasks": []},
{"name": "Task 2", "instructions": "do task 2", "subtasks": []},
{"name": "Task 3", "instructions": "do task 3", "subtasks": []}
],
"validation": {"type": "build", "steps": ["go build ./..."], "success_criteria": "compiles"}
}`
req := httptest.NewRequest("POST", "/api/stories/approve", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
srv.mux.ServeHTTP(w, req)
if w.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d: %s", w.Code, w.Body.String())
}
var resp struct {
Story task.Story `json:"story"`
TaskIDs []string `json:"task_ids"`
}
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
t.Fatalf("decode response: %v", err)
}
if len(resp.TaskIDs) != 3 {
t.Fatalf("expected 3 task IDs, got %d", len(resp.TaskIDs))
}
if resp.Story.Name != "My Story" {
t.Errorf("story name: want 'My Story', got %q", resp.Story.Name)
}
// Verify depends_on chain via the store.
store := srv.store
task1, err := store.GetTask(resp.TaskIDs[0])
if err != nil {
t.Fatalf("GetTask[0]: %v", err)
}
task2, err := store.GetTask(resp.TaskIDs[1])
if err != nil {
t.Fatalf("GetTask[1]: %v", err)
}
task3, err := store.GetTask(resp.TaskIDs[2])
if err != nil {
t.Fatalf("GetTask[2]: %v", err)
}
if len(task1.DependsOn) != 0 {
t.Errorf("task1.DependsOn: want [], got %v", task1.DependsOn)
}
if len(task2.DependsOn) != 1 || task2.DependsOn[0] != task1.ID {
t.Errorf("task2.DependsOn: want [%s], got %v", task1.ID, task2.DependsOn)
}
if len(task3.DependsOn) != 1 || task3.DependsOn[0] != task2.ID {
t.Errorf("task3.DependsOn: want [%s], got %v", task2.ID, task3.DependsOn)
}
}
|