11import {
22 getTransferRisk ,
33 getLegTransferRisk ,
4+ getTripTransferRisk ,
5+ withTransferRisk ,
46 isTransitLeg ,
5- UNLIKELY_TRANSFER_LIMIT_IN_SECONDS ,
7+ TransferRisk ,
68 type TransferLeg ,
79} from '..' ;
810
9- const transitLeg = ( overrides : Partial < TransferLeg > = { } ) : TransferLeg => ( {
11+ /** A leg that can carry a stamped risk, as every real consumer's leg can. */
12+ type TestLeg = TransferLeg & { transferRisk ?: TransferRisk } ;
13+
14+ const transitLeg = ( overrides : Partial < TestLeg > = { } ) : TestLeg => ( {
1015 aimedStartTime : '2024-01-01T10:00:00.000Z' ,
1116 expectedStartTime : '2024-01-01T10:00:00.000Z' ,
1217 expectedEndTime : '2024-01-01T10:10:00.000Z' ,
1318 serviceJourney : { id : 'ATB:ServiceJourney:1' } ,
1419 ...overrides ,
1520} ) ;
1621
17- const footLeg = ( overrides : Partial < TransferLeg > = { } ) : TransferLeg => ( {
22+ const footLeg = ( overrides : Partial < TestLeg > = { } ) : TestLeg => ( {
1823 aimedStartTime : '2024-01-01T10:10:00.000Z' ,
1924 expectedStartTime : '2024-01-01T10:10:00.000Z' ,
2025 expectedEndTime : '2024-01-01T10:15:00.000Z' ,
@@ -32,18 +37,10 @@ describe('getTransferRisk', () => {
3237 expect ( getTransferRisk ( 0 ) ) . toBe ( 'uncertain' ) ;
3338 } ) ;
3439
35- it ( 'is uncertain down to the unlikely limit' , ( ) => {
40+ it ( 'is uncertain at any negative gap, however large' , ( ) => {
41+ expect ( getTransferRisk ( - 1 ) ) . toBe ( 'uncertain' ) ;
3642 expect ( getTransferRisk ( - 60 ) ) . toBe ( 'uncertain' ) ;
37- expect ( getTransferRisk ( UNLIKELY_TRANSFER_LIMIT_IN_SECONDS ) ) . toBe (
38- 'uncertain' ,
39- ) ;
40- } ) ;
41-
42- it ( 'is unlikely past the limit' , ( ) => {
43- expect ( getTransferRisk ( UNLIKELY_TRANSFER_LIMIT_IN_SECONDS - 1 ) ) . toBe (
44- 'unlikely' ,
45- ) ;
46- expect ( getTransferRisk ( - 600 ) ) . toBe ( 'unlikely' ) ;
43+ expect ( getTransferRisk ( - 600 ) ) . toBe ( 'uncertain' ) ;
4744 } ) ;
4845
4946 it ( 'passes when the gap is not a finite number' , ( ) => {
@@ -68,12 +65,12 @@ describe('getLegTransferRisk', () => {
6865 expect ( getLegTransferRisk ( legs , 1 ) ) . toBe ( 'uncertain' ) ;
6966 } ) ;
7067
71- it ( 'reports unlikely once the gap is past the limit ' , ( ) => {
68+ it ( 'stays uncertain on a badly missed transfer ' , ( ) => {
7269 const legs = [
7370 transitLeg ( { expectedEndTime : '2024-01-01T10:10:00.000Z' } ) ,
7471 transitLeg ( { expectedStartTime : '2024-01-01T10:05:00.000Z' } ) ,
7572 ] ;
76- expect ( getLegTransferRisk ( legs , 1 ) ) . toBe ( 'unlikely ' ) ;
73+ expect ( getLegTransferRisk ( legs , 1 ) ) . toBe ( 'uncertain ' ) ;
7774 } ) ;
7875
7976 it ( 'passes when there is time to spare' , ( ) => {
@@ -117,7 +114,7 @@ describe('getLegTransferRisk', () => {
117114 } ) ,
118115 transitLeg ( { expectedStartTime : '2024-01-01T10:11:00.000Z' } ) ,
119116 ] ;
120- expect ( getLegTransferRisk ( legs , 2 ) ) . toBe ( 'unlikely ' ) ;
117+ expect ( getLegTransferRisk ( legs , 2 ) ) . toBe ( 'uncertain ' ) ;
121118 } ) ;
122119
123120 it ( 'passes when the gap is unparseable rather than inventing a risk' , ( ) => {
@@ -207,7 +204,7 @@ describe('getLegTransferRisk', () => {
207204 } ) ,
208205 ] ;
209206 // Held until 10:13, but we do not arrive until 10:20.
210- expect ( getLegTransferRisk ( legs , 1 ) ) . toBe ( 'unlikely ' ) ;
207+ expect ( getLegTransferRisk ( legs , 1 ) ) . toBe ( 'uncertain ' ) ;
211208 } ) ;
212209
213210 it ( 'counts an intervening walk against the maximum wait time' , ( ) => {
@@ -226,7 +223,7 @@ describe('getLegTransferRisk', () => {
226223 } ) ,
227224 ] ;
228225 // Held until 10:13, but the walk does not end until 10:15.
229- expect ( getLegTransferRisk ( legs , 2 ) ) . toBe ( 'unlikely ' ) ;
226+ expect ( getLegTransferRisk ( legs , 2 ) ) . toBe ( 'uncertain ' ) ;
230227 } ) ;
231228
232229 it ( 'keeps the guarantee when the deadline is unparseable' , ( ) => {
@@ -244,3 +241,87 @@ describe('getLegTransferRisk', () => {
244241 } ) ;
245242 } ) ;
246243} ) ;
244+
245+ describe ( 'withTransferRisk' , ( ) => {
246+ it ( 'stamps the leg you might miss, not the one before' , ( ) => {
247+ const legs = withTransferRisk ( [
248+ transitLeg ( { expectedEndTime : '2024-01-01T10:10:00.000Z' } ) ,
249+ transitLeg ( { expectedStartTime : '2024-01-01T10:09:00.000Z' } ) ,
250+ ] ) ;
251+ expect ( legs [ 0 ] . transferRisk ) . toBeUndefined ( ) ;
252+ expect ( legs [ 1 ] . transferRisk ) . toBe ( 'uncertain' ) ;
253+ } ) ;
254+
255+ it ( 'leaves a comfortable transfer unstamped' , ( ) => {
256+ const legs = withTransferRisk ( [
257+ transitLeg ( { expectedEndTime : '2024-01-01T10:10:00.000Z' } ) ,
258+ transitLeg ( { expectedStartTime : '2024-01-01T10:15:00.000Z' } ) ,
259+ ] ) ;
260+ expect ( legs . every ( ( leg ) => leg . transferRisk === undefined ) ) . toBe ( true ) ;
261+ } ) ;
262+
263+ it ( 'clears a risk the caller passed back in, once the gap is fine' , ( ) => {
264+ const legs = withTransferRisk ( [
265+ transitLeg ( { expectedEndTime : '2024-01-01T10:10:00.000Z' } ) ,
266+ transitLeg ( {
267+ expectedStartTime : '2024-01-01T10:15:00.000Z' ,
268+ transferRisk : TransferRisk . Uncertain ,
269+ } ) ,
270+ ] ) ;
271+ expect ( legs [ 1 ] . transferRisk ) . toBeUndefined ( ) ;
272+ } ) ;
273+
274+ it ( 'does not stamp a guaranteed transfer' , ( ) => {
275+ const legs = withTransferRisk ( [
276+ transitLeg ( {
277+ expectedEndTime : '2024-01-01T10:10:00.000Z' ,
278+ interchangeTo : { guaranteed : true } ,
279+ } ) ,
280+ transitLeg ( { expectedStartTime : '2024-01-01T10:00:00.000Z' } ) ,
281+ ] ) ;
282+ expect ( legs [ 1 ] . transferRisk ) . toBeUndefined ( ) ;
283+ } ) ;
284+ } ) ;
285+
286+ describe ( 'getTripTransferRisk' , ( ) => {
287+ it ( 'passes when every transfer has time to spare' , ( ) => {
288+ const legs = [
289+ transitLeg ( { expectedEndTime : '2024-01-01T10:10:00.000Z' } ) ,
290+ transitLeg ( { expectedStartTime : '2024-01-01T10:15:00.000Z' } ) ,
291+ ] ;
292+ expect ( getTripTransferRisk ( legs ) ) . toBeUndefined ( ) ;
293+ } ) ;
294+
295+ it ( 'reports a risk from anywhere in the trip' , ( ) => {
296+ const legs = [
297+ transitLeg ( { expectedEndTime : '2024-01-01T10:10:00.000Z' } ) ,
298+ transitLeg ( {
299+ expectedStartTime : '2024-01-01T10:15:00.000Z' ,
300+ expectedEndTime : '2024-01-01T10:25:00.000Z' ,
301+ } ) ,
302+ transitLeg ( { expectedStartTime : '2024-01-01T10:24:00.000Z' } ) ,
303+ ] ;
304+ expect ( getTripTransferRisk ( legs ) ) . toBe ( 'uncertain' ) ;
305+ } ) ;
306+
307+ it ( 'ignores a guaranteed transfer when looking across the trip' , ( ) => {
308+ const legs = [
309+ transitLeg ( {
310+ expectedEndTime : '2024-01-01T10:10:00.000Z' ,
311+ interchangeTo : { guaranteed : true } ,
312+ } ) ,
313+ transitLeg ( { expectedStartTime : '2024-01-01T10:00:00.000Z' } ) ,
314+ ] ;
315+ expect ( getTripTransferRisk ( legs ) ) . toBeUndefined ( ) ;
316+ } ) ;
317+
318+ it ( 'does not need the legs to be stamped first' , ( ) => {
319+ const legs = [
320+ transitLeg ( { expectedEndTime : '2024-01-01T10:10:00.000Z' } ) ,
321+ transitLeg ( { expectedStartTime : '2024-01-01T10:09:00.000Z' } ) ,
322+ ] ;
323+ expect ( getTripTransferRisk ( legs ) ) . toBe (
324+ getTripTransferRisk ( withTransferRisk ( legs ) ) ,
325+ ) ;
326+ } ) ;
327+ } ) ;
0 commit comments