Title: refactor: migrate billing and test files to dependency injection pattern by brandonkachen · Pull Request #408 · CodebuffAI/codebuff · GitHub
Open Graph Title: refactor: migrate billing and test files to dependency injection pattern by brandonkachen · Pull Request #408 · CodebuffAI/codebuff
X Title: refactor: migrate billing and test files to dependency injection pattern by brandonkachen · Pull Request #408 · CodebuffAI/codebuff
Description: Non-Test Files Changed (22 files, +1,393 / -157 lines) File +/- Description TESTING.md +518 New comprehensive testing guide common/src/types/contracts/billing.ts +334 New DI contract types packages/billing/src/auto-topup.ts +128/-40 DI refactor packages/billing/src/grant-credits.ts +84/-39 DI refactor packages/billing/src/billing.knowledge.md +81/-3 Updated docs packages/billing/src/org-billing.ts +54/-13 DI refactor packages/billing/src/stripe-metering.ts +42/-14 DI refactor packages/billing/src/balance-calculator.ts +40/-14 DI refactor packages/internal/src/db/types.ts +39 New transaction types packages/billing/src/usage-service.ts +25/-5 DI refactor packages/billing/src/credit-delegation.ts +16/-4 DI refactor packages/agent-runtime/src/llm-api/linkup-api.ts +10/-2 Minor update web/src/app/api/admin/relabel-for-user/route.ts +4/-6 TEST_USER_ID removal packages/billing/src/org-monitoring.ts +4/-4 Minor update evals/buffbench/main*.ts (4 files) +3 each CI=true env packages/internal/src/db/index.ts +2 Exports common/src/old-constants.ts -1 TEST_USER_ID removal web/src/app/api/v1/agent-runs/[runId]/steps/_post.ts -6 TEST_USER_ID removal web/src/app/api/v1/agent-runs/_post.ts -6 TEST_USER_ID removal Summary This PR refactors billing code and tests to use dependency injection (DI) instead of module mocking, following the patterns outlined in TESTING.md. Changes DI Infrastructure Add DI contract types for billing dependencies (BillingDbConnection, UsageServiceDeps, etc.) Create mock database helpers (createMockDb, createMockTransaction, createTrackedMockDb) Create test fixtures for billing (createMockCreditGrant, createMockUser, etc.) Billing Package Refactor Refactor grant-credits.ts to accept optional deps with transaction for DI Refactor usage-service.ts to accept UsageServiceDeps for testing Update all billing tests to use DI instead of mockModule Add getBillingDbClient() helper for centralized DB client handling Fix transactional consistency in triggerMonthlyResetAndGrant TEST_USER_ID Removal Remove hardcoded TEST_USER_ID bypass from production billing code Remove from balance-calculator.ts, stripe-metering.ts, and agent-runs API endpoints Evals now set CI=true at entry points to skip Stripe billing Test Coverage 158 billing tests passing ~5,300+ lines of new test infrastructure code Zero mockModule usage remains in billing tests Documentation Add TESTING.md with comprehensive DI patterns and test fixture documentation Update billing.knowledge.md with DI patterns reference Validation ✅ All 158 billing tests pass ✅ All 13 packages pass typecheck ✅ All 17 CI jobs pass ✅ Zero mockModule usage remains in billing tests ✅ Reviewed by Gemini CLI, Codex CLI, and Claude Code - all approved
Open Graph Description: Non-Test Files Changed (22 files, +1,393 / -157 lines) File +/- Description TESTING.md +518 New comprehensive testing guide common/src/types/contracts/billing.ts +334 New DI contract types...
X Description: Non-Test Files Changed (22 files, +1,393 / -157 lines) File +/- Description TESTING.md +518 New comprehensive testing guide common/src/types/contracts/billing.ts +334 New DI contract types...
Opengraph URL: https://github.com/CodebuffAI/codebuff/pull/408
X: @github
Domain: patch-diff.githubusercontent.com
| route-pattern | /:user_id/:repository/pull/:id/checks(.:format) |
| route-controller | pull_requests |
| route-action | checks |
| fetch-nonce | v2:6bc74f03-b607-6f93-36e1-b5ec7fae2798 |
| current-catalog-service-hash | 87dc3bc62d9b466312751bfd5f889726f4f1337bdff4e8be7da7c93d6c00a25a |
| request-id | 92C0:27C649:4ED7B:5E00D:697F9ED0 |
| html-safe-nonce | 76193690d0aa56d38385bb2aa2eda1dc872f71a894b8d9ccf82fa95916e1bba8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MkMwOjI3QzY0OTo0RUQ3Qjo1RTAwRDo2OTdGOUVEMCIsInZpc2l0b3JfaWQiOiI1NzU4OTgzNjk0NjI2ODIzODg5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 705ff6cd5decdf46cd1771db1640c5c0eef7c4c9c8abd97691ae9b78318242e3 |
| hovercard-subject-tag | pull_request:3167609605 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,checks,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/CodebuffAI/codebuff/pull/408/checks |
| twitter:image | https://avatars.githubusercontent.com/u/9735006?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/9735006?s=400&v=4 |
| og:image:alt | Non-Test Files Changed (22 files, +1,393 / -157 lines) File +/- Description TESTING.md +518 New comprehensive testing guide common/src/types/contracts/billing.ts +334 New DI contract types... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 60279d4097367e16897439d16d6bbe4180663db828c666eeed2656988ffe59f6 |
| turbo-cache-control | no-preview |
| go-import | github.com/CodebuffAI/codebuff git https://github.com/CodebuffAI/codebuff.git |
| octolytics-dimension-user_id | 189203002 |
| octolytics-dimension-user_login | CodebuffAI |
| octolytics-dimension-repository_id | 826515105 |
| octolytics-dimension-repository_nwo | CodebuffAI/codebuff |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 826515105 |
| octolytics-dimension-repository_network_root_nwo | CodebuffAI/codebuff |
| turbo-body-classes | logged-out env-production page-responsive full-width full-width-p-0 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7c85641c598ad130c74f7bcc27f58575cac69551 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width