import { join } from 'path' ; import alias from '@rollup/plugin-alias' ; import commonjs from '@rollup/plugin-commonjs' ; import nodeResolve from '@rollup/plugin-node-resolve' ; import test from 'ava' ; import { launch } from 'puppeteer' ; import { rollup } from 'rollup' ; const prepareCode = async ( ) => { const realConsoleWarn = console . warn ; /**
     * Suppress Rollup warning:
     * `Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification`
     */ console . warn = ( suppress ) => suppress ; const bundle = await rollup ( { input : join ( __dirname , 'hash.browser.bench.helper.js' ) , plugins : [ alias ( { entries : { chuhai : './../../../bench/chuhai.js' , 'hash.js' : './../../../bench/hash.js' , } , } ) , commonjs ( ) , nodeResolve ( ) , ] , } ) ; console . warn = realConsoleWarn ; const result = await bundle . generate ( { format : 'esm' , } ) ; return result . output [ 0 ] . code ; } ; const preparePage = async ( ) => { const browser = await launch ( { args : [ '--no-sandbox' , '--disable-setuid-sandbox' ] , } ) ; const page = await browser . newPage ( ) ; await page . goto ( 'file:///' ) ; return { browser , page } ; } ; ( async ( ) => { const [ code , { browser , page } ] = await Promise . all ( [ prepareCode ( ) , preparePage ( ) , ] ) ; test ( `# browser: ${ await browser . version ( ) }` , async ( t ) => { page . on ( 'console' , ( msg ) => { console . log ( msg . text ( ) ) ; } ) ; page . on ( 'error' , ( err ) => { console . error ( `error: ${ String ( err ) }` ) ; } ) ; page . on ( 'pageerror' , ( err ) => { console . error ( `pageerror: ${ String ( err ) }` ) ; } ) ; await new Promise < void > ( async ( resolve ) => { await page . exposeFunction ( 'benchError' , ( error ) => { console . error ( error ) ; } ) ; await page . exposeFunction ( 'benchComplete' , async ( ) => { console . log ( 'Browser benchmark complete, closing browser.' ) ; await browser . close ( ) ; t . pass ( ) ; resolve ( ) ; } ) ; await page . setContent ( `<script type="module">${ code }</script>` ) ; } ) ; } ) ; } ) ( ) . catch ( ( err ) => { console . error ( err ) ; } ) ;