refactor: testing from the beginning. a journey in 150k lines of tests to none
This commit is contained in:
651 files changed
-146443
No files matched your search
@@ -1,93 +0,0 @@
|
||||
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, ActionPayload{}); 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, ActionPayload{}); 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)
|
||||
}
|
||||
}
|
||||
@@ -1,230 +0,0 @@
|
||||
package taskactions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/prreview"
|
||||
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
// gateStore is a fakeStore that records the review gate response write and
|
||||
// the plain requeue separately, so a test can tell which path an approval
|
||||
// took.
|
||||
type gateStore struct {
|
||||
fakeStore
|
||||
|
||||
requeues []requeueCall
|
||||
gateWrites []gateWrite
|
||||
gateErr error
|
||||
}
|
||||
|
||||
type requeueCall struct {
|
||||
taskID int64
|
||||
from, workflow string
|
||||
}
|
||||
|
||||
type gateWrite struct {
|
||||
taskID int64
|
||||
from string
|
||||
gate string
|
||||
rereview bool
|
||||
max int
|
||||
}
|
||||
|
||||
func (g *gateStore) Requeue(_ context.Context, taskID int64, from, workflow string) error {
|
||||
g.requeues = append(g.requeues, requeueCall{taskID: taskID, from: from, workflow: workflow})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gateStore) RespondReviewGate(_ context.Context, taskID int64, from, gate string, rereview bool, maxRounds int) error {
|
||||
if g.gateErr != nil {
|
||||
return g.gateErr
|
||||
}
|
||||
g.gateWrites = append(g.gateWrites, gateWrite{taskID: taskID, from: from, gate: gate, rereview: rereview, max: maxRounds})
|
||||
return nil
|
||||
}
|
||||
|
||||
// offeredGate is a task waiting with the review gate offer recorded, which is
|
||||
// what an operator's approve or re-review answers.
|
||||
func offeredGate(findings ...prreview.ScoredFinding) *Task {
|
||||
return &Task{
|
||||
ID: 7, Owner: "acme", Repo: "widgets", Status: taskstate.WaitingHuman,
|
||||
ReviewGate: workflowtask.EncodeReviewGate(workflowtask.ReviewGate{
|
||||
Findings: gateDocuments(findings...), HeadSHA: "abc", Owner: "acme", Repo: "widgets",
|
||||
PRNumber: 42, Workflow: "pr-review",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// gateDocuments renders scored findings the way the pipeline's gate stage
|
||||
// does: the stable key an operator's selection names, beside the finding's
|
||||
// own encoding.
|
||||
func gateDocuments(findings ...prreview.ScoredFinding) []workflowtask.ReviewGateFinding {
|
||||
out := make([]workflowtask.ReviewGateFinding, len(findings))
|
||||
for i, f := range findings {
|
||||
out[i] = prreview.GateFinding(f)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestApproveOfAGateRecordsTheSelectionOnTheOffer(t *testing.T) {
|
||||
findings := []prreview.ScoredFinding{
|
||||
{Title: "keep", File: "main.go", LineStart: 1, Body: "keep me"},
|
||||
{Title: "drop", File: "main.go", LineStart: 9, Body: "drop me"},
|
||||
}
|
||||
store := &gateStore{task: offeredGate(findings...)}
|
||||
service := Service{Store: store}
|
||||
|
||||
if err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionApprove,
|
||||
ActionPayload{Findings: []string{findings[0].Key()}}); err != nil {
|
||||
t.Fatalf("Apply(approve): %v", err)
|
||||
}
|
||||
if len(store.requeues) != 0 {
|
||||
t.Fatalf("approve of a gate requeued plainly: %+v", store.requeues)
|
||||
}
|
||||
if len(store.gateWrites) != 1 {
|
||||
t.Fatalf("gate writes = %+v, want exactly the one response write", store.gateWrites)
|
||||
}
|
||||
write := store.gateWrites[0]
|
||||
if write.rereview || write.max != workflowtask.MaxRereviewRounds || write.from != taskstate.WaitingHuman {
|
||||
t.Fatalf("write = %+v, want an approve, from waiting_human, at the domain cap", write)
|
||||
}
|
||||
gate, ok := workflowtask.DecodeReviewGate(write.gate)
|
||||
if !ok || !gate.Approved() {
|
||||
t.Fatalf("recorded gate = %q, want an approve", write.gate)
|
||||
}
|
||||
if len(gate.Selection) != 1 || gate.Selection[0] != findings[0].Key() {
|
||||
t.Fatalf("recorded selection = %v, want exactly the selected key", gate.Selection)
|
||||
}
|
||||
if len(gate.Findings) != len(findings) {
|
||||
t.Fatalf("recorded findings = %d, want the whole offer kept for the resume to post", len(gate.Findings))
|
||||
}
|
||||
if got := store.last(); got.Kind != events.KindHumanApproved {
|
||||
t.Fatalf("event kind = %q, want %q", got.Kind, events.KindHumanApproved)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApproveWithNoSelectionRecordsEveryOfferedFinding(t *testing.T) {
|
||||
findings := []prreview.ScoredFinding{{Title: "only", File: "main.go", LineStart: 1, Body: "b"}}
|
||||
store := &gateStore{task: offeredGate(findings...)}
|
||||
service := Service{Store: store}
|
||||
|
||||
if err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionApprove, ActionPayload{}); err != nil {
|
||||
t.Fatalf("Apply(approve): %v", err)
|
||||
}
|
||||
gate, _ := workflowtask.DecodeReviewGate(store.gateWrites[0].gate)
|
||||
if len(gate.Selection) != 1 || gate.Selection[0] != findings[0].Key() {
|
||||
t.Fatalf("selection = %v, want every offered finding's key", gate.Selection)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApproveRefusesANonOfferedFindingKey(t *testing.T) {
|
||||
store := &gateStore{task: offeredGate(prreview.ScoredFinding{Title: "offered", File: "main.go", LineStart: 1})}
|
||||
service := Service{Store: store}
|
||||
|
||||
err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionApprove,
|
||||
ActionPayload{Findings: []string{"main.go:99:stale"}})
|
||||
if !errors.Is(err, ErrConflict) {
|
||||
t.Fatalf("Apply(approve with a stale key) = %v, want ErrConflict", err)
|
||||
}
|
||||
if len(store.gateWrites) != 0 {
|
||||
t.Fatalf("refused approve wrote %+v", store.gateWrites)
|
||||
}
|
||||
}
|
||||
|
||||
// TestApproveWithoutAGateKeepsTheTasksWorkflow is the feasibility handoff:
|
||||
// no gate offer means there is no review to answer, and the approval releases
|
||||
// waiting work under the workflow the wait recorded. It must not hardcode
|
||||
// implement -- the PRD's Decision 1 -- which is why feasibility names
|
||||
// implement itself before it waits.
|
||||
func TestApproveWithoutAGateKeepsTheTasksWorkflow(t *testing.T) {
|
||||
store := &gateStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: taskstate.WaitingHuman}}
|
||||
service := Service{Store: store}
|
||||
|
||||
if err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionApprove, ActionPayload{}); err != nil {
|
||||
t.Fatalf("Apply(approve): %v", err)
|
||||
}
|
||||
if len(store.gateWrites) != 0 {
|
||||
t.Fatalf("a task with no gate wrote %+v", store.gateWrites)
|
||||
}
|
||||
if len(store.requeues) != 1 || store.requeues[0].workflow != "" || store.requeues[0].from != taskstate.WaitingHuman {
|
||||
t.Fatalf("requeues = %+v, want one waiting_human requeue that keeps the task's workflow", store.requeues)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRereviewRecordsInstructionsAndClearsTheOffer(t *testing.T) {
|
||||
findings := []prreview.ScoredFinding{{Title: "old", File: "main.go", LineStart: 1}}
|
||||
store := &gateStore{task: offeredGate(findings...)}
|
||||
service := Service{Store: store}
|
||||
|
||||
if err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionRereview,
|
||||
ActionPayload{Instructions: "look at the migration ordering"}); err != nil {
|
||||
t.Fatalf("Apply(rereview): %v", err)
|
||||
}
|
||||
write := store.gateWrites[0]
|
||||
if !write.rereview {
|
||||
t.Fatalf("write = %+v, want the rereview flag that increments the round count", write)
|
||||
}
|
||||
gate, _ := workflowtask.DecodeReviewGate(write.gate)
|
||||
if gate.Outcome != workflowtask.GateRereview || gate.Instructions != "look at the migration ordering" {
|
||||
t.Fatalf("recorded gate = %+v, want the re-review outcome and instructions", gate)
|
||||
}
|
||||
if len(gate.Findings) != 0 || len(gate.Selection) != 0 {
|
||||
t.Fatalf("re-review kept the offer: %+v; the resumed run recomputes it", gate)
|
||||
}
|
||||
if got := store.last(); got.Kind != events.KindHumanRereviewed {
|
||||
t.Fatalf("event kind = %q, want %q", got.Kind, events.KindHumanRereviewed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRereviewRefusedWithoutInstructionsAndAtTheCap(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
task *Task
|
||||
res ActionPayload
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "no instructions",
|
||||
task: offeredGate(prreview.ScoredFinding{Title: "f", File: "main.go", LineStart: 1}),
|
||||
want: "instructions",
|
||||
},
|
||||
{
|
||||
name: "no gate offer",
|
||||
task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: taskstate.WaitingHuman},
|
||||
res: ActionPayload{Instructions: "look again"},
|
||||
want: "no review gate",
|
||||
},
|
||||
{
|
||||
name: "at the cap",
|
||||
task: func() *Task {
|
||||
task := offeredGate(prreview.ScoredFinding{Title: "f", File: "main.go", LineStart: 1})
|
||||
task.RereviewRounds = workflowtask.MaxRereviewRounds
|
||||
return task
|
||||
}(),
|
||||
res: ActionPayload{Instructions: "look again"},
|
||||
want: "re-reviews",
|
||||
},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
store := &gateStore{task: tc.task}
|
||||
service := Service{Store: store}
|
||||
err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionRereview, tc.res)
|
||||
if !errors.Is(err, ErrConflict) {
|
||||
t.Fatalf("Apply(rereview) = %v, want ErrConflict", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatalf("error = %q, want it to name %q", err, tc.want)
|
||||
}
|
||||
if len(store.gateWrites) != 0 || len(store.requeues) != 0 || len(store.events) != 0 {
|
||||
t.Fatalf("a refused re-review wrote store state: gate=%+v requeues=%+v events=%+v", store.gateWrites, store.requeues, store.events)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,323 +0,0 @@
|
||||
package taskactions
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
type fakeStore struct {
|
||||
task *Task
|
||||
events []events.Event
|
||||
retryMode string
|
||||
retried bool
|
||||
}
|
||||
|
||||
func (f *fakeStore) TaskByID(context.Context, int64) (*Task, error) { return f.task, nil }
|
||||
func (f *fakeStore) Transition(context.Context, int64, string, string, string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) CancelExecution(context.Context, int64, string, string) ([]int64, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Requeue(context.Context, int64, string, string) error { return nil }
|
||||
|
||||
func (f *fakeStore) RespondReviewGate(context.Context, int64, string, string, bool, int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) RetryTask(_ context.Context, _ int64, _, _, retryMode string) error {
|
||||
f.retryMode = retryMode
|
||||
f.retried = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) ArchiveTask(_ context.Context, _ int64, _ string, e events.Event) (int64, error) {
|
||||
f.events = append(f.events, e)
|
||||
return int64(len(f.events)), nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) InsertEvent(_ context.Context, e events.Event) (int64, error) {
|
||||
f.events = append(f.events, e)
|
||||
return int64(len(f.events)), nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) last() events.Event {
|
||||
if len(f.events) == 0 {
|
||||
return events.Event{}
|
||||
}
|
||||
return f.events[len(f.events)-1]
|
||||
}
|
||||
|
||||
const (
|
||||
humanID = identity.IdentityID("20000000-0000-5000-8000-000000000001")
|
||||
agentID = identity.IdentityID("20000000-0000-5000-8000-000000000002")
|
||||
)
|
||||
|
||||
func humanActor() Actor {
|
||||
return ActorFor(identity.Identity{ID: humanID, Kind: identity.KindUser, Lifecycle: identity.LifecycleActive})
|
||||
}
|
||||
|
||||
func agentActor() Actor {
|
||||
return ActorFor(identity.Identity{ID: agentID, Kind: identity.KindBot, Lifecycle: identity.LifecycleActive})
|
||||
}
|
||||
|
||||
// TestEventKindDescribesTheActor is the rule that the bug turned on: an agent's
|
||||
// approval was written down as a human's. The kind names the actor, so an
|
||||
// agent's action can never be read as a person's, and an action with no verified
|
||||
// actor is recorded as a task-level event that credits nobody.
|
||||
func TestEventKindDescribesTheActor(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
action taskstate.Action
|
||||
actor Actor
|
||||
wantKind string
|
||||
}{
|
||||
{"a person approves", taskstate.ActionApprove, humanActor(), events.KindHumanApproved},
|
||||
{"an agent approves", taskstate.ActionApprove, agentActor(), events.KindAgentApproved},
|
||||
{"a person rejects", taskstate.ActionReject, humanActor(), events.KindHumanRejected},
|
||||
{"an agent rejects", taskstate.ActionReject, agentActor(), events.KindAgentRejected},
|
||||
{"an unattributed approval", taskstate.ActionApprove, Actor{}, events.KindTaskApproved},
|
||||
{"an unattributed rejection", taskstate.ActionReject, Actor{}, events.KindTaskRejected},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human"}}
|
||||
service := Service{Store: store}
|
||||
|
||||
if err := service.Apply(context.Background(), nil, tc.actor, 7, tc.action, ActionPayload{}); err != nil {
|
||||
t.Fatalf("Apply() error = %v", err)
|
||||
}
|
||||
got := store.last()
|
||||
if got.Kind != tc.wantKind {
|
||||
t.Fatalf("event kind = %q, want %q", got.Kind, tc.wantKind)
|
||||
}
|
||||
if tc.actor.Human() && got.Kind == events.KindAgentApproved {
|
||||
t.Fatal("a person's action was recorded as an agent's")
|
||||
}
|
||||
if tc.actor.Attributed() && !tc.actor.Human() && got.Kind == events.KindHumanApproved {
|
||||
t.Fatal("an agent's action was recorded as a human approval")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestActorAndPrincipalAreRecordedSeparately is the two-attribution requirement:
|
||||
// an agent acting under a person's standing authority records both, so "approved
|
||||
// by came from me" is answerable from the record.
|
||||
func TestActorAndPrincipalAreRecordedSeparately(t *testing.T) {
|
||||
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human"}}
|
||||
service := Service{Store: store}
|
||||
|
||||
actor := agentActor().AuthorisedBy(humanID)
|
||||
if err := service.Apply(context.Background(), nil, actor, 7, taskstate.ActionApprove, ActionPayload{}); err != nil {
|
||||
t.Fatalf("Apply() error = %v", err)
|
||||
}
|
||||
|
||||
got := store.last()
|
||||
if got.ActorID != string(agentID) {
|
||||
t.Fatalf("actor = %q, want %q", got.ActorID, agentID)
|
||||
}
|
||||
if got.ActorKind != string(identity.KindBot) {
|
||||
t.Fatalf("actor kind = %q, want %q", got.ActorKind, identity.KindBot)
|
||||
}
|
||||
if got.PrincipalID != string(humanID) {
|
||||
t.Fatalf("principal = %q, want %q", got.PrincipalID, humanID)
|
||||
}
|
||||
if got.Kind != events.KindAgentApproved {
|
||||
t.Fatalf("event kind = %q, want %q", got.Kind, events.KindAgentApproved)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnattributedActionClaimsNoApprover: with no verified actor the record must
|
||||
// not credit the actor's own authority, and must not name a human.
|
||||
func TestUnattributedActionClaimsNoApprover(t *testing.T) {
|
||||
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human"}}
|
||||
service := Service{Store: store}
|
||||
|
||||
if err := service.Apply(context.Background(), nil, Actor{}, 7, taskstate.ActionApprove, ActionPayload{}); err != nil {
|
||||
t.Fatalf("Apply() error = %v", err)
|
||||
}
|
||||
|
||||
got := store.last()
|
||||
if got.ActorID != "" || got.ActorKind != "" || got.PrincipalID != "" {
|
||||
t.Fatalf("unattributed action recorded an actor: %+v", got)
|
||||
}
|
||||
if got.Kind == events.KindHumanApproved || got.Kind == events.KindAgentApproved {
|
||||
t.Fatalf("unattributed action claimed an actor through its kind: %q", got.Kind)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEveryActionCarriesItsAttribution: no action path may record an event that
|
||||
// skipped the attribution, including the ones that also close a forge issue.
|
||||
func TestEveryActionCarriesItsAttribution(t *testing.T) {
|
||||
actions := []struct {
|
||||
action taskstate.Action
|
||||
status string
|
||||
}{
|
||||
{taskstate.ActionApprove, "waiting_human"},
|
||||
{taskstate.ActionReject, "waiting_human"},
|
||||
{taskstate.ActionRetry, "parked"},
|
||||
{taskstate.ActionStop, "running"},
|
||||
{taskstate.ActionCancel, "queued"},
|
||||
{taskstate.ActionAbandon, "parked"},
|
||||
}
|
||||
|
||||
for _, tc := range actions {
|
||||
t.Run(string(tc.action), func(t *testing.T) {
|
||||
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: tc.status}}
|
||||
service := Service{
|
||||
Store: store,
|
||||
CancelTask: func(int64) bool { return true },
|
||||
}
|
||||
if err := service.Apply(context.Background(), nil, agentActor(), 7, tc.action, ActionPayload{}); err != nil {
|
||||
t.Fatalf("Apply(%s) error = %v", tc.action, err)
|
||||
}
|
||||
got := store.last()
|
||||
if got.ActorID != string(agentID) || got.ActorKind != string(identity.KindBot) {
|
||||
t.Fatalf("%s recorded without its actor: %+v", tc.action, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestScopeStillLimitsATaskButIsNotTheActor: scope answers which task a caller
|
||||
// may touch. It is not an actor, and passing one must not attribute the action
|
||||
// to it.
|
||||
func TestScopeStillLimitsATaskButIsNotTheActor(t *testing.T) {
|
||||
other := "sam"
|
||||
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: "waiting_human", Identity: "archie"}}
|
||||
service := Service{Store: store}
|
||||
|
||||
if err := service.Apply(context.Background(), &other, agentActor(), 7, taskstate.ActionApprove, ActionPayload{}); err == nil {
|
||||
t.Fatal("Apply() allowed an identity to act on another identity's task")
|
||||
}
|
||||
if len(store.events) != 0 {
|
||||
t.Fatalf("a refused action recorded %d events", len(store.events))
|
||||
}
|
||||
|
||||
if err := service.Apply(context.Background(), &other, agentActor(), 7, taskstate.ActionApprove, ActionPayload{}); err == nil {
|
||||
t.Fatal("Apply() allowed a scoped caller to act on a foreign task")
|
||||
}
|
||||
}
|
||||
|
||||
func TestActorForCarriesTheKindAndLeavesTheActionUnattributedWithoutAPrincipal(t *testing.T) {
|
||||
actor := agentActor()
|
||||
if actor.Principal != "" {
|
||||
t.Fatalf("ActorFor() set a principal: %q", actor.Principal)
|
||||
}
|
||||
if !actor.Attributed() {
|
||||
t.Fatal("ActorFor() produced an unattributed actor for a real identity")
|
||||
}
|
||||
if (Actor{}).Attributed() {
|
||||
t.Fatal("the zero Actor reported itself as attributed")
|
||||
}
|
||||
if (Actor{}).Human() {
|
||||
t.Fatal("the zero Actor reported itself as a human")
|
||||
}
|
||||
}
|
||||
|
||||
// TestOnlyRejectAndCancelCloseTheIssue: abandoning is archie giving up on its
|
||||
// own run, not a verdict on the issue, so the issue stays open for a human.
|
||||
func TestOnlyRejectAndCancelCloseTheIssue(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
action taskstate.Action
|
||||
status string
|
||||
closes bool
|
||||
}{
|
||||
{taskstate.ActionReject, "waiting_human", true},
|
||||
{taskstate.ActionCancel, "queued", true},
|
||||
{taskstate.ActionAbandon, "parked", false},
|
||||
} {
|
||||
t.Run(string(tc.action), func(t *testing.T) {
|
||||
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: tc.status, ForgeBacked: true}}
|
||||
closed := false
|
||||
service := Service{Store: store, CloseIssue: func(context.Context, string, string, int, string) error {
|
||||
closed = true
|
||||
return nil
|
||||
}}
|
||||
if err := service.Apply(context.Background(), nil, agentActor(), 7, tc.action, ActionPayload{}); err != nil {
|
||||
t.Fatalf("Apply(%s) error = %v", tc.action, err)
|
||||
}
|
||||
if closed != tc.closes {
|
||||
t.Fatalf("Apply(%s) closed the issue = %v, want %v", tc.action, closed, tc.closes)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRetryPersistsTheOperatorsWorktreeMode pins the retry half of the
|
||||
// operator surface: the mode the dashboard sent reaches the store write, so
|
||||
// the daemon reads the operator's choice instead of inferring one. The event
|
||||
// records the mode too, so the timeline says how the retry was dispatched.
|
||||
func TestRetryPersistsTheOperatorsWorktreeMode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mode taskstate.RetryMode
|
||||
persisted string
|
||||
branch string
|
||||
wantMode string
|
||||
}{
|
||||
{"continue pushed work", taskstate.RetryContinuePushedWork, "", "fix/7-thing", "continue_pushed_work"},
|
||||
{"refresh onto base", taskstate.RetryRefreshOntoBase, "", "", "refresh_onto_base"},
|
||||
{"an empty mode is the explicit refresh default", "", "", "", "refresh_onto_base"},
|
||||
{"an empty mode keeps the persisted choice", "", string(taskstate.RetryContinuePushedWork), "fix/7-thing", "continue_pushed_work"},
|
||||
{"an explicit mode overrides the persisted choice", taskstate.RetryRefreshOntoBase, string(taskstate.RetryContinuePushedWork), "fix/7-thing", "refresh_onto_base"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: taskstate.Parked, Branch: tc.branch, RetryMode: tc.persisted}}
|
||||
service := Service{Store: store}
|
||||
|
||||
if err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionRetry, ActionPayload{RetryMode: tc.mode}); err != nil {
|
||||
t.Fatalf("Apply(retry) error = %v", err)
|
||||
}
|
||||
if !store.retried || store.retryMode != tc.wantMode {
|
||||
t.Fatalf("RetryTask mode = %q (called %v), want %q", store.retryMode, store.retried, tc.wantMode)
|
||||
}
|
||||
if got := store.last().Data["retry_mode"]; got != tc.wantMode {
|
||||
t.Errorf("retry event retry_mode = %v, want %q", got, tc.wantMode)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRetryRefusesModesThatCannotLand keeps a refuse-from-the-operator rule at
|
||||
// the service, where every surface reaches it: continuing work with no pushed
|
||||
// branch would queue a resume that cannot land, and an unknown mode is a
|
||||
// caller bug rather than a task to reset. Both are conflicts and neither
|
||||
// writes.
|
||||
func TestRetryRefusesModesThatCannotLand(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mode taskstate.RetryMode
|
||||
branch string
|
||||
}{
|
||||
{"continue with no pushed branch", taskstate.RetryContinuePushedWork, ""},
|
||||
{"an unknown mode", taskstate.RetryMode("reset_to_branch_head"), "fix/7-thing"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
store := &fakeStore{task: &Task{ID: 7, Owner: "acme", Repo: "widgets", Status: taskstate.Parked, Branch: tc.branch}}
|
||||
service := Service{Store: store}
|
||||
|
||||
err := service.Apply(context.Background(), nil, humanActor(), 7, taskstate.ActionRetry, ActionPayload{RetryMode: tc.mode})
|
||||
if err == nil {
|
||||
t.Fatal("Apply(retry) = nil error, want a conflict")
|
||||
}
|
||||
if !errors.Is(err, ErrConflict) {
|
||||
t.Fatalf("Apply(retry) error = %v, want ErrConflict", err)
|
||||
}
|
||||
if store.retried {
|
||||
t.Error("RetryTask was called, want the refusal to write nothing")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user