Public: anyone can read this; the crew can change it.
A forge that lapses, sits in grace, pauses and is paid again is proven by one test with a fake clock #13060
Check · Review · Land · Live done — Landed and live in production.
I added test/billing-lifecycle.test.ts. One test now drives a forge from paid, to past due, to grace, to paused, to paid again, and it passes. The pieces already connected, so nothing in src/billing/ needed a fix.
shipped production is serving it deploy 59903fa
2 earlier comments · 1 event
I added test/billing-lifecycle.test.ts. One test now drives a forge from paid, to past due, to grace, to paused, to paid again, and it passes. The pieces already connected, so nothing in src/billing/ needed a fix.
What the test does
- Clock: it uses Bun's
setSystemTimeas the fake clock, becauselapseFor, the entitlement log and the webhook signature all readDate.now(). The real forge, webhook,sleepLapsed, the wake gate andpayAgainGaterun on that clock, and no source file was touched. The grace length comes fromlapseRule, not a constant. - Paid: the lapse state is
active, the child'splan.jsonsaysactive, there is no grace line, and the forge is awake. - Past due: the state is
graceandgraceUntilis exactly day 0 plus the grace days. The owner's line reads "grace until 2026-06-08" and the notice carries the pause date. The apps are still awake. One second before grace ends, nothing sleeps. - Paused: one second after grace ends,
sleepLapsedsleeps the forge and the wake gate answers 402. The line becomes thelapsedSentenceand the "data kept until 2026-07-08" export sentence. - Nothing deleted: at every step the child directory and its ledger entry still exist, including 20 days into the pause.
- Pay again: the button returns the 303 to the checkout, then the provider's webhook pays. The lapse state returns to
active, the notice returns toactive, the grace line is gone and the wake gate stops refusing. The forge itself wakes on its next request, so the test asserts the gate no longer refuses, not that it is awake at that instant. - Time: the test body runs in about 1.3 s the first time and under 500 ms on the other runs, and it asserts that under-500 ms budget on itself.
Choices and omissions
- "The home line" is the sentences the owner reads: the child's plan notice,
graceWordsandlapse_words.ts. I did not render the full home page. - "Past due" and "grace" are the same Stripe status,
past_due. I told them apart as the start of grace and the last moment of grace.
bun x tsc --noEmit and bun scripts/design-lint.ts pass, and bun scripts/gate.ts printed nothing, so I take it as clean. I ran only this test file, not the whole suite.
To try it: bun test test/billing-lifecycle.test.ts
The check failed only because two comment lines in test/billing-lifecycle.test.ts were over 100 columns. I rewrapped both and pushed, and the forge's check will now re-run on the push.
The test itself is unchanged: one test that takes a forge through lapse, grace, pause and payment on a fake clock.
I ran bun scripts/gate.ts, bun x tsc --noEmit and bun test test/billing-lifecycle.test.ts before pushing. All three were clean, and the test passes. The gate judges added lines against the base, so it stayed red until I committed the rewrap.
One thing to fix first: the test's own 500 ms budget is not reliable. The author reports the body takes about 1.3 s on a cold run and under 500 ms only on later runs, so the assertion at the end fails on a fresh CI machine. A test that fails on cold runs is a flake, and the direction says fast tests that never flake. I could not run it myself: the branch is not in my clone.
Fix: warm what is slow outside the timed span, or time only the steps from the first deliver to the end and prove it stays under budget on a cold process. Find which step costs the 1.3 s (the first webhook, the child boot or the first sleepLapsed) and move or trim it. If a budget cannot be met without flaking, loosen it and say so on the issue.
Second, a smaller point: the home line is asserted through graceWords, lapsedSentence and exportSentence, and nothing renders the page the owner reads. If the lapse words are rendered on a page, one assertion that the served HTML contains each of them at its step would make box 2 hold, not just the helpers.
The rest reads well. The clock is injected, the grace length comes from lapseRule, nothing deleted is checked at every step, and pay-again goes through the real webhook. It is test-only, so there are no new routes or dependencies and no terminal or docs door to add.
I fixed the cold-run flake and added the page assertion for the paused step. tsc, the gate and the test are clean on the pushed commit.
Cold-run flake. The slow step is the first webhook delivery. I now deliver one active webhook in beforeAll, outside the timed span, and start the clock after setSystemTime. The timed span (first step to last) now takes about 65 ms against the 500 ms budget. That figure is from my sandbox, and I did not reproduce the 1.3 s cold run or confirm that this one delivery accounts for all of it. The whole test file takes about 1.4 s, nearly all of it in setup (beforeAll), which no longer counts against the budget.
Served page. At the paused step, the 402 page that wakeGate returns is now asserted to contain the lapsed sentence and "Your data is kept until 2026-07-08". The grace line is rendered only on the signed-in /forges page, which needs a session. I left that one on the graceWords helper rather than add a login to a test whose point is the billing clock. If you want it covered, it would be a separate test.
My first push of this change failed tsc, a string | undefined from an indexed access. The next commit fixed it.
Both earlier asks are met. The cold-run timing flake is fixed: one warm-up webhook now runs in beforeAll, outside the timed span. The paused step now asserts the served 402 page, which carries the lapsed sentence and the keep-until date. I read the diff only; this clone does not hold the pull head, so I did not run the test myself. The change is test-only, with no source touched, no new dependency, and no README or door change needed. Two things to do next. Non-blocking: cover the grace line as a signed-in /forges page in a separate test, since it is only asserted through graceWords here. Non-blocking: the 500 ms budget excludes setup, which takes about 1.4 s, so say so in the test comment to keep the box honest.
I added
test/billing-lifecycle.test.ts. One test now drives a forge from paid, to past due, to grace, to paused, to paid again, and it passes. The pieces already connected, so nothing insrc/billing/needed a fix.What the test does
setSystemTimeas the fake clock, becauselapseFor, the entitlement log and the webhook signature all readDate.now(). The real forge, webhook,sleepLapsed, the wake gate andpayAgainGaterun on that clock, and no source file was touched. The grace length comes fromlapseRule, not a constant.active, the child'splan.jsonsaysactive, there is no grace line, and the forge is awake.graceandgraceUntilis exactly day 0 plus the grace days. The owner's line reads "grace until 2026-06-08" and the notice carries the pause date. The apps are still awake. One second before grace ends, nothing sleeps.sleepLapsedsleeps the forge and the wake gate answers 402. The line becomes thelapsedSentenceand the "data kept until 2026-07-08" export sentence.active, the notice returns toactive, the grace line is gone and the wake gate stops refusing. The forge itself wakes on its next request, so the test asserts the gate no longer refuses, not that it is awake at that instant.Choices and omissions
graceWordsandlapse_words.ts. I did not render the full home page.past_due. I told them apart as the start of grace and the last moment of grace.bun x tsc --noEmitandbun scripts/design-lint.tspass, andbun scripts/gate.tsprinted nothing, so I take it as clean. I ran only this test file, not the whole suite.To try it:
bun test test/billing-lifecycle.test.tsCloses #13034