Skip to content

Commit

Permalink
front: drop findFirstPdf()
Browse files Browse the repository at this point in the history
Instead, return the final file path in downloadSimulation().

Signed-off-by: Simon Ser <[email protected]>
  • Loading branch information
emersion committed Mar 4, 2025
1 parent 5ed4625 commit 31c6575
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 35 deletions.
15 changes: 4 additions & 11 deletions front/tests/013-stdcm-simulation-sheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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'),
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion front/tests/pages/stdcm/simulation-results-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class SimulationResultPage extends STDCMPage {
await expect(this.startNewQueryWithDataButton).toBeVisible();
}

async downloadSimulation(downloadDir: string): Promise<void> {
async downloadSimulation(downloadDir: string): Promise<string> {
// Wait until there are no network requests for stability
await this.page.waitForLoadState('networkidle');

Expand All @@ -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() {
Expand Down
23 changes: 0 additions & 23 deletions front/tests/utils/pdf-parser.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 31c6575

Please sign in to comment.