Files
archie/internal/domain/taskactions/cancel_path_test.go
T
sam 72fcab3a13 feat(taskactions): one approval path answers the review gate from every surface
Decision 1 of docs/prds/pr-review-operator-response.md: the dashboard's
POST /api/tasks/{id}/action is the authoritative surface, chat's /approve
is a shortcut that posts every offered finding, and both reach the same
Service.Apply so one operator intent cannot be recorded as two decisions.

The action body now carries instructions (required by rereview) and the
finding keys an approve posts. taskactions.Service answers an offer
through the guarded RespondReviewGate write: approve records the
selection on the offer, rereview records the instructions, increments the
round and clears the offer, and a rereview with no instructions, with no
offer, or at the cap is refused as a conflict with nothing written. Chat
reaches the same service: chatTaskControllerAdapter.ApproveChatTask
delegates to b.taskActions() instead of its own requeue, which is what
removed the handler's hardcoded implement handoff -- feasibility's deliver
stage now names implement on the task before it waits, the invariant the
handler needs.

The payload crosses every hop it must: the NATS action request, the
gateway.v1 gRPC contract (regenerated), the LocalChatAdapter, and the
webui handler, which answers 400 for a rereview without instructions and
409 for the cap. A rereview event kind names its actor the way the
approval and rejection trios do.

taskactions.Task gains review_gate and rereview_rounds, and the two
surfaces' tests assert they agree.
2026-10-02 02:27:59 +10:00

94 lines
3.7 KiB
Go

package taskactions
import (
"context"
"slices"
"testing"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// Every operator action that ends a run's work routes through the one cancel
// path, CancelExecution (docs/prds/execution-tree-state-machine.md,
// "Cancellation"): the store records the cancellation first, then the daemon
// cancels the in-memory context that was delivering the run. The actions'
// old Transition-then-interrupt combinations are gone, not alongside.
type cancelRecordingStore struct {
fakeStore
cancels []cancelCall
}
type cancelCall struct {
taskID int64
reason, to string
}
func (f *cancelRecordingStore) CancelExecution(_ context.Context, taskID int64, reason, to string) ([]int64, error) {
f.cancels = append(f.cancels, cancelCall{taskID: taskID, reason: reason, to: to})
return nil, nil
}
func TestOperatorActionsRouteThroughCancelExecution(t *testing.T) {
tests := []struct {
name string
status string
action taskstate.Action
wantTo string
}{
{name: "stop parks the run", status: taskstate.Running, action: taskstate.ActionStop, wantTo: taskstate.Parked},
{name: "cancel declines a queued run", status: taskstate.Queued, action: taskstate.ActionCancel, wantTo: taskstate.Declined},
{name: "abandon declines a parked run", status: taskstate.Parked, action: taskstate.ActionAbandon, wantTo: taskstate.Declined},
{name: "reject declines a running run", status: taskstate.Running, action: taskstate.ActionReject, wantTo: taskstate.Declined},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := &cancelRecordingStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: tt.status, IssueNumber: 3}}
var deliveries []int64
service := Service{
Store: store,
CancelTask: func(id int64) bool { deliveries = append(deliveries, id); return true },
}
if err := service.Apply(context.Background(), nil, humanActor(), 7, tt.action, ReviewResponse{}); err != nil {
t.Fatalf("Apply(%s): %v", tt.action, err)
}
if len(store.cancels) != 1 {
t.Fatalf("CancelExecution calls = %+v, want exactly the one cancel path", store.cancels)
}
if call := store.cancels[0]; call.taskID != 7 || call.to != tt.wantTo {
t.Errorf("CancelExecution = %+v, want task 7 to %q", call, tt.wantTo)
}
// The context cancel is the delivery mechanism, not a second
// store write: it rides the store record, never beside it.
if tt.status == taskstate.Running && !slices.Contains(deliveries, int64(7)) {
t.Errorf("deliveries = %v, want the running execution's context cancelled", deliveries)
}
// The action's own attribution event is the operator's record;
// the store wrote the mechanical rows.
if got := store.last(); got.Kind == "" {
t.Error("the operator event was not recorded")
}
})
}
}
// A stop's detail names the delivery, as before: a context cancel that found
// nothing executing says so, and the parked execution is recorded either way.
func TestStopRecordsEvenWhenNothingWasExecuting(t *testing.T) {
store := &cancelRecordingStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: taskstate.Running}}
service := Service{
Store: store,
CancelTask: func(int64) bool { return false },
}
if err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionStop, ReviewResponse{}); err != nil {
t.Fatalf("Apply(stop): %v", err)
}
if len(store.cancels) != 1 || store.cancels[0].to != taskstate.Parked {
t.Fatalf("CancelExecution = %+v, want task 7 parked", store.cancels)
}
if got := store.last(); got.Kind != events.KindTaskStopped || got.Detail == "" {
t.Fatalf("stop event = %+v, want the stop kind and the no-execution detail", got)
}
}