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

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

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

	validResult := validateResult{
		Clarity:     "clear",
		Ready:       true,
		Summary:     "Instructions are clear and actionable.",
		Questions:   []validateQuestion{},
		Suggestions: []string{},
	}
	resultJSON, _ := json.Marshal(validResult)
	wrapper := map[string]string{"result": string(resultJSON)}
	wrapperJSON, _ := json.Marshal(wrapper)
	srv.validateCmdPath = createFakeClaude(t, string(wrapperJSON), 0)

	body := `{"name":"Test Task","claude":{"instructions":"Run go test ./... and report results."}}`
	req := httptest.NewRequest("POST", "/api/tasks/validate", bytes.NewBufferString(body))
	req.Header.Set("Content-Type", "application/json")
	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 result validateResult
	if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
		t.Fatalf("failed to decode response: %v", err)
	}
	if result.Clarity == "" {
		t.Error("expected non-empty clarity field in response")
	}
}

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

	body := `{"name":"Test Task","claude":{"instructions":""}}`
	req := httptest.NewRequest("POST", "/api/tasks/validate", bytes.NewBufferString(body))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()

	srv.Handler().ServeHTTP(w, req)

	if w.Code != http.StatusBadRequest {
		t.Fatalf("status: want 400, got %d; body: %s", w.Code, w.Body.String())
	}
}

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

	body := `{"name":"","claude":{"instructions":"Do something useful."}}`
	req := httptest.NewRequest("POST", "/api/tasks/validate", bytes.NewBufferString(body))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()

	srv.Handler().ServeHTTP(w, req)

	if w.Code != http.StatusBadRequest {
		t.Fatalf("status: want 400, got %d; body: %s", w.Code, w.Body.String())
	}
}

func TestValidateTask_BadJSONFromClaude(t *testing.T) {
	srv, _ := testServer(t)
	srv.validateCmdPath = createFakeClaude(t, "not valid json at all", 0)

	body := `{"name":"Test Task","claude":{"instructions":"Do something useful."}}`
	req := httptest.NewRequest("POST", "/api/tasks/validate", bytes.NewBufferString(body))
	req.Header.Set("Content-Type", "application/json")
	w := httptest.NewRecorder()

	srv.Handler().ServeHTTP(w, req)

	if w.Code != http.StatusBadGateway {
		t.Fatalf("status: want 502, got %d; body: %s", w.Code, w.Body.String())
	}
}