Skip to content

scripts: billing reconciliation sandbox (#3623 prep) - #3643

Merged
bokelley merged 1 commit into
mainfrom
bokelley/stripe-db-remediate
Apr 30, 2026
Merged

bokelley merged 1 commit into
mainfrom
bokelley/stripe-db-remediate

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Adds a deterministic, idempotent fixture set for the cron auto-remediation work in #3623. Pure dev tooling — no production code touched.

Why

Building the cron auto-remediator (Path B of #3623) needs to exercise several edge cases that are hard to reproduce on demand:

  • Missed customer.subscription.created webhook (the Lina case)
  • Customer with multiple subscriptions (latent data[0] bug in /sync)
  • Stripe customer email ≠ org owner email (must refuse remediation)
  • Active Stripe customer with no AAO org link (orphan — flag, never auto-link)
  • WorkOS user resolution returning null (attestation can't be attributed)

The sandbox sets these up in Stripe test mode + local Postgres so each can be observed end-to-end against the real Stripe wire format and real DB state — per repo CLAUDE.md preference for not over-mocking.

What it does

server/scripts/setup-sandbox.ts creates 6 fixtures:

Fixture Stripe state DB state Exercises
lina_class active membership sub, customer email matches owner subscription_status=NULL, owner in organization_memberships Happy-path remediation
healthy active sub subscription_status='active' No-op (control)
email_mismatch active sub, customer email ≠ owner email drift Refuse remediation
multi_sub 2 subs (membership + non-membership) drift data[0] bug fix verification
orphan active sub no org linked Flag only — never auto-link
workos_no_resolution active sub no organization_memberships row Refuse: can't attribute attestation

server/scripts/verify-sandbox.ts runs the stripe-sub-reflected-in-org-row invariant (merged in #3627) against the local sandbox and prints the violations.

Safety

  • Refuses to run unless STRIPE_SECRET_KEY starts with sk_test_
  • Refuses if DATABASE_URL hostname ends with .fly.dev, .flycast, or .internal
  • All Stripe customers + subs tagged metadata.aao_sandbox_fixture=true (trivial cleanup)
  • All DB rows prefixed AAO Sandbox - and use aao-sandbox.test email domain
  • --teardown flag removes everything

Verified locally

$ npx tsx -r dotenv/config server/scripts/verify-sandbox.ts
[critical] organization=org_aao_sandbox_workos_no_resolution
[critical] organization=org_aao_sandbox_multi_sub
[critical] organization=org_aao_sandbox_email_mismatch
[critical] organization=org_aao_sandbox_lina_class
[warning]  customer=cus_xxx (orphan)
  • 4 critical drift violations on the right fixtures ✓
  • healthy correctly produces no violation ✓
  • multi_sub correctly picks the membership sub only (not the event-ticket non-membership sub) ✓
  • Orphan correctly flagged as warning, not critical ✓

Test plan

  • Type checks: npx tsc --noEmit
  • Pre-commit hook (unit tests + dynamic imports + typecheck) passes
  • Sandbox setup runs cleanly against local Postgres + sk_test_ Stripe
  • Invariant produces expected violations against the seeded state
  • --teardown removes everything
  • Reviewer can run setup-sandbox.ts locally and verify their own dev environment ends up in the same state

Refs

🤖 Generated with Claude Code

…rep)

Idempotent setup that creates a 6-fixture matrix in Stripe test mode +
local Postgres covering the failure modes the cron auto-remediator must
handle: Lina-class missed-webhook, multi-sub data[0] bug, email mismatch,
orphan customer, WorkOS no-resolution, and a healthy control.

setup-sandbox.ts is idempotent (Stripe customers tagged via
metadata.aao_sandbox_id; DB rows by org-id prefix). Refuses to run unless
STRIPE_SECRET_KEY is sk_test_* and DATABASE_URL is local — protects prod
from accidental sandbox writes.

verify-sandbox.ts runs the stripe-sub-reflected-in-org-row invariant
(merged in #3627) against the local sandbox and prints the violations.
Lets reconciliation work be observed end-to-end without exercising real
member accounts.

Companion infrastructure for #3623. No production code touched; pure
dev tooling. Also serves as the substrate for future billing-flow work
that needs reproducible cross-system fixtures.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant