package api import ( "bytes" "encoding/json" "errors" "fmt" "log/slog" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" "time" "context" "github.com/thepeterstone/claudomator/internal/executor" "github.com/thepeterstone/claudomator/internal/notify" "github.com/thepeterstone/claudomator/internal/storage" "github.com/thepeterstone/claudomator/internal/task" ) // mockNotifier records calls to Notify. type mockNotifier struct { events []notify.Event } func (m *mockNotifier) Notify(e notify.Event) error { m.events = append(m.events, e) return nil } func TestServer_ProcessResult_CallsNotifier(t *testing.T) { srv, store := testServer(t) mn := &mockNotifier{} srv.SetNotifier(mn) tk := &task.Task{ ID: "task-notifier-test", Name: "Notifier Task", State: task.StatePending, } if err := store.CreateTask(tk); err != nil { t.Fatal(err) } result := &executor.Result{ TaskID: tk.ID, Execution: &storage.Execution{ ID: "exec-1", TaskID: tk.ID, Status: "COMPLETED", CostUSD: 0.42, ErrorMsg: "", }, } srv.processResult(result) if len(mn.events) != 1 { t.Fatalf("expected 1 notify event, got %d", len(mn.events)) } ev := mn.events[0] if ev.TaskID != tk.ID { t.Errorf("event.TaskID = %q, want %q", ev.TaskID, tk.ID) } if ev.Status != "COMPLETED" { t.Errorf("event.Status = %q, want COMPLETED", ev.Status) } if ev.CostUSD != 0.42 { t.Errorf("event.CostUSD = %v, want 0.42", ev.CostUSD) } } func testServer(t *testing.T) (*Server, *storage.DB) { t.Helper() return testServerWithRunner(t, &mockRunner{}) } func testServerWithRunner(t *testing.T, runner executor.Runner) (*Server, *storage.DB) { t.Helper() dbPath := filepath.Join(t.TempDir(), "test.db") store, err := storage.Open(dbPath) if err != nil { t.Fatal(err) } t.Cleanup(func() { store.Close() }) logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) runners := map[string]executor.Runner{ "claude": runner, "gemini": runner, } pool := executor.NewPool(2, runners, store, logger) srv := NewServer(store, pool, logger, "claude", "gemini") return srv, store } type mockRunner struct { err error sleep time.Duration } func (m *mockRunner) Run(ctx context.Context, _ *task.Task, _ *storage.Execution) error { if m.sleep > 0 { select { case <-time.After(m.sleep): case <-ctx.Done(): return ctx.Err() } } return m.err } // pollState polls store.GetTask until the task reaches wantState or the timeout elapses. func pollState(t *testing.T, store *storage.DB, taskID string, wantState task.State, timeout time.Duration) task.State { t.Helper() deadline := time.Now().Add(timeout) for time.Now().Before(deadline) { tk, err := store.GetTask(taskID) if err == nil && tk.State == wantState { return tk.State } time.Sleep(5 * time.Millisecond) } tk, _ := store.GetTask(taskID) if tk != nil { return tk.State } return "" } func testServerWithGeminiMockRunner(t *testing.T) (*Server, *storage.DB) { t.Helper() dbPath := filepath.Join(t.TempDir(), "test.db") store, err := storage.Open(dbPath) if err != nil { t.Fatal(err) } t.Cleanup(func() { store.Close() }) logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) // Create the mock gemini binary script. mockBinDir := t.TempDir() mockGeminiPath := filepath.Join(mockBinDir, "mock-gemini-binary.sh") mockScriptContent := `#!/bin/bash OUTPUT_FILE=$(mktemp) echo "` + "```json" + `" > "$OUTPUT_FILE" echo "{\"type\":\"content_block_start\",\"content_block\":{\"text\":\"Hello, Gemini!\",\"type\":\"text\"}}" >> "$OUTPUT_FILE" echo "{\"type\":\"content_block_delta\",\"content_block\":{\"text\":\" How are you?\"}}" >> "$OUTPUT_FILE" echo "{\"type\":\"content_block_end\"}" >> "$OUTPUT_FILE" echo "{\"type\":\"message_delta\",\"message\":{\"role\":\"model\"}}" >> "$OUTPUT_FILE" echo "{\"type\":\"message_end\"}" >> "$OUTPUT_FILE" echo "` + "```" + `" >> "$OUTPUT_FILE" cat "$OUTPUT_FILE" rm "$OUTPUT_FILE" exit 0 ` if err := os.WriteFile(mockGeminiPath, []byte(mockScriptContent), 0755); err != nil { t.Fatalf("writing mock gemini script: %v", err) } // Configure GeminiRunner to use the mock script. geminiRunner := &executor.GeminiRunner{ BinaryPath: mockGeminiPath, Logger: logger, LogDir: t.TempDir(), // Ensure log directory is temporary for test APIURL: "http://localhost:8080", // Placeholder, not used by this mock } runners := map[string]executor.Runner{ "claude": &mockRunner{}, // Keep mock for claude to not interfere "gemini": geminiRunner, } pool := executor.NewPool(2, runners, store, logger) srv := NewServer(store, pool, logger, "claude", "gemini") // Pass original binary paths return srv, store } // TestGeminiLogs_ParsedCorrectly verifies that Gemini's markdown-wrapped stream-json // output is correctly unwrapped and parsed before being written to stdout.log // and exposed via the /api/tasks/{id}/executions/{exec-id}/log endpoint. func TestGeminiLogs_ParsedCorrectly(t *testing.T) { srv, store := testServerWithGeminiMockRunner(t) // Expected parsed JSON lines. expectedParsedLogs := []string{ `{"type":"content_block_start","content_block":{"text":"Hello, Gemini!","type":"text"}}`, `{"type":"content_block_delta","content_block":{"text":" How are you?"}}`, `{"type":"content_block_end"}`, `{"type":"message_delta","message":{"role":"model"}}`, `{"type":"message_end"}`, } // 1. Create a task with Gemini agent. tk := createTestTask(t, srv, `{ "name": "Gemini Log Test Task", "description": "Test Gemini log parsing", "agent": { "type": "gemini", "instructions": "generate some output", "model": "gemini-2.5-flash-lite" } }`) // 2. Run the task. req := httptest.NewRequest("POST", "/api/tasks/"+tk.ID+"/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Fatalf("run task status: want 202, got %d; body: %s", w.Code, w.Body.String()) } // 3. Wait for the task to complete. pollState(t, store, tk.ID, task.StateCompleted, 2*time.Second) // Re-fetch the task to ensure we have the updated execution details. updatedTask, err := store.GetTask(tk.ID) if err != nil { t.Fatalf("re-fetching task: %v", err) } // 4. Get the execution details to find the log path. executions, err := store.ListExecutions(updatedTask.ID) if err != nil { t.Fatalf("listing executions: %v", err) } if len(executions) != 1 { t.Fatalf("want 1 execution, got %d", len(executions)) } exec := executions[0] t.Logf("Re-fetched execution: %+v", exec) // Log the entire execution struct // 5. Verify the content of stdout.log directly. t.Logf("Attempting to read stdout.log from: %q", exec.StdoutPath) stdoutContent, err := os.ReadFile(exec.StdoutPath) if err != nil { t.Fatalf("reading stdout.log: %v", err) } stdoutLines := strings.Split(strings.TrimSpace(string(stdoutContent)), "\n") if len(stdoutLines) != len(expectedParsedLogs) { t.Errorf("stdout.log line count: want %d, got %d\nContent:\n%s", len(expectedParsedLogs), len(stdoutLines), stdoutContent) } for i, line := range stdoutLines { if i >= len(expectedParsedLogs) { break } if line != expectedParsedLogs[i] { t.Errorf("stdout.log line %d: want %q, got %q", i, expectedParsedLogs[i], line) } } // 6. Verify the content retrieved via the API endpoint. req = httptest.NewRequest("GET", "/api/executions/"+exec.ID+"/log", nil) w = httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("GET /log status: want 200, got %d; body: %s", w.Code, w.Body.String()) } apiLogContent := strings.TrimSpace(w.Body.String()) apiLogLines := strings.Split(apiLogContent, "\n") if len(apiLogLines) != len(expectedParsedLogs) { t.Errorf("API log line count: want %d, got %d\nContent:\n%s", len(expectedParsedLogs), len(apiLogLines), apiLogContent) } for i, line := range apiLogLines { if i >= len(expectedParsedLogs) { break } if line != expectedParsedLogs[i] { t.Errorf("API log line %d: want %q, got %q", i, expectedParsedLogs[i], line) } } } func TestListWorkspaces_UsesConfiguredRoot(t *testing.T) { srv, _ := testServer(t) root := t.TempDir() for _, name := range []string{"alpha", "beta", "gamma"} { if err := os.Mkdir(filepath.Join(root, name), 0755); err != nil { t.Fatal(err) } } // Also create a file (should be excluded from results). f, err := os.Create(filepath.Join(root, "notadir.txt")) if err != nil { t.Fatal(err) } f.Close() srv.SetWorkspaceRoot(root) req := httptest.NewRequest("GET", "/api/workspaces", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("status: want 200, got %d", w.Code) } var dirs []string if err := json.NewDecoder(w.Body).Decode(&dirs); err != nil { t.Fatalf("decode: %v", err) } want := map[string]bool{ root + "/alpha": true, root + "/beta": true, root + "/gamma": true, } if len(dirs) != len(want) { t.Fatalf("want %d dirs, got %d: %v", len(want), len(dirs), dirs) } for _, d := range dirs { if !want[d] { t.Errorf("unexpected dir in response: %s", d) } } } func TestHealthEndpoint(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("GET", "/api/health", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Errorf("status: want 200, got %d", w.Code) } var body map[string]string json.NewDecoder(w.Body).Decode(&body) if body["status"] != "ok" { t.Errorf("want status=ok, got %v", body) } } func TestCreateTask_Success(t *testing.T) { srv, _ := testServer(t) payload := `{ "name": "API Task", "description": "Created via API", "agent": { "type": "claude", "instructions": "do the thing", "model": "sonnet" }, "timeout": "5m", "tags": ["api"] }` req := httptest.NewRequest("POST", "/api/tasks", 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 task.Task json.NewDecoder(w.Body).Decode(&created) if created.Name != "API Task" { t.Errorf("name: want 'API Task', got %q", created.Name) } if created.ID == "" { t.Error("expected auto-generated ID") } } func TestCreateTask_InvalidJSON(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("POST", "/api/tasks", bytes.NewBufferString("{bad json")) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusBadRequest { t.Errorf("status: want 400, got %d", w.Code) } } func TestCreateTask_ValidationFailure(t *testing.T) { srv, _ := testServer(t) payload := `{"name": "", "agent": {"type": "claude", "instructions": ""}}` req := httptest.NewRequest("POST", "/api/tasks", bytes.NewBufferString(payload)) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusBadRequest { t.Errorf("status: want 400, got %d", w.Code) } } func TestProject_RoundTrip(t *testing.T) { srv, _ := testServer(t) payload := `{ "name": "Project Task", "project": "test-project", "agent": { "type": "claude", "instructions": "do the thing", "model": "sonnet" } }` req := httptest.NewRequest("POST", "/api/tasks", 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("create: want 201, got %d; body: %s", w.Code, w.Body.String()) } var created task.Task json.NewDecoder(w.Body).Decode(&created) if created.Project != "test-project" { t.Errorf("create response: project want 'test-project', got %q", created.Project) } // GET the task and verify project is persisted req2 := httptest.NewRequest("GET", "/api/tasks/"+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 task.Task json.NewDecoder(w2.Body).Decode(&fetched) if fetched.Project != "test-project" { t.Errorf("get response: project want 'test-project', got %q", fetched.Project) } } func TestListTasks_Empty(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("GET", "/api/tasks", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Errorf("status: want 200, got %d", w.Code) } var tasks []task.Task json.NewDecoder(w.Body).Decode(&tasks) if len(tasks) != 0 { t.Errorf("want 0 tasks, got %d", len(tasks)) } } func TestGetTask_NotFound(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("GET", "/api/tasks/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 TestListTasks_WithTasks(t *testing.T) { srv, store := testServer(t) // Create tasks directly in store. for i := 0; i < 3; i++ { tk := &task.Task{ ID: fmt.Sprintf("lt-%d", i), Name: fmt.Sprintf("T%d", i), Agent: task.AgentConfig{Type: "claude", Instructions: "x"}, Priority: task.PriorityNormal, Retry: task.RetryConfig{MaxAttempts: 1, Backoff: "linear"}, Tags: []string{}, DependsOn: []string{}, State: task.StatePending, } store.CreateTask(tk) } req := httptest.NewRequest("GET", "/api/tasks", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) var tasks []task.Task json.NewDecoder(w.Body).Decode(&tasks) if len(tasks) != 3 { t.Errorf("want 3 tasks, got %d", len(tasks)) } } // stateWalkPaths defines the sequence of intermediate states needed to reach each target state. var stateWalkPaths = map[task.State][]task.State{ task.StatePending: {}, task.StateQueued: {task.StateQueued}, task.StateRunning: {task.StateQueued, task.StateRunning}, task.StateCompleted: {task.StateQueued, task.StateRunning, task.StateCompleted}, task.StateFailed: {task.StateQueued, task.StateRunning, task.StateFailed}, task.StateTimedOut: {task.StateQueued, task.StateRunning, task.StateTimedOut}, task.StateCancelled: {task.StateCancelled}, task.StateBudgetExceeded: {task.StateQueued, task.StateRunning, task.StateBudgetExceeded}, task.StateReady: {task.StateQueued, task.StateRunning, task.StateReady}, task.StateBlocked: {task.StateQueued, task.StateRunning, task.StateBlocked}, } func createTaskWithState(t *testing.T, store *storage.DB, id string, state task.State) *task.Task { t.Helper() tk := &task.Task{ ID: id, Name: "test-task-" + id, Agent: task.AgentConfig{Type: "claude", Instructions: "do something"}, Priority: task.PriorityNormal, Retry: task.RetryConfig{MaxAttempts: 1, Backoff: "linear"}, Tags: []string{}, DependsOn: []string{}, State: task.StatePending, } if err := store.CreateTask(tk); err != nil { t.Fatalf("createTaskWithState: CreateTask: %v", err) } for _, s := range stateWalkPaths[state] { if err := store.UpdateTaskState(id, s); err != nil { t.Fatalf("createTaskWithState: UpdateTaskState(%s): %v", s, err) } } tk.State = state return tk } func TestRunTask_PendingTask_Returns202(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "run-pending", task.StatePending) req := httptest.NewRequest("POST", "/api/tasks/run-pending/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } } func TestRunTask_FailedTask_Returns202(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "run-failed", task.StateFailed) req := httptest.NewRequest("POST", "/api/tasks/run-failed/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } } func TestRunTask_TimedOutTask_Returns202(t *testing.T) { srv, store := testServer(t) // TIMED_OUT → QUEUED is a valid transition (retry path). // We need to get the task into TIMED_OUT state; storage allows direct state writes. createTaskWithState(t, store, "run-timedout", task.StateTimedOut) req := httptest.NewRequest("POST", "/api/tasks/run-timedout/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } } func TestRunTask_WithAgentParam(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "run-agent-param", task.StatePending) // Request run with agent=gemini. req := httptest.NewRequest("POST", "/api/tasks/run-agent-param/run?agent=gemini", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Fatalf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } // Wait for the task to complete via the mock runner. pollState(t, store, "run-agent-param", task.StateReady, 2*time.Second) } func TestRunTask_CompletedTask_Returns409(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "run-completed", task.StateCompleted) req := httptest.NewRequest("POST", "/api/tasks/run-completed/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusConflict { t.Errorf("status: want 409, got %d; body: %s", w.Code, w.Body.String()) } var body map[string]string json.NewDecoder(w.Body).Decode(&body) if !strings.Contains(body["error"], "invalid state transition") { t.Errorf("error body: want it to contain 'invalid state transition', got %q", body["error"]) } } func TestAcceptTask_ReadyTask_Returns200(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "accept-ready", task.StateReady) req := httptest.NewRequest("POST", "/api/tasks/accept-ready/accept", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Errorf("status: want 200, got %d; body: %s", w.Code, w.Body.String()) } got, _ := store.GetTask("accept-ready") if got.State != task.StateCompleted { t.Errorf("task state: want COMPLETED, got %v", got.State) } } func TestAcceptTask_NonReadyTask_Returns409(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "accept-pending", task.StatePending) req := httptest.NewRequest("POST", "/api/tasks/accept-pending/accept", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusConflict { t.Errorf("status: want 409, got %d; body: %s", w.Code, w.Body.String()) } } func TestRejectTask_ReadyTask_Returns200(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "reject-ready", task.StateReady) body := bytes.NewBufferString(`{"comment": "needs more detail"}`) req := httptest.NewRequest("POST", "/api/tasks/reject-ready/reject", body) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Errorf("status: want 200, got %d; body: %s", w.Code, w.Body.String()) } got, _ := store.GetTask("reject-ready") if got.State != task.StatePending { t.Errorf("task state: want PENDING, got %v", got.State) } if got.RejectionComment != "needs more detail" { t.Errorf("rejection_comment: want 'needs more detail', got %q", got.RejectionComment) } } func TestRejectTask_NonReadyTask_Returns409(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "reject-pending", task.StatePending) body := bytes.NewBufferString(`{"comment": "comment"}`) req := httptest.NewRequest("POST", "/api/tasks/reject-pending/reject", body) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusConflict { t.Errorf("status: want 409, got %d; body: %s", w.Code, w.Body.String()) } } func TestCORS_Headers(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("OPTIONS", "/api/tasks", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Header().Get("Access-Control-Allow-Origin") != "*" { t.Error("missing CORS origin header") } if w.Code != http.StatusOK { t.Errorf("OPTIONS status: want 200, got %d", w.Code) } } func TestAnswerQuestion_NoTask_Returns404(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("POST", "/api/tasks/nonexistent/answer", bytes.NewBufferString(`{"answer":"blue"}`)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusNotFound { t.Errorf("status: want 404, got %d; body: %s", w.Code, w.Body.String()) } } func TestAnswerQuestion_TaskNotBlocked_Returns409(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "answer-task-1", task.StatePending) req := httptest.NewRequest("POST", "/api/tasks/answer-task-1/answer", bytes.NewBufferString(`{"answer":"blue"}`)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusConflict { t.Errorf("status: want 409, got %d; body: %s", w.Code, w.Body.String()) } } func TestAnswerQuestion_MissingAnswer_Returns400(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "answer-task-2", task.StateBlocked) req := httptest.NewRequest("POST", "/api/tasks/answer-task-2/answer", bytes.NewBufferString(`{}`)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusBadRequest { t.Errorf("status: want 400, got %d; body: %s", w.Code, w.Body.String()) } } func TestAnswerQuestion_BlockedTask_QueuesResume(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "answer-task-3", task.StateBlocked) // Create an execution with a session ID, as the runner would have. exec := &storage.Execution{ ID: "exec-blocked-1", TaskID: "answer-task-3", SessionID: "550e8400-e29b-41d4-a716-446655440001", Status: "BLOCKED", } if err := store.CreateExecution(exec); err != nil { t.Fatalf("create execution: %v", err) } req := httptest.NewRequest("POST", "/api/tasks/answer-task-3/answer", bytes.NewBufferString(`{"answer":"main"}`)) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Errorf("status: want 200, got %d; body: %s", w.Code, w.Body.String()) } // Task should now be QUEUED (or RUNNING since the mock runner is instant). got, _ := store.GetTask("answer-task-3") if got.State != task.StateQueued && got.State != task.StateRunning && got.State != task.StateReady { t.Errorf("task state: want QUEUED/RUNNING/READY after answer, got %v", got.State) } } func TestHandleStartNextTask_Success(t *testing.T) { dir := t.TempDir() script := filepath.Join(dir, "start-next-task") if err := os.WriteFile(script, []byte("#!/bin/sh\necho 'claudomator start abc-123'\n"), 0755); err != nil { t.Fatal(err) } srv, _ := testServer(t) srv.SetScripts(ScriptRegistry{"start-next-task": script}) req := httptest.NewRequest("POST", "/api/scripts/start-next-task", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String()) } var body map[string]interface{} json.NewDecoder(w.Body).Decode(&body) if body["output"] != "claudomator start abc-123\n" { t.Errorf("unexpected output: %v", body["output"]) } if body["exit_code"] != float64(0) { t.Errorf("unexpected exit_code: %v", body["exit_code"]) } } func TestHandleStartNextTask_NoTask(t *testing.T) { dir := t.TempDir() script := filepath.Join(dir, "start-next-task") if err := os.WriteFile(script, []byte("#!/bin/sh\necho 'No task to start.'\n"), 0755); err != nil { t.Fatal(err) } srv, _ := testServer(t) srv.SetScripts(ScriptRegistry{"start-next-task": script}) req := httptest.NewRequest("POST", "/api/scripts/start-next-task", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String()) } var body map[string]interface{} json.NewDecoder(w.Body).Decode(&body) if body["output"] != "No task to start.\n" { t.Errorf("unexpected output: %v", body["output"]) } } func TestResumeTimedOut_NoTask_Returns404(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("POST", "/api/tasks/nonexistent/resume", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusNotFound { t.Errorf("status: want 404, got %d; body: %s", w.Code, w.Body.String()) } } func TestResumeTimedOut_TaskNotTimedOut_Returns409(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "resume-task-1", task.StatePending) req := httptest.NewRequest("POST", "/api/tasks/resume-task-1/resume", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusConflict { t.Errorf("status: want 409, got %d; body: %s", w.Code, w.Body.String()) } } func TestResumeTimedOut_NoSession_Returns500(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "resume-task-2", task.StateTimedOut) // No execution created — so no session ID. req := httptest.NewRequest("POST", "/api/tasks/resume-task-2/resume", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusInternalServerError { t.Errorf("status: want 500, got %d; body: %s", w.Code, w.Body.String()) } } func TestResumeTimedOut_Success_Returns202(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "resume-task-3", task.StateTimedOut) exec := &storage.Execution{ ID: "exec-timedout-1", TaskID: "resume-task-3", SessionID: "550e8400-e29b-41d4-a716-446655440002", Status: "TIMED_OUT", } if err := store.CreateExecution(exec); err != nil { t.Fatalf("create execution: %v", err) } req := httptest.NewRequest("POST", "/api/tasks/resume-task-3/resume", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } got, _ := store.GetTask("resume-task-3") if got.State != task.StateQueued && got.State != task.StateRunning && got.State != task.StateReady { t.Errorf("task state: want QUEUED/RUNNING/READY after resume, got %v", got.State) } } // TestRunTask_ManualRunIgnoresRetryLimit verifies that a manual POST /run always // succeeds regardless of MaxAttempts — retry limits only gate automatic retries. func TestRunTask_ManualRunIgnoresRetryLimit(t *testing.T) { srv, store := testServer(t) tk := &task.Task{ ID: "retry-limit-manual", Name: "Retry Limit Task", Agent: task.AgentConfig{Instructions: "do something"}, Priority: task.PriorityNormal, Retry: task.RetryConfig{MaxAttempts: 1, Backoff: "linear"}, Tags: []string{}, DependsOn: []string{}, State: task.StateFailed, } if err := store.CreateTask(tk); err != nil { t.Fatal(err) } // Record one execution — MaxAttempts already exhausted. exec := &storage.Execution{ ID: "exec-retry-manual", TaskID: "retry-limit-manual", StartTime: time.Now(), Status: "FAILED", } if err := store.CreateExecution(exec); err != nil { t.Fatal(err) } req := httptest.NewRequest("POST", "/api/tasks/retry-limit-manual/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) // Manual run must succeed (202) even though MaxAttempts is exhausted. if w.Code != http.StatusAccepted { t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } } func TestRunTask_WithinRetryLimit_Returns202(t *testing.T) { srv, store := testServer(t) // Task with MaxAttempts: 3 — 1 execution used, 2 remaining. tk := &task.Task{ ID: "retry-within-1", Name: "Retry Within Task", Agent: task.AgentConfig{Instructions: "do something"}, Priority: task.PriorityNormal, Retry: task.RetryConfig{MaxAttempts: 3, Backoff: "linear"}, Tags: []string{}, DependsOn: []string{}, State: task.StatePending, } if err := store.CreateTask(tk); err != nil { t.Fatal(err) } exec := &storage.Execution{ ID: "exec-within-1", TaskID: "retry-within-1", StartTime: time.Now(), Status: "FAILED", } if err := store.CreateExecution(exec); err != nil { t.Fatal(err) } store.UpdateTaskState("retry-within-1", task.StateFailed) req := httptest.NewRequest("POST", "/api/tasks/retry-within-1/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } } func TestHandleStartNextTask_ScriptNotFound(t *testing.T) { srv, _ := testServer(t) srv.SetScripts(ScriptRegistry{"start-next-task": "/nonexistent/start-next-task"}) req := httptest.NewRequest("POST", "/api/scripts/start-next-task", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusInternalServerError { t.Errorf("want 500, got %d; body: %s", w.Code, w.Body.String()) } } func TestDeleteTask_Success(t *testing.T) { srv, store := testServer(t) // Create a task to delete. created := createTestTask(t, srv, `{"name":"Delete Me","agent":{"type":"claude","instructions":"x","model":"sonnet"}}`) req := httptest.NewRequest("DELETE", "/api/tasks/"+created.ID, nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusNoContent { t.Fatalf("want 204, got %d; body: %s", w.Code, w.Body.String()) } _, err := store.GetTask(created.ID) if err == nil { t.Error("task should be deleted from store") } } func TestDeleteTask_NotFound(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("DELETE", "/api/tasks/does-not-exist", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusNotFound { t.Errorf("want 404, got %d", w.Code) } } func TestDeleteTask_RunningTaskRejected(t *testing.T) { srv, store := testServer(t) // Create the task directly in RUNNING state to avoid going through state transitions. tk := &task.Task{ ID: "running-task-del", Name: "Running Task", Agent: task.AgentConfig{Instructions: "x", Model: "sonnet"}, Priority: task.PriorityNormal, Tags: []string{}, DependsOn: []string{}, State: task.StateRunning, } if err := store.CreateTask(tk); err != nil { t.Fatal(err) } req := httptest.NewRequest("DELETE", "/api/tasks/running-task-del", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusConflict { t.Errorf("want 409 for running task, got %d", w.Code) } } // createTestTask is a helper that POSTs a task and returns the parsed Task. func createTestTask(t *testing.T, srv *Server, payload string) task.Task { t.Helper() req := httptest.NewRequest("POST", "/api/tasks", 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("createTestTask: want 201, got %d; body: %s", w.Code, w.Body.String()) } var tk task.Task json.NewDecoder(w.Body).Decode(&tk) return tk } func TestServer_CancelTask_Pending_TransitionsToCancelled(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "cancel-pending-1", task.StatePending) req := httptest.NewRequest("POST", "/api/tasks/cancel-pending-1/cancel", 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()) } updated, err := store.GetTask("cancel-pending-1") if err != nil { t.Fatal(err) } if updated.State != task.StateCancelled { t.Errorf("state: want CANCELLED, got %s", updated.State) } } func TestServer_CancelTask_Queued_TransitionsToCancelled(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "cancel-queued-1", task.StateQueued) req := httptest.NewRequest("POST", "/api/tasks/cancel-queued-1/cancel", 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()) } updated, err := store.GetTask("cancel-queued-1") if err != nil { t.Fatal(err) } if updated.State != task.StateCancelled { t.Errorf("state: want CANCELLED, got %s", updated.State) } } func TestServer_CancelTask_Blocked_TransitionsToCancelled(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "cancel-blocked-1", task.StateBlocked) req := httptest.NewRequest("POST", "/api/tasks/cancel-blocked-1/cancel", 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()) } updated, err := store.GetTask("cancel-blocked-1") if err != nil { t.Fatal(err) } if updated.State != task.StateCancelled { t.Errorf("state: want CANCELLED, got %s", updated.State) } } func TestServer_CancelTask_Completed_Returns409(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "cancel-completed-1", task.StateCompleted) req := httptest.NewRequest("POST", "/api/tasks/cancel-completed-1/cancel", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusConflict { t.Errorf("status: want 409, got %d; body: %s", w.Code, w.Body.String()) } } // mockQuestionStore implements questionStore for testing handleAnswerQuestion. type mockQuestionStore struct { getTaskFn func(id string) (*task.Task, error) getLatestExecutionFn func(taskID string) (*storage.Execution, error) updateTaskQuestionFn func(taskID, questionJSON string) error updateTaskStateFn func(id string, newState task.State) error appendInteractionFn func(taskID string, interaction task.Interaction) error } func (m *mockQuestionStore) GetTask(id string) (*task.Task, error) { return m.getTaskFn(id) } func (m *mockQuestionStore) GetLatestExecution(taskID string) (*storage.Execution, error) { return m.getLatestExecutionFn(taskID) } func (m *mockQuestionStore) UpdateTaskQuestion(taskID, questionJSON string) error { return m.updateTaskQuestionFn(taskID, questionJSON) } func (m *mockQuestionStore) UpdateTaskState(id string, newState task.State) error { return m.updateTaskStateFn(id, newState) } func (m *mockQuestionStore) AppendTaskInteraction(taskID string, interaction task.Interaction) error { if m.appendInteractionFn != nil { return m.appendInteractionFn(taskID, interaction) } return nil } func TestServer_AnswerQuestion_UpdateQuestionFails_Returns500(t *testing.T) { srv, _ := testServer(t) srv.questionStore = &mockQuestionStore{ getTaskFn: func(id string) (*task.Task, error) { return &task.Task{ID: id, State: task.StateBlocked}, nil }, getLatestExecutionFn: func(taskID string) (*storage.Execution, error) { return &storage.Execution{SessionID: "sess-1"}, nil }, updateTaskQuestionFn: func(taskID, questionJSON string) error { return fmt.Errorf("db error") }, updateTaskStateFn: func(id string, newState task.State) error { return nil }, } body := bytes.NewBufferString(`{"answer":"yes"}`) req := httptest.NewRequest("POST", "/api/tasks/task-1/answer", body) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusInternalServerError { t.Errorf("status: want 500, got %d; body: %s", w.Code, w.Body.String()) } } func TestServer_AnswerQuestion_UpdateStateFails_Returns500(t *testing.T) { srv, _ := testServer(t) srv.questionStore = &mockQuestionStore{ getTaskFn: func(id string) (*task.Task, error) { return &task.Task{ID: id, State: task.StateBlocked}, nil }, getLatestExecutionFn: func(taskID string) (*storage.Execution, error) { return &storage.Execution{SessionID: "sess-1"}, nil }, updateTaskQuestionFn: func(taskID, questionJSON string) error { return nil }, updateTaskStateFn: func(id string, newState task.State) error { return fmt.Errorf("db error") }, } body := bytes.NewBufferString(`{"answer":"yes"}`) req := httptest.NewRequest("POST", "/api/tasks/task-1/answer", body) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusInternalServerError { t.Errorf("status: want 500, got %d; body: %s", w.Code, w.Body.String()) } } func TestRateLimit_ElaborateRejectsExcess(t *testing.T) { srv, _ := testServer(t) // Use burst-1 and rate-0 so the second request from the same IP is rejected. srv.elaborateLimiter = newIPRateLimiter(0, 1) makeReq := func(remoteAddr string) int { req := httptest.NewRequest("POST", "/api/tasks/elaborate", bytes.NewBufferString(`{"description":"x"}`)) req.Header.Set("Content-Type", "application/json") req.RemoteAddr = remoteAddr w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) return w.Code } // First request from IP A: limiter allows it (non-429). if code := makeReq("192.0.2.1:1234"); code == http.StatusTooManyRequests { t.Errorf("first request should not be rate limited, got 429") } // Second request from IP A: bucket exhausted, must be 429. if code := makeReq("192.0.2.1:1234"); code != http.StatusTooManyRequests { t.Errorf("second request from same IP should be 429, got %d", code) } // First request from IP B: separate bucket, not limited. if code := makeReq("192.0.2.2:1234"); code == http.StatusTooManyRequests { t.Errorf("first request from different IP should not be rate limited, got 429") } } func TestListWorkspaces_RequiresAuth(t *testing.T) { srv, _ := testServer(t) srv.SetAPIToken("secret-token") // No token: expect 401. req := httptest.NewRequest("GET", "/api/workspaces", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusUnauthorized { t.Errorf("expected 401 without token, got %d", w.Code) } } func TestListWorkspaces_RejectsWrongToken(t *testing.T) { srv, _ := testServer(t) srv.SetAPIToken("secret-token") req := httptest.NewRequest("GET", "/api/workspaces", nil) req.Header.Set("Authorization", "Bearer wrong-token") w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusUnauthorized { t.Errorf("expected 401 with wrong token, got %d", w.Code) } } func TestListWorkspaces_SuppressesRawError(t *testing.T) { srv, _ := testServer(t) // No token configured so auth is bypassed; /workspace likely doesn't exist in test env. req := httptest.NewRequest("GET", "/api/workspaces", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code == http.StatusInternalServerError { body := w.Body.String() if strings.Contains(body, "/workspace") || strings.Contains(body, "no such file") { t.Errorf("response leaks filesystem details: %s", body) } } } func TestListTasks_InvalidState_Returns400(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("GET", "/api/tasks?state=BOGUS", nil) 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()) } var body map[string]string json.NewDecoder(w.Body).Decode(&body) if body["error"] == "" { t.Error("expected non-empty error message") } } func TestListTasks_ValidState_Returns200(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("GET", "/api/tasks?state=PENDING", 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()) } } func TestCancelTask_ResponseShape(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "cancel-shape-1", task.StatePending) req := httptest.NewRequest("POST", "/api/tasks/cancel-shape-1/cancel", 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 body map[string]string json.NewDecoder(w.Body).Decode(&body) if body["task_id"] != "cancel-shape-1" { t.Errorf("task_id: want 'cancel-shape-1', got %q", body["task_id"]) } if body["message"] == "" { t.Error("expected non-empty message field") } if _, hasStatus := body["status"]; hasStatus { t.Error("response must not contain legacy 'status' field") } } func TestAnswerQuestion_ResponseShape(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "answer-shape-1", task.StateBlocked) exec := &storage.Execution{ ID: "exec-shape-1", TaskID: "answer-shape-1", SessionID: "550e8400-e29b-41d4-a716-446655440010", Status: "BLOCKED", } if err := store.CreateExecution(exec); err != nil { t.Fatalf("create execution: %v", err) } req := httptest.NewRequest("POST", "/api/tasks/answer-shape-1/answer", bytes.NewBufferString(`{"answer":"yes"}`)) 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 body map[string]string json.NewDecoder(w.Body).Decode(&body) if body["task_id"] != "answer-shape-1" { t.Errorf("task_id: want 'answer-shape-1', got %q", body["task_id"]) } if body["message"] == "" { t.Error("expected non-empty message field") } if _, hasStatus := body["status"]; hasStatus { t.Error("response must not contain legacy 'status' field") } } func TestRunTask_ResponseShape(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "run-shape-1", task.StatePending) req := httptest.NewRequest("POST", "/api/tasks/run-shape-1/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Fatalf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } var body map[string]string json.NewDecoder(w.Body).Decode(&body) if body["task_id"] != "run-shape-1" { t.Errorf("task_id: want 'run-shape-1', got %q", body["task_id"]) } if body["message"] == "" { t.Error("expected non-empty message field") } } func TestResumeTimedOut_ResponseShape(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "resume-shape-1", task.StateTimedOut) exec := &storage.Execution{ ID: "exec-resume-shape-1", TaskID: "resume-shape-1", SessionID: "550e8400-e29b-41d4-a716-446655440020", Status: "TIMED_OUT", } if err := store.CreateExecution(exec); err != nil { t.Fatalf("create execution: %v", err) } req := httptest.NewRequest("POST", "/api/tasks/resume-shape-1/resume", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Fatalf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } var body map[string]string json.NewDecoder(w.Body).Decode(&body) if body["task_id"] != "resume-shape-1" { t.Errorf("task_id: want 'resume-shape-1', got %q", body["task_id"]) } if body["message"] == "" { t.Error("expected non-empty message field") } } func TestResumeInterrupted_Cancelled_Success_Returns202(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "resume-cancelled-1", task.StateCancelled) exec := &storage.Execution{ ID: "exec-cancelled-1", TaskID: "resume-cancelled-1", SessionID: "550e8400-e29b-41d4-a716-446655440030", Status: "CANCELLED", } if err := store.CreateExecution(exec); err != nil { t.Fatalf("create execution: %v", err) } req := httptest.NewRequest("POST", "/api/tasks/resume-cancelled-1/resume", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } got, _ := store.GetTask("resume-cancelled-1") if got.State != task.StateQueued && got.State != task.StateRunning && got.State != task.StateReady { t.Errorf("task state: want QUEUED/RUNNING/READY after resume, got %v", got.State) } } func TestResumeInterrupted_Failed_Success_Returns202(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "resume-failed-1", task.StateFailed) exec := &storage.Execution{ ID: "exec-failed-1", TaskID: "resume-failed-1", SessionID: "550e8400-e29b-41d4-a716-446655440031", Status: "FAILED", } if err := store.CreateExecution(exec); err != nil { t.Fatalf("create execution: %v", err) } req := httptest.NewRequest("POST", "/api/tasks/resume-failed-1/resume", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } got, _ := store.GetTask("resume-failed-1") if got.State != task.StateQueued && got.State != task.StateRunning && got.State != task.StateReady { t.Errorf("task state: want QUEUED/RUNNING/READY after resume, got %v", got.State) } } func TestResumeInterrupted_BudgetExceeded_Success_Returns202(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "resume-budget-1", task.StateBudgetExceeded) exec := &storage.Execution{ ID: "exec-budget-1", TaskID: "resume-budget-1", SessionID: "550e8400-e29b-41d4-a716-446655440032", Status: "BUDGET_EXCEEDED", } if err := store.CreateExecution(exec); err != nil { t.Fatalf("create execution: %v", err) } req := httptest.NewRequest("POST", "/api/tasks/resume-budget-1/resume", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Errorf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } got, _ := store.GetTask("resume-budget-1") if got.State != task.StateQueued && got.State != task.StateRunning && got.State != task.StateReady { t.Errorf("task state: want QUEUED/RUNNING/READY after resume, got %v", got.State) } } func TestResumeInterrupted_NoSession_Returns500(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "resume-nosess-1", task.StateCancelled) // No execution — no session ID available. req := httptest.NewRequest("POST", "/api/tasks/resume-nosess-1/resume", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusInternalServerError { t.Errorf("status: want 500, got %d; body: %s", w.Code, w.Body.String()) } } func TestResumeInterrupted_WrongState_Returns409(t *testing.T) { srv, store := testServer(t) createTaskWithState(t, store, "resume-wrong-1", task.StatePending) req := httptest.NewRequest("POST", "/api/tasks/resume-wrong-1/resume", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusConflict { t.Errorf("status: want 409, got %d; body: %s", w.Code, w.Body.String()) } } func TestRateLimit_ValidateRejectsExcess(t *testing.T) { srv, _ := testServer(t) srv.elaborateLimiter = newIPRateLimiter(0, 1) makeReq := func(remoteAddr string) int { req := httptest.NewRequest("POST", "/api/tasks/validate", bytes.NewBufferString(`{"description":"x"}`)) req.Header.Set("Content-Type", "application/json") req.RemoteAddr = remoteAddr w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) return w.Code } if code := makeReq("192.0.2.1:1234"); code == http.StatusTooManyRequests { t.Errorf("first validate request should not be rate limited, got 429") } if code := makeReq("192.0.2.1:1234"); code != http.StatusTooManyRequests { t.Errorf("second validate request from same IP should be 429, got %d", code) } } func TestRunTask_AgentFails_TaskSetToFailed(t *testing.T) { runner := &mockRunner{err: errors.New("agent error")} srv, store := testServerWithRunner(t, runner) createTaskWithState(t, store, "async-fail-1", task.StatePending) req := httptest.NewRequest("POST", "/api/tasks/async-fail-1/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Fatalf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } got := pollState(t, store, "async-fail-1", task.StateFailed, 2*time.Second) if got != task.StateFailed { t.Errorf("task state: want FAILED, got %v", got) } } func TestRunTask_AgentTimesOut_TaskSetToTimedOut(t *testing.T) { runner := &mockRunner{sleep: 5 * time.Second} srv, store := testServerWithRunner(t, runner) tk := &task.Task{ ID: "async-timeout-1", Name: "timeout-test", Agent: task.AgentConfig{Type: "claude", Instructions: "do something"}, Priority: task.PriorityNormal, Retry: task.RetryConfig{MaxAttempts: 1, Backoff: "linear"}, Tags: []string{}, DependsOn: []string{}, State: task.StatePending, Timeout: task.Duration{Duration: 50 * time.Millisecond}, } if err := store.CreateTask(tk); err != nil { t.Fatal(err) } req := httptest.NewRequest("POST", "/api/tasks/async-timeout-1/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Fatalf("status: want 202, got %d; body: %s", w.Code, w.Body.String()) } got := pollState(t, store, "async-timeout-1", task.StateTimedOut, 2*time.Second) if got != task.StateTimedOut { t.Errorf("task state: want TIMED_OUT, got %v", got) } } func TestRunTask_AgentCancelled_TaskSetToCancelled(t *testing.T) { runner := &mockRunner{sleep: 5 * time.Second} srv, store := testServerWithRunner(t, runner) createTaskWithState(t, store, "async-cancel-1", task.StatePending) req := httptest.NewRequest("POST", "/api/tasks/async-cancel-1/run", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusAccepted { t.Fatalf("POST /run status: want 202, got %d; body: %s", w.Code, w.Body.String()) } // Wait for the pool to start the task (cancel func must be registered). pollState(t, store, "async-cancel-1", task.StateRunning, 2*time.Second) // Cancel via the API — pool.Cancel() cancels the context; runner returns ctx.Err(). cancelReq := httptest.NewRequest("POST", "/api/tasks/async-cancel-1/cancel", nil) cancelW := httptest.NewRecorder() srv.Handler().ServeHTTP(cancelW, cancelReq) if cancelW.Code != http.StatusOK { t.Fatalf("POST /cancel status: want 200, got %d; body: %s", cancelW.Code, cancelW.Body.String()) } got := pollState(t, store, "async-cancel-1", task.StateCancelled, 2*time.Second) if got != task.StateCancelled { t.Errorf("task state: want CANCELLED, got %v", got) } } // TestGetTask_IncludesChangestats verifies that after processResult parses git diff stats // from the execution stdout log, they appear in the execution history response. func TestGetTask_IncludesChangestats(t *testing.T) { srv, store := testServer(t) tk := createTaskWithState(t, store, "cs-task-1", task.StateCompleted) // Write a stdout log with a git diff --stat summary line. dir := t.TempDir() stdoutPath := filepath.Join(dir, "stdout.log") logContent := "Agent output line 1\n3 files changed, 50 insertions(+), 10 deletions(-)\nAgent output line 2\n" if err := os.WriteFile(stdoutPath, []byte(logContent), 0600); err != nil { t.Fatal(err) } exec := &storage.Execution{ ID: "cs-exec-1", TaskID: tk.ID, StartTime: time.Now().UTC(), EndTime: time.Now().UTC().Add(time.Minute), Status: "COMPLETED", StdoutPath: stdoutPath, } if err := store.CreateExecution(exec); err != nil { t.Fatal(err) } // processResult should parse changestats from the stdout log and store them. result := &executor.Result{ TaskID: tk.ID, Execution: exec, } srv.processResult(result) // GET the task's execution history and assert changestats are populated. req := httptest.NewRequest("GET", "/api/tasks/"+tk.ID+"/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 []map[string]interface{} if err := json.NewDecoder(w.Body).Decode(&execs); err != nil { t.Fatalf("decode: %v", err) } if len(execs) != 1 { t.Fatalf("want 1 execution, got %d", len(execs)) } csVal, ok := execs[0]["Changestats"] if !ok || csVal == nil { t.Fatal("execution missing Changestats field after processResult") } csMap, ok := csVal.(map[string]interface{}) if !ok { t.Fatalf("Changestats is not an object: %T", csVal) } if csMap["files_changed"].(float64) != 3 { t.Errorf("files_changed: want 3, got %v", csMap["files_changed"]) } if csMap["lines_added"].(float64) != 50 { t.Errorf("lines_added: want 50, got %v", csMap["lines_added"]) } if csMap["lines_removed"].(float64) != 10 { t.Errorf("lines_removed: want 10, got %v", csMap["lines_removed"]) } } // TestListExecutions_IncludesChangestats verifies that changestats stored on an execution // are returned correctly by GET /api/tasks/{id}/executions. func TestListExecutions_IncludesChangestats(t *testing.T) { srv, store := testServer(t) tk := createTaskWithState(t, store, "cs-task-2", task.StateCompleted) cs := &task.Changestats{FilesChanged: 2, LinesAdded: 100, LinesRemoved: 20} exec := &storage.Execution{ ID: "cs-exec-2", TaskID: tk.ID, StartTime: time.Now().UTC(), EndTime: time.Now().UTC().Add(time.Minute), Status: "COMPLETED", Changestats: cs, } if err := store.CreateExecution(exec); err != nil { t.Fatal(err) } req := httptest.NewRequest("GET", "/api/tasks/"+tk.ID+"/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 []map[string]interface{} if err := json.NewDecoder(w.Body).Decode(&execs); err != nil { t.Fatalf("decode: %v", err) } if len(execs) != 1 { t.Fatalf("want 1 execution, got %d", len(execs)) } csVal, ok := execs[0]["Changestats"] if !ok || csVal == nil { t.Fatal("execution missing Changestats field") } csMap, ok := csVal.(map[string]interface{}) if !ok { t.Fatalf("Changestats is not an object: %T", csVal) } if csMap["files_changed"].(float64) != 2 { t.Errorf("files_changed: want 2, got %v", csMap["files_changed"]) } if csMap["lines_added"].(float64) != 100 { t.Errorf("lines_added: want 100, got %v", csMap["lines_added"]) } if csMap["lines_removed"].(float64) != 20 { t.Errorf("lines_removed: want 20, got %v", csMap["lines_removed"]) } } // TestDeploymentStatus_ReturnsStatusForReadyTask verifies that // GET /api/tasks/{id}/deployment-status returns a valid deployment status // with deployed_commit, fix_commits, and includes_fix fields. func TestDeploymentStatus_ReturnsStatusForReadyTask(t *testing.T) { srv, store := testServer(t) // Create a READY task using the walk-path helper. tk := createTaskWithState(t, store, "deploy-status-task-1", task.StateReady) // Create an execution with commits. exec := &storage.Execution{ ID: "deploy-exec-1", TaskID: tk.ID, StartTime: time.Now(), EndTime: time.Now(), Status: "COMPLETED", Commits: []task.GitCommit{ {Hash: "abc123def456", Message: "fix: resolve the bug"}, }, } if err := store.CreateExecution(exec); err != nil { t.Fatal(err) } // GET /api/tasks/{id}/deployment-status req := httptest.NewRequest("GET", "/api/tasks/deploy-status-task-1/deployment-status", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String()) } var resp map[string]interface{} if err := json.NewDecoder(w.Body).Decode(&resp); err != nil { t.Fatalf("decode response: %v", err) } // deployed_commit must be present (will be "dev" in test environment). if _, ok := resp["deployed_commit"]; !ok { t.Error("response missing deployed_commit field") } // fix_commits must be an array. fixCommits, ok := resp["fix_commits"] if !ok { t.Fatal("response missing fix_commits field") } commits, ok := fixCommits.([]interface{}) if !ok { t.Fatalf("fix_commits is not an array: %T", fixCommits) } if len(commits) != 1 { t.Fatalf("fix_commits length: want 1, got %d", len(commits)) } commit := commits[0].(map[string]interface{}) if commit["hash"] != "abc123def456" { t.Errorf("fix_commits[0].hash: want abc123def456, got %v", commit["hash"]) } // includes_fix must be present (false in test env since version is "dev"). if _, ok := resp["includes_fix"]; !ok { t.Error("response missing includes_fix field") } } // TestDeploymentStatus_NotFound returns 404 for unknown task. func TestDeploymentStatus_NotFound(t *testing.T) { srv, _ := testServer(t) req := httptest.NewRequest("GET", "/api/tasks/nonexistent-task/deployment-status", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusNotFound { t.Fatalf("want 404, got %d", w.Code) } } // TestListTasks_ReadyTask_IncludesDeploymentStatus verifies that GET /api/tasks // returns a deployment_status field for READY tasks containing deployed_commit, // fix_commits, and includes_fix. func TestListTasks_ReadyTask_IncludesDeploymentStatus(t *testing.T) { srv, store := testServer(t) tk := createTaskWithState(t, store, "enrich-list-ready-1", task.StateReady) exec := &storage.Execution{ ID: "enrich-list-exec-1", TaskID: tk.ID, StartTime: time.Now(), EndTime: time.Now(), Status: "COMPLETED", Commits: []task.GitCommit{{Hash: "aabbcc", Message: "fix: list test"}}, } if err := store.CreateExecution(exec); err != nil { t.Fatal(err) } req := httptest.NewRequest("GET", "/api/tasks", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("want 200, got %d; body: %s", w.Code, w.Body.String()) } var tasks []map[string]interface{} if err := json.NewDecoder(w.Body).Decode(&tasks); err != nil { t.Fatalf("decode: %v", err) } var found map[string]interface{} for _, tsk := range tasks { if tsk["id"] == tk.ID { found = tsk break } } if found == nil { t.Fatalf("task %q not found in list response", tk.ID) } ds, ok := found["deployment_status"].(map[string]interface{}) if !ok { t.Fatalf("READY task missing deployment_status field; got: %v", found["deployment_status"]) } if _, ok := ds["deployed_commit"]; !ok { t.Error("deployment_status missing deployed_commit") } if _, ok := ds["includes_fix"]; !ok { t.Error("deployment_status missing includes_fix") } } // TestGetTask_ReadyTask_IncludesDeploymentStatus verifies that GET /api/tasks/{id} // returns a deployment_status field for a READY task. func TestGetTask_ReadyTask_IncludesDeploymentStatus(t *testing.T) { srv, store := testServer(t) tk := createTaskWithState(t, store, "enrich-get-ready-1", task.StateReady) exec := &storage.Execution{ ID: "enrich-get-exec-1", TaskID: tk.ID, StartTime: time.Now(), EndTime: time.Now(), Status: "COMPLETED", Commits: []task.GitCommit{{Hash: "ddeeff", Message: "fix: get test"}}, } if err := store.CreateExecution(exec); err != nil { t.Fatal(err) } req := httptest.NewRequest("GET", "/api/tasks/"+tk.ID, nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("want 200, got %d", w.Code) } var resp map[string]interface{} if err := json.NewDecoder(w.Body).Decode(&resp); err != nil { t.Fatalf("decode: %v", err) } ds, ok := resp["deployment_status"].(map[string]interface{}) if !ok { t.Fatalf("READY task GET response missing deployment_status; got: %v", resp["deployment_status"]) } if _, ok := ds["deployed_commit"]; !ok { t.Error("deployment_status missing deployed_commit") } if _, ok := ds["includes_fix"]; !ok { t.Error("deployment_status missing includes_fix") } } // TestListTasks_NonReadyTask_OmitsDeploymentStatus verifies that non-READY tasks // (e.g. PENDING) do not include a deployment_status field. func TestListTasks_NonReadyTask_OmitsDeploymentStatus(t *testing.T) { srv, store := testServer(t) tk := createTaskWithState(t, store, "enrich-list-pending-1", task.StatePending) req := httptest.NewRequest("GET", "/api/tasks", nil) w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, req) if w.Code != http.StatusOK { t.Fatalf("want 200, got %d", w.Code) } var tasks []map[string]interface{} if err := json.NewDecoder(w.Body).Decode(&tasks); err != nil { t.Fatalf("decode: %v", err) } var found map[string]interface{} for _, tsk := range tasks { if tsk["id"] == tk.ID { found = tsk break } } if found == nil { t.Fatalf("task %q not found in list", tk.ID) } if _, ok := found["deployment_status"]; ok { t.Error("PENDING task should not include deployment_status field") } }