Skip to content

chore: split chages testcases - #3960

Merged
turip merged 2 commits into
mainfrom
chore/refactor-split-chages-testcases
Mar 17, 2026
Merged

turip merged 2 commits into
mainfrom
chore/refactor-split-chages-testcases

Conversation

@turip

@turip turip commented Mar 17, 2026 •

Copy link
Copy Markdown
Member

Overview

Improves the categorization of the testcases.

Notes for reviewer

Summary by CodeRabbit

  • Tests
    • Added a shared test foundation for the charges service with setup/teardown and mock charge utilities.
    • Streamlined and renamed the credit-purchase test suite for clearer focus and reduced setup duplication.
    • Added an end-to-end invoicable-charges test suite covering charge creation, invoice generation, credit realization, authorization, and settlement.

@turip
turip requested a review from a team as a code owner March 17, 2026 14:59
@coderabbitai

coderabbitai Bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 64f57a11-948c-4abf-9534-7dee04d571fa

📥 Commits

Reviewing files that changed from the base of the PR and between 614e9d0 and 3462978.

📒 Files selected for processing (3)
  • openmeter/billing/charges/service/base_test.go
  • openmeter/billing/charges/service/creditpurchase_test.go
  • openmeter/billing/charges/service/invoicable_test.go

📝 Walkthrough

Walkthrough

Adds a reusable BaseSuite test scaffold for charges, refactors credit-purchase tests to embed it, and introduces a new InvoicableChargesTestSuite with end-to-end flat-fee + credit-realization invoice/payment flows.

Changes

Cohort / File(s) Summary
Test scaffolding
openmeter/billing/charges/service/base_test.go
Adds BaseSuite with SetupSuite/TeardownTest, in-memory adapters wiring, helpers for creating validated mock charge intents (createMockChargeIntentInput) and mustGetChargeByID, and a USD currency constant.
Test refactoring
openmeter/billing/charges/service/creditpurchase_test.go
Replaces ChargesServiceTestSuite with CreditPurchaseTestSuite embedding BaseSuite; removes duplicated setup/config code, updates test entry point (TestCreditPurchase) and delegates teardown to BaseSuite.
New test coverage
openmeter/billing/charges/service/invoicable_test.go
Adds InvoicableChargesTestSuite and TestInvoicableCharges with E2E tests for flat-fee charge creation, invoice generation (partial credit realization), authorization (ledger/assert callbacks), and payment settlement flows.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

area/billing

Suggested reviewers

  • GAlexIHU
  • tothandras
🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title contains a typo ('chages' instead of 'charges') and is vague about what's being split, making it unclear what the main change accomplishes. Consider revising to 'chore: split charges test cases' and be more specific about the refactoring goal, such as 'chore: extract base test suite for charges service' to better convey the organizational improvement.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/refactor-split-chages-testcases
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can disable the changed files summary in the walkthrough.

Disable the reviews.changed_files_summary setting to disable the changed files summary in the walkthrough.

@turip turip added the release-note/ignore Ignore this change when generating release notes label Mar 17, 2026
@turip
turip requested review from GAlexIHU and tothandras March 17, 2026 15:00
tothandras
tothandras previously approved these changes Mar 17, 2026
@turip
turip enabled auto-merge (squash) March 17, 2026 15:01

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
openmeter/billing/charges/service/base_test.go (1)

112-130: Validate servicePeriod in helper input for earlier and clearer failures.

createMockChargeIntentInput.Validate() currently skips period validation, so invalid ranges fail deeper with less actionable errors.

Suggested patch
 func (i *createMockChargeIntentInput) Validate() error {
 	if i.price == nil {
 		return errors.New("price is required")
 	}
@@
 	if i.currency == "" {
 		return errors.New("currency is required")
 	}
+
+	if err := i.servicePeriod.Validate(); err != nil {
+		return err
+	}
 
 	return nil
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/billing/charges/service/base_test.go` around lines 112 - 130, The
helper Validate method for createMockChargeIntentInput is missing checks for the
servicePeriod; update createMockChargeIntentInput.Validate() to verify that
servicePeriod is not nil, that servicePeriod.Start and servicePeriod.End are set
(non-zero), and that Start is before End (or End is after Start), returning
clear errors like "servicePeriod is required", "servicePeriod start is
required", "servicePeriod end is required", or "servicePeriod start must be
before end" to fail fast and produce actionable messages.
openmeter/billing/charges/service/invoicable_test.go (1)

115-115: Please replace this TODO with concrete assertions (periods/price) in this flow.

This scenario is the core invoicable path, so asserting only charge/link presence leaves a blind spot for regression on period and pricing mapping.

If you want, I can draft the exact assertions for service/billing/full period and price fields in this subtest.
As per coding guidelines: "**/*_test.go: Make sure the tests are comprehensive and cover the changes."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/billing/charges/service/invoicable_test.go` at line 115, Replace
the TODO with concrete assertions that validate the invoicable charge's period
and pricing fields: locate the core invoicable subtest in invoicable_test.go and
after you assert presence of charge and link, add assertions on
charge.Period.Start and charge.Period.End (or the equivalent Start/End fields)
to match the expected start/end timestamps for the full billing period, and
assert pricing fields such as charge.Price.Amount (or UnitPrice/Total) and
charge.Price.Currency equal the expected values; use the same test
helpers/assert style present in the file (e.g., require.Equal or assert.Equal)
so failures show exact mismatches for period and price.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@openmeter/billing/charges/service/invoicable_test.go`:
- Around line 69-70: The test calls clock.SetTime(servicePeriod.From) which
mutates global clock state and is not restored; modify the test so it saves the
previous clock value before calling clock.SetTime and ensures restoration at
test end (e.g., defer restoring the saved time or invoking the package’s clock
reset helper) so global clock state is reset after the test; locate the mutation
by looking for clock.SetTime and servicePeriod.From in invoicable_test.go and
add the corresponding save+defer restore (or call the package reset) immediately
after setting the time.

---

Nitpick comments:
In `@openmeter/billing/charges/service/base_test.go`:
- Around line 112-130: The helper Validate method for
createMockChargeIntentInput is missing checks for the servicePeriod; update
createMockChargeIntentInput.Validate() to verify that servicePeriod is not nil,
that servicePeriod.Start and servicePeriod.End are set (non-zero), and that
Start is before End (or End is after Start), returning clear errors like
"servicePeriod is required", "servicePeriod start is required", "servicePeriod
end is required", or "servicePeriod start must be before end" to fail fast and
produce actionable messages.

In `@openmeter/billing/charges/service/invoicable_test.go`:
- Line 115: Replace the TODO with concrete assertions that validate the
invoicable charge's period and pricing fields: locate the core invoicable
subtest in invoicable_test.go and after you assert presence of charge and link,
add assertions on charge.Period.Start and charge.Period.End (or the equivalent
Start/End fields) to match the expected start/end timestamps for the full
billing period, and assert pricing fields such as charge.Price.Amount (or
UnitPrice/Total) and charge.Price.Currency equal the expected values; use the
same test helpers/assert style present in the file (e.g., require.Equal or
assert.Equal) so failures show exact mismatches for period and price.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a049301a-aec8-41a2-a0e3-03206a9b4ff6

📥 Commits

Reviewing files that changed from the base of the PR and between 4f30760 and 614e9d0.

📒 Files selected for processing (3)
  • openmeter/billing/charges/service/base_test.go
  • openmeter/billing/charges/service/creditpurchase_test.go
  • openmeter/billing/charges/service/invoicable_test.go

Comment on lines +69 to +70
clock.SetTime(servicePeriod.From)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Reset the test clock after mutation to avoid cross-test leakage.

clock.SetTime(...) mutates global state, but this test never restores it. That can make later tests flaky depending on execution order. Please add cleanup to restore the previous clock value (or use the package’s reset helper) at test end.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/billing/charges/service/invoicable_test.go` around lines 69 - 70,
The test calls clock.SetTime(servicePeriod.From) which mutates global clock
state and is not restored; modify the test so it saves the previous clock value
before calling clock.SetTime and ensures restoration at test end (e.g., defer
restoring the saved time or invoking the package’s clock reset helper) so global
clock state is reset after the test; locate the mutation by looking for
clock.SetTime and servicePeriod.From in invoicable_test.go and add the
corresponding save+defer restore (or call the package reset) immediately after
setting the time.

turip added 2 commits March 17, 2026 16:20
Improves the categorization of the testcases.
@turip
turip force-pushed the chore/refactor-split-chages-testcases branch from 4cd91e5 to 3462978 Compare March 17, 2026 15:20
@turip
turip disabled auto-merge March 17, 2026 15:21
@turip
turip merged commit 3e8da3c into main Mar 17, 2026
20 checks passed
@turip
turip deleted the chore/refactor-split-chages-testcases branch March 17, 2026 15:21

This branch was previously deployed

1 inactive deployment
dev — 34629787 Deployed Mar 17, 2026 by turip via Migration Checks #12596
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/ignore Ignore this change when generating release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants