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

Closed opened this
Closed without merging

scout/forge:scout/16133-a-forge-in-grace-after-a-lapse-shows-the → main 5 commits fork 1 thread · 1 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
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
4242 - **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`.
4343
4444 - **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 =>
2424 lapse.state === "grace"
2525 ? `Your payment lapsed: the grace period ends on ${zoned(lapse.graceUntil)}, and then your forge sleeps; its data is kept until ${zoned(lapse.keepUntil)}.`
2626 : 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";
1212 import { getPlans } from "../../settings/plans.ts";
1313 import type { Ctx, Screen } from "../app/app.ts";
1414 import { lapseFor } from "../../billing/lapse.ts";
15 import { lapseNotice } from "../../billing/lapse_words.ts";
15 import { lapseDaysLeft, lapseNotice } from "../../billing/lapse_words.ts";
1616 import { getStored } from "../../billing/store.ts";
1717 import { listPayments } from "../../billing/payments.ts";
1818 import { binding } from "../app/keys.ts";
@@ -44,8 +44,10 @@ export function billingRows(f: Session): ListRow[] {
4444 return p === undefined ? "" : ` · last payment ${p.status} ${p.amountMinor} ${p.currency} (minor units)`;
4545 };
4646 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] }) };
4951 });
5052 }
5153
src/web/pages/billing.ts +3 −5
@@ -13,7 +13,7 @@ import { retryOrder } from "../../actions/order.ts";
1313 import { REFUND_URL } from "../../billing/checkout.ts";
1414 import { changeLine, previewChange, same, seen } from "../../billing/plan_change.ts";
1515 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";
1717 import { failedOrders } from "../../billing/orders.ts";
1818 import { CODE_ERRORS, makeCode, readCodes, revoke, type CodeError } from "../../actions/codes.ts";
1919 import { publicOrigin } from "../../core/public_origin.ts";
@@ -182,10 +182,8 @@ function funnelRow(r: FunnelReport): Html {
182182
183183 /** The lapse state as a suffix for a forge's row: nothing while active. */
184184 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());

Principle 1 (nothing explains itself twice): the suffix now says "sleeps in 4 days unless the payment is fixed", and the lapseNotice sentence 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 and below repeat it.

186 return w === undefined ? "" : ` · ${w}`;
189187 }
190188
191189 // 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", () => {
218218 putEntitlement(app.db, { forge: "kid", plan: was.plan, status: "grace", source: "stripe", provider: "stripe" }, "small", "u");
219219 const row = billingRows(open(alice).session).find((r) => r.title === "kid");
220220 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/);
222222 expect(row?.below?.[0]).toMatch(/Your payment lapsed: the grace period ends on \d{4}-\d\d-\d\d UTC/);
223223 });
test/billing.test.ts +4 −5
@@ -239,17 +239,16 @@ describe("billing seam", () => {
239239 };
240240 const soon = new Date(Date.now() - 86_400_000);
241241 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");
244243
245244 enter("none", "2026-01-01T00:00:00.000Z");
246245 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);
249248
250249 putEntitlement(app.db, { forge: "kid", plan: "medium", status: "active", source: "granted" }, "small", "test");
251250 const back = await row();
252 expect(back).not.toContain("lapsed");
251 expect(back).not.toContain("sleeps");
253252 expect(back).not.toContain("past due");
254253 });
255254 });
test/lapse-words.test.ts +15 −1
@@ -4,7 +4,7 @@ import { describe, expect, test } from "bun:test";
44 import { readFileSync } from "node:fs";
55 import { join } from "node:path";
66 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";
88
99 const DAY = 86_400_000;
1010 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", () => {
4141 }
4242 });
4343 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs