test(taskstate): seed fixture task states along the transition table

The execution transition table refuses most of the pairs shipped fixtures
wrote to build a finished task -- running -> merged, queued -> parked,
queued -> waiting_human, queued -> merged -- so those fixtures fail the
moment the store enforces it.

internal/taskstate/taskstatetest seeds a status by walking the table's
legal edges (shortest route, computed from taskstate.CanTransition), so a
fixture cannot write an illegal pair and fails loudly when no route
exists. The fixtures in internal/daemon, internal/app/archied,
internal/webui and internal/infrastructure/postgres go through it, and
the table's own test names running -> merged as refused.
This commit is contained in:
sam committed 2026-09-26 04:01:08 +10:00
1 parent 9a3f02a17a
commit 1024feda21
10 files changed
+291 -65

No files matched your search

@@ -0,0 +1,105 @@
// Package taskstatetest seeds task lifecycle states for tests.
//
// A fixture that needs a task in a finished state used to write the pair it
// wanted -- queued -> merged, queued -> parked -- because the store accepted
// it. The transition tables (internal/taskstate) refuse most of those pairs,
// so a fixture that keeps writing one fails the moment the store enforces
// them. Seeding through this package moves the task along the table's legal
// edges instead, and fails loudly when no legal route exists.
package taskstatetest
import (
"context"
"fmt"
"slices"
"testing"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// Transitioner is the guarded status write the seeder needs: the store's own
// Transition, which refuses a from that is not the task's current status.
type Transitioner interface {
Transition(ctx context.Context, taskID int64, from, to, detail string) error
}
// Seed moves taskID from the status it holds to want, one legal edge at a
// time, recording detail on every edge. It fails the test on the edge that the
// store refuses, or before any edge when the table has no route at all.
//
// ctx is the caller's, so a fixture that seeds with a context of its own
// chooses it rather than inheriting one the helper invented.
func Seed(ctx context.Context, t testing.TB, st Transitioner, taskID int64, from, want, detail string) {
t.Helper()
route, err := Path(from, want)
if err != nil {
t.Fatalf("seed task %d: %v", taskID, err)
}
at := from
for _, to := range route {
if err := st.Transition(ctx, taskID, at, to, detail); err != nil {
t.Fatalf("seed task %d to %s: %s -> %s: %v", taskID, want, at, to, err)
}
at = to
}
}
// Path returns the statuses a task moves through to get from one status to
// another, as the shortest route through the execution transition table. A nil
// route means from is already want.
//
// It is exported for the fixtures that seed from a goroutine, where t.Fatalf is
// not callable: those compute the route on the test goroutine and apply it in
// the callback.
func Path(from, want string) ([]string, error) {
statuses := statusIDs()
if !slices.Contains(statuses, from) {
return nil, fmt.Errorf("%q is not a task status", from)
}
if !slices.Contains(statuses, want) {
return nil, fmt.Errorf("%q is not a task status", want)
}
if from == want {
return nil, nil
}
// Breadth-first, walking each row's edges in catalog order, so the route a
// fixture takes is the shortest one and the same on every run.
arrivedFrom := map[string]string{from: from}
queue := []string{from}
for len(queue) > 0 {
at := queue[0]
queue = queue[1:]
for _, next := range statuses {
if _, seen := arrivedFrom[next]; seen || !taskstate.CanTransition(at, next) {
continue
}
arrivedFrom[next] = at
if next == want {
return route(arrivedFrom, from, want), nil
}
queue = append(queue, next)
}
}
return nil, fmt.Errorf("no legal route from %s to %s", from, want)
}
// route reconstructs the way to want, ending with it and leaving out the
// status the task was already in.
func route(arrivedFrom map[string]string, from, want string) []string {
var reverse []string
for at := want; at != from; at = arrivedFrom[at] {
reverse = append(reverse, at)
}
slices.Reverse(reverse)
return reverse
}
func statusIDs() []string {
catalog := taskstate.Statuses()
ids := make([]string, 0, len(catalog))
for _, meta := range catalog {
ids = append(ids, meta.ID)
}
return ids
}
@@ -0,0 +1,133 @@
package taskstatetest
import (
"context"
"slices"
"testing"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// The routes the fixtures ask for, written out rather than derived from the
// table: a route that changed shape -- a longer way round, or through a status
// whose transition writes something else -- would otherwise be invisible.
func TestPathIsTheShortLegalRoute(t *testing.T) {
tests := []struct {
name, from, target string
want []string
}{
{name: "already there", from: taskstate.Queued, target: taskstate.Queued},
{name: "claim", from: taskstate.Queued, target: taskstate.Running, want: []string{taskstate.Running}},
{name: "await approval", from: taskstate.Queued, target: taskstate.WaitingHuman, want: []string{taskstate.Running, taskstate.WaitingHuman}},
{name: "park", from: taskstate.Queued, target: taskstate.Parked, want: []string{taskstate.Running, taskstate.Parked}},
{name: "spend the retries", from: taskstate.Queued, target: taskstate.Dead, want: []string{taskstate.Running, taskstate.Parked, taskstate.Dead}},
{name: "merge", from: taskstate.Queued, target: taskstate.Merged, want: []string{taskstate.Running, taskstate.PROpen, taskstate.Merged}},
{name: "rejected pull request", from: taskstate.Queued, target: taskstate.Rejected, want: []string{taskstate.Running, taskstate.PROpen, taskstate.Rejected}},
{name: "finish with no pull request", from: taskstate.Queued, target: taskstate.Completed, want: []string{taskstate.Running, taskstate.Completed}},
{name: "refuse the work", from: taskstate.Queued, target: taskstate.Declined, want: []string{taskstate.Declined}},
{name: "merge a running task", from: taskstate.Running, target: taskstate.Merged, want: []string{taskstate.PROpen, taskstate.Merged}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Path(tt.from, tt.target)
if err != nil {
t.Fatalf("Path(%s, %s) error = %v", tt.from, tt.target, err)
}
if !slices.Equal(got, tt.want) {
t.Errorf("Path(%s, %s) = %v, want %v", tt.from, tt.target, got, tt.want)
}
})
}
}
// Every route is walkable. A route through a pair the table refuses is exactly
// what this package exists to prevent, so it is checked here rather than
// discovered later at the store.
func TestRoutesOnlyTakeLegalEdges(t *testing.T) {
for _, from := range statusIDs() {
for _, want := range statusIDs() {
got, err := Path(from, want)
if err != nil {
continue
}
at := from
for _, to := range got {
if !taskstate.CanTransition(at, to) {
t.Errorf("route %s -> %s takes %s -> %s, which the table refuses", from, want, at, to)
}
at = to
}
if at != want {
t.Errorf("route %s -> %s ends at %s", from, want, at)
}
}
}
}
// Nothing follows a terminal status, so no fixture can seed out of one: it
// would have to have been written there illegitimately in the first place.
func TestTerminalStatusIsNotAStartingPoint(t *testing.T) {
for _, from := range statusIDs() {
if !taskstate.Terminal(from) {
continue
}
for _, want := range statusIDs() {
if want == from {
continue
}
got, err := Path(from, want)
if err == nil {
t.Errorf("Path(%s, %s) = %v, want an error", from, want, got)
}
}
}
}
func TestUnknownStatusIsNeverARoute(t *testing.T) {
const unknown = "half_written"
if got, err := Path(unknown, taskstate.Running); err == nil {
t.Errorf("Path(%s, running) = %v, want an error", unknown, got)
}
if got, err := Path(taskstate.Queued, unknown); err == nil {
t.Errorf("Path(queued, %s) = %v, want an error", unknown, got)
}
if got, err := Path(unknown, unknown); err == nil {
t.Errorf("Path(%s, %s) = %v, want an error", unknown, unknown, got)
}
}
// Seed applies the route edge by edge, in order, carrying the fixture's own
// reason: a park seeded this way keeps the reason the fixture passed, which is
// what the retry path reads back as the previous failure.
func TestSeedWalksTheRouteThroughTheStore(t *testing.T) {
st := &recorder{}
Seed(t.Context(), t, st, 7, taskstate.Queued, taskstate.Merged, "fixture reason")
want := []edge{
{from: taskstate.Queued, to: taskstate.Running, detail: "fixture reason"},
{from: taskstate.Running, to: taskstate.PROpen, detail: "fixture reason"},
{from: taskstate.PROpen, to: taskstate.Merged, detail: "fixture reason"},
}
if !slices.Equal(st.edges, want) {
t.Errorf("Seed wrote %v, want %v", st.edges, want)
}
}
// A task already in the wanted status is left untouched rather than written to
// itself: a guarded transition from running to running matches no row.
func TestSeedOfTheSameStatusWritesNothing(t *testing.T) {
st := &recorder{}
Seed(t.Context(), t, st, 7, taskstate.Completed, taskstate.Completed, "")
if len(st.edges) != 0 {
t.Errorf("Seed wrote %v, want no writes", st.edges)
}
}
type edge struct{ from, to, detail string }
type recorder struct{ edges []edge }
func (r *recorder) Transition(_ context.Context, _ int64, from, to, detail string) error {
r.edges = append(r.edges, edge{from: from, to: to, detail: detail})
return nil
}
+10
View File
@@ -82,6 +82,16 @@ func TestQueuedCannotReachMerged(t *testing.T) {
}
}
// The other pair written before the table existed, by every fixture that
// needed a finished task: a merge is observed on an open pull request, never
// straight from running. Named here so the next fixture that reaches for it is
// a failure in the table's own test rather than a surprise at the store.
func TestRunningCannotReachMerged(t *testing.T) {
if CanTransition(Running, Merged) {
t.Error("CanTransition(running, merged) = true; a merge is only observed on an open pull request")
}
}
// An unknown status must be illegal in both positions. A status written by a
// newer version, or a typo, must not be able to start or end a transition.
func TestUnknownStatusIsNeverATransition(t *testing.T) {