| index 6f8d15261..64da11a4c 100644 |
| --- a/internal/console/apiinstancemove_offline_test.go |
| +++ b/internal/console/apiinstancemove_offline_test.go |
| @@ -2,6 +2,7 @@ package console |
| |
| import ( |
| "context" |
| + "crypto/rand" |
| "crypto/sha256" |
| "encoding/hex" |
| "encoding/json" |
| @@ -1591,3 +1592,122 @@ func TestOfflineStartKeepsTheLockWhenTheRecordWriteFails(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// otherMoveID is a move id no record in these worlds carries. |
| +func otherMoveID(t *testing.T) string { |
| + t.Helper() |
| + id, err := moves.NewMoveID(rand.Reader) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return id |
| +} |
| + |
| +// withID is a door's JSON body: its own members and, when id is not empty, |
| +// the move id the caller expects. |
| +func withID(members, id string) string { |
| + if id != "" { |
| + if members != "" { |
| + members += "," |
| + } |
| + members += `"id":"` + id + `"` |
| + } |
| + return "{" + members + "}" |
| +} |
| + |
| +// Every door that acts on a move takes the id of the move its caller expects |
| +// (plan 2026-11-15-offline-move-run, Task 3). Another move's id is refused |
| +// with the record's own sentence (MV-O35) and nothing written: not the |
| +// record, its record_seq, the serial or the lock. The move's own id is the |
| +// request with no id. |
| +func TestOfflineDoorsRefuseAnotherMovesID(t *testing.T) { |
| + for _, tc := range []struct { |
| + name, path, members string |
| + setup func(w *offlineWorld) |
| + want string // the state the move's own id leaves |
| + }{ |
| + {"stage again", "/stage", `"region":"uk"`, func(w *offlineWorld) { w.stage() }, provision.OfflineStaging}, |
| + {"start a staged move", "", `"region":"uk"`, func(w *offlineWorld) { w.stage() }, provision.OfflineStopping}, |
| + {"seal", "/seal", "", func(w *offlineWorld) { |
| + w.start() |
| + w.stopped(nil, "ie-box") |
| + }, provision.OfflineSealing}, |
| + {"flip", "/flip", "", func(w *offlineWorld) { |
| + w.start() |
| + w.stopped(nil, "ie-box") |
| + w.ok("/seal", "", provision.OfflineSealing) |
| + w.sealed("ie-box") |
| + w.sealerFresh("ie-box") |
| + w.stopped(nil, "ie-box") |
| + }, provision.OfflineLanding}, |
| + {"open", "/open", "", func(w *offlineWorld) { w.toLanded() }, provision.OfflineOpen}, |
| + {"finish", "/finish", "", func(w *offlineWorld) { |
| + w.start() |
| + w.ok("/abort", "", provision.OfflineReverting) |
| + w.report(moves.OfflineRoleSource, "ie-box", moves.OfflinePhaseReverted, nil) |
| + }, "none"}, |
| + {"abort", "/abort", "", func(w *offlineWorld) { w.start() }, provision.OfflineReverting}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + w := newOfflineWorld(t, oneEach...) |
| + tc.setup(w) |
| + seq := w.record().RecordSeq |
| + w.refused(http.MethodPost, tc.path, withID(tc.members, otherMoveID(t)), http.StatusConflict, "the record carries a different offline move") |
| + if got := w.record().RecordSeq; got != seq { |
| + t.Fatalf("record_seq %d after a refused id, want %d", got, seq) |
| + } |
| + w.ok(tc.path, withID(tc.members, w.id), tc.want) |
| + }) |
| + } |
| +} |
| + |
| +// An id on a record with no offline move expects a move that is not there: |
| +// MV-O24, as the verbs answer it with no id, and nothing written. The stage |
| +// and start doors, which begin a move when none is in flight, begin none. |
| +func TestOfflineDoorsRefuseAnIDWithNoMove(t *testing.T) { |
| + w := newOfflineWorld(t, oneEach...) |
| + id := otherMoveID(t) |
| + for _, rt := range offlineRoutes { |
| + if rt.method != http.MethodPost { |
| + continue |
| + } |
| + w.refused(rt.method, rt.path, withID(strings.Trim(rt.body, "{}"), id), http.StatusConflict, offlineHost+" has no offline move in flight") |
| + } |
| + if rec := w.record(); rec.Move != nil { |
| + t.Fatalf("a refused id began a move: %+v", rec.Move) |
| + } |
| +} |
| + |
| +// The move's own id, and no id at all, are answered with the same bytes: |
| +// refusals in the state each door does not act on, and the abort that finds |
| +// the move already reverting. Nothing is written by any of them. |
| +func TestOfflineDoorsTheMovesOwnIDIsNoID(t *testing.T) { |
| + w := newOfflineWorld(t, oneEach...) |
| + w.start() |
| + same := func(path, members string) { |
| + t.Helper() |
| + before := bucketState(t, w.fx.rel) |
| + bodies := []string{withID(members, ""), withID(members, w.id)} |
| + if members == "" { |
| + bodies = append(bodies, "") // the verbs have never needed a body |
| + } |
| + code0, out0 := w.req(http.MethodPost, path, bodies[0]) |
| + for _, body := range bodies[1:] { |
| + if code, out := w.req(http.MethodPost, path, body); code != code0 || out != out0 { |
| + t.Fatalf("POST %s %q = %d %s\nwant %d %s", path, body, code, out, code0, out0) |
| + } |
| + } |
| + if after := bucketState(t, w.fx.rel); fmt.Sprint(after) != fmt.Sprint(before) { |
| + t.Fatalf("POST %s changed the bucket", path) |
| + } |
| + } |
| + // offline-stopping with no box report yet: every door but abort refuses. |
| + same("/stage", `"region":"uk"`) |
| + same("", `"region":"uk"`) |
| + for _, verb := range []string{"/seal", "/flip", "/open", "/finish"} { |
| + same(verb, "") |
| + } |
| + // Already reverting: abort answers the view and writes nothing. |
| + w.ok("/abort", "", provision.OfflineReverting) |
| + same("/abort", "") |
| +} |