-
Notifications
You must be signed in to change notification settings - Fork 0
allow past a zero credit balance when overage is on #30
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
Merged
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| package rulesengine_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/schematichq/rulesengine" | ||
| "github.com/schematichq/rulesengine/null" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // SCHX-582: credit overage. With overage enabled on a credit, consumption | ||
| // continues past a zero balance and accrues at a configured rate, so the balance | ||
| // stops gating the check. | ||
| // | ||
| // These mirror credit_overage_tests in rulesengine-rust; the two engines must | ||
| // agree (SCHY-515) for as long as both are in use. | ||
| func TestCreditOverage(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| const creditID = "test-credit-id" | ||
|
|
||
| companyWith := func(balance float64, overage *bool) *rulesengine.Company { | ||
| company := createTestCompany() | ||
| company.CreditBalances = map[string]float64{creditID: balance} | ||
| if overage != nil { | ||
| company.CreditOverageEnabled = map[string]bool{creditID: *overage} | ||
| } | ||
| return company | ||
| } | ||
|
|
||
| creditRule := func() *rulesengine.Rule { | ||
| rule := createTestRule() | ||
| condition := createTestCondition(rulesengine.ConditionTypeCredit) | ||
| condition.CreditID = null.Nullable(creditID) | ||
| condition.ConsumptionRate = null.Nullable(1.0) | ||
| rule.Conditions = []*rulesengine.Condition{condition} | ||
| return rule | ||
| } | ||
|
|
||
| matches := func(t *testing.T, company *rulesengine.Company) bool { | ||
| t.Helper() | ||
|
|
||
| result, err := rulesengine.NewRuleCheckService().Check(ctx, &rulesengine.CheckScope{ | ||
| Company: company, | ||
| Rule: creditRule(), | ||
| }) | ||
| require.NoError(t, err) | ||
| return result.Match | ||
| } | ||
|
|
||
| // The existing hard stop, unchanged when nobody has opted in. | ||
| t.Run("denies at zero without overage", func(t *testing.T) { | ||
| assert.False(t, matches(t, companyWith(0, nil))) | ||
| }) | ||
|
|
||
| t.Run("allows with balance", func(t *testing.T) { | ||
| assert.True(t, matches(t, companyWith(5, nil))) | ||
| }) | ||
|
|
||
| // The point of the feature. | ||
| t.Run("allows past zero with overage enabled", func(t *testing.T) { | ||
| assert.True(t, matches(t, companyWith(0, null.Nullable(true)))) | ||
| }) | ||
|
|
||
| // A negative balance is legal (SCH-5103); overage is what makes it billable, | ||
| // and an already-overdrafted company must keep working. | ||
| t.Run("allows when already negative", func(t *testing.T) { | ||
| assert.True(t, matches(t, companyWith(-40, null.Nullable(true)))) | ||
| }) | ||
|
|
||
| // Explicitly false must behave exactly like absent, or switching the setting | ||
| // back off would not restore the hard stop. | ||
| t.Run("explicit false still denies", func(t *testing.T) { | ||
| assert.False(t, matches(t, companyWith(0, null.Nullable(false)))) | ||
| }) | ||
|
|
||
| // Overage is per credit: enabling it on one must not unblock another. | ||
| t.Run("does not leak across credits", func(t *testing.T) { | ||
| company := companyWith(0, null.Nullable(true)) | ||
| company.CreditBalances["other-credit-id"] = 0 | ||
|
|
||
| rule := createTestRule() | ||
| condition := createTestCondition(rulesengine.ConditionTypeCredit) | ||
| condition.CreditID = null.Nullable("other-credit-id") | ||
| condition.ConsumptionRate = null.Nullable(1.0) | ||
| rule.Conditions = []*rulesengine.Condition{condition} | ||
|
|
||
| result, err := rulesengine.NewRuleCheckService().Check(ctx, &rulesengine.CheckScope{ | ||
| Company: company, | ||
| Rule: rule, | ||
| }) | ||
| require.NoError(t, err) | ||
| assert.False(t, result.Match, "the other credit has no overage and no balance") | ||
| }) | ||
|
|
||
| // Overage has to beat the more specific branches too. A caller-supplied | ||
| // credit cost would otherwise re-impose the balance gate it short-circuits. | ||
| t.Run("overrides the credit cost option", func(t *testing.T) { | ||
| flag := createTestFlag() | ||
| // Pinned false: createTestFlag randomizes DefaultValue, and CheckFlag falls | ||
| // back to it when no rule matches — so a true default would let this pass | ||
| // without the rule ever matching. | ||
| flag.DefaultValue = false | ||
| flag.Rules = []*rulesengine.Rule{creditRule()} | ||
|
|
||
| result, err := rulesengine.CheckFlag( | ||
| ctx, | ||
| companyWith(0, null.Nullable(true)), | ||
| nil, | ||
| flag, | ||
| rulesengine.WithCreditCost(creditID, 25), | ||
| ) | ||
| require.NoError(t, err) | ||
| assert.True(t, result.Value) | ||
| }) | ||
| } |
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.
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.
If we need to have a map, document what the [string] is here.
But do we need a map here? Could we have a slice of credit ids instead? It's a narrower set of states.
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.
i'll add the documentation, but i'm going to keep it as a map because to match the rust rulesengine.