Skip to content

test(earnings): cover driverEarningsService grouping, boundaries and settled-only totals - #310

Merged
Tybravo merged 1 commit into
SwiftChainn:mainfrom
lankeethebeast:test/driver-earnings-service
Oct 23, 2026
Merged

Tybravo merged 1 commit into
SwiftChainn:mainfrom
lankeethebeast:test/driver-earnings-service

Conversation

@lankeethebeast

Copy link
Copy Markdown
Contributor

Closes #227

Summary

driverEarningsService backs GET /api/v1/drivers/:id/earnings and had no tests. This adds 22 tests in tests/driverEarningsService.test.ts, which follows the repo's flat tests/ layout.

Escrow and delivery fixtures are stored in MongoMemoryServer and the real $lookup → $group pipeline runs against them, so every total asserted here is what MongoDB actually computes. Nothing in the service is mocked.

What is covered

  • Daily grouping: UTC day buckets (23:59:59 vs 00:00:00), oldest-first ordering, per-period and summary totals, only the requested driver's deliveries counted, two-decimal rounding (0.1 + 0.2 → 0.3), and the updatedAt fallback when releasedAt is missing.
  • Week boundaries: ISO weeks (%G-W%V) run Monday to Sunday (Sun 2026-01-04 → 2026-W01, Mon 2026-01-05 → 2026-W02). A week that straddles New Year takes its ISO week-year (Mon 2025-12-29 → 2026-W01, Sun 2025-12-28 → 2025-W52).
  • Month boundaries: UTC calendar months (Jan 31 23:59:59 vs Feb 1 00:00:00), and the summary is the same whichever grouping is used.
  • Settled only: only RELEASED escrows count. DISPUTED, REFUNDED, LOCKED, PENDING, EXPIRED and RESOLVED are excluded, and so is a released escrow whose delivery no longer exists.
  • Date range: startDate and endDate are inclusive on releasedAt. Open-ended ranges work on either side, and startDate == endDate is accepted.
  • Zero earnings: for day, week and month, a driver with no earnings gets periods: [] and summary: { totalAmount: 0, totalDeliveries: 0 } instead of an error. The same applies to an empty date range.
  • Validation: blank driverId, unknown groupBy, startDate > endDate, and assertValidDriverId.

Verification

npx jest tests/driverEarningsService.test.ts --coverage --collectCoverageFrom=src/services/driverEarningsService.ts
Tests:       22 passed, 22 total
driverEarningsService.ts | 100 % Stmts | 100 % Branch | 100 % Funcs | 100 % Lines

eslint and prettier are clean on the new file.

Note

The date-range filter matches on releasedAt only. A released escrow with no releasedAt is still bucketed by updatedAt, but it's excluded once a startDate or endDate is given. The tests don't depend on that edge case, but you may want to align the two.

…settled-only totals

Runs the real lookup/group pipeline against an in-memory MongoDB: daily totals, ISO week and month boundaries in UTC, RELEASED-only filtering, date ranges, zero-earnings responses and validation.
@drips-wave

drips-wave Bot commented Oct 21, 2026

Copy link
Copy Markdown

@lankeethebeast Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Tybravo
Tybravo merged commit c612e84 into SwiftChainn:main Oct 23, 2026
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.

[Testing] Write tests for the driverEarningsService aggregation

2 participants