carlos / platform Public

console: HA fallback region posts as ha_region; enable refusal test

With no picker, the create form named the home in a hidden region input,
so a plain create from a listed account's form stored its region source
as chosen. The fallback is now ha_region, read only when ha is set; an
unticked form posts what it did before G6 (regression test added).

The enable-refusal test now asserts no manifest was written, and a
control shows the same enable succeeds once the fault is removed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 2a9a930005de3f77d3ed2e5dd1837611e96b4541 parent 749f98c
4 files changed, +111 −19
  • internal/console/newapp_ha_test.go +88 −6
  • internal/console/regionpicker.go +4 −3
  • internal/console/server.go +15 −7
  • internal/console/templates/app_new.html +4 −3

CI cancelled — superseded by a newer push — run details

Intent

Attributed to this commit: 33.7 g CO₂e (4,333,334 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/newapp_ha_test.go b/internal/console/newapp_ha_test.go
index ff3493e75..0ce1edf11 100644
--- a/internal/console/newapp_ha_test.go
+++ b/internal/console/newapp_ha_test.go
@@ -2,9 +2,13 @@ package console
import (
"context"
+ "errors"
"fmt"
+ "html"
"net/http"
"net/url"
+ "os"
+ "regexp"
"slices"
"strings"
"testing"
@@ -34,6 +38,23 @@ func haPickerAbsentWorld(t *testing.T, accounts []string) *haRegionWorld {
// usHAOption is the picker's us option with its HA mark.
const usHAOption = `<option value="us" data-ha>`
+// hiddenInput matches a rendered hidden input's name and value.
+var hiddenInput = regexp.MustCompile(`<input type="hidden" name="([^"]+)" value="([^"]*)">`)
+
+// renderedNewAppFields is what the create form as rendered for w posts
+// with nothing ticked or typed: its hidden inputs (the CSRF value is
+// replaced by postNewApp). Tests that must post what the page posts use
+// it rather than a hand-written field set.
+func renderedNewAppFields(t *testing.T, w *haRegionWorld, c *http.Client) url.Values {
+ t.Helper()
+ _, _, body := get(t, c, w.u("/apps/new"))
+ form := url.Values{}
+ for _, m := range hiddenInput.FindAllStringSubmatch(newAppForm(t, body), -1) {
+ form.Add(html.UnescapeString(m[1]), html.UnescapeString(m[2]))
+ }
+ return form
+}
+
// postNewApp posts the browser create form, refused or not, and reports
// the status and the redirect.
func postNewApp(t *testing.T, w *haRegionWorld, c *http.Client, csrf string, form url.Values) (int, string) {
@@ -97,8 +118,8 @@ func TestNewAppHACheckboxOnlyForListedAccounts(t *testing.T) {
t.Errorf("listed: a non-HA option is marked: %s", o)
}
}
- if strings.Contains(form, `type="hidden" name="region"`) {
- t.Errorf("listed with a picker: a hidden region input in\n%s", form)
+ if strings.Contains(form, `name="ha_region"`) {
+ t.Errorf("listed with a picker: a hidden HA region input in\n%s", form)
}
})
@@ -143,8 +164,13 @@ func TestNewAppHACheckboxOnlyForListedAccounts(t *testing.T) {
if !strings.Contains(form, `name="ha"`) || !has(form, haCopyLabel) {
t.Errorf("picker absent: no HA checkbox in\n%s", form)
}
- if !strings.Contains(form, `<input type="hidden" name="region" value="ie">`) {
- t.Errorf("picker absent: no hidden region ie in\n%s", form)
+ if !strings.Contains(form, `<input type="hidden" name="ha_region" value="ie">`) {
+ t.Errorf("picker absent: no hidden HA region ie in\n%s", form)
+ }
+ // The fallback is HA's own field: the form posts no region, as
+ // before Task 6 (TestNewAppHAUncheckedKeepsRegionSource).
+ if strings.Contains(form, `name="region"`) {
+ t.Errorf("picker absent: the form posts a region in\n%s", form)
}
})
@@ -156,7 +182,7 @@ func TestNewAppHACheckboxOnlyForListedAccounts(t *testing.T) {
if strings.Contains(form, `id="app-region"`) {
t.Fatalf("control: the picker rendered in\n%s", form)
}
- if strings.Contains(form, `name="ha"`) || strings.Contains(form, `name="region"`) {
+ if strings.Contains(form, `name="ha"`) || strings.Contains(form, `name="region"`) || strings.Contains(form, `name="ha_region"`) {
t.Errorf("unlisted: the form offers HA:\n%s", form)
}
})
@@ -233,9 +259,20 @@ func TestNewAppHACreatesAndEnables(t *testing.T) {
t.Run("picker absent, the home", func(t *testing.T) {
w := haPickerAbsentWorld(t, []string{"bab"})
c, csrf := haBrowser(t, w)
- status, loc := postNewApp(t, w, c, csrf, url.Values{"name": {"shop"}, "region": {"ie"}, "ha": {"1"}})
+ // What the page posts with the box ticked: its hidden fields (the
+ // HA region among them), the name and ha.
+ form := renderedNewAppFields(t, w, c)
+ if form.Get("ha_region") != "ie" || form.Has("region") {
+ t.Fatalf("fixture: the rendered form posts %v, want ha_region ie and no region", form)
+ }
+ form.Set("name", "shop")
+ form.Set("ha", "1")
+ status, loc := postNewApp(t, w, c, csrf, form)
wantCreated(t, w, "ha at home", "shop", status, loc)
wantHAApp(t, w, "shop", "")
+ if r, src, _ := w.st.AppRegion(OpsAccountID, "shop"); r != "" || src != "chosen" {
+ t.Errorf("ha at home: AppRegion = %q %q, want the home, chosen", r, src)
+ }
})
t.Run("the enable refuses after the claim", func(t *testing.T) {
@@ -254,9 +291,54 @@ func TestNewAppHACreatesAndEnables(t *testing.T) {
if _, _, body := get(t, c, w.u("/apps/shop")); !has(body, sentenceCase(ref.Msg)) || has(body, "Created app shop") {
t.Errorf("the app page does not carry the enable's refusal alone")
}
+ // The refused enable wrote no manifest.
+ if _, err := provision.GetManifest(context.Background(), w.rel, w.sqid, "shop"); !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("refused enable: manifest read = %v, want it absent", err)
+ }
+ // Control: with the fault removed, the same enable succeeds and
+ // binds the row's pool and region.
+ w.srv.cfg.AppsDomain = "apps.example.test"
+ acct, err := w.st.AccountByID(OpsAccountID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if ref := w.srv.enableHAManifest(context.Background(), acct, "shop", "203.0.113.9"); ref != nil {
+ t.Fatalf("control: enable with the apps domain set = %+v", ref)
+ }
+ wantHAApp(t, w, "shop", "us")
})
}
+// TestNewAppHAUncheckedKeepsRegionSource: with no picker and an HA home, a
+// listed account's form carries the HA fallback region, and submitting it
+// with the box unticked stores the region and source a plain create stored
+// before Task 6 (the form then posted no region), never "chosen".
+func TestNewAppHAUncheckedKeepsRegionSource(t *testing.T) {
+ w := haPickerAbsentWorld(t, []string{"bab"})
+ c, csrf := haBrowser(t, w)
+ // Control: the pre-Task-6 form's post (no region field).
+ status, loc := postNewApp(t, w, c, csrf, url.Values{"name": {"before"}})
+ wantCreated(t, w, "pre-Task-6 form", "before", status, loc)
+ wantRegion, wantSource, err := w.st.AppRegion(OpsAccountID, "before")
+ if err != nil || wantSource == "chosen" {
+ t.Fatalf("control: AppRegion = %q %q %v, want a source the person did not choose", wantRegion, wantSource, err)
+ }
+ // What the page posts now with the box unticked.
+ form := renderedNewAppFields(t, w, c)
+ if !form.Has("ha_region") {
+ t.Fatalf("fixture: the rendered form carries no HA fallback: %v", form)
+ }
+ form.Set("name", "after")
+ status, loc = postNewApp(t, w, c, csrf, form)
+ wantCreated(t, w, "unticked HA form", "after", status, loc)
+ if r, src, err := w.st.AppRegion(OpsAccountID, "after"); err != nil || r != wantRegion || src != wantSource {
+ t.Errorf("unticked HA form: AppRegion = %q %q %v, want %q %q as before Task 6", r, src, err, wantRegion, wantSource)
+ }
+ if pool, _, _ := w.st.AppHAPool(context.Background(), OpsAccountID, "after"); pool != "" {
+ t.Errorf("unticked HA form stored pool %q", pool)
+ }
+}
+
func TestAppSettingsHAStatusLine(t *testing.T) {
w := haRegionFixture(t, haRegionOpts{Accounts: []string{"bab"}})
c, csrf := haBrowser(t, w)
diff --git a/internal/console/regionpicker.go b/internal/console/regionpicker.go
index 51bfd230f..71c51b8fb 100644
--- a/internal/console/regionpicker.go
+++ b/internal/console/regionpicker.go
@@ -78,14 +78,15 @@ func (s *Server) newAppPicker(ctx context.Context, ip net.IP) *regionPickerView
return v
}
-// pickedRegion turns the form's region field into a regionReq. ok=false
+// pickedRegion turns the form's region (v, the region field or the HA
+// fallback handleNewApp chose) into a regionReq. ok=false
// means the submitted code is not an acceptable region NOW (C5). It is the
// API door's check (validateExplicitRegion), because claimApp does not
// re-validate an explicit region; with the switch off an explicit region is
// neither validated nor refused (spec §9.6, as apicli.go does).
-func (s *Server) pickedRegion(ctx context.Context, r *http.Request) (regionReq, bool) {
+func (s *Server) pickedRegion(ctx context.Context, r *http.Request, v string) (regionReq, bool) {
rq := regionReq{tz: r.FormValue("tz"), ip: s.personIP(r)}
- v := strings.TrimSpace(r.FormValue("region"))
+ v = strings.TrimSpace(v)
if v == "" || v == autoRegion || !s.appRegionsOn() {
return rq, true
}
diff --git a/internal/console/server.go b/internal/console/server.go
index 8f11a71a1..8e224956b 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -5739,21 +5739,29 @@ func (s *Server) handleNewApp(w http.ResponseWriter, r *http.Request) {
name := normalizeAppName(r.FormValue("name"))
// The region is resolved inside claimApp, once it knows the claim is not
// an adoption; the timezone and the address only ever feed that guess.
- rq, regionOK := s.pickedRegion(r.Context(), r)
+ // The high availability checkbox (G6) asks for HA with any value; an
+ // older form never posts it. With no picker the form names the home in
+ // ha_region, which counts only for an HA create: a plain create's
+ // region (and its stored source) is what the form posted before G6.
+ ha := r.FormValue("ha") != ""
+ region := r.FormValue("region")
+ if ha && strings.TrimSpace(region) == "" {
+ region = r.FormValue("ha_region")
+ }
+ rq, regionOK := s.pickedRegion(r.Context(), r, region)
if !regionOK {
fail(regionCopy.Unavailable)
return
}
- // The high availability checkbox (G6): the API door's "ha" choice, for
- // the person this session is, before anything is written. Any value
- // asks for it; an older form never posts the field.
- if r.FormValue("ha") != "" {
- pool, region, ref := s.haCreateChoice(Principal{Kind: PrincipalUser, Address: addr}, acct, r.FormValue("region"), "")
+ // The HA choice is the API door's "ha", for the person this session
+ // is, before anything is written.
+ if ha {
+ pool, haRegion, ref := s.haCreateChoice(Principal{Kind: PrincipalUser, Address: addr}, acct, region, "")
if ref != nil {
fail(sentenceCase(ref.Msg))
return
}
- rq.haPool, rq.haRegion = pool, region
+ rq.haPool, rq.haRegion = pool, haRegion
}
// The analytics checkbox (the on-ramp): "count" is edge or off from an
// owner's form, absent from a member's (whose checkbox is disabled) and
diff --git a/internal/console/templates/app_new.html b/internal/console/templates/app_new.html
index 2577be543..365ea82de 100644
--- a/internal/console/templates/app_new.html
+++ b/internal/console/templates/app_new.html
@@ -42,11 +42,12 @@
{{- /* High availability (G6): server-rendered enabled. With the picker,
a small script disables it while the picked region has no data-ha
and enables it on change; with JS off it stays enabled and the
- server's refusal answers. With no picker, the hidden region names
- the home, the one region this create can land in. */}}
+ server's refusal answers. With no picker, the hidden ha_region names
+ the home, the one region this create can land in; the server reads
+ it only with ha set, so an unticked form posts what it always did. */}}
{{- if .NewAppHA}}
{{- with .NewAppHAHomeRegion}}
- <input type="hidden" name="region" value="{{.}}">
+ <input type="hidden" name="ha_region" value="{{.}}">
{{- end}}
<label><input type="checkbox" name="ha" value="1">
{{.HAPoolCopy.Label}}</label>