Skip to content

Commit

Permalink
front: add stdcm simulation sheet e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: maymanaf <[email protected]>
  • Loading branch information
Maymanaf committed Dec 24, 2024
1 parent 41e05a4 commit 0471e40
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 0 deletions.
37 changes: 37 additions & 0 deletions front/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"maplibre-gl": "^4.0.0",
"openapi-typescript-codegen": "^0.29.0",
"party-js": "^2.2.0",
"pdf-parse": "^1.1.1",
"prop-types": "^15.8.1",
"rc-slider": "^11.1.6",
"react": "^18.2.0",
Expand Down Expand Up @@ -112,6 +113,7 @@
"@types/jest": "^29.5.14",
"@types/lodash": "^4.17.13",
"@types/node": "^22",
"@types/pdf-parse": "^1.1.4",
"@types/react": "^18.2.53",
"@types/react-beautiful-dnd": "^13.1.8",
"@types/react-dom": "^18.2.18",
Expand Down
47 changes: 47 additions & 0 deletions front/tests/013-stdcm-simulation-sheet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import fs from 'fs';

import pdfParse from 'pdf-parse';

import simulationSheetDetails from './assets/simulationSheet-const';
import HomePage from './pages/home-page-model';
import test, { logger } from './test-logger';
import { findFirstPdf, verifySimulationContent, type Simulation } from './utils/simulationSheet';

let OSRDLanguage: string;

test.beforeEach(
'Navigate to Times and Stops tab with rolling stock and route set',
async ({ page }) => {
const homePage = new HomePage(page);
await homePage.goToHomePage();
OSRDLanguage = await homePage.getOSRDLanguage();
}
);

test('Verify PDF content', async ({ browserName }) => {
/**
* Finds the first `.pdf` file in the specified directory.
* @param directory The directory to search in.
* @returns The path of the first `.pdf` file or `null` if not found.
*/

const downloadDir = `./tests/stdcm-results/${browserName}`;
const pdfFilePath = findFirstPdf(downloadDir);

if (!pdfFilePath) {
throw new Error(`No PDF files found in directory: ${downloadDir}`);
}

// Read and parse the PDF
const pdfBuffer = fs.readFileSync(pdfFilePath);
const pdfData = await pdfParse(pdfBuffer);

// Dynamically create the expected simulation based on language
const expectedSimulation: Simulation = simulationSheetDetails(OSRDLanguage);

// Verify PDF content
const pdfText = pdfData.text;
const isCorrect = verifySimulationContent(pdfText, expectedSimulation);

logger.info(isCorrect ? 'Simulation data is correct!' : 'Mismatch in simulation data.');
});
104 changes: 104 additions & 0 deletions front/tests/assets/simulationSheet-const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import enTranslations from '../../public/locales/en/stdcm-simulation-report-sheet.json';
import frTranslations from '../../public/locales/fr/stdcm-simulation-report-sheet.json';
import type { Simulation } from '../utils/simulationSheet';

const simulationSheetDetails = (selectedLanguage: string): Simulation => {
const translations = selectedLanguage === 'English' ? enTranslations : frTranslations;

return {
header: {
toolDescription: translations.warningMessage,
documentTitle: translations.stdcm,
creationDetails: {
number: 'n°1224-352-455',
date: translations.formattedDate
.replace('{{day}}', '24')
.replace('{{month}}', '12')
.replace('{{year}}', '2024')
.replace('{{hours}}', '12')
.replace('{{minutes}}', '53'),
time: '12:53',
},
},
responsiblePerson: {
role: 'Super Fret',
name: 'Jane Smith',
phone: '01 23 45 67 89',
email: '[email protected]',
},
applicationDate: translations.applicationDate,
referenceTrack: translations.referencePath.replace('XXXXXX', '1224'),
trainDetails: {
compositionCode: translations.speedLimitByTag,
towedMaterial: translations.towedMaterial,
maxSpeed: translations.maxSpeed.replace('{{value}}', '180 km/h'),
maxTonnage: translations.maxWeight.replace('{{value}}', '400 t'),
referenceEngine: translations.referenceEngine,
maxLength: translations.maxLength.replace('{{value}}', '300 m'),
},
requestedRoute: [
{
stopNumber: 1,
station: 'North_West_station',
type: 'BV',
arrivalTime: '20:21',
departureTime: '20:21',
reason: translations.serviceStop,
},
{
stopNumber: 2,
station: 'Mid_West_station',
type: 'BV',
reason: translations.passageStop,
},
{
stopNumber: 3,
station: 'South_station',
type: 'BV',
arrivalTime: translations.asap,
departureTime: translations.asap,
reason: translations.serviceStop,
},
],
simulationDetails: {
referenceTrackNumber: translations.referencePath.replace('XXXXXX', 'YYYYYY'),
viewSimulationLink: translations.viewSimulation,
totalDistance: '51 km',
simulationPath: [
{
stopNumber: 1,
station: 'North_West_station',
type: 'BV',
arrivalTime: '20:21',
tonnage: translations.weight.replace('{{value}}', '400 t'),
},
{
stopNumber: 2,
station: 'Mid_West_station',
type: 'BV',
departureTime: '20:41',
},
{
stopNumber: 3,
station: 'Mid_East_station',
type: 'BV',
departureTime: '20:46',
},
{
stopNumber: 4,
station: 'North_station',
type: 'BV',
departureTime: '20:52',
},
{
stopNumber: 5,
station: 'South_station',
type: 'BV',
departureTime: '20:56',
},
],
disclaimer: translations.withoutWarranty,
},
};
};
export default simulationSheetDetails;
94 changes: 94 additions & 0 deletions front/tests/utils/simulationSheet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import fs from 'fs';
import path from 'path';

export interface Simulation {
header: {
toolDescription: string;
documentTitle: string;
creationDetails: {
number: string;
date: string;
time: string;
};
};
responsiblePerson: {
role: string;
name: string;
phone: string;
email: string;
};
applicationDate: string;
referenceTrack: string;
trainDetails: {
compositionCode: string;
towedMaterial: string | null;
maxSpeed: string;
maxTonnage: string;
referenceEngine: string | null;
maxLength: string;
};
requestedRoute: Array<{
stopNumber: number;
station: string;
type: string;
arrivalTime?: string;
departureTime?: string;
reason?: string;
}>;
simulationDetails: {
referenceTrackNumber: string;
viewSimulationLink: string;
totalDistance: string;
simulationPath: Array<{
stopNumber: number;
station: string;
type: string;
arrivalTime?: string;
passageTime?: string;
departureTime?: string;
tonnage?: string;
referenceEngine?: string | null;
signal?: string | null;
crossedTrain?: string | null;
}>;
disclaimer: string;
};
}

export function findFirstPdf(directory: string): string | null {
try {
const files = fs.readdirSync(directory);
const pdfFile = files.find((file) => file.endsWith('.pdf'));
return pdfFile ? path.resolve(directory, pdfFile) : null;
} catch (error) {
console.error(`Error reading directory ${directory}:`, error);
return null;
}
}

/**
* Verifies the PDF content against the expected simulation.
* @param pdfText The text extracted from the PDF.
* @param expectedSimulation The expected simulation data.
* @returns Whether the content matches the expected simulation.
*/
export function verifySimulationContent(pdfText: string, expectedSimulation: Simulation): boolean {
const textChecks = [
expectedSimulation.header.toolDescription,
expectedSimulation.header.documentTitle,
expectedSimulation.header.creationDetails.number,
expectedSimulation.header.creationDetails.date,
expectedSimulation.header.creationDetails.time,
expectedSimulation.applicationDate,
expectedSimulation.referenceTrack,
expectedSimulation.trainDetails.compositionCode,
expectedSimulation.trainDetails.maxSpeed,
expectedSimulation.trainDetails.maxTonnage,
expectedSimulation.trainDetails.maxLength,
...expectedSimulation.requestedRoute.map((route) => route.station),
...expectedSimulation.simulationDetails.simulationPath.map((p) => p.station),
expectedSimulation.simulationDetails.disclaimer,
];

return textChecks.every((check) => pdfText.includes(check));
}

0 comments on commit 0471e40

Please sign in to comment.