Files
davidandClaude Opus 5.5 aa0408fb14 Phase 14, part 4: what happens when it lapses
An hourly platform job: a trial over without a subscription goes to Free;
past due, the owner is told and every page has a banner; at 14 days
read-only; at 53 days everyone is warned; at 60 days the team is deleted
with 30 days to undo. Each email once. Undoing a deletion for not paying
gives a week more.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 13:27:03 -07:00

102 lines
3.3 KiB
Go

package web
import (
"context"
"net/http"
"strings"
"testing"
"time"
"supermarket/internal/appmail"
"supermarket/internal/billing"
"supermarket/internal/users"
)
func TestLapses(t *testing.T) {
s, h := newBilled(t)
c := signUp(t, h, "olu@acme.example", "Acme", "acme.example")
u, _ := users.ByEmail(s.platform(), "olu@acme.example")
list, _ := users.TeamsOf(s.platform(), u.ID)
team := list[0].ID
ob := s.Mail.(*appmail.Outbox)
now := time.Now()
turn := func(at time.Time) { s.billingTurn(context.Background(), at) }
subject := func(n int) string {
t.Helper()
return waitMail(t, s, "olu@acme.example", n).Subject
}
// The trial ends without a subscription: Free, and an email.
before := ob.Count()
turn(now)
if a, _ := billing.Get(s.platform(), team); a.Status != billing.Trialing {
t.Fatalf("trial ended early: %+v", a)
}
turn(now.Add(15 * 24 * time.Hour))
if a, _ := billing.Get(s.platform(), team); a.Status != billing.Free {
t.Errorf("after the trial: %+v", a)
}
if got := subject(before + 1); !strings.Contains(got, "trial has ended") {
t.Errorf("trial mail %q", got)
}
// A failed payment: an email once, and a banner.
due := now.Add(-time.Hour)
setAccount(t, s, team, func(a *billing.Account) {
a.Status, a.Subscription, a.PastDueSince, a.Warned = billing.PastDue, "sub_1", due, ""
})
before = ob.Count()
turn(now)
if got := subject(before + 1); !strings.Contains(got, "payment for Acme failed") {
t.Errorf("past-due mail %q", got)
}
turn(now)
time.Sleep(50 * time.Millisecond)
if ob.Count() != before+1 {
t.Error("the past-due mail went twice")
}
if rec := as(h, c, http.MethodGet, "/", nil); !strings.Contains(rec.Body.String(), "payment is due since") || !strings.Contains(rec.Body.String(), "Pay now") {
t.Errorf("no banner: %s", rec.Body)
}
// 14 days: read-only, and an email.
turn(due.Add(15 * 24 * time.Hour))
if got := subject(before + 2); !strings.Contains(got, "read-only") {
t.Errorf("read-only mail %q", got)
}
// 53 days: everyone's warned.
turn(due.Add(54 * 24 * time.Hour))
if got := subject(before + 3); !strings.Contains(got, "deleted in a week") {
t.Errorf("warning mail %q", got)
}
// 60 days: deleting, with 30 days to undo.
turn(due.Add(61 * 24 * time.Hour))
if got := subject(before + 4); !strings.Contains(got, "being deleted") {
t.Errorf("deleting mail %q", got)
}
tm, _ := users.GetTeam(s.platform(), team)
if tm.Status != users.TeamDeleting {
t.Fatalf("team %+v", tm)
}
if rec := as(h, c, http.MethodGet, "/", nil); rec.Code != http.StatusForbidden || !strings.Contains(rec.Body.String(), "Undo") {
t.Errorf("closed page: %d", rec.Code)
}
// The owner undoes it: a week more to pay, warned at the next turn.
if rec := as(h, c, http.MethodPost, "/teams/undelete", nil); rec.Code != http.StatusSeeOther {
t.Fatalf("undelete: %d %s", rec.Code, rec.Body)
}
a, _ := billing.Get(s.platform(), team)
if a.Warned != "read-only" || time.Since(a.PastDueSince) < 52*24*time.Hour {
t.Errorf("after undoing: %+v", a)
}
if rec := as(h, c, http.MethodGet, "/", nil); !strings.Contains(rec.Body.String(), "read-only: nothing is fetched") {
t.Errorf("no read-only banner: %s", rec.Body)
}
n := ob.Count()
turn(time.Now())
if got := waitMail(t, s, "olu@acme.example", n+1).Subject; !strings.Contains(got, "deleted in a week") {
t.Errorf("after undoing, mail %q", got)
}
}