-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat(billing): plan source-of-truth + plan seed + meterExempt Zod (PR-N6) #3555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PierreBrisorgueil
merged 4 commits into
master
from
feat/billing-pr-n6-plan-source-of-truth
May 1, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
04f1465
feat(billing): plan source-of-truth + plan seed + meterExempt Zod (PR…
PierreBrisorgueil 9e71c74
fix(billing): lean subscription lookup + E11000-safe ensureSeeded + f…
PierreBrisorgueil b7a0189
test(billing): add coverage for billing.init fail-fast + findPlan guard
PierreBrisorgueil b61a7f3
fix(billing): add enterprise to planDefinitions
PierreBrisorgueil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
modules/billing/tests/billing.plan.ensureSeeded.unit.tests.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /** | ||
| * Module dependencies. | ||
| */ | ||
| import { jest, describe, test, beforeEach, afterEach, expect } from '@jest/globals'; | ||
|
|
||
| /** | ||
| * Unit tests for BillingPlanService.ensureSeeded | ||
| */ | ||
| describe('BillingPlanService.ensureSeeded unit tests:', () => { | ||
| let BillingPlanService; | ||
| let mockBillingPlanRepository; | ||
| let mockConfig; | ||
|
|
||
| beforeEach(async () => { | ||
| jest.resetModules(); | ||
|
|
||
| mockConfig = { | ||
| billing: { | ||
| meterMode: true, | ||
| planDefinitions: {}, | ||
| }, | ||
| }; | ||
|
|
||
| mockBillingPlanRepository = { | ||
| findActive: jest.fn(), | ||
| findByVersion: jest.fn(), | ||
| deactivateAll: jest.fn(), | ||
| count: jest.fn(), | ||
| create: jest.fn(), | ||
| }; | ||
|
|
||
| jest.unstable_mockModule('../../../config/index.js', () => ({ | ||
| default: mockConfig, | ||
| })); | ||
|
|
||
| jest.unstable_mockModule('../repositories/billing.plan.repository.js', () => ({ | ||
| default: mockBillingPlanRepository, | ||
| })); | ||
|
|
||
| const mod = await import('../services/billing.plan.service.js'); | ||
| BillingPlanService = mod.default; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test('meterMode=false returns zeroes and never touches the repository', async () => { | ||
| mockConfig.billing.meterMode = false; | ||
|
|
||
| const result = await BillingPlanService.ensureSeeded(); | ||
|
|
||
| expect(result).toEqual({ seeded: 0, skipped: 0 }); | ||
| expect(mockBillingPlanRepository.findActive).not.toHaveBeenCalled(); | ||
| expect(mockBillingPlanRepository.create).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('meterMode=true with empty planDefinitions returns zeroes', async () => { | ||
| const result = await BillingPlanService.ensureSeeded(); | ||
|
|
||
| expect(result).toEqual({ seeded: 0, skipped: 0 }); | ||
| expect(mockBillingPlanRepository.findActive).not.toHaveBeenCalled(); | ||
| expect(mockBillingPlanRepository.create).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('meterMode=true with 3 planDefinitions and none existing seeds 3 plans', async () => { | ||
| mockConfig.billing.planDefinitions = { | ||
| free: { meterQuota: 0, ratios: { default: 1 } }, | ||
| starter: { meterQuota: 50000, ratios: { default: 1 } }, | ||
| pro: { meterQuota: 500000, ratios: { default: 2 } }, | ||
| }; | ||
| mockBillingPlanRepository.findActive.mockResolvedValue(null); | ||
| // count=0 → version 'v1' for each plan | ||
| mockBillingPlanRepository.count.mockResolvedValue(0); | ||
| mockBillingPlanRepository.create.mockResolvedValue({}); | ||
|
|
||
| const result = await BillingPlanService.ensureSeeded(); | ||
|
|
||
| expect(result).toEqual({ seeded: 3, skipped: 0 }); | ||
| expect(mockBillingPlanRepository.findActive).toHaveBeenCalledTimes(3); | ||
| expect(mockBillingPlanRepository.count).toHaveBeenCalledTimes(3); | ||
| expect(mockBillingPlanRepository.create).toHaveBeenCalledTimes(3); | ||
| expect(mockBillingPlanRepository.create).toHaveBeenNthCalledWith( | ||
| 1, | ||
| expect.objectContaining({ planId: 'free', version: 'v1', meterQuota: 0, ratios: { default: 1 }, active: true }), | ||
| ); | ||
| }); | ||
|
|
||
| test('meterMode=true with 3 planDefinitions and 1 existing seeds 2 and skips 1', async () => { | ||
| mockConfig.billing.planDefinitions = { | ||
| free: { meterQuota: 0, ratios: { default: 1 } }, | ||
| starter: { meterQuota: 50000, ratios: { default: 1 } }, | ||
| pro: { meterQuota: 500000, ratios: { default: 1 } }, | ||
| }; | ||
| mockBillingPlanRepository.findActive | ||
| .mockResolvedValueOnce({ planId: 'free', version: 'v1' }) | ||
| .mockResolvedValueOnce(null) | ||
| .mockResolvedValueOnce(null); | ||
| mockBillingPlanRepository.count.mockResolvedValue(0); | ||
| mockBillingPlanRepository.create.mockResolvedValue({}); | ||
|
|
||
| const result = await BillingPlanService.ensureSeeded(); | ||
|
|
||
| expect(result).toEqual({ seeded: 2, skipped: 1 }); | ||
| expect(mockBillingPlanRepository.create).toHaveBeenCalledTimes(2); | ||
| expect(mockBillingPlanRepository.create).toHaveBeenNthCalledWith( | ||
| 1, | ||
| expect.objectContaining({ planId: 'starter', version: 'v1' }), | ||
| ); | ||
| expect(mockBillingPlanRepository.create).toHaveBeenNthCalledWith( | ||
| 2, | ||
| expect.objectContaining({ planId: 'pro', version: 'v1' }), | ||
| ); | ||
| }); | ||
|
|
||
| test('count > 0 yields next version string correctly', async () => { | ||
| mockConfig.billing.planDefinitions = { | ||
| pro: { meterQuota: 500000, ratios: { default: 1 } }, | ||
| }; | ||
| mockBillingPlanRepository.findActive.mockResolvedValue(null); | ||
| // Existing inactive v1 is present (total count = 1) | ||
| mockBillingPlanRepository.count.mockResolvedValue(1); | ||
| mockBillingPlanRepository.create.mockResolvedValue({}); | ||
|
|
||
| const result = await BillingPlanService.ensureSeeded(); | ||
|
|
||
| expect(result).toEqual({ seeded: 1, skipped: 0 }); | ||
| expect(mockBillingPlanRepository.create).toHaveBeenCalledWith( | ||
| expect.objectContaining({ planId: 'pro', version: 'v2' }), | ||
| ); | ||
| }); | ||
|
|
||
| test('E11000 on create is absorbed as a skip (concurrent pod race)', async () => { | ||
| mockConfig.billing.planDefinitions = { | ||
| free: { meterQuota: 0, ratios: { default: 1 } }, | ||
| pro: { meterQuota: 500000, ratios: { default: 1 } }, | ||
| }; | ||
| mockBillingPlanRepository.findActive.mockResolvedValue(null); | ||
| mockBillingPlanRepository.count.mockResolvedValue(0); | ||
| // First create throws E11000; second succeeds | ||
| const e11000 = Object.assign(new Error('E11000 duplicate key error'), { code: 11000 }); | ||
| mockBillingPlanRepository.create.mockRejectedValueOnce(e11000).mockResolvedValueOnce({}); | ||
|
|
||
| const result = await BillingPlanService.ensureSeeded(); | ||
|
|
||
| expect(result).toEqual({ seeded: 1, skipped: 1 }); | ||
| }); | ||
|
|
||
| test('non-E11000 error on create propagates and aborts', async () => { | ||
| mockConfig.billing.planDefinitions = { | ||
| pro: { meterQuota: 500000, ratios: { default: 1 } }, | ||
| }; | ||
| mockBillingPlanRepository.findActive.mockResolvedValue(null); | ||
| mockBillingPlanRepository.count.mockResolvedValue(0); | ||
| mockBillingPlanRepository.create.mockRejectedValue(new Error('DB connection lost')); | ||
|
|
||
| await expect(BillingPlanService.ensureSeeded()).rejects.toThrow('DB connection lost'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.