package api import ( "bytes" "encoding/json" "fmt" "net/http" "net/http/httptest" "os" "path/filepath" "testing" ) // createFakeClaude writes a stub `claude` script that prints the given output // and exits with the given code, returning its path for use as a binary // override in tests. func createFakeClaude(t *testing.T, output string, exitCode int) string { t.Helper() dir := t.TempDir() outputFile := filepath.Join(dir, "output.json") if err := os.WriteFile(outputFile, []byte(output), 0600); err != nil { t.Fatal(err) } script := filepath.Join(dir, "claude") content := fmt.Sprintf("#!/bin/sh\ncat %q\nexit %d\n", outputFile, exitCode) if err := os.WriteFile(script, []byte(content), 0755); err != nil { t.Fatal(err) } return script } 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","agent":{"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","agent":{"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":"","agent":{"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","agent":{"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()) } }