- Expanded
tests/integration/billing.test.tsto cover end-to-end invoice generation, settlement success paths, retry/failure paths, concurrency behavior, and malformed-event edge cases. - Fixed the pg-mem-backed integration helpers so the database-backed invoice tests execute against synchronous
db.publicqueries instead of silently returning empty results. - Added same-instance batch serialization in
RevenueSettlementServiceso concurrentrunBatch()calls do not double-process the same unsettled events within a single service instance.
npm run lint- Passed with
0errors. - Repo still has
93existing lint warnings outside this change set.
- Passed with
npm run typecheck- Passed.
npx jest tests/integration/billing.test.ts --runInBand- Passed:
1suite,26tests.
- Passed:
npm test- Still failing outside this task area, even when rerun outside the sandbox.
- Observed unrelated failures include
tests/integration/billing-http.test.ts,src/__tests__/developerRevenue.test.ts, andsrc/__tests__/ipAllowlist.test.ts. - Those failures are primarily authorization and allowlist expectation mismatches, not regressions introduced by the invoice-generation changes.
- Billing idempotency is still enforced with
request_iduniqueness plus the billing service transaction boundary that persists a pending row before external settlement side effects. - Failed payout attempts leave usage events unsettled so they can be retried; tests verify failed settlements are recorded without falsely marking events as paid.
- The new
RevenueSettlementServiceserialization guard protects against duplicate processing from concurrentrunBatch()calls on the same service instance. - Database-backed invoice integration tests now exercise real settlement persistence and settled-event linkage instead of relying on async helpers that could mask data-loss bugs.
- SQL used by the integration-only pg-mem helpers escapes interpolated string literals before executing direct
db.publicstatements.
Validation summary:
- npm run lint: passed with 0 errors (93 pre-existing repo warnings remain)
- npm run typecheck: passed
- npx jest tests/integration/billing.test.ts --runInBand: passed (26/26)
- npm test: still failing in unrelated existing suites outside the billing invoice E2E change (observed in billing-http, developerRevenue, and ipAllowlist tests)
Security/data-integrity notes:
- request_id idempotency and pending-row transaction boundaries remain covered
- failed settlements stay retryable and are not marked as paid
- concurrent same-instance settlement batches are serialized to avoid duplicate processing
- DB-backed invoice tests now use real pg-mem-backed persistence paths for settlement linkage