diff options
| author | Peter Stone <thepeterstone@gmail.com> | 2026-03-03 21:15:50 +0000 |
|---|---|---|
| committer | Peter Stone <thepeterstone@gmail.com> | 2026-03-03 21:15:50 +0000 |
| commit | 74cc740398cf2d90804ab19db728c844c2e056b7 (patch) | |
| tree | e8532d1da9273e1613beb7b762b16134da0de286 /internal/api/server_test.go | |
| parent | f527972f4d8311a09e639ede6c4da4ca669cfd5e (diff) | |
Add elaborate, logs-stream, templates, and subtask-list endpoints
- POST /api/tasks/elaborate: calls claude to draft a task config from
a natural-language prompt
- GET /api/executions/{id}/logs/stream: SSE tail of stdout.log
- CRUD /api/templates: create/list/get/update/delete reusable task configs
- GET /api/tasks/{id}/subtasks: list child tasks
- Server.NewServer accepts claudeBinPath for elaborate; injectable
elaborateCmdPath and logStore for test isolation
- Valid-transition guard added to POST /api/tasks/{id}/run
- CLI passes claude binary path through to the server
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/api/server_test.go')
| -rw-r--r-- | internal/api/server_test.go | 84 |
1 files changed, 83 insertions, 1 deletions
diff --git a/internal/api/server_test.go b/internal/api/server_test.go index 1628636..68f3657 100644 --- a/internal/api/server_test.go +++ b/internal/api/server_test.go @@ -30,7 +30,7 @@ func testServer(t *testing.T) (*Server, *storage.DB) { logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) runner := &mockRunner{} pool := executor.NewPool(2, runner, store, logger) - srv := NewServer(store, pool, logger) + srv := NewServer(store, pool, logger, "claude") return srv, store } @@ -170,6 +170,88 @@ func TestListTasks_WithTasks(t *testing.T) { } } +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, + Claude: task.ClaudeConfig{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) + } + if state != task.StatePending { + if err := store.UpdateTaskState(id, state); err != nil { + t.Fatalf("createTaskWithState: UpdateTaskState(%s): %v", state, 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_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) + wantMsg := "task cannot be queued from state COMPLETED" + if body["error"] != wantMsg { + t.Errorf("error body: want %q, got %q", wantMsg, body["error"]) + } +} + func TestCORS_Headers(t *testing.T) { srv, _ := testServer(t) |
