carlos / platform Public

console: banner and CLI say which plan stage is active (plans 4b)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 60813760ff27a5918a20820983dcd06275aab854 parent a089ae0
5 files changed, +94 −10
  • internal/console/plancaps_copy.go +5 −0
  • internal/console/plangrace_cli_test.go +18 −1
  • internal/console/plangrace_test.go +2 −2
  • internal/console/plangrace_view_test.go +33 −2
  • internal/console/plangracepass.go +36 −5
diff --git a/internal/console/plancaps_copy.go b/internal/console/plancaps_copy.go
index 1282ea191..b0d4851a1 100644
--- a/internal/console/plancaps_copy.go
+++ b/internal/console/plancaps_copy.go
@@ -43,4 +43,9 @@ var planNoticeCopy = map[string]string{
"email.warn": "- %s.",
"email.over": "- %s, over the limit. Nothing changes until %s.",
"email.ways_out": "To stay within the plan, buy a pack, move to a bigger plan, or clean up.\n\nAccount settings:\n%s",
+ // Plans 4b: an after-grace stage in force. Luna pass pending.
+ "banner.email_paused": "Email is paused until midnight UTC: this account sent %s.",
+ "cli.email_paused": "email is paused until midnight utc: this account sent %s",
+ "banner.storage_denied": "New uploads to this account's storage are refused: %s. Downloads and deletes still work.",
+ "cli.storage_denied": "new uploads are refused: %s. downloads and deletes still work",
}
diff --git a/internal/console/plangrace_cli_test.go b/internal/console/plangrace_cli_test.go
index 105b1a4ca..f216249d0 100644
--- a/internal/console/plangrace_cli_test.go
+++ b/internal/console/plangrace_cli_test.go
@@ -4,6 +4,7 @@ import (
"net/http"
"strings"
"testing"
+ "time"
"github.com/carlosframework/platform/internal/svcrole"
)
@@ -36,7 +37,7 @@ func TestCLIWarningHeaderUnderAPlan(t *testing.T) {
overRow(t, fx)
token := cliToken(t, fx, "paul@keymail.dev", "code-cli-warn")
got := cliWarnings(t, fx, token, true)
- if len(got) != 1 || !strings.HasPrefix(got[0], "this account is over its plan: 2 GB of 1 GB object storage") {
+ if len(got) != 1 || !strings.HasPrefix(got[0], "this account is over its plan: 1.5 GB of 1 GB object storage") {
t.Fatalf("warning header = %q", got)
}
}
@@ -98,3 +99,19 @@ func TestCLIWarningOnAppScopedDoors(t *testing.T) {
t.Fatalf("app door = %d, warnings %q", resp.StatusCode, got)
}
}
+
+func TestCLIWarningSaysStorageWritesDenied(t *testing.T) {
+ fx := newConsoleFixture(t)
+ fx.srv.cfg.PlanCatalog = examplePlanCatalog(t)
+ acct, _ := fx.st.AccountBySqid(fx.sqid)
+ st := evaluateCap(nil, capStorage, 3_000_000_000, 1_000_000_000, time.Now().Add(-time.Hour))
+ st.AccountID = acct.ID
+ if err := fx.st.PutCapState(st); err != nil {
+ t.Fatal(err)
+ }
+ token := cliToken(t, fx, "paul@keymail.dev", "code-cli-stage")
+ got := cliWarnings(t, fx, token, true)
+ if len(got) != 1 || !strings.HasPrefix(got[0], "new uploads are refused") {
+ t.Fatalf("warning = %q", got)
+ }
+}
diff --git a/internal/console/plangrace_test.go b/internal/console/plangrace_test.go
index 4243c0082..c9f2c4b99 100644
--- a/internal/console/plangrace_test.go
+++ b/internal/console/plangrace_test.go
@@ -231,10 +231,10 @@ func TestExpiredPeriodIsNotDueOrShown(t *testing.T) {
if afterGraceDue(st, nov) {
t.Error("October's bandwidth row is due in November")
}
- if got := planNoticesOf([]capState{st}, nov); len(got) != 0 {
+ if got := planNoticesOf([]capState{st}, nov, nil); len(got) != 0 {
t.Errorf("October's row shown in November: %v", got)
}
- if got := planNoticesOf([]capState{st}, oct.Add(time.Hour)); len(got) != 1 {
+ if got := planNoticesOf([]capState{st}, oct.Add(time.Hour), nil); len(got) != 1 {
t.Errorf("current row not shown: %v", got)
}
}
diff --git a/internal/console/plangrace_view_test.go b/internal/console/plangrace_view_test.go
index c35342f57..be2fbb535 100644
--- a/internal/console/plangrace_view_test.go
+++ b/internal/console/plangrace_view_test.go
@@ -16,7 +16,7 @@ func overRow(t *testing.T, fx *consoleFixture) *Account {
acct, _ := fx.st.AccountBySqid(fx.sqid)
// The real clock: page views and CLI requests record notice at
// time.Now(), which must not fall before the first over.
- st := evaluateCap(nil, capStorage, 2_000_000_000, 1_000_000_000, time.Now().Add(-time.Hour))
+ st := evaluateCap(nil, capStorage, 1_500_000_000, 1_000_000_000, time.Now().Add(-time.Hour))
st.AccountID = acct.ID
if err := fx.st.PutCapState(st); err != nil {
t.Fatal(err)
@@ -29,7 +29,7 @@ func TestBannerShowsPlanNotices(t *testing.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") {
+ if !strings.Contains(body, "This account is over its plan: 1.5 GB of 1 GB object storage") {
t.Fatalf("account page has no plan banner:\n%s", body)
}
}
@@ -88,3 +88,34 @@ func TestBannerNoticeOnlyWhenRendered(t *testing.T) {
t.Fatal("a rendered banner recorded no notice")
}
}
+
+// Plans 4b: while a stage is active the banner says what is refused.
+func TestBannerSaysEmailPaused(t *testing.T) {
+ fx := newConsoleFixture(t)
+ fx.srv.cfg.PlanCatalog = examplePlanCatalog(t)
+ acct, _ := fx.st.AccountBySqid(fx.sqid)
+ st := evaluateCap(nil, capEmail, 100, 50, time.Now()) // free: 50 a day, paused at 100
+ st.AccountID = acct.ID
+ if err := fx.st.PutCapState(st); err != nil {
+ t.Fatal(err)
+ }
+ c := signInPaul(t, fx)
+ if _, _, body := get(t, c, fx.u("/")); !strings.Contains(body, "Email is paused until midnight UTC") {
+ t.Fatalf("no email-paused banner:\n%s", body)
+ }
+}
+
+func TestBannerSaysStorageWritesDenied(t *testing.T) {
+ fx := newConsoleFixture(t)
+ fx.srv.cfg.PlanCatalog = examplePlanCatalog(t)
+ acct, _ := fx.st.AccountBySqid(fx.sqid)
+ st := evaluateCap(nil, capStorage, 3_000_000_000, 1_000_000_000, time.Now().Add(-time.Hour)) // 3x: the hard ceiling
+ st.AccountID = acct.ID
+ if err := fx.st.PutCapState(st); err != nil {
+ t.Fatal(err)
+ }
+ c := signInPaul(t, fx)
+ if _, _, body := get(t, c, fx.u("/")); !strings.Contains(body, "New uploads to this account&#39;s storage are refused") {
+ t.Fatalf("no storage-denied banner:\n%s", body)
+ }
+}
diff --git a/internal/console/plangracepass.go b/internal/console/plangracepass.go
index 8f40f3571..c372f4a3f 100644
--- a/internal/console/plangracepass.go
+++ b/internal/console/plangracepass.go
@@ -170,7 +170,7 @@ func (s *Server) notifyPlanOwners(ctx context.Context, acct *Account, states []c
func (s *Server) planNoticeEmail(acct *Account, due []capState) (string, string) {
lines := []string{fmt.Sprintf(planNoticeCopy["email.intro"], acct.Sqid), ""}
- for _, n := range planNoticesOf(due, time.Now()) {
+ for _, n := range planNoticesOf(due, time.Now(), nil) {
lines = append(lines, planNoticeText(n, "email"))
}
settings := s.cfg.Origin + "/a/" + url.PathEscape(acct.Sqid) + "/settings"
@@ -184,16 +184,24 @@ type planNotice struct {
Level capLevel
Used, Limit int64
GraceEnds time.Time
+ // Stage names an after-grace stage in force (plans 4b): "email_paused"
+ // or "storage_denied"; "" when none.
+ Stage string
}
// planNoticesOf is the caps at warn or over in the current period, in
// capOrder. A row left from an ended day or month is not shown.
-func planNoticesOf(states []capState, now time.Time) []planNotice {
+// stage, when not nil, names the after-grace stage in force for a row.
+func planNoticesOf(states []capState, now time.Time, stage func(capState) string) []planNotice {
var out []planNotice
for _, c := range capOrder {
for _, st := range states {
if st.Cap == c && st.Period == capPeriod(c, now) && (st.Level == levelWarn || st.Level.over()) {
- out = append(out, planNotice{Cap: st.Cap, Level: st.Level, Used: st.Used, Limit: st.Limit, GraceEnds: st.GraceEnds})
+ n := planNotice{Cap: st.Cap, Level: st.Level, Used: st.Used, Limit: st.Limit, GraceEnds: st.GraceEnds}
+ if stage != nil {
+ n.Stage = stage(st)
+ }
+ out = append(out, n)
}
}
}
@@ -204,6 +212,9 @@ func planNoticesOf(states []capState, now time.Time) []planNotice {
// "email".
func planNoticeText(n planNotice, surface string) string {
use := usagePhrase(n)
+ if f, ok := planNoticeCopy[surface+"."+n.Stage]; ok && n.Stage != "" {
+ return fmt.Sprintf(f, use)
+ }
if n.Level.over() {
return fmt.Sprintf(planNoticeCopy[surface+".over"], use, n.GraceEnds.UTC().Format("2 January 2006"))
}
@@ -241,7 +252,7 @@ func (s *Server) planBanner(acct *Account, role string, now time.Time) ([]string
return nil, nil
}
var lines []string
- for _, n := range planNoticesOf(states, now) {
+ for _, n := range planNoticesOf(states, now, s.planStage(acct, now)) {
lines = append(lines, planNoticeText(n, "banner"))
}
if role != RoleOwner || len(lines) == 0 {
@@ -293,7 +304,7 @@ func (s *Server) writePlanWarnings(w http.ResponseWriter, r *http.Request, p Pri
return
}
now := time.Now()
- for _, n := range planNoticesOf(states, now) {
+ for _, n := range planNoticesOf(states, now, s.planStage(acct, now)) {
w.Header().Add(planWarningHeader, planNoticeText(n, "cli"))
}
seen := shownStates(states, now)
@@ -319,3 +330,23 @@ func (s *Server) isOwner(acct *Account, addr string) bool {
}
return false
}
+
+// planStage names the after-grace stage in force for a cap row, for the
+// banner and CLI line (plans 4b): the same predicates enforcement uses.
+// nil when the account's limits cannot be read.
+func (s *Server) planStage(acct *Account, now time.Time) func(capState) string {
+ l, err := s.EffectiveLimits(acct)
+ if err != nil || l == nil {
+ return nil
+ }
+ pauseAt := planMailPauseAt(l, s.cfg.PlanCatalog.Ceilings)
+ return func(st capState) string {
+ switch {
+ case st.Cap == capEmail && pauseAt > 0 && st.Used >= pauseAt:
+ return "email_paused"
+ case st.Cap == capStorage && storageDenyDue(st, now):
+ return "storage_denied"
+ }
+ return ""
+ }
+}