Files
archie/internal/taskstate/taskstate_test.go
T
sam 0d82f3fa66 feat(workflow): typed park classification and split remediation budgets
The F2+F3 schema wave from the auto-fix/resume investigation, sequenced
together as one migration. Every failure funneled to one parked status
with free-text ParkReason, and Task.RetryCount doubled as the
operator-retry budget and the review-remediation round budget -- so N
operator retries ate the remediation budget, a repo-level round cap
could re-park a task an operator had just legally retried, and no
surface could answer what a park needed from an operator.

Park class (F2): a producer-recorded vocabulary in taskstate --
needs_human (the default, so an unclassified park always reads as
operator-actionable), transient (environmental; a requeue can succeed),
terminal (retrying is pointless). Recorded at the park site, never
inferred from reason text; NormalizeParkClass makes unknown values
persist as needs_human. Transition-to-parked defaults the class, so
every existing unclassified park site reads correctly with no call-site
churn; the classified ParkTask write follows for parks that know
better. Requeue, RetryTask and BeginRemediation clear the class back to
the default with the reason.

Split budgets (F3): Task.RemediationRounds is the remediation's own
budget; RetryCount stays the operator's. The round-cap stage counts and
caps rounds; operator retry keeps counting RetryCount and can drive a
task to dead without touching rounds. remediation_rounds is a
presence-gated migrator column written through Update.

Known state: proto fields, the ParkTask RPC, the staterpc mapping and
the daemon-side park-site classification follow in the wire half of
this wave (the proto parity test is intentionally red until then).
2026-09-23 12:55:03 +10:00

177 lines
6.3 KiB
Go

package taskstate
import (
"slices"
"testing"
)
// The rules must be the same wherever an operator acts. These cases are the
// contract both the dashboard and chat are held to; a divergence between them
// is what this package exists to prevent.
func TestActionRules(t *testing.T) {
tests := []struct {
status string
approve, retry, decline bool
terminal bool
actions []Action
}{
{status: Queued, decline: true, actions: []Action{ActionCancel, ActionReject}},
{status: Running, decline: true, actions: []Action{ActionStop, ActionReject}},
{status: WaitingHuman, approve: true, decline: true, actions: []Action{ActionApprove, ActionReject}},
{status: PROpen, decline: true, actions: []Action{ActionOpenPR, ActionOpenIssue, ActionReject}},
{status: Parked, retry: true, decline: true, actions: []Action{ActionRetry, ActionAbandon, ActionReject}},
{status: Merged, terminal: true, actions: []Action{ActionArchive}},
{status: Rejected, terminal: true, actions: []Action{ActionArchive}},
{status: Dead, terminal: true, actions: []Action{ActionArchive}},
{status: Declined, terminal: true, actions: []Action{ActionArchive}},
}
for _, tc := range tests {
t.Run(tc.status, func(t *testing.T) {
if got := CheckApprove(tc.status) == nil; got != tc.approve {
t.Errorf("CheckApprove allowed = %v, want %v", got, tc.approve)
}
if got := CheckRetry(tc.status) == nil; got != tc.retry {
t.Errorf("CheckRetry allowed = %v, want %v", got, tc.retry)
}
if got := CheckDecline(tc.status) == nil; got != tc.decline {
t.Errorf("CheckDecline allowed = %v, want %v", got, tc.decline)
}
if got := Terminal(tc.status); got != tc.terminal {
t.Errorf("Terminal = %v, want %v", got, tc.terminal)
}
if got := Actions(tc.status); !slices.Equal(got, tc.actions) {
t.Errorf("Actions = %v, want %v", got, tc.actions)
}
for _, action := range tc.actions {
if err := CheckAction(tc.status, action); err != nil {
t.Errorf("CheckAction(%q) = %v", action, err)
}
}
})
}
}
// An unknown status must not be silently actionable: a typo or a status from
// a newer version should refuse rather than approve.
func TestUnknownStatusIsNotApprovable(t *testing.T) {
const unknown = "quantum_superposition"
if err := CheckApprove(unknown); err == nil {
t.Error("CheckApprove allowed an unknown status")
}
if err := CheckRetry(unknown); err == nil {
t.Error("CheckRetry allowed an unknown status")
}
if err := CheckDecline(unknown); err == nil {
t.Error("CheckDecline allowed an unknown status")
}
}
// Rejected and Declined must stay distinct. Collapsing them would lose the
// difference between "the forge closed the PR" and "a human said no", which
// is the drift this package was created to end.
func TestRejectedAndDeclinedAreDistinct(t *testing.T) {
if Rejected == Declined {
t.Fatal("Rejected and Declined are the same string")
}
if !Terminal(Rejected) || !Terminal(Declined) {
t.Error("both must be terminal")
}
}
// The presentation catalog must cover every status and action the lifecycle
// exposes, so the frontend can render anything the rules allow without holding
// its own copy of the vocabulary.
func TestPresentationCatalog(t *testing.T) {
allowedKinds := map[string]bool{"idle": true, "info": true, "ok": true, "warn": true, "danger": true}
statuses := Statuses()
if len(statuses) == 0 {
t.Fatal("Statuses() returned no entries")
}
seen := map[string]bool{}
for _, meta := range statuses {
if meta.ID == "" || meta.Label == "" || !allowedKinds[meta.Kind] {
t.Errorf("status %q: id=%q label=%q kind=%q", meta.ID, meta.ID, meta.Label, meta.Kind)
}
if seen[meta.ID] {
t.Errorf("duplicate status id %q", meta.ID)
}
seen[meta.ID] = true
if meta.NeedsYou && meta.Kind == "idle" {
t.Errorf("status %q is marked needs_you but has idle severity", meta.ID)
}
}
// Every lifecycle status must have presentation metadata.
for _, status := range []string{Queued, Running, WaitingHuman, PROpen, Merged, Parked, Dead, Rejected, Declined} {
if !seen[status] {
t.Errorf("status %q missing from Statuses()", status)
}
}
actions := ActionCatalog()
if len(actions) == 0 {
t.Fatal("ActionCatalog() returned no entries")
}
actionSeen := map[string]bool{}
for _, meta := range actions {
if meta.ID == "" || meta.Label == "" {
t.Errorf("action %q: id=%q label=%q", meta.ID, meta.ID, meta.Label)
}
if actionSeen[meta.ID] {
t.Errorf("duplicate action id %q", meta.ID)
}
actionSeen[meta.ID] = true
// link actions navigate; the rest must carry a button kind.
if meta.Kind != "link" && meta.Kind != "primary" && meta.Kind != "quiet" && meta.Kind != "danger" {
t.Errorf("action %q has unknown kind %q", meta.ID, meta.Kind)
}
}
// Every action the lifecycle table can emit must be describable, otherwise
// the frontend has no way to present it.
byID := func(status string) map[Action]bool {
out := map[Action]bool{}
for _, a := range Actions(status) {
out[a] = true
}
return out
}
for _, status := range []string{Queued, Running, WaitingHuman, PROpen, Parked, Merged, Rejected, Dead, Declined} {
for a := range byID(status) {
if !actionSeen[string(a)] {
t.Errorf("action %q from status %q has no ActionCatalog entry", a, status)
}
}
}
}
func TestActionsReturnsIndependentSlices(t *testing.T) {
first := Actions(WaitingHuman)
first[0] = ActionArchive
if got := Actions(WaitingHuman); !slices.Equal(got, []Action{ActionApprove, ActionReject}) {
t.Fatalf("mutating returned actions changed lifecycle table: %v", got)
}
}
func TestNormalizeParkClass(t *testing.T) {
tests := []struct {
name string
input string
want ParkClass
}{
{name: "empty defaults to needs_human", input: "", want: ParkNeedsHuman},
{name: "a known class passes through", input: ParkTransient, want: ParkTransient},
{name: "terminal passes through", input: ParkTerminal, want: ParkTerminal},
{name: "an unknown class falls back to needs_human", input: "urgent", want: ParkNeedsHuman},
{name: "case differences are not classes", input: "TRANSIENT", want: ParkNeedsHuman},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NormalizeParkClass(tt.input); got != tt.want {
t.Errorf("NormalizeParkClass(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}