-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathsetup-utils.ts
255 lines (232 loc) · 8.07 KB
/
setup-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { promises } from 'fs';
import type {
Infra,
PostInfraRailjsonApiResponse,
Project,
ProjectCreateForm,
RailJson,
StdcmSearchEnvironment,
Study,
StudyCreateForm,
} from 'common/api/osrdEditoastApi';
import {
getApiRequest,
getInfra,
getStdcmEnvironment,
postApiRequest,
setStdcmEnvironment,
} from './api-utils';
import readJsonFile from './file-utils';
import createScenario from './scenario';
import sendTrainSchedules from './train-schedule';
import {
dualModeRollingStockName,
electricRollingStockName,
fastRollingStockName,
globalProjectName,
globalStudyName,
improbableRollingStockName,
infrastructureName,
slowRollingStockName,
trainScheduleProjectName,
trainScheduleScenarioName,
trainScheduleStudyName,
} from '../assets/constants/project-const';
import { logger } from '../logging-fixture';
import { createDateInSpecialTimeZone } from './date-utils';
import type { ProjectData, StudyData } from './types';
const projectData: ProjectData = readJsonFile('tests/assets/operation-studies/project.json');
const studyData: StudyData = readJsonFile('tests/assets/operation-studies/study.json');
/**
* Helper function to create infrastructure using RailJson.
*
* @param infraName - The name of the infrastructure to create.
* @returns {Promise<Infra>} - The created infrastructure object.
*/
async function createInfrastructure(infraName = infrastructureName): Promise<Infra> {
const smallInfraRailjson: RailJson = readJsonFile('./tests/assets/infrastructure/infra.json');
const createdInfra: PostInfraRailjsonApiResponse = await postApiRequest(
`/api/infra/railjson`,
{ ...smallInfraRailjson },
{
name: infraName,
generate_data: true,
},
'Failed to create infrastructure'
);
// Fetch and return the created infrastructure by its ID
const smallInfra: Infra = await getApiRequest(`/api/infra/${createdInfra.infra}`);
return smallInfra;
}
/**
* Helper function to create rolling stocks in parallel.
* Create multiple rolling stock entries by posting to the API.
*/
async function createRollingStocks(): Promise<void> {
const rollingStocks = [
{
json: readJsonFile('./../tests/data/rolling_stocks/electric_rolling_stock.json'),
name: electricRollingStockName,
},
{
json: readJsonFile('./tests/assets/rolling-stock/slow_rolling_stock.json'),
name: slowRollingStockName,
},
{
json: readJsonFile('./tests/assets/rolling-stock/dual-mode_rolling_stock.json'),
name: dualModeRollingStockName,
},
{
json: readJsonFile('./tests/assets/rolling-stock/fast_rolling_stock.json'),
name: fastRollingStockName,
},
{
json: readJsonFile('./tests/assets/rolling-stock/improbable_rolling_stock.json'),
name: improbableRollingStockName,
},
];
// Post each rolling stock in parallel
await Promise.all(
rollingStocks.map(({ json, name }) =>
postApiRequest(
'/api/rolling_stock',
{ ...json, name },
undefined,
'Failed to create rolling stocks'
)
)
);
}
/**
* Helper function to create a project.
*
* @param projectName - The name of the project to create.
* @returns {Promise<Project>} - The created project object.
*/
export async function createProject(projectName = globalProjectName): Promise<Project> {
const project: Project = await postApiRequest(
'/api/projects',
{
...projectData,
name: projectName,
budget: 1234567890,
} as ProjectCreateForm,
undefined,
'Failed to create project'
);
return project;
}
/**
* Helper function to create a study for a given project.
*
* @param projectId - The ID of the project under which the study will be created.
* @param studyName - The name of the study to create.
* @returns {Promise<Study>} - The created study object.
*/
export async function createStudy(projectId: number, studyName = globalStudyName): Promise<Study> {
const study: Study = await postApiRequest(
`/api/projects/${projectId}/studies`,
{
...studyData,
name: studyName,
budget: 1234567890,
} as StudyCreateForm,
undefined,
'Failed to create study'
);
return study;
}
/**
* Load and save the stdcm environment to ensure it is not erased by the e2e-tests environment
* @param testInfraId
*/
async function saveFormerStdcmEnvironment(testInfraId: number) {
const savedStdcmEnvFilePath = './tests/test-saved-environment/savedStdcmEnvironment.json';
let stdcmEnvironment = await getStdcmEnvironment();
try {
if (stdcmEnvironment && testInfraId !== stdcmEnvironment.infra_id) {
// If the stdcm env in the database isn't using the test infra, we know it is a non-test env and we save it, to avoid loss on test interruption.
await promises.mkdir('./tests/test-saved-environment', { recursive: true });
await promises.writeFile(
savedStdcmEnvFilePath,
JSON.stringify(stdcmEnvironment, null, 2),
'utf-8'
);
} else {
try {
// Otherwise, we check if we previously saved a non-test env and recover it.
// The only way this can occur normally is if the tests were interrupted before teardown.
stdcmEnvironment = readJsonFile<StdcmSearchEnvironment>(savedStdcmEnvFilePath);
} catch (error: unknown) {
if (!(error instanceof Error && 'code' in error && error.code === 'ENOENT')) {
throw error; // Rethrow errors other than file not found
}
}
}
// Finally we put the current stdcm env in an env variable in prevision of the teardown,
// where we will delete the saved file and put the stdcm env back into the database
process.env.STDCM_ENVIRONMENT = JSON.stringify(stdcmEnvironment);
} catch (error) {
logger.error('Error handling saved STDCM environment: ', error);
}
}
/**
* Main function to create all necessary test data including infrastructure, rolling stocks,
* project, study, and scenario.
*/
export async function createDataForTests(): Promise<void> {
const trainSchedulesJson: JSON = readJsonFile(
'./tests/assets/train-schedule/train_schedules.json'
);
try {
// Step 1: Create infrastructure
let smallInfra = await getInfra();
if (!smallInfra) smallInfra = await createInfrastructure();
process.env.TEST_INFRA_ID = String(smallInfra.id);
// Step 2: Create rolling stocks
await createRollingStocks();
// Step 3: Create a project
const project = await createProject();
// Step 4: Create a study under the project
const study = await createStudy(project.id);
// Step 5: Create a scenario for the study
await createScenario(undefined, project.id, study.id, smallInfra.id);
// Step 6: Create a project, study, scenario and import train schedule and paced train data
const projectTrainSchedule = await createProject(trainScheduleProjectName);
const studyTrainSchedule = await createStudy(projectTrainSchedule.id, trainScheduleStudyName);
const scenarioTrainSchedule = (
await createScenario(
trainScheduleScenarioName,
projectTrainSchedule.id,
studyTrainSchedule.id,
smallInfra.id
)
).scenario;
await sendTrainSchedules(scenarioTrainSchedule.timetable_id, trainSchedulesJson);
// Step 7: Configure STDCM search environment for the tests
await saveFormerStdcmEnvironment(smallInfra.id);
const stdcmEnvironment = {
infra_id: smallInfra.id,
search_window_begin: createDateInSpecialTimeZone(
'2024-10-17T00:00:00',
'Europe/Paris'
).toISOString(),
search_window_end: createDateInSpecialTimeZone(
'2024-10-18T23:59:59',
'Europe/Paris'
).toISOString(),
timetable_id: scenarioTrainSchedule.timetable_id,
enabled_from: createDateInSpecialTimeZone(
'2024-10-16T00:00:00',
'Europe/Paris'
).toISOString(),
enabled_until: createDateInSpecialTimeZone(
'2024-10-17T00:00:00',
'Europe/Paris'
).toISOString(),
} as StdcmSearchEnvironment;
await setStdcmEnvironment(stdcmEnvironment);
} catch (error) {
throw new Error('Error during test data setup', { cause: error });
}
}