1
+ import { keyBy } from 'lodash' ;
1
2
import { describe , it , expect } from 'vitest' ;
2
3
3
4
import type { TrainScheduleResult } from 'common/api/osrdEditoastApi' ;
5
+ import type { ScheduleEntry } from 'modules/timesStops/types' ;
4
6
5
- import computeMargins from '../computeMargins' ;
7
+ import computeMargins , { getTheoreticalMargins } from '../computeMargins' ;
6
8
7
9
describe ( 'computeMargins' , ( ) => {
8
10
const path = [
@@ -18,33 +20,92 @@ describe('computeMargins', () => {
18
20
id : 'c' ,
19
21
uic : 3 ,
20
22
} ,
23
+ {
24
+ id : 'd' ,
25
+ uic : 4 ,
26
+ } ,
27
+ {
28
+ id : 'e' ,
29
+ uic : 5 ,
30
+ } ,
21
31
] ;
22
- const margins = { boundaries : [ 'c' ] , values : [ '10%' , '0 %' ] } ;
32
+ const margins = { boundaries : [ 'c' ] , values : [ '10%' , '5 %' ] } ;
23
33
const pathItemTimes = {
24
- base : [ 0 , 100 * 1000 , 200 * 1000 ] ,
25
- provisional : [ 0 , 110 * 1000 , 220 * 1000 ] ,
26
- final : [ 0 , 115 * 1000 , 230 * 1000 ] ,
34
+ base : [ 0 , 100 * 1000 , 200 * 1000 , 400 * 1000 , 500 * 1000 ] ,
35
+ provisional : [ 0 , 110 * 1000 , 220 * 1000 , 430 * 1000 , 535 * 1000 ] ,
36
+ final : [ 0 , 115 * 1000 , 230 * 1000 , 440 * 1000 , 545 * 1000 ] ,
27
37
} ;
38
+ const schedule = [
39
+ {
40
+ at : 'a' ,
41
+ } ,
42
+ {
43
+ at : 'c' ,
44
+ } ,
45
+ {
46
+ at : 'd' ,
47
+ } ,
48
+ {
49
+ at : 'e' ,
50
+ } ,
51
+ ] ;
28
52
29
53
it ( 'should compute simple margin' , ( ) => {
30
- const train = { path, margins } as TrainScheduleResult ;
31
- expect ( computeMargins ( train , 0 , pathItemTimes ) ) . toEqual ( {
54
+ const train = { path, margins, schedule } as TrainScheduleResult ;
55
+ const scheduleByAt : Record < string , ScheduleEntry > = keyBy ( train . schedule , 'at' ) ;
56
+ const theoreticalMargins = getTheoreticalMargins ( train ) ;
57
+ expect ( computeMargins ( theoreticalMargins , train , scheduleByAt , 0 , pathItemTimes ) ) . toEqual ( {
32
58
theoreticalMargin : '10 %' ,
33
- theoreticalMarginSeconds : '10 s' ,
34
- calculatedMargin : '15 s' ,
35
- diffMargins : '5 s' ,
36
- } ) ;
37
- expect ( computeMargins ( train , 1 , pathItemTimes ) ) . toEqual ( {
38
- theoreticalMargin : '' ,
59
+ isTheoreticalMarginBoundary : true ,
39
60
theoreticalMarginSeconds : '20 s' ,
40
61
calculatedMargin : '30 s' ,
41
62
diffMargins : '10 s' ,
42
63
} ) ;
43
- expect ( computeMargins ( train , 2 , pathItemTimes ) ) . toEqual ( {
64
+ expect ( computeMargins ( theoreticalMargins , train , scheduleByAt , 1 , pathItemTimes ) ) . toEqual ( {
44
65
theoreticalMargin : undefined ,
66
+ isTheoreticalMarginBoundary : undefined ,
45
67
theoreticalMarginSeconds : undefined ,
46
68
calculatedMargin : undefined ,
47
69
diffMargins : undefined ,
48
70
} ) ;
71
+ expect ( computeMargins ( theoreticalMargins , train , scheduleByAt , 2 , pathItemTimes ) ) . toEqual ( {
72
+ theoreticalMargin : '5 %' ,
73
+ isTheoreticalMarginBoundary : true ,
74
+ theoreticalMarginSeconds : '10 s' ,
75
+ calculatedMargin : '10 s' ,
76
+ diffMargins : '0 s' ,
77
+ } ) ;
78
+ expect ( computeMargins ( theoreticalMargins , train , scheduleByAt , 3 , pathItemTimes ) ) . toEqual ( {
79
+ theoreticalMargin : '5 %' ,
80
+ isTheoreticalMarginBoundary : false ,
81
+ theoreticalMarginSeconds : '5 s' ,
82
+ calculatedMargin : '5 s' ,
83
+ diffMargins : '0 s' ,
84
+ } ) ;
85
+ expect ( computeMargins ( theoreticalMargins , train , scheduleByAt , 4 , pathItemTimes ) ) . toEqual ( {
86
+ theoreticalMargin : undefined ,
87
+ isTheoreticalMarginBoundary : undefined ,
88
+ theoreticalMarginSeconds : undefined ,
89
+ calculatedMargin : undefined ,
90
+ diffMargins : undefined ,
91
+ } ) ;
92
+ } ) ;
93
+ } ) ;
94
+
95
+ describe ( 'getTheoreticalMargins' , ( ) => {
96
+ it ( 'should compute theoretical margins with boundaries correctly' , ( ) => {
97
+ const path = [ { id : 'a' } , { id : 'b' } , { id : 'c' } , { id : 'd' } , { id : 'e' } ] ;
98
+ const margins = { boundaries : [ 'c' , 'd' ] , values : [ '10%' , '0%' , '10 min/100km' ] } ;
99
+ const trainSchedule = { path, margins } as TrainScheduleResult ;
100
+
101
+ const theoreticalMargins = getTheoreticalMargins ( trainSchedule ) ;
102
+
103
+ expect ( theoreticalMargins ) . toEqual ( {
104
+ a : { theoreticalMargin : '10%' , isBoundary : true } ,
105
+ b : { theoreticalMargin : '10%' , isBoundary : false } ,
106
+ c : { theoreticalMargin : '0%' , isBoundary : true } ,
107
+ d : { theoreticalMargin : '10 min/100km' , isBoundary : true } ,
108
+ e : { theoreticalMargin : '10 min/100km' , isBoundary : false } ,
109
+ } ) ;
49
110
} ) ;
50
111
} ) ;
0 commit comments