summaryrefslogtreecommitdiff
path: root/internal/api/server_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/api/server_test.go')
-rw-r--r--internal/api/server_test.go125
1 files changed, 125 insertions, 0 deletions
diff --git a/internal/api/server_test.go b/internal/api/server_test.go
index ec927c0..d090313 100644
--- a/internal/api/server_test.go
+++ b/internal/api/server_test.go
@@ -1433,3 +1433,128 @@ func TestRunTask_AgentCancelled_TaskSetToCancelled(t *testing.T) {
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"])
+ }
+}