console: plan notice banner on account pages (plans 4a)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+101
−3
internal/console/plangrace_view_test.go+64 −0internal/console/plangracepass.go+29 −0internal/console/server.go+6 −2internal/console/templates/layout.html+2 −1
diff --git a/internal/console/plangrace_view_test.go b/internal/console/plangrace_view_test.go| new file mode 100644 |
| index 000000000..cd44e5884 |
| --- /dev/null |
| +++ b/internal/console/plangrace_view_test.go |
| @@ -0,0 +1,64 @@ |
| +package console |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// overRow puts the ops account over its storage cap under the example |
| +// catalog. |
| +func overRow(t *testing.T, fx *consoleFixture) *Account { |
| + t.Helper() |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + acct, _ := fx.st.AccountBySqid(fx.sqid) |
| + st := evaluateCap(nil, capStorage, 2_000_000_000, 1_000_000_000, planNow) |
| + st.AccountID = acct.ID |
| + if err := fx.st.PutCapState(st); err != nil { |
| + t.Fatal(err) |
| + } |
| + return acct |
| +} |
| + |
| +func TestBannerShowsPlanNotices(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + overRow(t, fx) |
| + c := signInPaul(t, fx) |
| + _, _, body := get(t, c, fx.u("/")) |
| + if !strings.Contains(body, "This account is over its plan: 2 GB of 1 GB object storage") { |
| + t.Fatalf("account page has no plan banner:\n%s", body) |
| + } |
| +} |
| + |
| +func TestBannerRecordsNoticeForOwnersOnly(t *testing.T) { |
| + fx, _, _ := memberFixture(t) |
| + acct := overRow(t, fx) |
| + kim := newBrowser(t) |
| + fx.setUser("kim@keymail.dev") |
| + signUp(t, fx, kim, "code-kim-banner") |
| + if _, _, body := get(t, kim, fx.u("/")); !strings.Contains(body, "over its plan") { |
| + t.Fatal("a member sees no banner") |
| + } |
| + if st, _ := capRow(t, fx, acct, capStorage); !st.NoticeAt.IsZero() { |
| + t.Fatalf("a member's page view recorded notice: %v", st.NoticeAt) |
| + } |
| + fx.setUser("paul@keymail.dev") |
| + owner := signInPaul(t, fx) |
| + get(t, owner, fx.u("/")) |
| + if st, _ := capRow(t, fx, acct, capStorage); st.NoticeAt.IsZero() { |
| + t.Fatal("an owner's page view recorded no notice") |
| + } |
| +} |
| + |
| +func TestBannerWithoutACatalogUnchanged(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct, _ := fx.st.AccountBySqid(fx.sqid) |
| + st := evaluateCap(nil, capStorage, 2_000_000_000, 1_000_000_000, planNow) |
| + st.AccountID = acct.ID |
| + if err := fx.st.PutCapState(st); err != nil { // a row left from when plans were on |
| + t.Fatal(err) |
| + } |
| + c := signInPaul(t, fx) |
| + if _, _, body := get(t, c, fx.u("/")); strings.Contains(body, "over its plan") { |
| + t.Fatal("plan banner with no catalog") |
| + } |
| +} |
diff --git a/internal/console/plangracepass.go b/internal/console/plangracepass.go| index ff61f6115..0ed21a52a 100644 |
| --- a/internal/console/plangracepass.go |
| +++ b/internal/console/plangracepass.go |
| @@ -215,3 +215,32 @@ func humanGB(b int64) string { |
| s := strconv.FormatFloat(float64(b)/1e9, 'f', 1, 64) |
| return strings.TrimSuffix(s, ".0") + " GB" |
| } |
| + |
| +// planBanner is the account page's plan warnings, one line per cap at warn |
| +// or over. An owner seeing a cap that is over is notice received (spec §6); |
| +// a member seeing it is not. A store error shows no banner and never fails |
| +// the page. |
| +func (s *Server) planBanner(acct *Account, role string, now time.Time) []string { |
| + if s.cfg.PlanCatalog == nil { |
| + return nil |
| + } |
| + states, err := s.store.CapStates(acct.ID) |
| + if err != nil { |
| + log.Printf("console: plan banner %s: %v", acct.Sqid, err) |
| + return nil |
| + } |
| + var lines []string |
| + var over []capID |
| + for _, n := range planNoticesOf(states) { |
| + lines = append(lines, planNoticeText(n, "banner")) |
| + if n.Level.over() { |
| + over = append(over, n.Cap) |
| + } |
| + } |
| + if role == RoleOwner && len(over) > 0 { |
| + if err := s.store.RecordCapNotice(acct.ID, over, now); err != nil { |
| + log.Printf("console: plan banner %s: notice: %v", acct.Sqid, err) |
| + } |
| + } |
| + return lines |
| +} |
diff --git a/internal/console/server.go b/internal/console/server.go| index 49a1022e3..50780c63d 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -1871,8 +1871,11 @@ type page struct { |
| Msg string // generic message page body |
| - Flash string // one-shot success notice (cookie-carried across a redirect) |
| - FlashErr string // one-shot error notice |
| + Flash string // one-shot success notice (cookie-carried across a redirect) |
| + // PlanNotices are the account's plan warnings (plans 4a), one banner |
| + // line each; nil when plans are off or nothing is near a limit. |
| + PlanNotices []string |
| + FlashErr string // one-shot error notice |
| Apps []appRow // account home |
| Detail *AppView // /a/{sqid}/apps/{app} |
| @@ -3350,6 +3353,7 @@ func (s *Server) accountPage(w http.ResponseWriter, r *http.Request, addr, raw s |
| p.Accounts[i].Seg = preferredSeg(p.Accounts, &p.Accounts[i]) |
| } |
| p.Nav = "/a/" + url.PathEscape(preferredSeg(p.Accounts, acct)) |
| + p.PlanNotices = s.planBanner(acct, role, time.Now()) |
| return p |
| } |
diff --git a/internal/console/templates/layout.html b/internal/console/templates/layout.html| index 3f4e63949..8d3884f33 100644 |
| --- a/internal/console/templates/layout.html |
| +++ b/internal/console/templates/layout.html |
| @@ -591,7 +591,8 @@ |
| </nav>{{end}} |
| </header> |
| <main> |
| -{{end}} |
| +{{range .PlanNotices}}<p class="banner">{{.}}</p> |
| +{{end}}{{end}} |
| {{define "bottom"}} |
| </main> |