console: HA region tests observe the pre-hook phase; browser place pairs
Fix round 1 of the G6 pool Task 4 review. The paused region and place tests now also record each request's phase before its hook: exact reads, no writes. The region race test gains the same us-to-uk change on a plain app as its success control. Both browser placement forms get an HA refusal (flash, no side effects) and a plain success.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+125
−29
internal/console/appregion_ha_test.go+125 −29
Intent
Attributed to this commit: 19.9 g CO₂e (2,569,736 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on g6-pool, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/console/appregion_ha_test.go b/internal/console/appregion_ha_test.go| index aee458990..4273fc343 100644 |
| --- a/internal/console/appregion_ha_test.go |
| +++ b/internal/console/appregion_ha_test.go |
| @@ -26,32 +26,45 @@ import ( |
| "github.com/carlosframework/platform/internal/provision" |
| ) |
| -// unchanged runs req, a request on an existing app the test expects |
| -// refused, and proves it had no side effects: its witnessed bucket |
| -// operations (puts and deletes whether or not they succeeded, and every |
| -// listing) are exactly allow.Ops, every bucket object's bytes are as they |
| -// were (but for allow.Changed), and every console database table is |
| -// unchanged (no row written, no audit line). |
| -func (w *haRegionWorld) unchanged(t *testing.T, what string, allow haAllow, req func()) { |
| +// haObservation watches a request on an existing app the test expects |
| +// refused, from observe to check: every witnessed bucket operation (puts |
| +// and deletes whether or not they succeeded, and every listing), and the |
| +// bucket's and the console database's state at observe. |
| +type haObservation struct { |
| + w *haRegionWorld |
| + bucket, db map[string]string |
| + mu sync.Mutex |
| + ops []string |
| +} |
| + |
| +// observe snapshots the bucket and every console table and starts |
| +// witnessing bucket operations. |
| +func (w *haRegionWorld) observe(t *testing.T) *haObservation { |
| t.Helper() |
| - bucket, db := w.bucketSnap(t), w.dbSnap(t) |
| - var mu sync.Mutex |
| - var ops []string |
| - ds := w.rel.(*deletableStore) |
| - ds.setOpWitness(func(op, key string) { |
| - mu.Lock() |
| - defer mu.Unlock() |
| - ops = append(ops, op+" "+key) |
| + o := &haObservation{w: w, bucket: w.bucketSnap(t), db: w.dbSnap(t)} |
| + w.rel.(*deletableStore).setOpWitness(func(op, key string) { |
| + o.mu.Lock() |
| + defer o.mu.Unlock() |
| + o.ops = append(o.ops, op+" "+key) |
| }) |
| - req() |
| - ds.setOpWitness(nil) |
| - mu.Lock() |
| - defer mu.Unlock() |
| - got, want := slices.Sorted(slices.Values(ops)), slices.Sorted(slices.Values(allow.Ops)) |
| + return o |
| +} |
| + |
| +// check stops witnessing and proves what ran since observe had no side |
| +// effects: its bucket operations are exactly allow.Ops, every bucket |
| +// object's bytes are as they were (but for allow.Changed), and every |
| +// console database table is unchanged (no row written, no audit line). |
| +func (o *haObservation) check(t *testing.T, what string, allow haAllow) { |
| + t.Helper() |
| + w := o.w |
| + w.rel.(*deletableStore).setOpWitness(nil) |
| + o.mu.Lock() |
| + defer o.mu.Unlock() |
| + got, want := slices.Sorted(slices.Values(o.ops)), slices.Sorted(slices.Values(allow.Ops)) |
| if !slices.Equal(got, want) { |
| - t.Errorf("%s: the refused request's bucket operations = %v, want exactly %v", what, got, want) |
| + t.Errorf("%s: the request's bucket operations = %v, want exactly %v", what, got, want) |
| } |
| - after, before := w.bucketSnap(t), maps.Clone(bucket) |
| + after, before := w.bucketSnap(t), maps.Clone(o.bucket) |
| for _, k := range allow.Changed { |
| delete(after, k) |
| delete(before, k) |
| @@ -59,11 +72,20 @@ func (w *haRegionWorld) unchanged(t *testing.T, what string, allow haAllow, req |
| if !maps.Equal(after, before) { |
| t.Errorf("%s: the bucket changed: %v", what, mapDiff(before, after)) |
| } |
| - if got := w.dbSnap(t); !maps.Equal(got, db) { |
| - t.Errorf("%s: the console database changed: %v", what, mapDiff(db, got)) |
| + if got := w.dbSnap(t); !maps.Equal(got, o.db) { |
| + t.Errorf("%s: the console database changed: %v", what, mapDiff(o.db, got)) |
| } |
| } |
| +// unchanged runs req, a request on an existing app the test expects |
| +// refused, under one observation: it must have had no side effects. |
| +func (w *haRegionWorld) unchanged(t *testing.T, what string, allow haAllow, req func()) { |
| + t.Helper() |
| + o := w.observe(t) |
| + req() |
| + o.check(t, what, allow) |
| +} |
| + |
| // haCreate creates app through the API create door as the world's person: |
| // born HA in region when ha, else plain. It must answer 200. |
| func (w *haRegionWorld) haCreate(t *testing.T, app, region string, ha bool) { |
| @@ -154,11 +176,17 @@ func (w *haRegionWorld) browser(t *testing.T) (*http.Client, string) { |
| // postFlash posts form to the browser door at path, wants the Settings |
| // redirect, and returns the flash it left. |
| func (w *haRegionWorld) postFlash(t *testing.T, c *http.Client, path string, form url.Values) (kind, msg string) { |
| + t.Helper() |
| + return w.postFlashTo(t, c, path, "/settings", form) |
| +} |
| + |
| +// postFlashTo is postFlash for a door whose redirect is to back. |
| +func (w *haRegionWorld) postFlashTo(t *testing.T, c *http.Client, path, back string, form url.Values) (kind, msg string) { |
| t.Helper() |
| claimClearFlash(c, w.ts.URL) |
| status, loc, body := postForm(t, c, w.ts.URL+path, form) |
| - if status != http.StatusSeeOther || !strings.Contains(loc, "/settings") { |
| - t.Fatalf("POST %s = %d %q %s, want the Settings redirect", path, status, loc, body) |
| + if status != http.StatusSeeOther || !strings.Contains(loc, back) { |
| + t.Fatalf("POST %s = %d %q %s, want the redirect to %s", path, status, loc, body, back) |
| } |
| return claimFlashOf(c, w.ts.URL) |
| } |
| @@ -378,21 +406,36 @@ func TestStoreSetAppRegionSkipsHARow(t *testing.T) { |
| // |
| // The mutation that makes it fail: delete the incarnation comparison in |
| // setAppRegion AND in Store.SetAppRegion, AND Store.SetAppRegion's empty |
| -// ha_pool condition; step 5 then answers 200 and the row reads uk. With any one layer left it |
| -// passes (TestStoreSetAppRegionSkipsHARow pins the ha_pool layer alone). |
| +// ha_pool condition; step 5 then answers 200 and the row reads uk. With |
| +// any one layer left it passes (TestStoreSetAppRegionSkipsHARow pins the |
| +// ha_pool layer alone). |
| func TestRegionChangeAcrossHARecreation(t *testing.T) { |
| w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}}) |
| + // Control: the same us to uk change, on a plain app nothing recreates, |
| + // answers 200 and lands. |
| + w.haCreate(t, "steady", "us", false) |
| + if status, res := putJSON(t, w.appURL("steady", "region"), map[string]any{"region": "uk"}, w.token); status != http.StatusOK || |
| + res["region"] != "uk" || res["changed"] != true { |
| + t.Fatalf("control: steady us to uk = %d %v, want 200 uk changed", status, res) |
| + } |
| + if r := w.regionOf(t, "steady"); r != "uk" { |
| + t.Fatalf("control: steady's region = %q, want uk", r) |
| + } |
| // 1. A plain shop in us. |
| w.haCreate(t, "shop", "us", false) |
| if r := w.regionOf(t, "shop"); r != "us" { |
| t.Fatalf("shop's region = %q, want us", r) |
| } |
| - // 2. The change to uk, paused at its admitted point. |
| + // 2. The change to uk, paused at its admitted point. Before the hook it |
| + // reads only the region choices and the edge evidence (haRegionCheckOps: |
| + // appRegionChoices, then liveEdges) and writes nothing. |
| pause := newHAPause(t) |
| appRegionAdmitted = pause.hook("shop") |
| t.Cleanup(func() { appRegionAdmitted = nil }) |
| + pre := w.observe(t) |
| done := haSend(http.MethodPut, w.appURL("shop", "region"), w.token, map[string]any{"region": "uk"}) |
| pause.wait(t, done) |
| + pre.check(t, "region change, before its hook", haAllow{Ops: haRegionCheckOps()}) |
| // 3. The name is recreated as HA, under the same created_at. |
| w.recreate(t, "shop", true) |
| // 4. The new shop's first enable binds pool ha to us. |
| @@ -475,6 +518,55 @@ func TestPlaceRefusesHAApp(t *testing.T) { |
| refused("place, after enable") |
| } |
| +// TestPlaceBrowserRefusesHAApp: both browser placement forms (the app's |
| +// Settings > Placement and the fleet page's "Place an app here") refuse a |
| +// born-HA app with the err flash haCopyRegionLocked and change nothing. |
| +// Controls: the same forms for plain apps place them, with the ok flash. |
| +func TestPlaceBrowserRefusesHAApp(t *testing.T) { |
| + w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}, HistoryInactive: true}) |
| + if _, err := w.st.CreateHAAppInRegion(OpsAccountID, "haapp", "paul@keymail.dev", "us", "chosen", "ha", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, app := range []string{"plainsettings", "plainfleet"} { |
| + if _, err := w.st.CreateAppIn(OpsAccountID, app, "paul@keymail.dev", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + own := "fleet/" + w.ownFleet |
| + c, csrf := w.browser(t) |
| + doors := []struct { |
| + name, plain string |
| + post func(app string) (kind, msg string) |
| + }{ |
| + {"settings placement", "plainsettings", func(app string) (string, string) { |
| + return w.postFlashTo(t, c, "/a/bab/apps/"+app+"/place", "/a/bab/apps/"+app+"/settings/placement", |
| + url.Values{"csrf": {csrf}, "target": {own}}) |
| + }}, |
| + {"fleet page place", "plainfleet", func(app string) (string, string) { |
| + return w.postFlashTo(t, c, "/a/bab/fleets/"+w.ownFleet+"/place", fleetPageURL("bab", w.ownFleet), |
| + url.Values{"csrf": {csrf}, "app": {app}}) |
| + }}, |
| + } |
| + for _, d := range doors { |
| + var kind, msg string |
| + w.unchanged(t, d.name, haAllow{}, func() { kind, msg = d.post("haapp") }) |
| + if kind != "err" || msg != haCopyRegionLocked { |
| + t.Errorf("%s: flash = %q %q, want err %q", d.name, kind, msg, haCopyRegionLocked) |
| + } |
| + if p := w.placementOf(t, "haapp"); p != "" { |
| + t.Errorf("%s: haapp's placement = %q, want none", d.name, p) |
| + } |
| + // Control: the same form for a plain app. |
| + wantFlash, _ := placeFlash(d.plain, own, "(console)") |
| + if kind, msg := d.post(d.plain); kind != "ok" || msg != wantFlash { |
| + t.Fatalf("control: %s for %s: flash = %q %q, want ok %q", d.name, d.plain, kind, msg, wantFlash) |
| + } |
| + if p := w.placementOf(t, d.plain); p != own { |
| + t.Fatalf("control: %s: %s's placement = %q, want %q", d.name, d.plain, p, own) |
| + } |
| + } |
| +} |
| + |
| // TestPlaceRefusesHAAppHistoryActive: the early refusal (the clear answer) |
| // comes before the place door's hostname-history cut, so a born-HA app |
| // hears why it is never placed, not that placement is fixed for now. The |
| @@ -535,8 +627,12 @@ func TestPlacePausedAcrossRecreation(t *testing.T) { |
| } |
| pause := newHAPause(t) |
| placeAdmitted = pause.hook(app) |
| + // Before the hook the place reads no bucket listing and writes |
| + // nothing. |
| + pre := w.observe(t) |
| done := haSend(http.MethodPost, w.appURL(app, "place"), w.token, map[string]any{"target": own}) |
| pause.wait(t, done) |
| + pre.check(t, "place, before its hook", haAllow{}) |
| w.recreate(t, app, c.ha) |
| var r haReply |
| w.unchanged(t, "paused place", haAllow{}, func() { |