console: banner, CLI and email say what a lapse turned off (plans 4d)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+240
−6
internal/console/plancaps_copy.go+10 −0internal/console/plangrace_cli_test.go+11 −0internal/console/plangrace_view_test.go+61 −0internal/console/plangracepass.go+36 −6internal/console/plangracepass_test.go+44 −0internal/console/planlapse.go+78 −0
diff --git a/internal/console/plancaps_copy.go b/internal/console/plancaps_copy.go| index dd5dbe50a..2d1cc2289 100644 |
| --- a/internal/console/plancaps_copy.go |
| +++ b/internal/console/plancaps_copy.go |
| @@ -61,4 +61,14 @@ var planNoticeCopy = map[string]string{ |
| "audit.reason.running_things": "the plan covers %d apps and instances", |
| "audit.reason.sidecars": "the plan covers %d workers per app", |
| "audit.reason.custom_domains": "the plan does not include custom domains", |
| + // Plans 4d: a lapse in grace, and what it turned off. |
| + "usage.sidecars": "%d workers over the %d per app this plan allows", |
| + "usage.custom_domains": "%d custom domains, which this plan does not include", |
| + "banner.lapse_grace": "This account's plan no longer covers %s. On %s the newest are turned off. Their settings and data are kept, and they come back when the plan covers them again. You can choose which stay.", |
| + "cli.lapse_grace": "this account's plan no longer covers %s. on %s the newest are turned off. settings and data are kept. choose which stay in the console", |
| + "email.lapse_grace": "- %s. On %s the newest are turned off unless the plan covers them again.", |
| + "banner.lapse_off": "Turned off because this account's plan no longer covers them: %s. Their settings and data are kept, and they come back when the plan covers them again.", |
| + "cli.lapse_off": "turned off because this account's plan no longer covers them: %s. settings and data are kept", |
| + "email.lapse_off": "- Turned off: %s.", |
| + "email.lapse_off_list": "Turned off: %s.\n\nChoose which stay:\n%s", |
| } |
diff --git a/internal/console/plangrace_cli_test.go b/internal/console/plangrace_cli_test.go| index 3804a024c..bcf98a152 100644 |
| --- a/internal/console/plangrace_cli_test.go |
| +++ b/internal/console/plangrace_cli_test.go |
| @@ -115,3 +115,14 @@ func TestCLIWarningSaysStorageWritesDenied(t *testing.T) { |
| t.Fatalf("warning = %q", got) |
| } |
| } |
| + |
| +func TestCLIWarningSaysLapseOff(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct, _ := lapsedOps(t, fx) |
| + turnOffOne(t, fx, acct, "blog", "blog-1.example.com") |
| + token := cliToken(t, fx, "paul@keymail.dev", "code-cli-lapse") |
| + got := cliWarnings(t, fx, token, true) |
| + if len(got) != 1 || !strings.HasPrefix(got[0], "turned off because this account's plan no longer covers them: 5 of 3 apps and instances") { |
| + t.Fatalf("warning = %q", got) |
| + } |
| +} |
diff --git a/internal/console/plangrace_view_test.go b/internal/console/plangrace_view_test.go| index 9f8f77cc8..640ff89d0 100644 |
| --- a/internal/console/plangrace_view_test.go |
| +++ b/internal/console/plangrace_view_test.go |
| @@ -134,3 +134,64 @@ func TestBannerSaysBandwidthThrottled(t *testing.T) { |
| t.Fatalf("no bandwidth-throttled banner:\n%s", body) |
| } |
| } |
| + |
| +// lapsedOps puts the ops account on a lapse: free from a paid plan, over |
| +// its running-things cap in grace on the real clock. |
| +func lapsedOps(t *testing.T, fx *consoleFixture) (*Account, capState) { |
| + t.Helper() |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + acct, _ := fx.st.AccountBySqid(fx.sqid) |
| + if _, err := fx.st.SetAccountPlan(acct.ID, "free", nil, PlanSourceOperator, nil, "r", "ops@example.com", time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := fx.st.MarkLapse(acct.ID, time.Now().Add(-time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + st := evaluateCap(nil, capRunningThings, 5, 3, time.Now().Add(-time.Hour)) |
| + st.AccountID = acct.ID |
| + if err := fx.st.PutCapState(st); err != nil { |
| + t.Fatal(err) |
| + } |
| + return acct, st |
| +} |
| + |
| +func turnOffOne(t *testing.T, fx *consoleFixture, acct *Account, app, host string) { |
| + t.Helper() |
| + if err := fx.st.PutLapseItem(t.Context(), lapseItem{AccountID: acct.ID, App: app, Kind: lapseInstance, Ref: host, |
| + ThingCreated: planNow, DisabledAt: time.Now(), UpdatedAt: time.Now()}); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +func TestBannerSaysLapseGrace(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + _, st := lapsedOps(t, fx) |
| + c := signInPaul(t, fx) |
| + _, _, body := get(t, c, fx.u("/")) |
| + if !strings.Contains(body, "plan no longer covers 5 of 3 apps and instances") || !strings.Contains(body, st.GraceEnds.UTC().Format("2 January 2006")) { |
| + t.Fatalf("no lapse-grace banner with the date:\n%s", body) |
| + } |
| +} |
| + |
| +func TestBannerSaysLapseOff(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct, _ := lapsedOps(t, fx) |
| + turnOffOne(t, fx, acct, "blog", "blog-1.example.com") |
| + c := signInPaul(t, fx) |
| + if _, _, body := get(t, c, fx.u("/")); !strings.Contains(body, "Turned off because this account's plan no longer covers them") { |
| + t.Fatalf("no lapse-off banner:\n%s", body) |
| + } |
| +} |
| + |
| +func TestNonEligibleBannerUnchanged(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct, _ := lapsedOps(t, fx) |
| + if err := fx.st.ClearLapse(acct.ID); err != nil { |
| + t.Fatal(err) |
| + } |
| + c := signInPaul(t, fx) |
| + _, _, body := get(t, c, fx.u("/")) |
| + if !strings.Contains(body, "This account is over its plan: 5 of 3 apps and instances") || strings.Contains(body, "no longer covers") { |
| + t.Fatalf("never-lapsed account's banner changed:\n%s", body) |
| + } |
| +} |
diff --git a/internal/console/plangracepass.go b/internal/console/plangracepass.go| index c4c81eb45..0ccdc1534 100644 |
| --- a/internal/console/plangracepass.go |
| +++ b/internal/console/plangracepass.go |
| @@ -156,8 +156,14 @@ func (s *Server) planGraceAccount(ctx context.Context, acct *Account, now time.T |
| } |
| // noticeDue is whether st should go out in this pass's email: the first |
| -// 80% warning of the period, the first over, then a reminder a day. |
| -func noticeDue(st capState, now time.Time) bool { |
| +// 80% warning of the period, the first over, then a reminder a day. Once a |
| +// lapse has turned things off (stage "lapse_off", plans 4d) the reminder |
| +// is weekly instead, and goes at once when something more is turned off |
| +// (lastOff, the newest turned off of the cap's kind). |
| +func noticeDue(st capState, now time.Time, stage string, lastOff time.Time) bool { |
| + if stage == "lapse_off" && st.Level.over() { |
| + return st.RemindedAt.IsZero() || st.RemindedAt.Before(lastOff) || !now.Before(st.RemindedAt.Add(7*24*time.Hour)) |
| + } |
| switch { |
| case st.Level == levelWarn: |
| return st.WarnedAt.IsZero() |
| @@ -171,9 +177,15 @@ func noticeDue(st capState, now time.Time) bool { |
| // due. Marks are set, and notice recorded, only when at least one owner's |
| // delivery was accepted; otherwise the next pass tries again. |
| func (s *Server) notifyPlanOwners(ctx context.Context, acct *Account, states []capState, now time.Time) error { |
| + stage := s.planStage(acct, now) |
| + lastOff := s.lapseLastOff(acct) |
| var due []capState |
| for _, st := range states { |
| - if noticeDue(st, now) { |
| + stg := "" |
| + if stage != nil { |
| + stg = stage(st) |
| + } |
| + if noticeDue(st, now, stg, lastOff[st.Cap]) { |
| due = append(due, st) |
| } |
| } |
| @@ -184,7 +196,7 @@ func (s *Server) notifyPlanOwners(ctx context.Context, acct *Account, states []c |
| if err != nil { |
| return err |
| } |
| - subject, body := s.planNoticeEmail(acct, due) |
| + subject, body := s.planNoticeEmail(acct, due, stage) |
| delivered := false |
| for _, m := range members { |
| if m.Role != RoleOwner { |
| @@ -216,10 +228,16 @@ func (s *Server) notifyPlanOwners(ctx context.Context, acct *Account, states []c |
| return s.store.RecordCapNotice(acct.ID, due, now) |
| } |
| -func (s *Server) planNoticeEmail(acct *Account, due []capState) (string, string) { |
| +func (s *Server) planNoticeEmail(acct *Account, due []capState, stage func(capState) string) (string, string) { |
| lines := []string{fmt.Sprintf(planNoticeCopy["email.intro"], acct.Sqid), ""} |
| - for _, n := range planNoticesOf(due, time.Now(), nil) { |
| + lapsed := false |
| + for _, n := range planNoticesOf(due, time.Now(), stage) { |
| lines = append(lines, planNoticeText(n, "email")) |
| + lapsed = lapsed || n.Stage == "lapse_off" |
| + } |
| + if names := s.lapseOffNames(acct); lapsed && len(names) > 0 { |
| + keep := s.cfg.Origin + "/a/" + url.PathEscape(acct.Sqid) + "/plan/keep" |
| + lines = append(lines, "", fmt.Sprintf(planNoticeCopy["email.lapse_off_list"], strings.Join(names, ", "), keep)) |
| } |
| settings := s.cfg.Origin + "/a/" + url.PathEscape(acct.Sqid) + "/settings" |
| lines = append(lines, "", fmt.Sprintf(planNoticeCopy["email.ways_out"], settings)) |
| @@ -260,6 +278,9 @@ func planNoticesOf(states []capState, now time.Time, stage func(capState) string |
| // "email". |
| func planNoticeText(n planNotice, surface string) string { |
| use := usagePhrase(n) |
| + if n.Stage == "lapse_grace" { |
| + return fmt.Sprintf(planNoticeCopy[surface+".lapse_grace"], use, n.GraceEnds.UTC().Format("2 January 2006")) |
| + } |
| if f, ok := planNoticeCopy[surface+"."+n.Stage]; ok && n.Stage != "" { |
| return fmt.Sprintf(f, use) |
| } |
| @@ -274,6 +295,8 @@ func usagePhrase(n planNotice) string { |
| switch n.Cap { |
| case capStorage, capBandwidth: |
| return fmt.Sprintf(f, humanGB(n.Used), humanGB(n.Limit)) |
| + case capCustomDomains: |
| + return fmt.Sprintf(f, n.Used) |
| } |
| return fmt.Sprintf(f, n.Used, n.Limit) |
| } |
| @@ -381,6 +404,9 @@ func (s *Server) isOwner(acct *Account, addr string) bool { |
| // 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. |
| +// For an account on the lapse path (plans 4d) a lapse cap's row is |
| +// "lapse_off" while something of its kind is turned off, otherwise |
| +// "lapse_grace" while it is over. |
| // 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) |
| @@ -388,7 +414,11 @@ func (s *Server) planStage(acct *Account, now time.Time) func(capState) string { |
| return nil |
| } |
| pauseAt := planMailPauseAt(l, s.cfg.PlanCatalog.Ceilings) |
| + lapse := s.lapseStages(acct) |
| return func(st capState) string { |
| + if stage := lapse(st); stage != "" { |
| + return stage |
| + } |
| switch { |
| case st.Cap == capEmail && pauseAt > 0 && st.Used >= pauseAt: |
| return "email_paused" |
diff --git a/internal/console/plangracepass_test.go b/internal/console/plangracepass_test.go| index 39c0f06d3..aa6955169 100644 |
| --- a/internal/console/plangracepass_test.go |
| +++ b/internal/console/plangracepass_test.go |
| @@ -144,3 +144,47 @@ func TestGracePassMembersGetNoEmail(t *testing.T) { |
| } |
| } |
| } |
| + |
| +func TestLapseOffRemindsWeekly(t *testing.T) { |
| + fx, acct, mailer := lapseFixture(t) |
| + sent := func() int { |
| + mailer.mu.Lock() |
| + defer mailer.mu.Unlock() |
| + return len(mailer.sent) |
| + } |
| + applyPlan(t, fx, acct, "free", nil, nil, planNow) |
| + if sent() != 1 { |
| + t.Fatalf("on the fall: %d emails, want 1", sent()) |
| + } |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week)) |
| + if sent() != 2 { |
| + t.Fatalf("on turning off: %d emails, want 2", sent()) |
| + } |
| + fx.srv.planGracePass(context.Background(), planNow.Add(week+24*time.Hour)) |
| + if sent() != 2 { |
| + t.Fatalf("a day later: %d emails, want still 2", sent()) |
| + } |
| + fx.srv.planGracePass(context.Background(), planNow.Add(2*week)) |
| + if sent() != 3 { |
| + t.Fatalf("a week later: %d emails, want 3", sent()) |
| + } |
| + lapseInstanceRec(t, fx, acct, "blog", "blog-2.example.com", at(10)) |
| + fx.srv.planGracePass(context.Background(), planNow.Add(2*week+time.Hour)) |
| + if sent() != 4 { |
| + t.Fatalf("another thing off: %d emails, want 4", sent()) |
| + } |
| +} |
| + |
| +func TestLapseEmailNamesWhatIsOff(t *testing.T) { |
| + fx, acct, mailer := lapseFixture(t) |
| + fx.srv.cfg.Origin = "https://console.example.com" |
| + lapseAndTurnOff(t, fx, acct) |
| + mailer.mu.Lock() |
| + last := mailer.sent[len(mailer.sent)-1] |
| + mailer.mu.Unlock() |
| + for _, want := range []string{"blog-1.example.com", "shop-2.example.com", "https://console.example.com/a/" + acct.Sqid + "/plan/keep", "Turned off"} { |
| + if !strings.Contains(last, want) { |
| + t.Fatalf("email lacks %q:\n%s", want, last) |
| + } |
| + } |
| +} |
diff --git a/internal/console/planlapse.go b/internal/console/planlapse.go| index 287e22b96..f98a61760 100644 |
| --- a/internal/console/planlapse.go |
| +++ b/internal/console/planlapse.go |
| @@ -4,6 +4,7 @@ import ( |
| "context" |
| "fmt" |
| "log" |
| + "slices" |
| "time" |
| "github.com/carlosframework/platform/internal/plans" |
| @@ -249,3 +250,80 @@ func (s *Server) clearLapseOff(ctx context.Context, acct *Account, now time.Time |
| _, err = s.store.SetAccountDisabled(acct.ID, nil, now) |
| return err |
| } |
| + |
| +// lapseCapOf is the lapse cap an item kind is turned off under. |
| +func lapseCapOf(kind string) capID { |
| + switch kind { |
| + case lapseSidecar: |
| + return capSidecars |
| + case lapseDomain: |
| + return capCustomDomains |
| + } |
| + return capRunningThings |
| +} |
| + |
| +// lapseStages answers planStage's lapse question for acct, reading the plan |
| +// row and the lapse items once. A store error, or an account not on the |
| +// lapse path, answers "" for every row. |
| +func (s *Server) lapseStages(acct *Account) func(capState) string { |
| + none := func(capState) string { return "" } |
| + row, err := s.store.AccountPlan(acct.ID) |
| + if err != nil || !lapseEligible(row) { |
| + return none |
| + } |
| + items, err := s.store.LapseItems(acct.ID) |
| + if err != nil { |
| + return none |
| + } |
| + off := map[capID]bool{} |
| + for _, it := range items { |
| + if !it.DisabledAt.IsZero() { |
| + off[lapseCapOf(it.Kind)] = true |
| + } |
| + } |
| + return func(st capState) string { |
| + if !slices.Contains(lapseCaps, st.Cap) { |
| + return "" |
| + } |
| + switch { |
| + case off[st.Cap]: |
| + return "lapse_off" |
| + case st.Level.over(): |
| + return "lapse_grace" |
| + } |
| + return "" |
| + } |
| +} |
| + |
| +// lapseLastOff is, per lapse cap, when the newest thing of its kind was |
| +// turned off. A store error is no times: reminders fall back to weekly. |
| +func (s *Server) lapseLastOff(acct *Account) map[capID]time.Time { |
| + out := map[capID]time.Time{} |
| + items, err := s.store.LapseItems(acct.ID) |
| + if err != nil { |
| + return out |
| + } |
| + for _, it := range items { |
| + if c := lapseCapOf(it.Kind); it.DisabledAt.After(out[c]) { |
| + out[c] = it.DisabledAt |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// lapseOffNames is what a lapse has turned off, as owners know it: app |
| +// names and hosts, sorted. |
| +func (s *Server) lapseOffNames(acct *Account) []string { |
| + items, err := s.store.LapseItems(acct.ID) |
| + if err != nil { |
| + return nil |
| + } |
| + var out []string |
| + for _, it := range items { |
| + if !it.DisabledAt.IsZero() { |
| + out = append(out, it.Ref) |
| + } |
| + } |
| + slices.Sort(out) |
| + return out |
| +} |