Files
archie/internal/taskstate/taskstate_test.go
T
sam 1f2e7a42eb refactor: one definition of what approving or declining a task means
The dashboard and chat both let an operator approve or refuse work, and
they had drifted. Declining from the dashboard left the task
closed_wont_do; cancelling the same task from chat left it rejected. Two
operators looking at one queue saw different states for the same
decision, and "rejected" -- which reconcilePRs also uses for a pull
request closed without merging -- stopped meaning one thing.

internal/gateway deliberately does not import internal/store, so it kept
a hand-synced copy of the status strings with a comment asking future
editors to keep them aligned. That is the same defect one level down,
and it is how the drift happened.

internal/taskstate is a leaf package with no dependencies holding the
lifecycle vocabulary and the rules for operator actions. gateway, store
and the dashboard all import it, so gateway stays decoupled from store
without a copy, and CheckApprove/CheckRetry/CheckDecline have one
implementation. Declined is named for what it is -- a human said no --
and stays distinct from Rejected, which is something archie observed.

Chat cancel now records Declined. That is the behaviour change: same
decision, same state, whichever surface the operator used.

Rules were preserved rather than tidied while unifying. A parked task
still cannot be declined, which both surfaces already refused, even
though "not terminal" would have allowed it -- widening a rule under
cover of a refactor is how you ship a change nobody reviewed. The
comment says where to change it if that is ever wanted.

TestDashboardAndChatAgreeOnTerminalStates drives both real surfaces
against one store, so this cannot drift apart again without a red test.

Also removes a second rule for the dash-top-standalone class: renderSetup
already reconciles it from the data at the top of every call, and a later
branch cleared it again. Harmless, but it reads as two sources of truth
for one bit of layout state.
2026-08-05 04:15:39 +10:00

71 lines
2.4 KiB
Go

package taskstate
import "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
}{
{status: Queued, decline: true},
{status: Running, decline: true},
{status: WaitingHuman, approve: true, decline: true},
{status: PROpen, decline: true},
{status: Parked, retry: true},
{status: Merged, terminal: true},
{status: Rejected, terminal: true},
{status: Dead, terminal: true},
{status: Declined, terminal: true},
}
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)
}
})
}
}
// 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")
}
// Declining is deliberately permissive: refusing work archie does not
// recognise is safer than being unable to stop it.
if err := CheckDecline(unknown); err != nil {
t.Errorf("CheckDecline refused an unknown status: %v", err)
}
}
// 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")
}
}