-
Notifications
You must be signed in to change notification settings - Fork 189
feat: apply unit config in rating via mutator - OM-397 #4616
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
Open
rolosp
wants to merge
3
commits into
main
Choose a base branch
from
feat/om-397-apply-unit-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+987
−48
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
174 changes: 174 additions & 0 deletions
174
openmeter/billing/charges/service/unitconfig_rating_test.go
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,174 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/alpacahq/alpacadecimal" | ||
| "github.com/samber/lo" | ||
| "github.com/stretchr/testify/suite" | ||
|
|
||
| "github.com/openmeterio/openmeter/openmeter/billing" | ||
| "github.com/openmeterio/openmeter/openmeter/billing/charges" | ||
| "github.com/openmeterio/openmeter/openmeter/billing/rating/service/mutator" | ||
| "github.com/openmeterio/openmeter/openmeter/productcatalog" | ||
| "github.com/openmeterio/openmeter/pkg/clock" | ||
| "github.com/openmeterio/openmeter/pkg/datetime" | ||
| "github.com/openmeterio/openmeter/pkg/timeutil" | ||
| billingtest "github.com/openmeterio/openmeter/test/billing" | ||
| ) | ||
|
|
||
| // These suites drive the REAL charges path end-to-end: a usage-based charge whose | ||
| // intent carries a unit_config is created, usage is seeded, and the charge is | ||
| // invoiced — exercising rating.GenerateDetailedLines via RateableIntent.GetUnitConfig. | ||
| // The enabled/disabled split proves the unitConfig.enabled flag actually gates the | ||
| // converted amount through the production wiring (not just the in-memory mutator). | ||
|
|
||
| func TestUsageBasedUnitConfigRatingEnabled(t *testing.T) { | ||
| suite.Run(t, new(unitConfigRatingEnabledSuite)) | ||
| } | ||
|
|
||
| type unitConfigRatingEnabledSuite struct { | ||
| BaseSuite | ||
| } | ||
|
|
||
| func (s *unitConfigRatingEnabledSuite) SetupSuite() { | ||
| s.UnitConfigEnabled = true | ||
| s.BaseSuite.SetupSuite() | ||
| } | ||
|
|
||
| func (s *unitConfigRatingEnabledSuite) TearDownTest() { | ||
| s.BaseSuite.TearDownTest() | ||
| } | ||
|
|
||
| func (s *unitConfigRatingEnabledSuite) TestRatesConvertedQuantity() { | ||
| // flag on: 7400 raw / 1000, ceiling => 8 billed units * $1 = $8. | ||
| invoices, err := s.invoiceUnitConfigChargesScenario() | ||
| s.Require().NoError(err) | ||
| s.Require().Len(invoices, 1) | ||
| s.Require().Len(invoices[0].Lines.OrEmpty(), 1) | ||
|
|
||
| stdLine := invoices[0].Lines.OrEmpty()[0] | ||
|
|
||
| // MeteredQuantity records the raw metered value and stays 7400: the unit_config | ||
| // conversion changes the priced amount (asserted below), not the recorded metered | ||
| // quantity. | ||
| // | ||
| // TODO: once the charges line-mapper converts+rounds the displayed UsageBased.Quantity, | ||
| // extend this test to also assert the converted displayed quantity. | ||
| // MeteredQuantity stays raw (7400) as the audit value. | ||
| s.Require().NotNil(stdLine.UsageBased.MeteredQuantity) | ||
| s.Equal(float64(7400), lo.FromPtr(stdLine.UsageBased.MeteredQuantity).InexactFloat64()) | ||
|
|
||
| s.RequireTotals(billingtest.ExpectedTotals{ | ||
| Amount: 8, | ||
| Total: 8, | ||
| }, stdLine.Totals) | ||
| } | ||
|
|
||
| func TestUsageBasedUnitConfigRatingDisabled(t *testing.T) { | ||
| suite.Run(t, new(unitConfigRatingDisabledSuite)) | ||
| } | ||
|
|
||
| type unitConfigRatingDisabledSuite struct { | ||
| BaseSuite | ||
| } | ||
|
|
||
| func (s *unitConfigRatingDisabledSuite) SetupSuite() { | ||
| // UnitConfigEnabled defaults to false: the intent still carries a unit_config, so | ||
| // ForbidUnitConfig must reject rating rather than silently bill the raw quantity. | ||
| s.BaseSuite.SetupSuite() | ||
| } | ||
|
|
||
| func (s *unitConfigRatingDisabledSuite) TearDownTest() { | ||
| s.BaseSuite.TearDownTest() | ||
| } | ||
|
|
||
| func (s *unitConfigRatingDisabledSuite) TestErrorsWhenFlagOff() { | ||
| // flag off + a unit_config on the charge: ForbidUnitConfig surfaces the | ||
| // inconsistency instead of silently billing the raw quantity. | ||
| _, err := s.invoiceUnitConfigChargesScenario() | ||
| s.Require().Error(err) | ||
| s.Require().ErrorIs(err, mutator.ErrUnitConfigDisabled) | ||
| } | ||
|
|
||
| // invoiceUnitConfigChargesScenario creates a usage-based charge carrying a divide-by-1000 | ||
| // ceiling unit_config, seeds 7400 raw units, and invoices mid-period. It returns the | ||
| // InvoicePendingLines result so each suite asserts its own outcome (converted amount | ||
| // when the flag is on, a ForbidUnitConfig error when it is off). | ||
| func (s *BaseSuite) invoiceUnitConfigChargesScenario() ([]billing.StandardInvoice, error) { | ||
| s.T().Helper() | ||
|
|
||
| ctx := s.T().Context() | ||
| ns := s.GetUniqueNamespace("charges-service-unit-config-rating") | ||
| s.ProvisionDefaultTaxCodes(ctx, ns) | ||
|
|
||
| customInvoicing := s.SetupCustomInvoicing(ns) | ||
|
|
||
| cust := s.CreateTestCustomer(ns, "test-subject") | ||
| s.NotEmpty(cust.ID) | ||
|
|
||
| _ = s.ProvisionBillingProfile( | ||
| ctx, ns, customInvoicing.App.GetID(), | ||
| billingtest.WithProgressiveBilling(), | ||
| billingtest.WithCollectionInterval(datetime.MustParseDuration(s.T(), "P2D")), | ||
| billingtest.WithManualApproval(), | ||
| ) | ||
|
|
||
| createAt := datetime.MustParseTimeInLocation(s.T(), "2025-12-01T00:00:00Z", time.UTC).AsTime() | ||
| servicePeriod := timeutil.ClosedPeriod{ | ||
| From: datetime.MustParseTimeInLocation(s.T(), "2026-01-01T00:00:00Z", time.UTC).AsTime(), | ||
| To: datetime.MustParseTimeInLocation(s.T(), "2026-02-01T00:00:00Z", time.UTC).AsTime(), | ||
| } | ||
| invoiceAt := datetime.MustParseTimeInLocation(s.T(), "2026-01-16T00:00:00Z", time.UTC).AsTime() | ||
|
|
||
| apiRequestsTotal := s.SetupApiRequestsTotalFeature(ctx, ns) | ||
| meterSlug := apiRequestsTotal.Feature.Key | ||
|
|
||
| clock.FreezeTime(createAt) | ||
| defer clock.UnFreeze() | ||
| defer s.UsageBasedTestHandler.Reset() | ||
|
|
||
| // Cap credit-only accrual at 0 so the full amount is invoiced (no credits). | ||
| s.UsageBasedTestHandler.onCreditsOnlyUsageAccrued, _ = newCappedCreditAllocator(0) | ||
|
|
||
| // Meter is in raw units, bill in thousands: divide by 1000, round up. | ||
| unitConfig := &productcatalog.UnitConfig{ | ||
| Operation: productcatalog.UnitConfigOperationDivide, | ||
| ConversionFactor: alpacadecimal.NewFromInt(1000), | ||
| Rounding: productcatalog.UnitConfigRoundingModeCeiling, | ||
| } | ||
|
|
||
| res, err := s.Charges.Create(ctx, charges.CreateInput{ | ||
| Namespace: ns, | ||
| Intents: []charges.ChargeIntent{ | ||
| s.createMockChargeIntent(createMockChargeIntentInput{ | ||
| customer: cust.GetID(), | ||
| currency: USD, | ||
| servicePeriod: servicePeriod, | ||
| settlementMode: productcatalog.CreditThenInvoiceSettlementMode, | ||
| price: productcatalog.NewPriceFrom(productcatalog.UnitPrice{Amount: alpacadecimal.NewFromFloat(1)}), | ||
| unitConfig: unitConfig, | ||
| name: "usage-based-unit-config", | ||
| managedBy: billing.SubscriptionManagedLine, | ||
| uniqueReferenceID: "usage-based-unit-config", | ||
| featureKey: meterSlug, | ||
| }), | ||
| }, | ||
| }) | ||
| s.Require().NoError(err) | ||
| s.Require().Len(res, 1) | ||
|
|
||
| // 7400 raw units. Flag on: ceil(7400/1000) = 8 billed units. | ||
| s.MockStreamingConnector.AddSimpleEvent( | ||
| meterSlug, | ||
| 7400, | ||
| datetime.MustParseTimeInLocation(s.T(), "2026-01-15T00:00:00Z", time.UTC).AsTime(), | ||
| ) | ||
| clock.FreezeTime(invoiceAt) | ||
|
|
||
| return s.BillingService.InvoicePendingLines(ctx, billing.InvoicePendingLinesInput{ | ||
| Customer: cust.GetID(), | ||
| AsOf: lo.ToPtr(invoiceAt), | ||
| }) | ||
| } |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Subscription charges drop unit_config before rating
This new rating path only sees unit_config through RateableIntent.GetUnitConfig(), but normal subscription-created usage-based charges still never populate Intent.UnitConfig: newUsageBasedChargeIntent in openmeter/billing/worker/subscriptionsync/service/reconciler/patchchargeusagebased.go copies price and discounts from rateCardMeta but leaves UnitConfig behind. With unitConfig.enabled on, a plan/subscription rate card using unit_config will still invoice raw metered quantity, while only direct charge-intent tests exercise the converted path. Please snapshot rateCardMeta.UnitConfig into the usage-based charge intent, with a subscription round-trip/invoice test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, newUsageBasedChargeIntent now snapshots rateCardMeta.UnitConfig onto the usage-based charge intent