package plan3test import ( "context" "errors" "testing" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runnerproto" ) func TestCoordinatorDefinitiveRefusalReleasesQueue(t *testing.T) { for _, op := range []string{"claim", "recover", "containment", "delivery"} { for _, phase := range []string{"queued", "claimed", "completed-unreported"} { t.Run(op+"/"+phase, func(t *testing.T) { f, x, p, b := coordinatorFixture(t) x.SetRunnerValidators(equipo.RunnerValidators{Containment: func(context.Context, equipo.Assignment, equipo.RunnerRequest) error { return nil }}) turn := queueCoordinator(t, f, "first", "first") c := equipo.CoordinatorClaim{Turn: turn} if phase != "queued" { var err error c, err = x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision) must(t, err) } if phase == "completed-unreported" { must(t, x.Core.Record(ctx, c.Scope, c.Admission.Dispatch.Action, team.Receipt{ID: "completed", Outcome: team.OutcomeSuccess})) } if phase != "queued" { proof := runnerproto.ContainmentProof{Version: 2, Scope: c.Scope, Handle: c.Handle} must(t, x.ContainCoordinator(ctx, p, c, proof)) } _, err := f.Store.ResetCoordinator(ctx, owner, equipo.Mutation{RequestID: "reset", Revision: b.Revision}) must(t, err) var refusal error switch op { case "claim": _, refusal = x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision) case "recover": _, refusal = x.RecoverCoordinator(ctx, p, turn.ID, turn.Revision) case "containment": refusal = x.ContainCoordinator(ctx, p, c, runnerproto.ContainmentProof{}) case "delivery": refusal = x.CoordinatorEvent(ctx, p, c, runnerproto.Event{Kind: "coordinator.delivery"}) } var definitive *runnerproto.RefusalError if !errors.As(refusal, &definitive) || definitive.Reason != "coordinator.fenced" { t.Fatal("missing definitive code", refusal) } var state string must(t, f.DB.Writer().QueryRow("SELECT state FROM coordinator_turns WHERE id=?", turn.ID).Scan(&state)) if state != "fenced" { t.Fatal("definitive refusal without durable fence", state) } next := queueCoordinator(t, f, "later", "later") _, err = x.ClaimCoordinator(ctx, p, next.ID, next.Revision) must(t, err) }) } } } func TestCoordinatorRefusalDoesNotFenceTransientOrPending(t *testing.T) { f, x, p, _ := coordinatorFixture(t) turn := queueCoordinator(t, f, "first", "first") bad := p bad.CredentialID = "stale" _, err := x.RecoverCoordinator(ctx, bad, turn.ID, turn.Revision) var coded *runnerproto.RefusalError if err == nil || errors.As(err, &coded) { t.Fatal("authentication error made definitive", err) } c, err := x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision) must(t, err) f.exec(t, "UPDATE coordinator_turns SET state='uncertain' WHERE id=?", turn.ID) _, err = x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision) if errors.As(err, &coded) { t.Fatal("pending containment made definitive", err) } var state string must(t, f.DB.Writer().QueryRow("SELECT containment FROM attempts WHERE id=?", c.Handle.Attempt).Scan(&state)) if state != "pending" { t.Fatal("refusal invented containment", state) } } func TestCoordinatorUncertainSameGenerationWaitsForPerson(t *testing.T) { acceptanceUncertainRequiresPerson(t) } func TestCoordinatorDefinitiveFenceAtomic(t *testing.T) { f, x, p, b := coordinatorFixture(t) first := queueCoordinator(t, f, "first", "first") _, err := f.Store.ResetCoordinator(ctx, owner, equipo.Mutation{RequestID: "reset", Revision: b.Revision}) must(t, err) f.exec(t, `CREATE TRIGGER reject_fence BEFORE UPDATE ON coordinator_turns WHEN NEW.state='fenced' BEGIN SELECT RAISE(ABORT,'fence write failed'); END`) _, err = x.ClaimCoordinator(ctx, p, first.ID, first.Revision) var refusal *runnerproto.RefusalError if err == nil || errors.As(err, &refusal) { t.Fatal("definitive response escaped failed commit", err) } f.exec(t, "DROP TRIGGER reject_fence") _, err = x.ClaimCoordinator(ctx, p, first.ID, first.Revision) if !errors.As(err, &refusal) { t.Fatal("restored fence not definitive", err) } } func TestCoordinatorFenceUnknownContainmentDenied(t *testing.T) { f, x, p, _ := coordinatorFixture(t) first := queueCoordinator(t, f, "first", "first") _, err := x.ClaimCoordinator(ctx, p, first.ID, first.Revision) must(t, err) f.exec(t, "UPDATE coordinator_turns SET state='uncertain' WHERE id=?", first.ID) f.exec(t, "ALTER TABLE attempts RENAME TO unavailable_attempts") _, err = x.ClaimCoordinator(ctx, p, first.ID, first.Revision) var refusal *runnerproto.RefusalError if err == nil || errors.As(err, &refusal) { t.Fatal("unknown containment became definitive", err) } }