summaryrefslogtreecommitdiff
path: root/internal/api/templates_test.go
blob: bbcfc87bc1cb9197f372f284fb020aaef739b7ad (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
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
package api

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

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

func TestListTemplates_Empty(t *testing.T) {
	srv, _ := testServer(t)

	req := httptest.NewRequest("GET", "/api/templates", 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 templates []storage.Template
	json.NewDecoder(w.Body).Decode(&templates)
	if len(templates) != 0 {
		t.Errorf("want 0 templates, got %d", len(templates))
	}
}

func TestCreateTemplate_Success(t *testing.T) {
	srv, _ := testServer(t)

	payload := `{
		"name": "Go: Run Tests",
		"description": "Run the full test suite with race detector",
		"claude": {
			"model": "sonnet",
			"instructions": "Run go test -race ./...",
			"max_budget_usd": 0.50,
			"allowed_tools": ["Bash"]
		},
		"timeout": "10m",
		"priority": "normal",
		"tags": ["go", "testing"]
	}`
	req := httptest.NewRequest("POST", "/api/templates", bytes.NewBufferString(payload))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)

	if w.Code != http.StatusCreated {
		t.Fatalf("status: want 201, got %d; body: %s", w.Code, w.Body.String())
	}
	var created storage.Template
	json.NewDecoder(w.Body).Decode(&created)
	if created.Name != "Go: Run Tests" {
		t.Errorf("name: want 'Go: Run Tests', got %q", created.Name)
	}
	if created.ID == "" {
		t.Error("expected auto-generated ID")
	}
}

func TestGetTemplate_AfterCreate(t *testing.T) {
	srv, _ := testServer(t)

	payload := `{"name": "Fetch Me", "claude": {"instructions": "do thing", "model": "haiku"}}`
	req := httptest.NewRequest("POST", "/api/templates", bytes.NewBufferString(payload))
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)
	if w.Code != http.StatusCreated {
		t.Fatalf("create: want 201, got %d", w.Code)
	}
	var created storage.Template
	json.NewDecoder(w.Body).Decode(&created)

	req2 := httptest.NewRequest("GET", fmt.Sprintf("/api/templates/%s", created.ID), nil)
	w2 := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w2, req2)

	if w2.Code != http.StatusOK {
		t.Fatalf("get: want 200, got %d; body: %s", w2.Code, w2.Body.String())
	}
	var fetched storage.Template
	json.NewDecoder(w2.Body).Decode(&fetched)
	if fetched.ID != created.ID {
		t.Errorf("id: want %q, got %q", created.ID, fetched.ID)
	}
	if fetched.Name != "Fetch Me" {
		t.Errorf("name: want 'Fetch Me', got %q", fetched.Name)
	}
}

func TestGetTemplate_NotFound(t *testing.T) {
	srv, _ := testServer(t)

	req := httptest.NewRequest("GET", "/api/templates/nonexistent", nil)
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)

	if w.Code != http.StatusNotFound {
		t.Errorf("status: want 404, got %d", w.Code)
	}
}

func TestUpdateTemplate(t *testing.T) {
	srv, _ := testServer(t)

	payload := `{"name": "Original Name", "claude": {"instructions": "original"}}`
	req := httptest.NewRequest("POST", "/api/templates", bytes.NewBufferString(payload))
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)
	var created storage.Template
	json.NewDecoder(w.Body).Decode(&created)

	update := `{"name": "Updated Name", "claude": {"instructions": "updated"}}`
	req2 := httptest.NewRequest("PUT", fmt.Sprintf("/api/templates/%s", created.ID), bytes.NewBufferString(update))
	w2 := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w2, req2)

	if w2.Code != http.StatusOK {
		t.Fatalf("update: want 200, got %d; body: %s", w2.Code, w2.Body.String())
	}
	var updated storage.Template
	json.NewDecoder(w2.Body).Decode(&updated)
	if updated.Name != "Updated Name" {
		t.Errorf("name: want 'Updated Name', got %q", updated.Name)
	}
}

func TestUpdateTemplate_NotFound(t *testing.T) {
	srv, _ := testServer(t)

	update := `{"name": "Ghost", "claude": {"instructions": "x"}}`
	req := httptest.NewRequest("PUT", "/api/templates/nonexistent", bytes.NewBufferString(update))
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)

	if w.Code != http.StatusNotFound {
		t.Errorf("status: want 404, got %d", w.Code)
	}
}

func TestDeleteTemplate(t *testing.T) {
	srv, _ := testServer(t)

	payload := `{"name": "To Delete", "claude": {"instructions": "bye"}}`
	req := httptest.NewRequest("POST", "/api/templates", bytes.NewBufferString(payload))
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)
	var created storage.Template
	json.NewDecoder(w.Body).Decode(&created)

	req2 := httptest.NewRequest("DELETE", fmt.Sprintf("/api/templates/%s", created.ID), nil)
	w2 := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w2, req2)

	if w2.Code != http.StatusNoContent {
		t.Fatalf("delete: want 204, got %d; body: %s", w2.Code, w2.Body.String())
	}

	// Subsequent GET returns 404.
	req3 := httptest.NewRequest("GET", fmt.Sprintf("/api/templates/%s", created.ID), nil)
	w3 := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w3, req3)

	if w3.Code != http.StatusNotFound {
		t.Fatalf("get after delete: want 404, got %d", w3.Code)
	}
}

func TestDeleteTemplate_NotFound(t *testing.T) {
	srv, _ := testServer(t)

	req := httptest.NewRequest("DELETE", "/api/templates/nonexistent", nil)
	w := httptest.NewRecorder()
	srv.Handler().ServeHTTP(w, req)

	if w.Code != http.StatusNotFound {
		t.Errorf("status: want 404, got %d", w.Code)
	}
}