client: add resolveLegMode helper for endpoint-aware leg modes

This commit is contained in:
Ida Rogalska
2026-08-11 19:13:37 +02:00
committed by Maurice
parent cedbe39b35
commit 899316d5f8
2 changed files with 52 additions and 0 deletions
@@ -0,0 +1,30 @@
import { describe, it, expect } from 'vitest';
import { resolveLegMode } from './legMode';
const P = (o?: Partial<{ leg_transport_mode: string | null; incoming_leg_transport_mode: string | null }>) =>
({ isPlace: true, ...o });
const B = () => ({ isPlace: false });
describe('resolveLegMode', () => {
it('origin place with outgoing mode wins', () => {
expect(resolveLegMode(P({ leg_transport_mode: 'cycling' }), P(), 'walking')).toBe('cycling');
});
it('origin place, no override -> day default', () => {
expect(resolveLegMode(P(), P(), 'driving')).toBe('driving');
});
it('non-place origin -> destination incoming override', () => {
expect(resolveLegMode(B(), P({ incoming_leg_transport_mode: 'transit' }), 'walking')).toBe('transit');
});
it('non-place origin, no incoming override -> day default', () => {
expect(resolveLegMode(B(), P(), 'walking')).toBe('walking');
});
it('no place endpoint at all -> day default', () => {
expect(resolveLegMode(B(), B(), 'walking')).toBe('walking');
});
it('incoming is inert when origin is a place', () => {
expect(resolveLegMode(P(), P({ incoming_leg_transport_mode: 'transit' }), 'walking')).toBe('walking');
});
it('undefined dest falls back to day default for a non-place origin', () => {
expect(resolveLegMode(B(), undefined, 'walking')).toBe('walking');
});
});
+22
View File
@@ -0,0 +1,22 @@
export type LegPoint = {
isPlace: boolean;
leg_transport_mode?: string | null;
incoming_leg_transport_mode?: string | null;
};
/**
* Travel mode of the leg from `origin` to `dest`. The mode is owned by the leg's
* place endpoint: the origin's outgoing override wins when the origin is a place;
* otherwise the destination's incoming override applies (only meaningful when the
* origin is not a place - a booking arrival or the morning hotel); otherwise the
* day default. The incoming value is inert whenever the origin is a place.
*/
export function resolveLegMode(
origin: LegPoint,
dest: LegPoint | undefined,
dayDefault: string,
): string {
if (origin.isPlace) return origin.leg_transport_mode ?? dayDefault;
if (dest?.isPlace) return dest.incoming_leg_transport_mode ?? dayDefault;
return dayDefault;
}