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

opened this

Merged 16d 19h ago, 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.

Notes

opened this

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 setSystemTime as the fake clock, because lapseFor, the entitlement log and the webhook signature all read Date.now(). The real forge, webhook, sleepLapsed, the wake gate and payAgainGate run on that clock, and no source file was touched. The grace length comes from lapseRule, not a constant.
  • Paid: the lapse state is active, the child's plan.json says active, there is no grace line, and the forge is awake.
  • Past due: the state is grace and graceUntil is 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, sleepLapsed sleeps the forge and the wake gate answers 402. The line becomes the lapsedSentence and 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 to active, 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, graceWords and lapse_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

Closes #13034

2 earlier comments
commented

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 setSystemTime as the fake clock, because lapseFor, the entitlement log and the webhook signature all read Date.now(). The real forge, webhook, sleepLapsed, the wake gate and payAgainGate run on that clock, and no source file was touched. The grace length comes from lapseRule, not a constant.
  • Paid: the lapse state is active, the child's plan.json says active, there is no grace line, and the forge is awake.
  • Past due: the state is grace and graceUntil is 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, sleepLapsed sleeps the forge and the wake gate answers 402. The line becomes the lapsedSentence and 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 to active, 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, graceWords and lapse_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

commented

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.

commented

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.

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs