Skip to content

test: stabilize cost discount spec - #1938

Merged
steebchen merged 1 commit into
mainfrom
fix-cost-discount-test
Mar 30, 2026
Merged

steebchen merged 1 commit into
mainfrom
fix-cost-discount-test

Conversation

@steebchen

@steebchen steebchen commented Mar 30, 2026 •

Copy link
Copy Markdown
Member

Summary

  • mock effective discount lookup in costs unit tests so they do not depend on seeded DB state
  • preserve hardcoded provider discounts as the default mocked behavior
  • explicitly assert the 10% OpenAI discount case in the discount-specific spec

Verification

  • pnpm vitest run apps/gateway/src/lib/costs.spec.ts --no-file-parallelism
  • pnpm format
  • pnpm build

Summary by CodeRabbit

  • Tests
    • Improved test coverage for discount calculations with enhanced mocking infrastructure and more comprehensive assertions to verify discount application, cost calculations, and proper system behavior.

Copilot AI review requested due to automatic review settings March 30, 2026 13:43
@coderabbitai

coderabbitai Bot commented Mar 30, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 4d581108-94c1-4c53-899c-6918ddd6fb4e

📥 Commits

Reviewing files that changed from the base of the PR and between d1ba037 and 6821044.

📒 Files selected for processing (1)
  • apps/gateway/src/lib/costs.spec.ts

Walkthrough

Test refactoring in costs.spec.ts to improve discount-related test coverage. Introduces centralized mock management via beforeEach hook and replaces hardcoded discount assertions with controlled mock return values.

Changes

Cohort / File(s) Summary
Test Setup & Mock Management
apps/gateway/src/lib/costs.spec.ts
Added Vitest imports (beforeEach, vi), implemented hoisted mockGetEffectiveDiscount function, added module mock for @llmgateway/db, and introduced beforeEach hook for mock reset and default behavior configuration.
Discount Test Assertion
apps/gateway/src/lib/costs.spec.ts
Refactored "should apply discount when model has discount field" test to use controlled mock return values (discount: 0.1, source, discountId) and assert resulting cost calculations (discount, inputCost, outputCost, totalCost) and mock call arguments.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: stabilizing/fixing flaky cost discount unit tests through mocking.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-cost-discount-test

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Stabilizes calculateCosts unit tests by removing reliance on seeded DB discount state and explicitly controlling discount behavior via a Vitest module mock.

Changes:

  • Mock @llmgateway/db#getEffectiveDiscount in costs.spec.ts using a hoisted mock to avoid module-load ordering issues.
  • Provide a default mock implementation that preserves hardcoded (model/provider mapping) discounts.
  • Add an explicit assertion for a 10% OpenAI discount scenario.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -133,33 +151,31 @@ describe("calculateCosts", () => {
});

it("should apply discount when model has discount field", async () => {

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

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

The test name says "when model has discount field", but the test is actually validating that a discount returned by getEffectiveDiscount (DB/global provider discount) is applied. Renaming the test (and/or adjusting the description) would avoid confusion about what behavior is under test.

Suggested change
it("should apply discount when model has discount field", async () => {
it("should apply discount returned by getEffectiveDiscount (e.g., global provider discount)", async () => {

Copilot uses AI. Check for mistakes.
beforeEach(() => {
vi.resetAllMocks();
vi.mocked(mockGetEffectiveDiscount).mockImplementation(
async (_organizationId, _provider, _model, hardcodedDiscount = 0) => ({

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

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

The default mock implementation for getEffectiveDiscount only declares 4 parameters, but the real function takes a 5th (providerModelName). It works because extra args are ignored, but matching the full signature (even if unused) will keep the mock aligned with production code and make call expectations clearer.

Suggested change
async (_organizationId, _provider, _model, hardcodedDiscount = 0) => ({
async (
_organizationId,
_provider,
_model,
hardcodedDiscount = 0,
_providerModelName,
) => ({

Copilot uses AI. Check for mistakes.
@steebchen
steebchen merged commit cd52995 into main Mar 30, 2026
20 of 21 checks passed
@steebchen
steebchen deleted the fix-cost-discount-test branch March 30, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants