summaryrefslogtreecommitdiff
path: root/internal/api/stories_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/api/stories_test.go')
-rw-r--r--internal/api/stories_test.go131
1 files changed, 131 insertions, 0 deletions
diff --git a/internal/api/stories_test.go b/internal/api/stories_test.go
index b346af5..4cc2f17 100644
--- a/internal/api/stories_test.go
+++ b/internal/api/stories_test.go
@@ -8,6 +8,7 @@ import (
"testing"
"time"
+ "github.com/thepeterstone/claudomator/internal/event"
"github.com/thepeterstone/claudomator/internal/story"
"github.com/thepeterstone/claudomator/internal/task"
)
@@ -276,3 +277,133 @@ func TestServer_StoryTaskTree_UnknownStory_Returns404(t *testing.T) {
t.Fatalf("expected 404, got %d: %s", w.Code, w.Body.String())
}
}
+
+// TestServer_AcceptStory_SucceedsFromReviewReady is verification item (e)
+// from the Phase 7b task description: POST /api/stories/{id}/accept
+// transitions REVIEW_READY -> DONE and emits a KindHumanAccepted event
+// attached to the story's ID.
+func TestServer_AcceptStory_SucceedsFromReviewReady(t *testing.T) {
+ srv, store := testServer(t)
+ if err := store.CreateStory(&story.Story{ID: "s1", Name: "story", Status: "REVIEW_READY"}); err != nil {
+ t.Fatal(err)
+ }
+
+ req := httptest.NewRequest("POST", "/api/stories/s1/accept", nil)
+ w := httptest.NewRecorder()
+ srv.Handler().ServeHTTP(w, req)
+ if w.Code != http.StatusOK {
+ t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
+ }
+
+ got, err := store.GetStory("s1")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got.Status != "DONE" {
+ t.Errorf("Status: want DONE, got %q", got.Status)
+ }
+
+ evs, err := store.ListEvents("s1", 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var found bool
+ for _, e := range evs {
+ if e.Kind == event.KindHumanAccepted {
+ found = true
+ var payload struct {
+ StoryID string `json:"story_id"`
+ From string `json:"from"`
+ To string `json:"to"`
+ }
+ if err := json.Unmarshal(e.Payload, &payload); err != nil {
+ t.Fatalf("unmarshal payload: %v", err)
+ }
+ if payload.StoryID != "s1" || payload.From != "REVIEW_READY" || payload.To != "DONE" {
+ t.Errorf("unexpected payload: %+v", payload)
+ }
+ }
+ }
+ if !found {
+ t.Error("expected a KindHumanAccepted event attached to the story's event stream")
+ }
+}
+
+// TestServer_AcceptStory_FailsFromOtherStatuses is the other half of
+// verification item (e): accept must be rejected (409) from any status other
+// than REVIEW_READY.
+func TestServer_AcceptStory_FailsFromOtherStatuses(t *testing.T) {
+ for _, status := range []string{"DISCOVERY", "VALIDATING", "NEEDS_FIX", "DONE", "IN_PROGRESS"} {
+ t.Run(status, func(t *testing.T) {
+ srv, store := testServer(t)
+ id := "s-" + status
+ if err := store.CreateStory(&story.Story{ID: id, Name: "story", Status: status}); err != nil {
+ t.Fatal(err)
+ }
+
+ req := httptest.NewRequest("POST", "/api/stories/"+id+"/accept", nil)
+ w := httptest.NewRecorder()
+ srv.Handler().ServeHTTP(w, req)
+ if w.Code != http.StatusConflict {
+ t.Fatalf("expected 409, got %d: %s", w.Code, w.Body.String())
+ }
+
+ got, err := store.GetStory(id)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got.Status != status {
+ t.Errorf("status must not change on rejected accept: want %q, got %q", status, got.Status)
+ }
+ })
+ }
+}
+
+func TestServer_AcceptStory_NotFound(t *testing.T) {
+ srv, _ := testServer(t)
+ req := httptest.NewRequest("POST", "/api/stories/does-not-exist/accept", nil)
+ w := httptest.NewRecorder()
+ srv.Handler().ServeHTTP(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Fatalf("expected 404, got %d: %s", w.Code, w.Body.String())
+ }
+}
+
+// TestServer_ListStoryEvents_ReturnsEvalVerdictAndArbitrationEvents proves
+// GET /api/stories/{id}/events surfaces events written against the story's
+// own ID (not any task's ID) — the attachment point
+// internal/scheduler.StoryOrchestrator uses for KindEvalVerdict/
+// KindArbitrationDecided.
+func TestServer_ListStoryEvents_ReturnsEvalVerdictAndArbitrationEvents(t *testing.T) {
+ srv, store := testServer(t)
+ if err := store.CreateStory(&story.Story{ID: "s1", Name: "story", Status: "VALIDATING"}); err != nil {
+ t.Fatal(err)
+ }
+ if err := store.CreateEvent(&event.Event{TaskID: "s1", Kind: event.KindEvalVerdict, Actor: event.ActorSystem, Payload: []byte(`{"role":"evaluator_quality"}`)}); err != nil {
+ t.Fatal(err)
+ }
+
+ req := httptest.NewRequest("GET", "/api/stories/s1/events", nil)
+ w := httptest.NewRecorder()
+ srv.Handler().ServeHTTP(w, req)
+ if w.Code != http.StatusOK {
+ t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
+ }
+ var got []event.Event
+ if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
+ t.Fatalf("decode: %v", err)
+ }
+ if len(got) != 1 || got[0].Kind != event.KindEvalVerdict {
+ t.Fatalf("want 1 eval_verdict event, got %+v", got)
+ }
+}
+
+func TestServer_ListStoryEvents_UnknownStory_Returns404(t *testing.T) {
+ srv, _ := testServer(t)
+ req := httptest.NewRequest("GET", "/api/stories/does-not-exist/events", nil)
+ w := httptest.NewRecorder()
+ srv.Handler().ServeHTTP(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Fatalf("expected 404, got %d: %s", w.Code, w.Body.String())
+ }
+}