plans: team plans multiply a base plan's pooled limits per member, with a minimum; disk stays per running thing; max_members on other plans (plans 6b)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
15 files changed,
+468
−39
internal/billing/prices_test.go+5 −5internal/billingstripe/adapter_test.go+1 −1internal/console/apiaccountplan.go+2 −2internal/console/planlapse.go+2 −2internal/console/planlimits.go+2 −2internal/plans/catalog.go+27 −3internal/plans/catalog_ratelimit_test.go+9 −9internal/plans/catalog_test.go+10 −10internal/plans/export_test.go+1 −1internal/plans/pricebook_test.go+3 −1internal/plans/team.go+119 −0internal/plans/team_test.go+192 −0internal/plans/testdata/example-catalog.json+33 −0internal/plans/testdata/example-pricebook.json+3 −1internal/plans/testdata/example-public-pricebook.json+59 −2
diff --git a/internal/billing/prices_test.go b/internal/billing/prices_test.go| index b735a492a..e42785cc2 100644 |
| --- a/internal/billing/prices_test.go |
| +++ b/internal/billing/prices_test.go |
| @@ -10,8 +10,8 @@ func TestParsePriceMap(t *testing.T) { |
| book := exampleBook(t, cat) |
| full := fullPrices(book) |
| - if len(full) != 18 { |
| - t.Fatalf("example book needs %d prices, want 18", len(full)) |
| + if len(full) != 24 { |
| + t.Fatalf("example book needs %d prices, want 24", len(full)) |
| } |
| m, err := ParsePriceMap(rawPrices(t, full), book) |
| if err != nil { |
| @@ -25,7 +25,7 @@ func TestParsePriceMap(t *testing.T) { |
| t.Errorf("ID(personal usd) = %q, %v", id, ok) |
| } |
| refs := m.Refs("example-v1") |
| - if len(refs) != 18 { |
| + if len(refs) != 24 { |
| t.Errorf("Refs = %d entries", len(refs)) |
| } |
| for i := 1; i < len(refs); i++ { |
| @@ -51,7 +51,7 @@ func TestParsePriceMap(t *testing.T) { |
| if ref, ok := old.Lookup("price_old1"); !ok || ref.Book != "example-v0" || ref.Kind != KindPlan || ref.Name != "personal" { |
| t.Errorf("Lookup(old) = %+v, %v", ref, ok) |
| } |
| - if got := old.Refs("example-v1"); len(got) != 18 { |
| + if got := old.Refs("example-v1"); len(got) != 24 { |
| t.Errorf("Refs(current) with an old key = %d", len(got)) |
| } |
| @@ -64,7 +64,7 @@ func TestParsePriceMap(t *testing.T) { |
| "empty book": with(func(m map[string]string) { m["/personal/usd"] = "price_empty" }), |
| "upper currency": with(func(m map[string]string) { m["x/personal/USD"] = "price_upper" }), |
| "product id value": with(func(m map[string]string) { m["x/personal/usd"] = "prod_x" }), |
| - "current book extra": with(func(m map[string]string) { m["example-v1/business/usd"] = "price_biz" }), |
| + "current book extra": with(func(m map[string]string) { m["example-v1/gold/usd"] = "price_gold" }), |
| "current pack extra": with(func(m map[string]string) { m["example-v1/pack/seats/usd"] = "price_seats" }), |
| "not json": "{", |
| "not an object": `["price_x"]`, |
diff --git a/internal/billingstripe/adapter_test.go b/internal/billingstripe/adapter_test.go| index 0334737bd..ddaa895e4 100644 |
| --- a/internal/billingstripe/adapter_test.go |
| +++ b/internal/billingstripe/adapter_test.go |
| @@ -381,7 +381,7 @@ func TestCheckRefusesMismatchedPrices(t *testing.T) { |
| t.Fatalf("all good: %v", err) |
| } |
| // Request budget: the account, each price, each distinct product. |
| - if fs.reads > 1+18+6 { |
| + if fs.reads > 1+24+8 { |
| t.Errorf("check made %d reads", fs.reads) |
| } |
| } |
diff --git a/internal/console/apiaccountplan.go b/internal/console/apiaccountplan.go| index d93e16375..6d74db486 100644 |
| --- a/internal/console/apiaccountplan.go |
| +++ b/internal/console/apiaccountplan.go |
| @@ -110,7 +110,7 @@ func (s *Server) handleAPIAccountPlanSet(w http.ResponseWriter, r *http.Request) |
| jsonError(w, http.StatusBadRequest, msg) |
| return |
| } |
| - if _, err := s.cfg.PlanCatalog.Effective(req.Plan, req.Packs, req.Sponsored); err != nil { |
| + if _, err := s.cfg.PlanCatalog.Effective(req.Plan, req.Packs, req.Sponsored, 1); err != nil { |
| jsonError(w, http.StatusBadRequest, err.Error()) |
| return |
| } |
| @@ -179,7 +179,7 @@ func (s *Server) accountPlanReplyFor(acct *Account) (accountPlanReply, error) { |
| reply.PublishedRevision = 0 |
| } |
| } |
| - if l, err := cat.Effective(reply.Plan, reply.Packs, reply.Sponsored); err != nil { |
| + if l, err := cat.Effective(reply.Plan, reply.Packs, reply.Sponsored, 1); err != nil { |
| reply.LimitsError = err.Error() |
| } else { |
| reply.Limits = &l |
diff --git a/internal/console/planlapse.go b/internal/console/planlapse.go| index ca06169c8..d17ea9710 100644 |
| --- a/internal/console/planlapse.go |
| +++ b/internal/console/planlapse.go |
| @@ -51,11 +51,11 @@ func (s *Server) lapseStarted(old *AccountPlanRow, plan string, packs map[string |
| if !paid { |
| return false |
| } |
| - was, err := cat.Effective(oldPlan, oldPacks, nil) |
| + was, err := cat.Effective(oldPlan, oldPacks, nil, 1) |
| if err != nil { |
| return false |
| } |
| - next, err := cat.Effective(plan, packs, nil) |
| + next, err := cat.Effective(plan, packs, nil, 1) |
| if err != nil { |
| return false |
| } |
diff --git a/internal/console/planlimits.go b/internal/console/planlimits.go| index d3808f839..b508314ca 100644 |
| --- a/internal/console/planlimits.go |
| +++ b/internal/console/planlimits.go |
| @@ -80,7 +80,7 @@ func (s *Server) publishAccountLimits(ctx context.Context, row AccountPlanRow, n |
| if cat == nil { |
| return errors.New("no plan catalog") |
| } |
| - limits, err := cat.Effective(row.Plan, row.Packs, row.Sponsored) |
| + limits, err := cat.Effective(row.Plan, row.Packs, row.Sponsored, 1) |
| if err != nil { |
| return err |
| } |
| @@ -131,7 +131,7 @@ func (s *Server) EffectiveLimits(acct *Account) (*plans.Limits, error) { |
| if row != nil { |
| plan, packs, sponsored = row.Plan, row.Packs, row.Sponsored |
| } |
| - l, err := cat.Effective(plan, packs, sponsored) |
| + l, err := cat.Effective(plan, packs, sponsored, 1) |
| if err != nil { |
| return nil, err |
| } |
diff --git a/internal/plans/catalog.go b/internal/plans/catalog.go| index 20bd4ae5d..e3c3842e2 100644 |
| --- a/internal/plans/catalog.go |
| +++ b/internal/plans/catalog.go |
| @@ -68,6 +68,11 @@ type Plan struct { |
| Paid bool `json:"paid"` |
| Limits Limits `json:"limits"` |
| Packs []string `json:"packs,omitempty"` // pack names this plan may buy |
| + // Team makes this a team plan (team.go). Nil on every other plan. |
| + Team *Team `json:"team,omitempty"` |
| + // MaxMembers caps an account's members on a non-team plan. Zero is no |
| + // cap; a team plan may not set it. |
| + MaxMembers int `json:"max_members,omitempty"` |
| } |
| // Pack is one pack type: what a unit adds, and how many units one account |
| @@ -183,7 +188,7 @@ func (c *Catalog) validate() error { |
| return fmt.Errorf("pack %q may only add running_things, disk_bytes, storage_bytes, email_per_day or bandwidth_bytes_month", name) |
| } |
| } |
| - return nil |
| + return c.validateTeams() |
| } |
| func (l Limits) nonNegative() error { |
| @@ -224,12 +229,32 @@ func (l Limits) additive() Limits { |
| // sponsored override taking the more generous value per field. Sponsored |
| // values are not clamped: a sponsored figure above a ceiling is an explicit |
| // operator decision (spec §8.7). |
| -func (c *Catalog) Effective(plan string, packs map[string]int, sponsored *Limits) (Limits, error) { |
| +// |
| +// On a team plan, members is the account's member count (0 to |
| +// MaxTeamMembers). Each pooled field is the base plan's times the |
| +// per-member multiple times the billed members, rounded down; the rest, |
| +// disk included, are the team plan's own. Packs add flat per account. The |
| +// pooled fields' ceilings scale by the same factor when it is above 1; |
| +// idle and rate ceilings never do. On other plans members is ignored. |
| +func (c *Catalog) Effective(plan string, packs map[string]int, sponsored *Limits, members int) (Limits, error) { |
| p, ok := c.Plans[plan] |
| if !ok { |
| return Limits{}, fmt.Errorf("%w: %q", ErrUnknownPlan, plan) |
| } |
| l := p.Limits |
| + ce := c.Ceilings |
| + if t := p.Team; t != nil { |
| + if members < 0 || members > MaxTeamMembers { |
| + return Limits{}, fmt.Errorf("%w: %d, allowed 0 to %d", ErrTeamMembers, members, MaxTeamMembers) |
| + } |
| + scale := uint64(t.PerMemberMilli) * uint64(t.Billed(members)) |
| + if !scalePooled(&l, c.Plans[t.Base].Limits, scale) { |
| + return Limits{}, fmt.Errorf("%w: %d members on %q overflows", ErrTeamMembers, members, plan) |
| + } |
| + if scale > 1000 && !scalePooled(&ce, c.Ceilings, scale) { |
| + return Limits{}, fmt.Errorf("%w: %d members on %q overflows the ceilings", ErrTeamMembers, members, plan) |
| + } |
| + } |
| names := make([]string, 0, len(packs)) |
| for name := range packs { |
| names = append(names, name) |
| @@ -257,7 +282,6 @@ func (c *Catalog) Effective(plan string, packs map[string]int, sponsored *Limits |
| return Limits{}, fmt.Errorf("%w: %q overflows", ErrPackQuantity, name) |
| } |
| } |
| - ce := c.Ceilings |
| l.RunningThings = clamp(l.RunningThings, ce.RunningThings) |
| l.DiskBytes = clamp(l.DiskBytes, ce.DiskBytes) |
| l.StorageBytes = clamp(l.StorageBytes, ce.StorageBytes) |
diff --git a/internal/plans/catalog_ratelimit_test.go b/internal/plans/catalog_ratelimit_test.go| index 73059be34..fea627b07 100644 |
| --- a/internal/plans/catalog_ratelimit_test.go |
| +++ b/internal/plans/catalog_ratelimit_test.go |
| @@ -74,7 +74,7 @@ func TestEffectiveClampsRateFields(t *testing.T) { |
| c.Ceilings.RatePerSecond = 500 |
| c.Ceilings.RateBurst = 2000 |
| - got, err := c.Effective("personal", nil, nil) |
| + got, err := c.Effective("personal", nil, nil, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -82,7 +82,7 @@ func TestEffectiveClampsRateFields(t *testing.T) { |
| t.Fatalf("clamped = %+v, want %+v", got.RateCeiling(), want) |
| } |
| - got, err = c.Effective("personal", nil, &Limits{RateClientShare: 60, RatePerSecond: 5000, RateBurst: 9000}) |
| + got, err = c.Effective("personal", nil, &Limits{RateClientShare: 60, RatePerSecond: 5000, RateBurst: 9000}, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -91,7 +91,7 @@ func TestEffectiveClampsRateFields(t *testing.T) { |
| } |
| // A sponsored zero is unset: the clamped plan value stands. |
| - got, err = c.Effective("personal", nil, &Limits{RatePerSecond: 5000}) |
| + got, err = c.Effective("personal", nil, &Limits{RatePerSecond: 5000}, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -107,7 +107,7 @@ func TestEffectiveRateZeroUnderCeilingTakesTheCeiling(t *testing.T) { |
| c.Ceilings.RateBurst = 2000 |
| // "free" sets no rate fields: the ceiling applies, it is not unlimited. |
| - got, err := c.Effective("free", nil, nil) |
| + got, err := c.Effective("free", nil, nil, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -128,7 +128,7 @@ func TestEffectiveRateZeroUnderCeilingTakesTheCeiling(t *testing.T) { |
| p.Limits.RateClientShare, p.Limits.RatePerSecond, p.Limits.RateBurst = 0, 0, 0 |
| tc.set(&p.Limits) |
| c.Plans["personal"] = p |
| - got, err := c.Effective("personal", nil, nil) |
| + got, err := c.Effective("personal", nil, nil, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -138,7 +138,7 @@ func TestEffectiveRateZeroUnderCeilingTakesTheCeiling(t *testing.T) { |
| } |
| // A sponsored override still lifts it, and no ceiling stays zero. |
| - got, err = c.Effective("free", nil, &Limits{RatePerSecond: 5000}) |
| + got, err = c.Effective("free", nil, &Limits{RatePerSecond: 5000}, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -146,7 +146,7 @@ func TestEffectiveRateZeroUnderCeilingTakesTheCeiling(t *testing.T) { |
| t.Fatalf("sponsored rate = %d, want 5000", got.RatePerSecond) |
| } |
| c.Ceilings.RateClientShare, c.Ceilings.RatePerSecond, c.Ceilings.RateBurst = 0, 0, 0 |
| - got, err = c.Effective("free", nil, nil) |
| + got, err = c.Effective("free", nil, nil, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -163,7 +163,7 @@ func TestEffectiveSponsoredKeepsUnlimitedRate(t *testing.T) { |
| if c.Ceilings.RateClientShare != 0 || c.Ceilings.RatePerSecond != 0 || c.Ceilings.RateBurst != 0 { |
| t.Fatalf("example catalog has rate ceilings %+v; this test needs none", c.Ceilings.RateCeiling()) |
| } |
| - got, err := c.Effective("free", nil, &Limits{RateClientShare: 1, RatePerSecond: 1, RateBurst: 1}) |
| + got, err := c.Effective("free", nil, &Limits{RateClientShare: 1, RatePerSecond: 1, RateBurst: 1}, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -175,7 +175,7 @@ func TestEffectiveSponsoredKeepsUnlimitedRate(t *testing.T) { |
| p := c.Plans["personal"] |
| p.Limits.RateClientShare, p.Limits.RatePerSecond, p.Limits.RateBurst = 20, 0, 1500 |
| c.Plans["personal"] = p |
| - got, err = c.Effective("personal", nil, &Limits{RateClientShare: 40, RatePerSecond: 400, RateBurst: 1200}) |
| + got, err = c.Effective("personal", nil, &Limits{RateClientShare: 40, RatePerSecond: 400, RateBurst: 1200}, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
diff --git a/internal/plans/catalog_test.go b/internal/plans/catalog_test.go| index 86c974eb1..5cc6e854c 100644 |
| --- a/internal/plans/catalog_test.go |
| +++ b/internal/plans/catalog_test.go |
| @@ -100,7 +100,7 @@ func TestCatalogIDChangesWithContent(t *testing.T) { |
| func TestEffectivePlanOnly(t *testing.T) { |
| c := exampleCatalog(t) |
| - got, err := c.Effective("free", nil, nil) |
| + got, err := c.Effective("free", nil, nil, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -111,7 +111,7 @@ func TestEffectivePlanOnly(t *testing.T) { |
| func TestEffectiveAddsPacks(t *testing.T) { |
| c := exampleCatalog(t) |
| - got, err := c.Effective("personal", map[string]int{"apps": 2, "email": 1}, nil) |
| + got, err := c.Effective("personal", map[string]int{"apps": 2, "email": 1}, nil, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -122,7 +122,7 @@ func TestEffectiveAddsPacks(t *testing.T) { |
| func TestEffectiveClampsToCeilings(t *testing.T) { |
| c := exampleCatalog(t) |
| - got, err := c.Effective("personal", map[string]int{"apps": 20, "email": 20}, nil) |
| + got, err := c.Effective("personal", map[string]int{"apps": 20, "email": 20}, nil, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -133,7 +133,7 @@ func TestEffectiveClampsToCeilings(t *testing.T) { |
| t.Fatalf("email_per_day %d, want the 10000 ceiling", got.EmailPerDay) |
| } |
| c.Ceilings.RunningThings = 100 |
| - got, err = c.Effective("personal", map[string]int{"apps": 20}, nil) |
| + got, err = c.Effective("personal", map[string]int{"apps": 20}, nil, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -158,7 +158,7 @@ func TestEffectiveRefuses(t *testing.T) { |
| {"zero", "personal", map[string]int{"apps": 0}, ErrPackQuantity}, |
| } |
| for _, tc := range cases { |
| - if _, err := c.Effective(tc.plan, tc.packs, nil); !errors.Is(err, tc.want) { |
| + if _, err := c.Effective(tc.plan, tc.packs, nil, 1); !errors.Is(err, tc.want) { |
| t.Errorf("%s: err %v, want %v", tc.name, err, tc.want) |
| } |
| } |
| @@ -166,7 +166,7 @@ func TestEffectiveRefuses(t *testing.T) { |
| func TestEffectiveSponsoredTakesTheMoreGenerous(t *testing.T) { |
| c := exampleCatalog(t) |
| - got, err := c.Effective("personal", nil, &Limits{RunningThings: 300, EmailPerDay: 20}) |
| + got, err := c.Effective("personal", nil, &Limits{RunningThings: 300, EmailPerDay: 20}, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -176,7 +176,7 @@ func TestEffectiveSponsoredTakesTheMoreGenerous(t *testing.T) { |
| if got.EmailPerDay != 1000 { |
| t.Fatalf("email_per_day %d, want the plan's 1000", got.EmailPerDay) |
| } |
| - got, err = c.Effective("free", nil, &Limits{CustomDomains: true, MemoryTier: "default"}) |
| + got, err = c.Effective("free", nil, &Limits{CustomDomains: true, MemoryTier: "default"}, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -187,14 +187,14 @@ func TestEffectiveSponsoredTakesTheMoreGenerous(t *testing.T) { |
| func TestEffectiveSponsoredZeroIsUnset(t *testing.T) { |
| c := exampleCatalog(t) |
| - got, err := c.Effective("free", nil, &Limits{RunningThings: 40}) |
| + got, err := c.Effective("free", nil, &Limits{RunningThings: 40}, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| if got.MinScheduleSeconds != 900 || got.RunningThings != 40 { |
| t.Fatalf("min_schedule_seconds %d (want 900), running_things %d (want 40)", got.MinScheduleSeconds, got.RunningThings) |
| } |
| - got, err = c.Effective("free", nil, &Limits{MinScheduleSeconds: 60}) |
| + got, err = c.Effective("free", nil, &Limits{MinScheduleSeconds: 60}, 1) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -211,7 +211,7 @@ func TestEffectiveRefusesOverflow(t *testing.T) { |
| pk.Adds.StorageBytes = 1 << 62 |
| c.Packs["storage"] = pk |
| c.Ceilings.StorageBytes = 100 |
| - got, err := c.Effective("personal", map[string]int{"storage": 2}, nil) |
| + got, err := c.Effective("personal", map[string]int{"storage": 2}, nil, 1) |
| if !errors.Is(err, ErrPackQuantity) { |
| t.Fatalf("err %v (limits %+v), want ErrPackQuantity", err, got) |
| } |
diff --git a/internal/plans/export_test.go b/internal/plans/export_test.go| index aa3817c43..bb83b6333 100644 |
| --- a/internal/plans/export_test.go |
| +++ b/internal/plans/export_test.go |
| @@ -64,7 +64,7 @@ func TestPublicExportOrder(t *testing.T) { |
| for _, p := range e.Packs { |
| packs = append(packs, p.Key) |
| } |
| - if !slices.Equal(plans, []string{"free", "personal"}) { |
| + if !slices.Equal(plans, []string{"free", "community", "personal", "business"}) { |
| t.Errorf("plans = %v", plans) |
| } |
| if !slices.IsSorted(packs) || len(packs) != 5 { |
diff --git a/internal/plans/pricebook_test.go b/internal/plans/pricebook_test.go| index 9db06d743..3520ed186 100644 |
| --- a/internal/plans/pricebook_test.go |
| +++ b/internal/plans/pricebook_test.go |
| @@ -64,7 +64,7 @@ func TestPriceBookRefusesGaps(t *testing.T) { |
| {"personal missing gbp", `"personal": {"usd": 2800, "eur": 2400, "gbp": 2200}`, `"personal": {"usd": 2800, "eur": 2400}`, `plan "personal"`}, |
| {"pack email missing", `"email": {"usd": 2400, "eur": 2100, "gbp": 1900},`, ``, `pack "email"`}, |
| {"extra pack seats", `"packs": {`, `"packs": {"seats": {"usd": 1, "eur": 1, "gbp": 1},`, `no such pack`}, |
| - {"extra plan", `"plans": {`, `"plans": {"business": {"usd": 1, "eur": 1, "gbp": 1},`, `no such plan`}, |
| + {"extra plan", `"plans": {`, `"plans": {"gold": {"usd": 1, "eur": 1, "gbp": 1},`, `no such plan`}, |
| {"free plan priced", `"plans": {`, `"plans": {"free": {"usd": 1, "eur": 1, "gbp": 1},`, `plan "free"`}, |
| {"amount zero", `"usd": 2800`, `"usd": 0`, `must be positive`}, |
| {"amount negative", `"usd": 2800`, `"usd": -1`, `must be positive`}, |
| @@ -169,6 +169,8 @@ func TestChooseCurrency(t *testing.T) { |
| {`, |
| "gbp": ["GB"]`, ``}, |
| {`"personal": {"usd": 2800, "eur": 2400, "gbp": 2200}`, `"personal": {"usd": 2800, "eur": 2400}`}, |
| + {`"community": {"usd": 600, "eur": 500, "gbp": 450}`, `"community": {"usd": 600, "eur": 500}`}, |
| + {`"business": {"usd": 6000, "eur": 5000, "gbp": 4500}`, `"business": {"usd": 6000, "eur": 5000}`}, |
| {`"apps": {"usd": 1200, "eur": 1000, "gbp": 900}`, `"apps": {"usd": 1200, "eur": 1000}`}, |
| {`"disk": {"usd": 3600, "eur": 3100, "gbp": 2800}`, `"disk": {"usd": 3600, "eur": 3100}`}, |
| {`"storage": {"usd": 1200, "eur": 1000, "gbp": 900}`, `"storage": {"usd": 1200, "eur": 1000}`}, |
diff --git a/internal/plans/team.go b/internal/plans/team.go| new file mode 100644 |
| index 000000000..5602ab129 |
| --- /dev/null |
| +++ b/internal/plans/team.go |
| @@ -0,0 +1,119 @@ |
| +package plans |
| + |
| +import ( |
| + "errors" |
| + "fmt" |
| + "math" |
| + "math/bits" |
| + "strconv" |
| + "strings" |
| +) |
| + |
| +// Team makes a plan a team plan: its pooled limits are a paid base plan's, |
| +// multiplied per member and shared across the account. Disk is not pooled |
| +// (it stays per running thing, from the team plan's own limits), and packs |
| +// stay flat per account. |
| +type Team struct { |
| + Base string `json:"base"` // a paid, non-team plan whose pooled limits multiply |
| + PerMemberMilli int `json:"per_member_milli"` // 200 = 0.2, 2000 = 2 |
| + MinMembers int `json:"min_members"` // billed and limited at no fewer |
| +} |
| + |
| +// MaxTeamMembers bounds the member count Effective accepts and a team's |
| +// min_members. |
| +const MaxTeamMembers = 10000 |
| + |
| +// maxPerMemberMilli bounds per_member_milli: a multiple of 100 per member. |
| +const maxPerMemberMilli = 100000 |
| + |
| +// ErrTeamMembers refuses a member count outside 0 to MaxTeamMembers, or one |
| +// whose limits would overflow. |
| +var ErrTeamMembers = errors.New("team member count out of range") |
| + |
| +// PooledLimits are the limits a team plan takes from its base, multiplied |
| +// per member, by JSON name. Disk is not one of them (Call 7). |
| +var PooledLimits = []string{"running_things", "awake_hours_month", "storage_bytes", "email_per_day", "bandwidth_bytes_month"} |
| + |
| +// pooledFields is PooledLimits as accessors, in the same order (a test |
| +// keeps the two equal). |
| +var pooledFields = []struct { |
| + name string |
| + get func(Limits) int64 |
| + set func(*Limits, int64) |
| +}{ |
| + {"running_things", func(l Limits) int64 { return int64(l.RunningThings) }, func(l *Limits, v int64) { l.RunningThings = int(v) }}, |
| + {"awake_hours_month", func(l Limits) int64 { return int64(l.AwakeHoursMonth) }, func(l *Limits, v int64) { l.AwakeHoursMonth = int(v) }}, |
| + {"storage_bytes", func(l Limits) int64 { return l.StorageBytes }, func(l *Limits, v int64) { l.StorageBytes = v }}, |
| + {"email_per_day", func(l Limits) int64 { return int64(l.EmailPerDay) }, func(l *Limits, v int64) { l.EmailPerDay = int(v) }}, |
| + {"bandwidth_bytes_month", func(l Limits) int64 { return l.BandwidthBytesMonth }, func(l *Limits, v int64) { l.BandwidthBytesMonth = v }}, |
| +} |
| + |
| +// Billed is the member count the account is billed and limited at: never |
| +// fewer than MinMembers. |
| +func (t *Team) Billed(members int) int { return max(members, t.MinMembers) } |
| + |
| +// PerMember is the per-member multiple as a decimal: 200 is "0.2", 2000 |
| +// is "2", 1250 is "1.25". |
| +func (t *Team) PerMember() string { |
| + whole, frac := t.PerMemberMilli/1000, t.PerMemberMilli%1000 |
| + if frac == 0 { |
| + return strconv.Itoa(whole) |
| + } |
| + return strings.TrimRight(fmt.Sprintf("%d.%03d", whole, frac), "0") |
| +} |
| + |
| +// validateTeams checks every plan's team block and max_members. It runs |
| +// after each plan's own limits are checked. |
| +func (c *Catalog) validateTeams() error { |
| + for name, p := range c.Plans { |
| + if p.MaxMembers < 0 { |
| + return fmt.Errorf("plan %q: max_members may not be negative", name) |
| + } |
| + t := p.Team |
| + if t == nil { |
| + continue |
| + } |
| + if p.MaxMembers != 0 { |
| + return fmt.Errorf("plan %q: a team plan may not set max_members", name) |
| + } |
| + base, ok := c.Plans[t.Base] |
| + switch { |
| + case !ok: |
| + return fmt.Errorf("plan %q: team base %q is not a plan", name, t.Base) |
| + case !base.Paid: |
| + return fmt.Errorf("plan %q: team base %q is not a paid plan", name, t.Base) |
| + case base.Team != nil: |
| + return fmt.Errorf("plan %q: team base %q is itself a team plan", name, t.Base) |
| + } |
| + if t.PerMemberMilli < 1 || t.PerMemberMilli > maxPerMemberMilli { |
| + return fmt.Errorf("plan %q: per_member_milli must be 1 to %d", name, maxPerMemberMilli) |
| + } |
| + if t.MinMembers < 1 || t.MinMembers > MaxTeamMembers { |
| + return fmt.Errorf("plan %q: min_members must be 1 to %d", name, MaxTeamMembers) |
| + } |
| + for _, f := range pooledFields { |
| + if f.get(p.Limits) != 0 { |
| + return fmt.Errorf("plan %q: a team plan takes %s from its base and may not set it", name, f.name) |
| + } |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// scalePooled sets each pooled field of dst to src's times scale / 1000, |
| +// rounded down. ok is false if any result would overflow int64. Values are |
| +// non-negative (validate). |
| +func scalePooled(dst *Limits, src Limits, scale uint64) (ok bool) { |
| + for _, f := range pooledFields { |
| + hi, lo := bits.Mul64(uint64(f.get(src)), scale) |
| + if hi >= 1000 { // the quotient would not fit 64 bits |
| + return false |
| + } |
| + q, _ := bits.Div64(hi, lo, 1000) |
| + if q > math.MaxInt64 { |
| + return false |
| + } |
| + f.set(dst, int64(q)) |
| + } |
| + return true |
| +} |
diff --git a/internal/plans/team_test.go b/internal/plans/team_test.go| new file mode 100644 |
| index 000000000..65394ae9b |
| --- /dev/null |
| +++ b/internal/plans/team_test.go |
| @@ -0,0 +1,192 @@ |
| +package plans |
| + |
| +import ( |
| + "errors" |
| + "reflect" |
| + "slices" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func effective(t *testing.T, plan string, packs map[string]int, members int) Limits { |
| + t.Helper() |
| + got, err := exampleCatalog(t).Effective(plan, packs, nil, members) |
| + if err != nil { |
| + t.Fatalf("Effective(%q, %v, %d): %v", plan, packs, members, err) |
| + } |
| + return got |
| +} |
| + |
| +func TestEffectiveCommunityAtMinimum(t *testing.T) { |
| + got := effective(t, "community", nil, 4) // billed at the minimum of 10 |
| + if got.RunningThings != 50 || got.AwakeHoursMonth != 3000 || got.EmailPerDay != 2000 || |
| + got.StorageBytes != 53687091200 || got.BandwidthBytesMonth != 107374182400 { |
| + t.Errorf("pooled limits = %+v", got) |
| + } |
| + if got.DiskBytes != 10737418240 || got.IdleSeconds != 900 || got.HistoryDays != 0 { |
| + t.Errorf("own limits: disk %d, idle %d, history %d", got.DiskBytes, got.IdleSeconds, got.HistoryDays) |
| + } |
| + if !got.CustomDomains || !got.Fleets || !got.StorageSelfServe || got.MemoryTier != MemoryDefault || |
| + got.SidecarsPerApp != 5 || got.MinScheduleSeconds != 60 { |
| + t.Errorf("flags = %+v", got) |
| + } |
| +} |
| + |
| +func TestEffectiveCommunityTwentyThree(t *testing.T) { |
| + got := effective(t, "community", nil, 23) |
| + if got.RunningThings != 115 || got.AwakeHoursMonth != 6900 || got.EmailPerDay != 4600 || |
| + got.StorageBytes != 123480309760 || got.DiskBytes != 10737418240 { |
| + t.Errorf("limits = %+v", got) |
| + } |
| +} |
| + |
| +func TestEffectiveBusinessScalesCeilings(t *testing.T) { |
| + // 2 per member x 12 members = 24: running things 25 x 24 = 600, above |
| + // the unscaled ceiling of 200 but under 200 x 24. |
| + got := effective(t, "business", nil, 12) |
| + if got.RunningThings != 600 || got.EmailPerDay != 24000 || got.HistoryDays != 7 || got.DiskBytes != 10737418240 { |
| + t.Errorf("limits = %+v", got) |
| + } |
| + // Idle is not pooled: its ceiling never scales. |
| + if got.IdleSeconds != 900 { |
| + t.Errorf("idle = %d", got.IdleSeconds) |
| + } |
| +} |
| + |
| +func TestEffectiveTeamWithPacks(t *testing.T) { |
| + got := effective(t, "community", map[string]int{"apps": 2, "disk": 1}, 10) |
| + if got.RunningThings != 60 { |
| + t.Errorf("running things = %d, want 60 (packs are flat per account)", got.RunningThings) |
| + } |
| + if got.DiskBytes != 21474836480 { |
| + t.Errorf("disk = %d, want 21474836480", got.DiskBytes) |
| + } |
| +} |
| + |
| +func TestEffectiveIgnoresMembersOffTeam(t *testing.T) { |
| + want := effective(t, "personal", map[string]int{"apps": 1}, 1) |
| + for _, m := range []int{0, 50} { |
| + if got := effective(t, "personal", map[string]int{"apps": 1}, m); got != want { |
| + t.Errorf("members %d: %+v, want %+v", m, got, want) |
| + } |
| + } |
| +} |
| + |
| +func TestEffectiveTeamMemberBounds(t *testing.T) { |
| + c := exampleCatalog(t) |
| + for _, m := range []int{-1, MaxTeamMembers + 1} { |
| + if _, err := c.Effective("community", nil, nil, m); !errors.Is(err, ErrTeamMembers) { |
| + t.Errorf("members %d: err = %v, want ErrTeamMembers", m, err) |
| + } |
| + } |
| + if _, err := c.Effective("community", nil, nil, MaxTeamMembers); err != nil { |
| + t.Errorf("members %d: %v", MaxTeamMembers, err) |
| + } |
| +} |
| + |
| +func TestEffectiveTeamOverflow(t *testing.T) { |
| + body := strings.Replace(exampleBytes(t), `"storage_bytes": 26843545600,`, `"storage_bytes": 9223372036854775807,`, 1) |
| + c, err := ParseCatalog([]byte(body)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := c.Effective("business", nil, nil, 10); !errors.Is(err, ErrTeamMembers) { |
| + t.Errorf("err = %v, want ErrTeamMembers", err) |
| + } |
| +} |
| + |
| +func TestCatalogRefusesBadTeams(t *testing.T) { |
| + base := exampleBytes(t) |
| + community := `"team": {"base": "personal", "per_member_milli": 200, "min_members": 10}` |
| + team := func(base, milli, min string) string { |
| + return `"team": {"base": "` + base + `", "per_member_milli": ` + milli + `, "min_members": ` + min + `}` |
| + } |
| + // Community's own limits open with this line; a case that sets a |
| + // pooled field on a team plan adds it here. |
| + ownLimits := `"community": { |
| + "paid": true, |
| + "limits": { |
| + "sidecars_per_app": 5,` |
| + cases := map[string]string{ |
| + "base missing": strings.Replace(base, community, team("gold", "200", "10"), 1), |
| + "base free": strings.Replace(base, community, team("free", "200", "10"), 1), |
| + "base a team": strings.Replace(base, community, team("business", "200", "10"), 1), |
| + "milli zero": strings.Replace(base, community, team("personal", "0", "10"), 1), |
| + "milli too large": strings.Replace(base, community, team("personal", "100001", "10"), 1), |
| + "min zero": strings.Replace(base, community, team("personal", "200", "0"), 1), |
| + "min too large": strings.Replace(base, community, team("personal", "200", "10001"), 1), |
| + "team with max_members": strings.Replace(base, community, community+`, "max_members": 3`, 1), |
| + "negative max_members": strings.Replace(base, `"max_members": 3`, `"max_members": -1`, 1), |
| + } |
| + for _, field := range PooledLimits { |
| + cases["team sets "+field] = strings.Replace(base, ownLimits, ownLimits+` "`+field+`": 1,`, 1) |
| + } |
| + for name, body := range cases { |
| + if body == base { |
| + t.Fatalf("%s: replacement did not apply", name) |
| + } |
| + if _, err := ParseCatalog([]byte(body)); err == nil { |
| + t.Errorf("%s: ParseCatalog accepted it", name) |
| + } |
| + } |
| +} |
| + |
| +func TestTeamBilledAndPerMember(t *testing.T) { |
| + tm := &Team{Base: "personal", PerMemberMilli: 200, MinMembers: 10} |
| + if tm.Billed(3) != 10 || tm.Billed(12) != 12 { |
| + t.Errorf("Billed(3) = %d, Billed(12) = %d", tm.Billed(3), tm.Billed(12)) |
| + } |
| + for milli, want := range map[int]string{200: "0.2", 2000: "2", 1250: "1.25", 1: "0.001", 100000: "100"} { |
| + if got := (&Team{PerMemberMilli: milli}).PerMember(); got != want { |
| + t.Errorf("PerMember(%d) = %q, want %q", milli, got, want) |
| + } |
| + } |
| +} |
| + |
| +func TestCatalogExampleMaxMembers(t *testing.T) { |
| + c := exampleCatalog(t) |
| + for name, want := range map[string]int{"free": 3, "personal": 3, "community": 0, "business": 0} { |
| + if got := c.Plans[name].MaxMembers; got != want { |
| + t.Errorf("%s max_members = %d, want %d", name, got, want) |
| + } |
| + } |
| + if c.Plans["community"].Team == nil || c.Plans["business"].Team.PerMemberMilli != 2000 { |
| + t.Errorf("team blocks: community %+v, business %+v", c.Plans["community"].Team, c.Plans["business"].Team) |
| + } |
| +} |
| + |
| +// PooledLimits names exactly the fields Effective pools, by JSON name, and |
| +// never disk (Call 7). |
| +func TestTeamPooledLimitsMatchFields(t *testing.T) { |
| + var names []string |
| + for _, f := range pooledFields { |
| + names = append(names, f.name) |
| + } |
| + if !slices.Equal(names, PooledLimits) { |
| + t.Fatalf("pooledFields %v, PooledLimits %v", names, PooledLimits) |
| + } |
| + if slices.Contains(PooledLimits, "disk_bytes") { |
| + t.Fatal("disk is not pooled") |
| + } |
| + typ := reflect.TypeOf(Limits{}) |
| + for _, f := range pooledFields { |
| + var l Limits |
| + f.set(&l, 7) |
| + v := reflect.ValueOf(l) |
| + found := false |
| + for i := range typ.NumField() { |
| + tag, _, _ := strings.Cut(typ.Field(i).Tag.Get("json"), ",") |
| + if tag == f.name { |
| + found = true |
| + if v.Field(i).Int() != 7 || f.get(l) != 7 { |
| + t.Errorf("%s: set/get do not touch the %s field", f.name, typ.Field(i).Name) |
| + } |
| + } else if !v.Field(i).IsZero() { |
| + t.Errorf("%s: set touched %s", f.name, typ.Field(i).Name) |
| + } |
| + } |
| + if !found { |
| + t.Errorf("%s is not a Limits JSON field", f.name) |
| + } |
| + } |
| +} |
diff --git a/internal/plans/testdata/example-catalog.json b/internal/plans/testdata/example-catalog.json| index c7a888710..d563d1e07 100644 |
| --- a/internal/plans/testdata/example-catalog.json |
| +++ b/internal/plans/testdata/example-catalog.json |
| @@ -5,6 +5,7 @@ |
| "plans": { |
| "free": { |
| "paid": false, |
| + "max_members": 3, |
| "limits": { |
| "running_things": 3, |
| "sidecars_per_app": 1, |
| @@ -20,6 +21,7 @@ |
| }, |
| "personal": { |
| "paid": true, |
| + "max_members": 3, |
| "limits": { |
| "running_things": 25, |
| "sidecars_per_app": 5, |
| @@ -37,6 +39,37 @@ |
| "history_days": 7 |
| }, |
| "packs": ["apps", "disk", "storage", "email", "bandwidth"] |
| + }, |
| + "community": { |
| + "paid": true, |
| + "limits": { |
| + "sidecars_per_app": 5, |
| + "idle_seconds": 900, |
| + "memory_tier": "default", |
| + "disk_bytes": 10737418240, |
| + "storage_self_serve": true, |
| + "custom_domains": true, |
| + "fleets": true, |
| + "min_schedule_seconds": 60 |
| + }, |
| + "packs": ["apps", "disk", "storage", "email", "bandwidth"], |
| + "team": {"base": "personal", "per_member_milli": 200, "min_members": 10} |
| + }, |
| + "business": { |
| + "paid": true, |
| + "limits": { |
| + "sidecars_per_app": 5, |
| + "idle_seconds": 900, |
| + "memory_tier": "default", |
| + "disk_bytes": 10737418240, |
| + "storage_self_serve": true, |
| + "custom_domains": true, |
| + "fleets": true, |
| + "min_schedule_seconds": 60, |
| + "history_days": 7 |
| + }, |
| + "packs": ["apps", "disk", "storage", "email", "bandwidth"], |
| + "team": {"base": "personal", "per_member_milli": 2000, "min_members": 10} |
| } |
| }, |
| "packs": { |
diff --git a/internal/plans/testdata/example-pricebook.json b/internal/plans/testdata/example-pricebook.json| index 653edfbae..2c0c07d5a 100644 |
| --- a/internal/plans/testdata/example-pricebook.json |
| +++ b/internal/plans/testdata/example-pricebook.json |
| @@ -9,7 +9,9 @@ |
| "gbp": ["GB"] |
| }, |
| "plans": { |
| - "personal": {"usd": 2800, "eur": 2400, "gbp": 2200} |
| + "personal": {"usd": 2800, "eur": 2400, "gbp": 2200}, |
| + "community": {"usd": 600, "eur": 500, "gbp": 450}, |
| + "business": {"usd": 6000, "eur": 5000, "gbp": 4500} |
| }, |
| "packs": { |
| "apps": {"usd": 1200, "eur": 1000, "gbp": 900}, |
diff --git a/internal/plans/testdata/example-public-pricebook.json b/internal/plans/testdata/example-public-pricebook.json| index 2c806dab3..bfa095ed8 100644 |
| --- a/internal/plans/testdata/example-public-pricebook.json |
| +++ b/internal/plans/testdata/example-public-pricebook.json |
| @@ -1,10 +1,10 @@ |
| { |
| "schema": 2, |
| "price_book": "example-v1", |
| - "price_book_id": "example-v1@3be7730d025a", |
| + "price_book_id": "example-v1@00788b08d45d", |
| "status": "draft", |
| "catalog_version": "2026-10-20-v1", |
| - "catalog_id": "2026-10-20-v1@57355d1ce09d", |
| + "catalog_id": "2026-10-20-v1@063079f07e32", |
| "default_currency": "usd", |
| "currencies": [ |
| { |
| @@ -75,6 +75,34 @@ |
| }, |
| "packs": [] |
| }, |
| + { |
| + "key": "community", |
| + "paid": true, |
| + "default": false, |
| + "annual_minor": { |
| + "eur": 500, |
| + "gbp": 450, |
| + "usd": 600 |
| + }, |
| + "memory_mb": 512, |
| + "limits": { |
| + "sidecars_per_app": 5, |
| + "idle_seconds": 900, |
| + "memory_tier": "default", |
| + "disk_bytes": 10737418240, |
| + "storage_self_serve": true, |
| + "custom_domains": true, |
| + "fleets": true, |
| + "min_schedule_seconds": 60 |
| + }, |
| + "packs": [ |
| + "apps", |
| + "bandwidth", |
| + "disk", |
| + "email", |
| + "storage" |
| + ] |
| + }, |
| { |
| "key": "personal", |
| "paid": true, |
| @@ -108,6 +136,35 @@ |
| "email", |
| "storage" |
| ] |
| + }, |
| + { |
| + "key": "business", |
| + "paid": true, |
| + "default": false, |
| + "annual_minor": { |
| + "eur": 5000, |
| + "gbp": 4500, |
| + "usd": 6000 |
| + }, |
| + "memory_mb": 512, |
| + "limits": { |
| + "sidecars_per_app": 5, |
| + "idle_seconds": 900, |
| + "memory_tier": "default", |
| + "disk_bytes": 10737418240, |
| + "storage_self_serve": true, |
| + "custom_domains": true, |
| + "fleets": true, |
| + "min_schedule_seconds": 60, |
| + "history_days": 7 |
| + }, |
| + "packs": [ |
| + "apps", |
| + "bandwidth", |
| + "disk", |
| + "email", |
| + "storage" |
| + ] |
| } |
| ], |
| "packs": [ |