Skip to content

feat: add unit tests for BalancesService and enhance wallet limit refresh logic#2689

Open
baktun14 wants to merge 1 commit intomainfrom
fix/trial-flag
Open

feat: add unit tests for BalancesService and enhance wallet limit refresh logic#2689
baktun14 wants to merge 1 commit intomainfrom
fix/trial-flag

Conversation

@baktun14
Copy link
Contributor

@baktun14 baktun14 commented Feb 8, 2026

Summary by CodeRabbit

  • Bug Fixes

    • Improved trial period cleanup by ensuring trial-related background jobs are properly canceled when trials end.
    • Enhanced wallet limit updates to reliably persist changes during trial transitions.
  • Tests

    • Added comprehensive test coverage for wallet limit refresh and trial management scenarios.
    • Expanded job queue service tests for job cancellation operations.

@baktun14 baktun14 requested a review from a team as a code owner February 8, 2026 04:57
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 8, 2026

📝 Walkthrough

Walkthrough

This change implements trial job cancellation functionality in the BalancesService. When ending user trials, the system now cancels trial-related background jobs through a new pattern-based cancellation method in JobQueueService, with comprehensive test coverage across all affected components.

Changes

Cohort / File(s) Summary
Trial Job Cancellation
apps/api/src/billing/services/balances/balances.service.ts, apps/api/src/billing/services/balances/balances.service.spec.ts
Adds JobQueueService dependency, implements cancelTrialJobsByWalletId method to cancel CloseTrialDeployment and Notification jobs, and integrates cancellation into refreshUserWalletLimits flow. Includes test suite validating cancellation behavior, limit updates, and trial flag clearing.
Job Queue Pattern Cancellation
apps/api/src/core/services/job-queue/job-queue.service.ts, apps/api/src/core/services/job-queue/job-queue.service.spec.ts
Adds public method cancelCreatedByPattern for LIKE-based job cancellation, enabling pattern-matched job termination. Tests verify SQL execution with pattern matching, job ID logging, and empty result handling for both cancelCreatedBy and cancelCreatedByPattern methods.

Sequence Diagram(s)

sequenceDiagram
    participant BS as BalancesService
    participant JQS as JobQueueService
    participant DB as Database

    BS->>BS: refreshUserWalletLimits()
    alt endTrial is true and wallet is trialing
        BS->>BS: cancelTrialJobsByWalletId(walletId)
        BS->>JQS: cancelCreatedByPattern({<br/>  name: 'CloseTrialDeployment',<br/>  pattern: walletId pattern<br/>})
        JQS->>DB: UPDATE jobs SET state='cancelled'<br/>WHERE singleton_key LIKE pattern
        DB-->>JQS: cancelled job IDs
        JQS->>JQS: log JOBS_CANCELLED_BY_PATTERN event
        BS->>JQS: cancelCreatedByPattern({<br/>  name: 'NotificationJob',<br/>  pattern: walletId pattern<br/>})
        JQS->>DB: UPDATE jobs SET state='cancelled'<br/>WHERE singleton_key LIKE pattern
        DB-->>JQS: cancelled job IDs
        JQS->>JQS: log JOBS_CANCELLED_BY_PATTERN event
    end
    alt limits changed or endTrial is true
        BS->>BS: Update wallet limits and/or isTrialing
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • #1868: Directly complements this PR by adding CloseTrialDeployment and Notification job handlers and scheduled enqueues that work with the trial job cancellation implementation.
  • #2444: Modifies BalancesService with new limit-retrieval methods, overlapping with the same service's trial-management changes.

Suggested reviewers

  • ygrishajev
  • stalniy

🐰 A trial ends with a careful hand,
Jobs canceled by pattern, as planned,
Limits are set, the wallet's secure,
Clean databases, of that we're sure! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
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.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: adding unit tests for BalancesService and enhancing the wallet limit refresh logic with trial job cancellation.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/trial-flag

No actionable comments were generated in the recent review. 🎉


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

@codecov
Copy link

codecov bot commented Feb 8, 2026

❌ 2 Tests Failed:

Tests completed Failed Passed Skipped
952 2 950 1
View the top 2 failed test(s) by shortest run time
Stripe webhook POST /v1/stripe-webhook payment_intent.succeeded tops up wallet when payment intent succeeds
Stack Traces | 12.7s run time
Error: expect(received).toBe(expected) // Object.is equality

Expected: 200
Received: 500
    at Object.<anonymous> (.../test/functional/stripe-webhook.spec.ts:181:40)
    at processTicksAndRejections (node:internal/process/task_queues:103:5)
Stripe webhook POST /v1/stripe-webhook payment_intent.succeeded handles duplicate webhook deliveries idempotently
Stack Traces | 14.2s run time
Error: expect(received).toBe(expected) // Object.is equality

Expected: 200
Received: 500
    at Object.<anonymous> (.../test/functional/stripe-webhook.spec.ts:109:34)
    at processTicksAndRejections (node:internal/process/task_queues:103:5)

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

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.

1 participant