oficina / alta Public

adopt: Home preflight refuses plans before Plan A and unknown tables

A pending or open plan on either role is now refused before Plan A as
well as before initialize. A pasted count for a table outside
HomeQueries gives count-unknown-table. The error test now fails each
role alone and checks the exact fixed error and both endpoints.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev ecbc5bb4a8b7802e947fad8a200a08e01b99709b parent a3b3a3a
2 files changed, +86 −20
  • adopt/homepreflight.go +13 −2
  • adopt/homepreflight_test.go +73 −18
diff --git a/adopt/homepreflight.go b/adopt/homepreflight.go
index e727a0c..c7e30c6 100644
--- a/adopt/homepreflight.go
+++ b/adopt/homepreflight.go
@@ -9,6 +9,7 @@ package adopt
import (
"context"
"errors"
+ "slices"
"time"
"amadan.net/oficina/alta/homeop"
@@ -93,8 +94,6 @@ func CheckHome(ctx context.Context, h *homeop.Client, ps *provision.Store, count
refuse(!unused(dir), "directory-stream-used")
refuse(dir.Generation != nil, "generation-set")
refuse(r.ProposedID != 1, "proposed-not-one")
- refuse(r.Pending, "plan-pending")
- refuse(r.Open, "plan-open")
} else {
// Only Alta's initialize has run on the directory: sequence 1,
// and Home's head is the one Alta's journal holds.
@@ -103,6 +102,10 @@ func CheckHome(ctx context.Context, h *homeop.Client, ps *provision.Store, count
refuse(dir.Generation == nil, "generation-missing")
refuse(r.ProposedID != 1, "proposed-not-one")
}
+ // No plan may be in flight on either role. The spec allows none
+ // before Plan A, so initialize's own window must have expired.
+ refuse(r.Pending, "plan-pending")
+ refuse(r.Open, "plan-open")
for _, t := range homeTables {
n, ok := counts[t]
if !ok {
@@ -112,6 +115,14 @@ func CheckHome(ctx context.Context, h *homeop.Client, ps *provision.Store, count
r.Counts[t] = n
refuse(n != expectedCount(phase, t), "count-unexpected-"+t)
}
+ // A pasted table outside HomeQueries is a typo or a stray line. The
+ // code is fixed: the name Paul typed is not repeated.
+ for t := range counts {
+ if !slices.Contains(homeTables, t) {
+ codes = append(codes, "count-unknown-table")
+ break
+ }
+ }
return r, codes, nil
}
diff --git a/adopt/homepreflight_test.go b/adopt/homepreflight_test.go
index 948791e..dd4a024 100644
--- a/adopt/homepreflight_test.go
+++ b/adopt/homepreflight_test.go
@@ -20,9 +20,10 @@ import (
// inspectHome answers <role>.inspect with whatever state a test sets,
// echoing the signed challenge so the client accepts the answer.
type inspectHome struct {
- mu sync.Mutex
- state map[string]homeop.Inspection
- status int
+ mu sync.Mutex
+ state map[string]homeop.Inspection
+ // status, when set for a role, is that role's answer instead.
+ status map[string]int
calls map[string]int
}
@@ -31,8 +32,8 @@ func (f *inspectHome) handler(role string) http.Handler {
f.mu.Lock()
defer f.mu.Unlock()
f.calls[role]++
- if f.status != 0 {
- w.WriteHeader(f.status)
+ if s := f.status[role]; s != 0 {
+ w.WriteHeader(s)
return
}
raw, _ := io.ReadAll(r.Body)
@@ -245,13 +246,58 @@ func TestHomePreflightPrePlanAAcceptsOwnInitialize(t *testing.T) {
if r.CredentialSeq != 3 || r.CredentialHead != credHead {
t.Fatalf("record = %+v", r)
}
+}
- // Unknown pasted tables are not recorded.
- counts["extra"] = 9
- r, codes = p.check(t, counts, PhasePrePlanA)
- wantCodes(t, codes)
- if _, ok := r.Counts["extra"]; ok {
- t.Fatal("record holds a table HomeQueries does not ask for")
+// A pasted table HomeQueries does not ask for is a typo or a stray line:
+// refused, with one fixed code, and not recorded.
+func TestHomePreflightRefusesUnknownTable(t *testing.T) {
+ for _, phase := range []string{PhasePreInitialize, PhasePrePlanA} {
+ t.Run(phase, func(t *testing.T) {
+ p := newPreflightFixture(t)
+ counts := zeroCounts()
+ if phase == PhasePrePlanA {
+ _, counts = p.initialized(t)
+ }
+ counts["extra"] = 0
+ r, codes := p.check(t, counts, phase)
+ wantCodes(t, codes, "count-unknown-table")
+ if _, ok := r.Counts["extra"]; ok {
+ t.Fatal("record holds a table HomeQueries does not ask for")
+ }
+
+ delete(counts, "extra")
+ delete(counts, "teams")
+ counts["team"] = 0
+ _, codes = p.check(t, counts, phase)
+ wantCodes(t, codes, "count-missing-teams", "count-unknown-table")
+ })
+ }
+}
+
+// Before Plan A, a pending or open plan on either role is refused: the
+// spec allows none, and Plan A must be the only plan in flight.
+func TestHomePreflightPrePlanARefusesPendingOrOpenPlan(t *testing.T) {
+ cases := []struct {
+ name, role string
+ edit func(*homeop.Inspection)
+ code string
+ }{
+ {"credential-pending", homeop.Credential, func(in *homeop.Inspection) { in.PendingPlan = homeop.Token() }, "plan-pending"},
+ {"directory-pending", homeop.Directory, func(in *homeop.Inspection) { in.PendingPlan = homeop.Token() }, "plan-pending"},
+ {"credential-open", homeop.Credential, func(in *homeop.Inspection) { in.OpenPlan = homeop.Token() }, "plan-open"},
+ {"directory-open", homeop.Directory, func(in *homeop.Inspection) { in.OpenPlan = homeop.Token() }, "plan-open"},
+ }
+ for _, c := range cases {
+ t.Run(c.name, func(t *testing.T) {
+ p := newPreflightFixture(t)
+ _, counts := p.initialized(t)
+ p.set(c.role, c.edit)
+ r, codes := p.check(t, counts, PhasePrePlanA)
+ wantCodes(t, codes, c.code)
+ if r.Pending != (c.code == "plan-pending") || r.Open != (c.code == "plan-open") {
+ t.Fatalf("record pending=%v open=%v", r.Pending, r.Open)
+ }
+ })
}
}
@@ -366,12 +412,21 @@ func TestHomePreflightErrors(t *testing.T) {
t.Fatalf("phase %q accepted", phase)
}
}
- p.home.status = http.StatusServiceUnavailable
- _, codes, err := CheckHome(context.Background(), p.client, p.ps, zeroCounts(), PhasePreInitialize)
- if err == nil || codes != nil {
- t.Fatalf("an unavailable Home gave codes %q, err %v", codes, err)
- }
- if strings.Contains(err.Error(), p.client.Roles[homeop.Directory].URL) {
- t.Fatalf("error names the endpoint: %v", err)
+ // Each role failing alone gives that role's fixed error, naming no
+ // endpoint of either role.
+ for _, role := range []string{homeop.Credential, homeop.Directory} {
+ p.home.status = map[string]int{role: http.StatusServiceUnavailable}
+ _, codes, err := CheckHome(context.Background(), p.client, p.ps, zeroCounts(), PhasePreInitialize)
+ if err == nil || codes != nil {
+ t.Fatalf("%s unavailable gave codes %q, err %v", role, codes, err)
+ }
+ if want := "adopt: " + role + " inspect failed"; err.Error() != want {
+ t.Fatalf("error = %q, want %q", err, want)
+ }
+ for _, r := range p.client.Roles {
+ if strings.Contains(err.Error(), r.URL) || strings.Contains(err.Error(), r.Origin) {
+ t.Fatalf("error names an endpoint: %v", err)
+ }
+ }
}
}