From 31c65750d10b863272a2c18967a3feebe6a21ba2 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 4 Mar 2025 12:30:16 +0100 Subject: [PATCH] front: drop findFirstPdf() Instead, return the final file path in downloadSimulation(). Signed-off-by: Simon Ser --- .../tests/013-stdcm-simulation-sheet.spec.ts | 15 ++++-------- .../pages/stdcm/simulation-results-page.ts | 3 ++- front/tests/utils/pdf-parser.ts | 23 ------------------- 3 files changed, 6 insertions(+), 35 deletions(-) diff --git a/front/tests/013-stdcm-simulation-sheet.spec.ts b/front/tests/013-stdcm-simulation-sheet.spec.ts index c4001b04d66..e95161a33b7 100644 --- a/front/tests/013-stdcm-simulation-sheet.spec.ts +++ b/front/tests/013-stdcm-simulation-sheet.spec.ts @@ -13,7 +13,7 @@ import STDCMPage from './pages/stdcm/stdcm-page'; import ViaSection from './pages/stdcm/via-section'; import { waitForInfraStateToBeCached } from './utils'; import { getInfra } from './utils/api-utils'; -import { findFirstPdf, parsePdfText, verifySimulationContent } from './utils/pdf-parser'; +import { parsePdfText, verifySimulationContent } from './utils/pdf-parser'; import type { ConsistFields, PdfSimulationContent } from './utils/types'; test.describe('Verify stdcm simulation page', () => { @@ -71,7 +71,7 @@ test.describe('Verify stdcm simulation page', () => { await waitForInfraStateToBeCached(infra.id); }); - let downloadDir: string | undefined; + let downloadPath: string | undefined; /** *************** Test 1 **************** */ test('Verify STDCM stops and simulation sheet', async ({ browserName, context }, testInfo) => { @@ -102,8 +102,7 @@ test.describe('Verify stdcm simulation page', () => { await simulationResultPage.displayAllOperationalPoints(); await simulationResultPage.verifyTableData('./tests/assets/stdcm/stdcm-with-all-via.json'); await simulationResultPage.retainSimulation(); - downloadDir = testInfo.outputDir; - await simulationResultPage.downloadSimulation(downloadDir); + downloadPath = await simulationResultPage.downloadSimulation(testInfo.outputDir); // Reset and verify empty fields const [newPage] = await Promise.all([ context.waitForEvent('page'), @@ -122,13 +121,7 @@ test.describe('Verify stdcm simulation page', () => { /** *************** Test 2 *************** */ test('Verify simulation sheet content', async () => { - 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 pdfBuffer = fs.readFileSync(downloadPath!); const actualPdfSimulationContent = await parsePdfText(pdfBuffer); const expectedPdfSimulationContent: PdfSimulationContent = simulationSheetDetails(); verifySimulationContent(actualPdfSimulationContent, expectedPdfSimulationContent); diff --git a/front/tests/pages/stdcm/simulation-results-page.ts b/front/tests/pages/stdcm/simulation-results-page.ts index 0af1715a724..270687ae936 100644 --- a/front/tests/pages/stdcm/simulation-results-page.ts +++ b/front/tests/pages/stdcm/simulation-results-page.ts @@ -115,7 +115,7 @@ class SimulationResultPage extends STDCMPage { await expect(this.startNewQueryWithDataButton).toBeVisible(); } - async downloadSimulation(downloadDir: string): Promise { + async downloadSimulation(downloadDir: string): Promise { // Wait until there are no network requests for stability await this.page.waitForLoadState('networkidle'); @@ -133,6 +133,7 @@ class SimulationResultPage extends STDCMPage { await download.saveAs(downloadPath); logger.info(`The PDF was successfully downloaded to: ${downloadPath}`); + return downloadPath; } async startNewQuery() { diff --git a/front/tests/utils/pdf-parser.ts b/front/tests/utils/pdf-parser.ts index 22ad51df4e6..c353d9cb6f9 100644 --- a/front/tests/utils/pdf-parser.ts +++ b/front/tests/utils/pdf-parser.ts @@ -1,30 +1,7 @@ -import fs from 'fs'; -import path from 'path'; - import { expect } from '@playwright/test'; import { getDocument } from 'pdfjs-dist/legacy/build/pdf.mjs'; import type { PdfSimulationContent } from './types'; -import { logger } from '../logging-fixture'; - -/** - * Find the first PDF file in a directory. - * @param directory - The directory to search in. - * @returns The absolute path to the PDF file, or `null` if no PDF file is found. - */ -export function findFirstPdf(directory: string): string | null { - try { - const pdfFile = fs.readdirSync(directory).find((file) => file.endsWith('.pdf')); - if (!pdfFile) { - logger.error(`No PDF files found in directory: ${directory}`); - return null; - } - return path.resolve(directory, pdfFile); - } catch (error) { - logger.error(`Error reading directory: ${directory}`, error); - return null; - } -} export async function parsePdfText(buffer: Buffer) { const doc = await getDocument(new Uint8Array(buffer.buffer)).promise;