plans: media fix round 1, sponsor merge, emails, launch carry
Astra's Task 6 review, three P2s: - generous merges media minutes with generousRate: a plan at 0 (no limit) stays unlimited under a finite sponsored figure, pack or not. - email.media_over and email.media_refused: the owners' notice email says new conversions are refused, not "Nothing changes". Deck rows added, pending a Luna pass. - SettleLaunchSponsorship first carries launch sponsorships written before media_minutes_month (source sponsored, reason and writer the launch rule's, every present field at the launch value) to the media figure, bumping the revision; operator or edited rows are left alone; idempotent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+381
−5
docs/superpowers/notes/media-conversion-copy-deck.md+9 −0internal/console/plancaps_copy.go+2 −0internal/console/planmedia_test.go+208 −0internal/console/plansponsor.go+125 −0internal/console/plansponsor_test.go+15 −1internal/plans/catalog.go+3 −1internal/plans/catalog_test.go+19 −3
diff --git a/docs/superpowers/notes/media-conversion-copy-deck.md b/docs/superpowers/notes/media-conversion-copy-deck.md| index 68b4badfe..c389a3922 100644 |
| --- a/docs/superpowers/notes/media-conversion-copy-deck.md |
| +++ b/docs/superpowers/notes/media-conversion-copy-deck.md |
| @@ -152,6 +152,15 @@ because its plan values reach `12,000`. |
| | `cli.media_over` | `this account is over its media allowance: %s. at twice the allowance, new conversions are refused until %s. buy a media pack or move to a larger plan for more` | |
| | `banner.media_refused` | `This account has used twice its media allowance this month. New conversions are refused until the 1st of the UTC month, or until it buys a media pack or moves to a larger plan.` | |
| | `cli.media_refused` | `this account has used twice its media allowance this month. new conversions are refused until the 1st of the UTC month, or until it buys a media pack or moves to a larger plan` | |
| +| `email.media_over` | `- This account is over its media allowance: %s. At twice the allowance, new conversions are refused until %s. Buy a media pack or move to a larger plan for more.` (pending Luna pass) | |
| +| `email.media_refused` | `- This account has used twice its media allowance this month. New conversions are refused until the 1st of the UTC month, or until it buys a media pack or moves to a larger plan.` (pending Luna pass) | |
| + |
| +The two `email.` rows (added in Task 6 fix round 1, pending a Luna pass) |
| +are the owners' notice email lines for the same two states. Without them |
| +the email fell back to the generic `email.over`, "Nothing changes until", |
| +while the door already refuses at twice the allowance. They are the |
| +banner rows as email list items, `email.media_over` with the same |
| +placeholders as `banner.media_over`. |
| The warning uses the existing `banner.warn` and `cli.warn` with |
| `usage.media`. The pack's own row label is `plans.Capitalise("media")`, |
diff --git a/internal/console/plancaps_copy.go b/internal/console/plancaps_copy.go| index 358dd04b5..ed04d24e7 100644 |
| --- a/internal/console/plancaps_copy.go |
| +++ b/internal/console/plancaps_copy.go |
| @@ -60,6 +60,8 @@ var planNoticeCopy = map[string]string{ |
| "email.intro": "Account %s is near or over its plan limits:", |
| "email.warn": "- %s.", |
| "email.over": "- %s, over the limit. Nothing changes until %s.", |
| + "email.media_over": "- This account is over its media allowance: %s. At twice the allowance, new conversions are refused until %s. Buy a media pack or move to a larger plan for more.", // the media conversion copy deck, pending a Luna pass |
| + "email.media_refused": "- This account has used twice its media allowance this month. New conversions are refused until the 1st of the UTC month, or until it buys a media pack or moves to a larger plan.", // the media conversion copy deck, pending a Luna pass |
| "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. Approved by delegation (Luna, |
| // docs/superpowers/specs/LUNA-COPY-RESULT-PLANS-4B.md). |
diff --git a/internal/console/planmedia_test.go b/internal/console/planmedia_test.go| index 7e20591ca..821b2b14f 100644 |
| --- a/internal/console/planmedia_test.go |
| +++ b/internal/console/planmedia_test.go |
| @@ -1,12 +1,15 @@ |
| package console |
| import ( |
| + "encoding/json" |
| "fmt" |
| "net/http/httptest" |
| + "os" |
| "strings" |
| "testing" |
| "time" |
| + "github.com/carlosframework/platform/internal/domains" |
| "github.com/carlosframework/platform/internal/media" |
| "github.com/carlosframework/platform/internal/plans" |
| ) |
| @@ -270,3 +273,208 @@ func TestMediaCapNotices(t *testing.T) { |
| t.Errorf("usage = %q", got) |
| } |
| } |
| + |
| +// TestMediaCapEmails: the owners' notice email says what the door does. |
| +// Over the allowance it names the refusal date; at twice the allowance it |
| +// says new conversions are refused, never the generic "Nothing changes" |
| +// (Astra T6 P2 2). |
| +func TestMediaCapEmails(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct := planAccount(t, fx, 501) |
| + fx.srv.cfg.PlanCatalog = mediaLimitCatalog(t, 600) |
| + now := time.Now().UTC() |
| + date := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.UTC).Format("2 January 2006") |
| + for _, tc := range []struct { |
| + used int64 |
| + line string |
| + }{ |
| + {601, "- This account is over its media allowance: 601 of 600 media minutes this month. At twice the allowance, new conversions are refused until " + date + ". Buy a media pack or move to a larger plan for more."}, |
| + {1200, "- This account has used twice its media allowance this month. New conversions are refused until the 1st of the UTC month, or until it buys a media pack or moves to a larger plan."}, |
| + } { |
| + t.Run(fmt.Sprint(tc.used), func(t *testing.T) { |
| + st := evaluateCap(nil, capMedia, tc.used, 600, now) |
| + st.AccountID = acct.ID |
| + _, body := fx.srv.planNoticeEmail(acct, []capState{st}, fx.srv.planStage(acct, now)) |
| + lines := strings.Split(body, "\n") |
| + if len(lines) < 3 || lines[2] != tc.line { |
| + t.Fatalf("email body:\n%s\nwant line 3 %q", body, tc.line) |
| + } |
| + if strings.Contains(body, "Nothing changes") { |
| + t.Errorf("the email says nothing changes:\n%s", body) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// mediaSponsorCatalog is the media fixture catalog with |
| +// sponsor_accounts_before at cutoff. |
| +func mediaSponsorCatalog(t *testing.T, cutoff time.Time) *plans.Catalog { |
| + t.Helper() |
| + b, err := os.ReadFile(mediaCatalogFixture) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + s := strings.Replace(string(b), `"default_plan": "free",`, |
| + `"default_plan": "free", "sponsor_accounts_before": "`+cutoff.UTC().Format(time.RFC3339)+`",`, 1) |
| + c, err := plans.ParseCatalog([]byte(s)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return c |
| +} |
| + |
| +// putPlanRow persists an account_plans row as an older console wrote it. |
| +func putPlanRow(t *testing.T, st *Store, id int64, source string, sponsored plans.Limits, reason, by string) { |
| + t.Helper() |
| + sj, err := json.Marshal(sponsored) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := st.db.Exec(`INSERT INTO account_plans |
| + (account_id, plan, packs, seats, source, sponsored, reason, revision, updated_by, updated_at) |
| + VALUES (?, 'free', '{}', 0, ?, ?, ?, 1, ?, ?)`, id, source, string(sj), reason, by, planNow.Add(-24*time.Hour).Unix()); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// preMediaLaunch is the launch figures as a console before media |
| +// conversion wrote them; prePDFLaunch as one before the PDF service. |
| +func preMediaLaunch() plans.Limits { |
| + l := sponsorUnlimited() |
| + l.MediaMinutesMonth = 0 |
| + return l |
| +} |
| + |
| +func prePDFLaunch() plans.Limits { |
| + l := preMediaLaunch() |
| + l.PDFRendersMonth = 0 |
| + return l |
| +} |
| + |
| +type planRowSnap struct { |
| + sponsored string |
| + revision int64 |
| + updated int64 |
| +} |
| + |
| +func planRowsNow(t *testing.T, st *Store) map[int64]planRowSnap { |
| + t.Helper() |
| + rows, err := st.db.Query(`SELECT account_id, sponsored, revision, updated_at FROM account_plans`) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer rows.Close() |
| + out := map[int64]planRowSnap{} |
| + for rows.Next() { |
| + var id int64 |
| + var r planRowSnap |
| + if err := rows.Scan(&id, &r.sponsored, &r.revision, &r.updated); err != nil { |
| + t.Fatal(err) |
| + } |
| + out[id] = r |
| + } |
| + return out |
| +} |
| + |
| +// TestLaunchSponsorshipCarriesMedia (Astra T6 P2 3): a launch sponsorship |
| +// persisted before media_minutes_month existed gets the launch file's |
| +// media figure at the next start, so the account keeps no limit instead |
| +// of Free's 30; a deliberate override is left alone; a second start |
| +// changes nothing. |
| +func TestLaunchSponsorshipCarriesMedia(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + ctx := t.Context() |
| + cat := mediaSponsorCatalog(t, sponsorCutoffAt) |
| + fx.srv.cfg.PlanCatalog = cat |
| + |
| + launch := planAccount(t, fx, 501) // written by the launch rule before media |
| + older := planAccount(t, fx, 502) // written before the PDF service too |
| + operator := planAccount(t, fx, 503) // an operator sponsorship of the same figures |
| + changed := planAccount(t, fx, 504) // a launch row someone changed since |
| + withMedia := planAccount(t, fx, 505) // a launch row already naming media |
| + putPlanRow(t, fx.st, launch.ID, PlanSourceSponsored, preMediaLaunch(), planSponsorReason, planDefaultActor) |
| + putPlanRow(t, fx.st, older.ID, PlanSourceSponsored, prePDFLaunch(), planSponsorReason, planDefaultActor) |
| + putPlanRow(t, fx.st, operator.ID, PlanSourceSponsored, preMediaLaunch(), "launch partner", "ops@example.com") |
| + edited := preMediaLaunch() |
| + edited.RunningThings = 50 |
| + putPlanRow(t, fx.st, changed.ID, PlanSourceSponsored, edited, planSponsorReason, planDefaultActor) |
| + set := preMediaLaunch() |
| + set.MediaMinutesMonth = 600 |
| + putPlanRow(t, fx.st, withMedia.ID, PlanSourceSponsored, set, planSponsorReason, planDefaultActor) |
| + |
| + allowance := func(a *Account) int64 { |
| + t.Helper() |
| + n, err := fx.srv.mediaAllowance(ctx, a.Sqid) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return n |
| + } |
| + if got := allowance(launch); got != 30 { |
| + t.Fatalf("fixture: the pre-change row gives %d media minutes, want Free's 30", got) |
| + } |
| + before := planRowsNow(t, fx.st) |
| + |
| + if n, err := SettleLaunchSponsorship(fx.st, cat, planNow); err != nil || n != 0 { |
| + t.Fatalf("settle = %d, %v; want 0 newly sponsored", n, err) |
| + } |
| + after := planRowsNow(t, fx.st) |
| + row, err := fx.st.AccountPlan(launch.ID) |
| + if err != nil || row == nil || row.Sponsored == nil || *row.Sponsored != sponsorUnlimited() || |
| + row.Revision != 2 || row.Source != PlanSourceSponsored || row.Reason != planSponsorReason || row.UpdatedBy != planDefaultActor { |
| + t.Fatalf("carried launch row = %+v, %v", row, err) |
| + } |
| + if got := allowance(launch); got != 0 { |
| + t.Errorf("carried launch account: %d media minutes, want 0 (no limit)", got) |
| + } |
| + row, _ = fx.st.AccountPlan(older.ID) |
| + want := prePDFLaunch() |
| + want.MediaMinutesMonth = sponsorMost |
| + if row == nil || row.Sponsored == nil || *row.Sponsored != want || row.Revision != 2 { |
| + t.Fatalf("carried pre-PDF row = %+v, want media only added", row) |
| + } |
| + for name, a := range map[string]*Account{"operator": operator, "changed": changed, "with media": withMedia} { |
| + if after[a.ID] != before[a.ID] { |
| + t.Errorf("%s row changed: %+v, was %+v", name, after[a.ID], before[a.ID]) |
| + } |
| + } |
| + if got := allowance(operator); got != 30 { |
| + t.Errorf("operator sponsorship: %d, want its own figures (Free's 30)", got) |
| + } |
| + |
| + // The publish pass sends the carried figures to the account's record. |
| + fx.srv.planLimitsPass(ctx, planNow) |
| + if rec, _ := readLimits(t, fx, launch.Sqid); rec == nil || rec.Revision != 2 || rec.Limits.MediaMinutesMonth != sponsorMost { |
| + t.Fatalf("published record = %+v", rec) |
| + } |
| + |
| + // Twice: nothing changes. |
| + settled := planRowsNow(t, fx.st) |
| + if n, err := SettleLaunchSponsorship(fx.st, cat, planNow.Add(time.Hour)); err != nil || n != 0 { |
| + t.Fatalf("second settle = %d, %v", n, err) |
| + } |
| + if n, err := fx.st.carryLaunchMedia(sponsorUnlimited(), planNow.Add(2*time.Hour)); err != nil || n != 0 { |
| + t.Fatalf("second carry = %d, %v", n, err) |
| + } |
| + for id, r := range planRowsNow(t, fx.st) { |
| + if r != settled[id] { |
| + t.Errorf("account %d changed on a second start: %+v, was %+v", id, r, settled[id]) |
| + } |
| + } |
| +} |
| + |
| +// The carry runs with no cutoff too (those rows still sponsor), and |
| +// passes the host-claim store's SQL gate. |
| +func TestLaunchSponsorshipCarriesMediaWithoutCutoffAndUnderTheGate(t *testing.T) { |
| + s := openGatedStore(t) |
| + if err := s.CreateAccount(501, domains.Sqid(501), "acct", "owner@example.com", time.Unix(1_700_000_000, 0)); err != nil { |
| + t.Fatal(err) |
| + } |
| + putPlanRow(t, s, 501, PlanSourceSponsored, preMediaLaunch(), planSponsorReason, planDefaultActor) |
| + if n, err := SettleLaunchSponsorship(s, mediaPlanCatalog(t), planNow); err != nil || n != 0 { |
| + t.Fatalf("settle without a cutoff = %d, %v", n, err) |
| + } |
| + if row, err := s.AccountPlan(501); err != nil || row == nil || row.Sponsored == nil || *row.Sponsored != sponsorUnlimited() { |
| + t.Fatalf("row = %+v, %v", row, err) |
| + } |
| +} |
diff --git a/internal/console/plansponsor.go b/internal/console/plansponsor.go| index c6a6fa78c..9b69bba4b 100644 |
| --- a/internal/console/plansponsor.go |
| +++ b/internal/console/plansponsor.go |
| @@ -2,9 +2,13 @@ package console |
| import ( |
| "database/sql" |
| + "encoding/json" |
| "errors" |
| "fmt" |
| + "log" |
| "math" |
| + "reflect" |
| + "strings" |
| "time" |
| "github.com/carlosframework/platform/internal/plans" |
| @@ -113,7 +117,20 @@ func sponsorUnlimited() plans.Limits { |
| // An error that wraps ErrLaunchRuleNotCleared means the clearing itself |
| // failed, so an old rule may still be recorded and would go on sponsoring |
| // every account inserted. The console must not start then (Astra R3). |
| +// |
| +// With a catalog, it first carries launch sponsorships written before |
| +// media conversion forward (Store.carryLaunchMedia), cutoff or not: such |
| +// rows still sponsor their accounts. |
| func SettleLaunchSponsorship(st *Store, cat *plans.Catalog, now time.Time) (int, error) { |
| + if cat != nil { |
| + carried, err := st.carryLaunchMedia(sponsorUnlimited(), now) |
| + if err != nil { |
| + return 0, errors.Join(fmt.Errorf("carrying the launch sponsorships forward: %w", err), clearLaunchRule(st)) |
| + } |
| + if carried > 0 { |
| + log.Printf("console: plans: %d launch sponsorships now include media minutes", carried) |
| + } |
| + } |
| if cat == nil || cat.SponsorCutoff().IsZero() { |
| return 0, clearLaunchRule(st) |
| } |
| @@ -129,6 +146,114 @@ func SettleLaunchSponsorship(st *Store, cat *plans.Catalog, now time.Time) (int, |
| return n, nil |
| } |
| +// carryLaunchMedia gives every launch sponsorship written before |
| +// media_minutes_month existed the media figure launch now carries (Task 6 |
| +// fix round 1): without it such an account would get its plan's media |
| +// minutes (Free's 30) while every other figure is unlimited. One |
| +// transaction; idempotent, so every start runs it. |
| +// |
| +// A launch row is what launchRule.apply writes and nothing else does: |
| +// source sponsored, reason planSponsorReason and writer planDefaultActor |
| +// (the plan door records the operator's address, billing its own source). |
| +// It is carried only when its sponsored limits have no media field and |
| +// every field they do have holds launch's value: a row written by an |
| +// earlier launch figure set may lack later fields, but a field that |
| +// differs is a deliberate change, and that row is left alone. A carried |
| +// row's revision is bumped so the publish pass sends its limits record |
| +// again; nothing else in it changes. |
| +func (s *Store) carryLaunchMedia(launch plans.Limits, now time.Time) (int, error) { |
| + if launch.MediaMinutesMonth == 0 { |
| + return 0, errors.New("console: the launch figures have no media minutes to carry") |
| + } |
| + lj, err := json.Marshal(launch) |
| + if err != nil { |
| + return 0, err |
| + } |
| + want, err := jsonFields(string(lj)) |
| + if err != nil { |
| + return 0, err |
| + } |
| + tx, err := s.db.Begin() |
| + if err != nil { |
| + return 0, err |
| + } |
| + defer tx.Rollback() |
| + rows, err := tx.Query(`SELECT account_id, sponsored FROM account_plans |
| + WHERE source = ? AND reason = ? AND updated_by = ? ORDER BY account_id`, PlanSourceSponsored, planSponsorReason, planDefaultActor) |
| + if err != nil { |
| + return 0, err |
| + } |
| + type launchRow struct { |
| + id int64 |
| + sponsored string |
| + } |
| + var found []launchRow |
| + for rows.Next() { |
| + var r launchRow |
| + if err := rows.Scan(&r.id, &r.sponsored); err != nil { |
| + rows.Close() |
| + return 0, err |
| + } |
| + found = append(found, r) |
| + } |
| + rows.Close() |
| + if err := rows.Err(); err != nil { |
| + return 0, err |
| + } |
| + n := 0 |
| + for _, r := range found { |
| + have, err := jsonFields(r.sponsored) |
| + if err != nil || len(have) == 0 { |
| + continue // not launch's shape: leave it |
| + } |
| + if _, ok := have["media_minutes_month"]; ok { |
| + continue // carried already, or set on purpose |
| + } |
| + same := true |
| + for k, v := range have { |
| + if w, ok := want[k]; !ok || !reflect.DeepEqual(v, w) { |
| + same = false |
| + break |
| + } |
| + } |
| + if !same { |
| + continue |
| + } |
| + var l plans.Limits |
| + if err := json.Unmarshal([]byte(r.sponsored), &l); err != nil { |
| + continue |
| + } |
| + l.MediaMinutesMonth = launch.MediaMinutesMonth |
| + nj, err := json.Marshal(l) |
| + if err != nil { |
| + return 0, err |
| + } |
| + res, err := tx.Exec(`UPDATE account_plans SET sponsored = ?, revision = revision + 1, updated_at = ? |
| + WHERE account_id = ? AND sponsored = ?`, string(nj), now.Unix(), r.id, r.sponsored) |
| + if err != nil { |
| + return 0, err |
| + } |
| + if k, err := res.RowsAffected(); err != nil { |
| + return 0, err |
| + } else if k > 0 { |
| + n++ |
| + } |
| + } |
| + return n, tx.Commit() |
| +} |
| + |
| +// jsonFields is a JSON object's fields, numbers kept exact (json.Number), |
| +// so two figures compare equal only when they are the same digits. |
| +func jsonFields(s string) (map[string]any, error) { |
| + dec := json.NewDecoder(strings.NewReader(s)) |
| + dec.UseNumber() |
| + var m map[string]any |
| + if err := dec.Decode(&m); err != nil { |
| + return nil, err |
| + } |
| + return m, nil |
| +} |
| + |
| // ErrLaunchRuleNotCleared marks a SettleLaunchSponsorship error in which |
| // the recorded rule could not be removed. |
| var ErrLaunchRuleNotCleared = errors.New("the recorded launch sponsorship rule could not be removed") |
diff --git a/internal/console/plansponsor_test.go b/internal/console/plansponsor_test.go| index b329e2fbb..2742341ec 100644 |
| --- a/internal/console/plansponsor_test.go |
| +++ b/internal/console/plansponsor_test.go |
| @@ -7,6 +7,7 @@ import ( |
| "math" |
| "net/http" |
| "os" |
| + "slices" |
| "strings" |
| "testing" |
| "time" |
| @@ -132,6 +133,10 @@ func TestSponsorUnlimitedOverflowsNothing(t *testing.T) { |
| if p.Limits.RateBurst == 0 { |
| want.RateBurst = got.RateBurst |
| } |
| + // So does media minutes at zero, no limit (generousRate). |
| + if p.Limits.MediaMinutesMonth == 0 { |
| + want.MediaMinutesMonth = 0 |
| + } |
| if got != want { |
| t.Errorf("%s: effective %+v, want %+v", name, got, want) |
| } |
| @@ -375,10 +380,19 @@ func TestBillingShowsTheSponsorshipAsUnlimited(t *testing.T) { |
| for _, inc := range planIncludes(l, 0) { |
| got = append(got, inc.Text) |
| } |
| - want := []string{"Unlimited apps and instances", "Unlimited disk per app", "Unlimited PDFs a month", "Unlimited media minutes a month", "Custom domains"} |
| + want := []string{"Unlimited apps and instances", "Unlimited disk per app", "Unlimited PDFs a month", "Custom domains"} |
| if strings.Join(got, "|") != strings.Join(want, "|") { |
| t.Fatalf("includes = %q, want %q", got, want) |
| } |
| + // A plan with a media limit (Free's 30) shows the sponsorship's as |
| + // Unlimited; one with none (the example catalog) has nothing to show. |
| + ml, err := mediaSponsorCatalog(t, sponsorCutoffAt).Effective("free", nil, &sponsored) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if inc := planIncludes(ml, 0); !slices.ContainsFunc(inc, func(i billingInclude) bool { return i.Text == "Unlimited media minutes a month" }) { |
| + t.Fatalf("media includes = %+v", inc) |
| + } |
| if u := useOfLimit(map[capID]int64{capRunningThings: 4}, capRunningThings, int64(l.RunningThings)); u == nil || u.Text != "You use 4" { |
| t.Fatalf("use = %+v", u) |
| } |
diff --git a/internal/plans/catalog.go b/internal/plans/catalog.go| index 8918e7450..365e391d3 100644 |
| --- a/internal/plans/catalog.go |
| +++ b/internal/plans/catalog.go |
| @@ -487,7 +487,9 @@ func (l Limits) generous(o Limits) Limits { |
| l.EmailPerHour = generousRate(l.EmailPerHour, o.EmailPerHour) |
| l.BandwidthBytesMonth = max(l.BandwidthBytesMonth, o.BandwidthBytesMonth) |
| l.PDFRendersMonth = max(l.PDFRendersMonth, o.PDFRendersMonth) |
| - l.MediaMinutesMonth = max(l.MediaMinutesMonth, o.MediaMinutesMonth) |
| + // Zero media minutes on the plan side is no limit (generousRate's |
| + // rule), as Effective's pack path keeps it. |
| + l.MediaMinutesMonth = generousRate(l.MediaMinutesMonth, o.MediaMinutesMonth) |
| l.HistoryDays = max(l.HistoryDays, o.HistoryDays) |
| l.RateClientShare = generousRate(l.RateClientShare, o.RateClientShare) |
| l.RatePerSecond = generousRate(l.RatePerSecond, o.RatePerSecond) |
diff --git a/internal/plans/catalog_test.go b/internal/plans/catalog_test.go| index afb3a4ece..30d593da0 100644 |
| --- a/internal/plans/catalog_test.go |
| +++ b/internal/plans/catalog_test.go |
| @@ -479,11 +479,27 @@ func TestLimitsMediaMinutesMonth(t *testing.T) { |
| if got, err := c.Effective("personal", map[string]int{"media": 2}, nil); err != nil || got.MediaMinutesMonth != 0 { |
| t.Errorf("unlimited plan with packs = %d, %v; want 0", got.MediaMinutesMonth, err) |
| } |
| - // A sponsored figure is the more generous one, unclamped. |
| + // A sponsored figure is the more generous one, unclamped; a smaller one |
| + // or a sponsored zero (unset) keeps the plan's. |
| c, _ = LoadCatalog(mediaCatalogPath) |
| c.Ceilings.MediaMinutesMonth = 700 |
| - if got, _ := c.Effective("free", nil, &Limits{MediaMinutesMonth: 5000}); got.MediaMinutesMonth != 5000 { |
| - t.Errorf("sponsored = %d", got.MediaMinutesMonth) |
| + for sponsored, want := range map[int]int{5000: 5000, 10: 30, 0: 30} { |
| + if got, _ := c.Effective("free", nil, &Limits{MediaMinutesMonth: sponsored}); got.MediaMinutesMonth != want { |
| + t.Errorf("free sponsored %d = %d, want %d", sponsored, got.MediaMinutesMonth, want) |
| + } |
| + } |
| + // An unlimited plan stays unlimited under a finite sponsored figure, |
| + // with or without a pack (Astra T6 P2 1). |
| + p = c.Plans["personal"] |
| + p.Limits.MediaMinutesMonth = 0 |
| + c.Plans["personal"] = p |
| + for _, packs := range []map[string]int{nil, {"media": 1}} { |
| + if got, err := c.Effective("personal", packs, &Limits{MediaMinutesMonth: 600}); err != nil || got.MediaMinutesMonth != 0 { |
| + t.Errorf("unlimited plan, packs %v, sponsored 600 = %d, %v; want 0 (no limit)", packs, got.MediaMinutesMonth, err) |
| + } |
| + } |
| + if got := (Limits{}).generous(Limits{MediaMinutesMonth: 600}).MediaMinutesMonth; got != 0 { |
| + t.Errorf("generous(0, 600) = %d, want 0", got) |
| } |
| } |