-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
front: add stdcm simulation sheet e2e test
Signed-off-by: maymanaf <[email protected]>
- Loading branch information
Showing
5 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |