Skip to content

test(assignment): cover nearest-driver selection, radius expansion and claim contention - #309

Merged
Tybravo merged 1 commit into
SwiftChainn:mainfrom
lankeethebeast:test/assignment-service
Oct 22, 2026
Merged

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

Conversation

@lankeethebeast

Copy link
Copy Markdown
Contributor

Closes #226

Summary

assignmentService had no tests. This adds 22 unit tests in tests/assignmentService.test.ts, which follows the repo's flat tests/ layout. The issue names backend/tests/unit/services/, but that directory doesn't exist.

Deliveries, escrows and driver positions are stored in MongoMemoryServer. That means proximity search runs the real $geoNear pipeline against the real 2dsphere indexes, and assignment goes through the real deliveryService.assignDriver escrow guard. Only withLock (Redlock/Redis) is replaced, by an in-process lock fake that records acquire/release and can simulate contention.

What is covered

  • Nearest driver: closest available driver wins, and unavailable or offline drivers nearer to the pickup are ignored. Only the winner is marked on_delivery.
  • Radius expansion: base → ×2 → ×4 until a driver is in range. The search stops after ASSIGNMENT_RADIUS_EXPANSION_STEPS, never goes past DRIVER_PROXIMITY_MAX_RADIUS_M, and searches the cap only once.
  • Contention: a driver claimed elsewhere between search and claim is skipped and the runner-up is assigned. Two deliveries racing for one driver produce exactly one assignment. A held delivery lock rejects without touching any driver.
  • releaseClaim: when the escrow guard rejects (PENDING, DISPUTED, or no escrow), the driver goes back to isAvailable: true / online with no currentDeliveryId, and the lock is released. If the rollback write itself fails, the original error still surfaces.
  • autoAssignPendingDeliveries: only FUNDED, driverless deliveries are picked, oldest first, within the batch limit. Locked and failing deliveries are skipped without stopping the sweep, and the { attempted, assigned } counts are checked.
  • Validation: malformed id (rejected before the lock is taken), 404, a delivery that already has a driver, missing pickup coordinates.

Verification

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

eslint and prettier are clean on the new file.

Notes

  • When every expansion step runs out before the cap, the "not assigned" result reports radiusMeters as the next, unsearched radius. For example, with 3 steps it searches 5/10/20/40 km but reports 50 km. The tests don't lock that value in. Happy to fix it in a follow-up if you'd like.
  • releaseClaim resets availability only and never sets currentDeliveryId. The tests assert that it stays null.

…d claim contention

Unit tests for AssignmentService against an in-memory MongoDB with the real geoNear pipeline and escrow guard; only the Redlock wrapper is replaced by an in-process lock fake.
@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 b7a590a into SwiftChainn:main Oct 22, 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 unit tests for the assignmentService (nearest driver, radius expansion, Redlock)

2 participants