import path from 'path' ; import { promises as fs } from 'fs' ; import { comparePngs } from '../../../../test/functional/services/lib/compare_pngs' ; import { FtrProviderContext } from '../ftr_provider_context' ; export function CompareImagesProvider ( { getService } ) { const log = getService ( 'log' ) ; const config = getService ( 'config' ) ; const screenshotsDir = config . get ( 'screenshots.directory' ) ; const writeToSessionFile = async ( name , rawPdf ) => { const sessionDirectory = path . resolve ( screenshotsDir , 'session' ) ; await fs . mkdir ( sessionDirectory , { recursive : true } ) ; const sessionReportPath = path . resolve ( sessionDirectory , `${ name }.png` ) ; await fs . writeFile ( sessionReportPath , rawPdf ) ; return sessionReportPath ; } ; return { writeToSessionFile , async checkIfPngsMatch ( actualPngPath , baselinePngPath , screenshotsDirectory = screenshotsDir ) { log . debug ( `checkIfPngsMatch: ${ baselinePngPath }` ) ; const sessionDirectoryPath = path . resolve ( screenshotsDirectory , 'session' ) ; const failureDirectoryPath = path . resolve ( screenshotsDirectory , 'failure' ) ; await fs . mkdir ( sessionDirectoryPath , { recursive : true } ) ; await fs . mkdir ( failureDirectoryPath , { recursive : true } ) ; const actualPngFileName = path . basename ( actualPngPath , '.png' ) ; const baselinePngFileName = path . basename ( baselinePngPath , '.png' ) ; const baselineCopyPath = path . resolve ( sessionDirectoryPath , `${ baselinePngFileName }_baseline.png` ) ; try { log . debug ( `writeFile: ${ baselineCopyPath }` ) ; await fs . writeFile ( baselineCopyPath , await fs . readFile ( baselinePngPath ) ) ; } catch ( error ) { throw new Error ( `No baseline png found at ${ baselinePngPath }` ) ; } const actualCopyPath = path . resolve ( sessionDirectoryPath , `${ actualPngFileName }_actual.png` ) ; log . debug ( `writeFile: ${ actualCopyPath }` ) ; await fs . writeFile ( actualCopyPath , await fs . readFile ( actualPngPath ) ) ; let diffTotal = 0 ; const diffPngPath = path . resolve ( failureDirectoryPath , `${ baselinePngFileName }-${ 1 }.png` ) ; diffTotal += await comparePngs ( actualCopyPath , baselineCopyPath , diffPngPath , sessionDirectoryPath , log ) ; return diffTotal ; } , } ; }