update on the agents and registry side
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
import { calculateDrivingDistance, calculateTotalCharge, getLastRouteDurationMin } from '@/lib/distance';
|
||||
import {
|
||||
calculateDrivingDistance,
|
||||
calculateDrivingRoute,
|
||||
calculateTotalCharge,
|
||||
formatRouteDistance,
|
||||
formatRouteDuration,
|
||||
getLastRouteDurationMin
|
||||
} from '@/lib/distance';
|
||||
|
||||
/**
|
||||
* Distance and charge for a delivery leg.
|
||||
@@ -183,6 +190,60 @@ describe('calculateDrivingDistance', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('calculateDrivingRoute — sub-kilometre legs', () => {
|
||||
beforeEach(() => {
|
||||
global.fetch = jest.fn();
|
||||
});
|
||||
|
||||
it('should keep the unrounded road length in metres alongside whole-km distance', async () => {
|
||||
// A same-address order: OSRM snaps both pins to the road and returns ~120 m.
|
||||
// `distance` (billed) rounds to 0 km; `meters` is what the operator sees.
|
||||
global.fetch.mockResolvedValue(osrmOk(123.4, 20));
|
||||
const res = await calculateDrivingRoute(BLR, { latitude: 12.9717, longitude: 77.5947 });
|
||||
expect(res.distance).toBe(0);
|
||||
expect(res.meters).toBe(123);
|
||||
expect(res.minutes).toBe(0);
|
||||
});
|
||||
|
||||
it('should report metres on the Haversine fallback too', async () => {
|
||||
global.fetch.mockResolvedValue({ ok: false, json: async () => ({}) });
|
||||
const res = await calculateDrivingRoute(BLR, { ...BLR });
|
||||
expect(res.distance).toBe(0);
|
||||
expect(res.meters).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatRouteDistance', () => {
|
||||
it.each([
|
||||
[0, '0 m'],
|
||||
[123, '120 m'],
|
||||
[996, '1 km'],
|
||||
[1000, '1 km'],
|
||||
[1449, '1.4 km'],
|
||||
[8200, '8.2 km']
|
||||
])('should format %d m as %s', (meters, expected) => {
|
||||
expect(formatRouteDistance(meters)).toBe(expected);
|
||||
});
|
||||
|
||||
it.each([[null], [undefined], [NaN], [-5]])('should show a dash for %p', (value) => {
|
||||
expect(formatRouteDistance(value)).toBe('—');
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatRouteDuration', () => {
|
||||
it('should show "< 1 min" for a sub-minute drive rather than a default', () => {
|
||||
expect(formatRouteDuration(0)).toBe('< 1 min');
|
||||
});
|
||||
|
||||
it('should show whole minutes', () => {
|
||||
expect(formatRouteDuration(15)).toBe('15 mins');
|
||||
});
|
||||
|
||||
it.each([[null], [undefined]])('should show a dash when there is no ETA (%p)', (value) => {
|
||||
expect(formatRouteDuration(value)).toBe('—');
|
||||
});
|
||||
});
|
||||
|
||||
describe('calculateTotalCharge', () => {
|
||||
it('should charge only the base price below the minimum distance', () => {
|
||||
expect(calculateTotalCharge(3, 30, 6, 8)).toBe(30);
|
||||
|
||||
Reference in New Issue
Block a user