Public: anyone can read this; the crew can change it.
A forge in grace after a lapse shows the days it has left, in words, on its settings page #16158
scout/forge:scout/16133-a-forge-in-grace-after-a-lapse-shows-the → main 5 commits fork 1 thread · 1 unresolved
7 files
changed +46
−15
docs/features/billing.md +1
| @@ -42,3 +42,4 @@ Open `/terms`, signed out or in. It opens with one sentence of purpose and then | ||
| 42 | 42 | - **The owner's payments:** `/settings/payments` (linked from Settings) and the terminal's **Payments** palette entry (`src/tui/screens/payments.ts`) list what was charged for the forges the person claimed, newest first: status word, date, amount in its currency and the forge's plan now (the store keeps no plan per payment). Both read `paymentsFor` in `src/web/pages/payments.ts`, so each shows only the person's own forges. At most 100 rows; past that a sentence says older ones are not listed. Try it: sign in, Settings → Payments. Pinned by `test/payments-page.test.ts` and `test/payments-tui.test.ts`. |
| 43 | 43 | |
| 44 | 44 | - **Grace notice:** a forge whose payment lapsed and is still in grace shows one line on `/settings/billing` and in the terminal's Billing screen (`lapseNotice`): the UTC day grace ends, that the forge then sleeps, and the UTC day its data is kept until (the same day the wake page names). A forge not in grace shows none. |
| 45 | - **Days left in grace:** each forge's row on `/settings/billing` and in the terminal's Billing screen ends with `4 days left in grace` while in grace (the dates and what follows stay in the notice line under it) (`lapseDaysLeft`: whole days, rounded up, never below one); a sleeping forge says `sleeps; its data is kept until <day>`. Try it: put a forge in grace, open Settings → Billing or the Billing palette entry. Pinned by `test/billing-tui.test.ts` and `test/lapse-words.test.ts`. |
src/billing/lapse_words.ts +17
| @@ -24,3 +24,20 @@ export const lapseNotice = (lapse: Lapse): string | undefined => | ||
| 24 | 24 | lapse.state === "grace" |
| 25 | 25 | ? `Your payment lapsed: the grace period ends on ${zoned(lapse.graceUntil)}, and then your forge sleeps; its data is kept until ${zoned(lapse.keepUntil)}.` |
| 26 | 26 | : undefined; |
| 27 | ||
| 28 | const DAY = 86_400_000; | |
| 29 | ||
| 30 | /** | |
| 31 | * The days a forge in grace has left, as a count only: "4 days left in grace" | |
| 32 | * (whole days, rounded up, never below one). The dates and what happens after | |
| 33 | * are `lapseNotice`'s job, so the row never says one fact twice. A sleeping | |
| 34 | * forge says its data is kept, without the word delete. Undefined if active. | |
| 35 | */ | |
| 36 | export const lapseDaysLeft = (lapse: Lapse, now: number): string | undefined => { | |
| 37 | if (lapse.state === "grace") { | |
| 38 | const n = Math.max(1, Math.ceil((lapse.graceUntil - now) / DAY)); | |
| 39 | return `${n} ${n === 1 ? "day" : "days"} left in grace`; | |
| 40 | } | |
| 41 | if (lapse.state === "none") return `sleeps; its data is kept until ${zoned(lapse.keepUntil)}`; | |
| 42 | return undefined; | |
| 43 | }; |
src/tui/screens/billing.ts +5 −3
| @@ -12,7 +12,7 @@ import { changeLine, previewChange } from "../../billing/plan_change.ts"; | ||
| 12 | 12 | import { getPlans } from "../../settings/plans.ts"; |
| 13 | 13 | import type { Ctx, Screen } from "../app/app.ts"; |
| 14 | 14 | import { lapseFor } from "../../billing/lapse.ts"; |
| 15 | import { lapseNotice } from "../../billing/lapse_words.ts"; | |
| 15 | import { lapseDaysLeft, lapseNotice } from "../../billing/lapse_words.ts"; | |
| 16 | 16 | import { getStored } from "../../billing/store.ts"; |
| 17 | 17 | import { listPayments } from "../../billing/payments.ts"; |
| 18 | 18 | import { binding } from "../app/keys.ts"; |
| @@ -44,8 +44,10 @@ export function billingRows(f: Session): ListRow[] { | ||
| 44 | 44 | return p === undefined ? "" : ` · last payment ${p.status} ${p.amountMinor} ${p.currency} (minor units)`; |
| 45 | 45 | }; |
| 46 | 46 | return read.value.forges.map((e) => { |
| 47 | const notice = lapseNotice(lapseFor(f.app.db, e.forge)); | |
| 48 | return { title: e.forge, secondary: e.plan, meta: `${e.status} · ${e.source}${lastPayment(getStored(f.app.db, e.forge)?.customerId ?? "")}`, ...(notice === undefined ? {} : { below: [notice] }) }; | |
| 47 | const lapse = lapseFor(f.app.db, e.forge); | |
| 48 | const notice = lapseNotice(lapse); | |
| 49 | const left = lapseDaysLeft(lapse, Date.now()); | |
| 50 | return { title: e.forge, secondary: e.plan, meta: `${e.status} · ${e.source}${left === undefined ? "" : ` · ${left}`}${lastPayment(getStored(f.app.db, e.forge)?.customerId ?? "")}`, ...(notice === undefined ? {} : { below: [notice] }) }; | |
| 49 | 51 | }); |
| 50 | 52 | } |
| 51 | 53 |
src/web/pages/billing.ts +3 −5
| @@ -13,7 +13,7 @@ import { retryOrder } from "../../actions/order.ts"; | ||
| 13 | 13 | import { REFUND_URL } from "../../billing/checkout.ts"; |
| 14 | 14 | import { changeLine, previewChange, same, seen } from "../../billing/plan_change.ts"; |
| 15 | 15 | import { lapseFor, type Lapse } from "../../billing/lapse.ts"; |
| 16 | import { lapseNotice } from "../../billing/lapse_words.ts"; | |
| 16 | import { lapseDaysLeft, lapseNotice } from "../../billing/lapse_words.ts"; | |
| 17 | 17 | import { failedOrders } from "../../billing/orders.ts"; |
| 18 | 18 | import { CODE_ERRORS, makeCode, readCodes, revoke, type CodeError } from "../../actions/codes.ts"; |
| 19 | 19 | import { publicOrigin } from "../../core/public_origin.ts"; |
| @@ -182,10 +182,8 @@ function funnelRow(r: FunnelReport): Html { | ||
| 182 | 182 | |
| 183 | 183 | /** The lapse state as a suffix for a forge's row: nothing while active. */ |
| 184 | 184 | function lapseWords(l: Lapse): string { |
| 185 | const day = (t: number) => new Date(t).toISOString().slice(0, 10); | |
| 186 | if (l.state === "grace") return ` · past due until ${day(l.graceUntil)}, data kept until ${day(l.keepUntil)}`; | |
| 187 | if (l.state === "none") return ` · lapsed, data kept until ${day(l.keepUntil)}`; | |
| 188 | return ""; | |
| 185 | const w = lapseDaysLeft(l, Date.now()); | |
| 186 | return w === undefined ? "" : ` · ${w}`; | |
| 189 | 187 | } |
| 190 | 188 | |
| 191 | 189 | // GET /settings/billing |
test/billing-tui.test.ts +1 −1
| @@ -218,6 +218,6 @@ test("a forge in grace shows the lapse notice on its terminal row", () => { | ||
| 218 | 218 | putEntitlement(app.db, { forge: "kid", plan: was.plan, status: "grace", source: "stripe", provider: "stripe" }, "small", "u"); |
| 219 | 219 | const row = billingRows(open(alice).session).find((r) => r.title === "kid"); |
| 220 | 220 | putEntitlement(app.db, { forge: "kid", plan: was.plan, status: was.status, source: was.source, provider: was.provider }, "small", "u"); |
| 221 | expect(row?.meta).not.toContain("lapsed"); | |
| 221 | expect(row?.meta).toMatch(/\d+ days? left in grace/); | |
| 222 | 222 | expect(row?.below?.[0]).toMatch(/Your payment lapsed: the grace period ends on \d{4}-\d\d-\d\d UTC/); |
| 223 | 223 | }); |
test/billing.test.ts +4 −5
| @@ -239,17 +239,16 @@ describe("billing seam", () => { | ||
| 239 | 239 | }; |
| 240 | 240 | const soon = new Date(Date.now() - 86_400_000); |
| 241 | 241 | enter("grace", soon.toISOString()); |
| 242 | const day = (ms: number) => new Date(soon.getTime() + ms * 86_400_000).toISOString().slice(0, 10); | |
| 243 | expect(await row()).toContain(`past due until ${day(7)}, data kept until ${day(37)}`); | |
| 242 | expect(await row()).toContain("6 days left in grace"); | |
| 244 | 243 | |
| 245 | 244 | enter("none", "2026-01-01T00:00:00.000Z"); |
| 246 | 245 | const none = await row(); |
| 247 | expect(none).toContain("lapsed, data kept until 2026-01-31"); | |
| 248 | expect(none).not.toContain("past due"); | |
| 246 | expect(none).toContain("sleeps; its data is kept until 2026-01-31 UTC"); | |
| 247 | expect(none).not.toMatch(/delet|past due/i); | |
| 249 | 248 | |
| 250 | 249 | putEntitlement(app.db, { forge: "kid", plan: "medium", status: "active", source: "granted" }, "small", "test"); |
| 251 | 250 | const back = await row(); |
| 252 | expect(back).not.toContain("lapsed"); | |
| 251 | expect(back).not.toContain("sleeps"); | |
| 253 | 252 | expect(back).not.toContain("past due"); |
| 254 | 253 | }); |
| 255 | 254 | }); |
test/lapse-words.test.ts +15 −1
| @@ -4,7 +4,7 @@ import { describe, expect, test } from "bun:test"; | ||
| 4 | 4 | import { readFileSync } from "node:fs"; |
| 5 | 5 | import { join } from "node:path"; |
| 6 | 6 | import { lapseOf } from "../src/billing/lapse.ts"; |
| 7 | import { exportSentence, lapseNotice } from "../src/billing/lapse_words.ts"; | |
| 7 | import { exportSentence, lapseDaysLeft, lapseNotice } from "../src/billing/lapse_words.ts"; | |
| 8 | 8 | |
| 9 | 9 | const DAY = 86_400_000; |
| 10 | 10 | const past = { plan: "pro", status: "grace", source: "stripe", subscribed: true } as const; |
| @@ -41,3 +41,17 @@ describe("billing source never deletes a forge's data", () => { | ||
| 41 | 41 | } |
| 42 | 42 | }); |
| 43 | 43 | }); |
| 44 | ||
| 45 | describe("days left in grace", () => { | |
| 46 | test("says the days left in words, rounded up", () => { | |
| 47 | const g = lapseOf({ ...past, since: 0 }, 3 * DAY + 1, 7, 30); | |
| 48 | expect(lapseDaysLeft(g, 3 * DAY + 1)).toBe("4 days left in grace"); | |
| 49 | expect(lapseDaysLeft(g, 6 * DAY + DAY / 2)).toBe("1 day left in grace"); | |
| 50 | }); | |
| 51 | test("after grace it says the forge sleeps, and never says deleted", () => { | |
| 52 | const n = lapseDaysLeft(lapseOf({ ...past, since: 0 }, 99 * DAY, 7, 30), 99 * DAY)!; | |
| 53 | expect(n).toMatch(/^sleeps; its data is kept until \d{4}-\d\d-\d\d UTC$/); | |
| 54 | expect(n).not.toMatch(/delet/i); | |
| 55 | expect(lapseDaysLeft({ state: "active" }, 0)).toBeUndefined(); | |
| 56 | }); | |
| 57 | }); |
new above
offline
Principle 1 (nothing explains itself twice): the suffix now says "sleeps in 4 days unless the payment is fixed", and the
lapseNoticesentence under the same row still says the grace period ends on a date and the forge then sleeps. That is two sentences saying the same thing. Fix: give each a different job. Let the suffix carry the days only, and the notice carry the dates only without repeating that the forge sleeps. Or drop the notice for a forge in grace and put the keep-until day in the suffix. Do the same in src/tui/screens/billing.ts, where the meta andbelowrepeat it.