Skip to content

Commit

Permalink
fixup! fixup! front: add fourth stdcm e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Maymanaf committed Nov 19, 2024
1 parent 10a831c commit acb3303
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 26 deletions.
17 changes: 12 additions & 5 deletions front/tests/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import ROLLING_STOCK_NAMES, {
globalProjectName,
trainScheduleProjectName,
} from './assets/project-const';
import { getStdcmEnvironment } from './utils/api-setup';
import { createDataForTests } from './utils/setup-utils';
import { deleteProject, deleteRollingStocks } from './utils/teardown-utils';

setup('setup', async () => {
const stdcmEnvironment = await getStdcmEnvironment();
if (stdcmEnvironment) {
process.env.STDCM_ENVIRONMENT = JSON.stringify(stdcmEnvironment);
}

console.info('Starting test data setup ...');
await Promise.all([
await deleteProject(trainScheduleProjectName),
await deleteProject(globalProjectName),
deleteRollingStocks(ROLLING_STOCK_NAMES),
]);

await deleteProject(trainScheduleProjectName);
await deleteProject(globalProjectName);
await deleteRollingStocks(ROLLING_STOCK_NAMES);

await createDataForTests();
console.info('Test data setup completed successfully.');
});
29 changes: 21 additions & 8 deletions front/tests/global-teardown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,38 @@ import ROLLING_STOCK_NAMES, {
globalProjectName,
trainScheduleProjectName,
} from './assets/project-const';
import { setStdcmEnvironment } from './utils/api-setup';
import { deleteProject, deleteRollingStocks } from './utils/teardown-utils';

teardown('teardown', async ({ browser }) => {
try {
await Promise.all([
await deleteProject(trainScheduleProjectName),
await deleteProject(globalProjectName),
deleteRollingStocks(ROLLING_STOCK_NAMES),
]);
console.info('Starting test data teardown...');

// Delete projects and rolling stocks
await deleteProject(trainScheduleProjectName);
await deleteProject(globalProjectName);
await deleteRollingStocks(ROLLING_STOCK_NAMES);

// Delete saved files in the results directory
fs.rmSync('./tests/stdcm-results', { recursive: true, force: true });
console.info('All downloaded files have been removed from the results directory.');

// Close all browser contexts
for (const context of browser.contexts()) {
await context.close();
}
await Promise.all(browser.contexts().map((context) => context.close()));
console.info('All browser contexts closed successfully.');

// Restore STDCM environment
const savedEnvironment = process.env.STDCM_ENVIRONMENT
? JSON.parse(process.env.STDCM_ENVIRONMENT)
: null;

if (savedEnvironment) {
await setStdcmEnvironment(savedEnvironment);
console.info('STDCM environment restored successfully.');
} else {
console.warn('No STDCM environment to restore.');
}

console.info('Test data teardown completed successfully.');
} catch (error) {
console.error('Error during test data teardown:', error);
Expand Down
39 changes: 39 additions & 0 deletions front/tests/utils/api-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
Study,
Scenario,
LightRollingStock,
StdcmSearchEnvironment,
} from 'common/api/osrdEditoastApi';

import electricalProfileSet from '../assets/operationStudies/simulationSettings/electricalProfiles/electricalProfile.json';
Expand Down Expand Up @@ -221,3 +222,41 @@ export const setElectricalProfile = async (): Promise<ElectricalProfileSet> => {
);
return electricalProfile as ElectricalProfileSet;
};

/**
* Fetch the STDCM environment if not in CI mode.
*/
export async function getStdcmEnvironment(): Promise<StdcmSearchEnvironment | null> {
if (process.env.CI) return null; // Skip in CI mode.

try {
const apiContext = await getApiContext();
const response = await apiContext.get('api/stdcm/search_environment');

if (response.status() === 200) {
return (await response.json()) as StdcmSearchEnvironment;
}

console.warn(`STDCM environment not configured. HTTP status: ${response.status()}`);
return null;
} catch (error) {
console.error('Failed to fetch STDCM environment:', error);
return null;
}
}

/**
* Set the STDCM environment with the provided data.
*
* @param stdcmEnvironment -The stdcm search environment to use.
*/
export async function setStdcmEnvironment(stdcmEnvironment: StdcmSearchEnvironment): Promise<void> {
// Remove the `id` field to match the StdcmSearchEnvironmentCreateForm schema
const { id, ...stdcmEnvironmentWithoutId } = stdcmEnvironment;
await postApiRequest(
'/api/stdcm/search_environment',
stdcmEnvironmentWithoutId,
undefined,
'Failed to update STDCM configuration environment'
);
}
23 changes: 10 additions & 13 deletions front/tests/utils/setup-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
} from 'common/api/osrdEditoastApi';

import { readJsonFile } from '.';
import { getApiRequest, postApiRequest } from './api-setup';
import { getApiRequest, postApiRequest, setStdcmEnvironment } from './api-setup';
import createScenario from './scenario';
import { sendTrainSchedules } from './trainSchedule';
import projectData from '../assets/operationStudies/project.json';
Expand Down Expand Up @@ -172,18 +172,15 @@ export async function createDataForTests(): Promise<void> {
await sendTrainSchedules(scenarioTrainSchedule.timetable_id, trainSchedulesJson);

// Configure STDCM search environment for the tests
await postApiRequest(
'/api/stdcm/search_environment',
{
infra_id: smallInfra.id,
search_window_begin: '2024-10-17T00:00:01',
search_window_end: '2024-10-18T23:59:59',
timetable_id: scenarioTrainSchedule.timetable_id,
} as StdcmSearchEnvironment,
undefined,
'Failed to update STDCM configuration environment'
);
console.info('Test data setup completed successfully.');

const stdcmEnvironment = {
infra_id: smallInfra.id,
search_window_begin: '2024-10-17T00:00:01',
search_window_end: '2024-10-18T23:59:59',
timetable_id: scenarioTrainSchedule.timetable_id,
} as StdcmSearchEnvironment;

await setStdcmEnvironment(stdcmEnvironment);
} catch (error) {
console.error('Error during test data setup:', error);
}
Expand Down

0 comments on commit acb3303

Please sign in to comment.