import * as path from 'path' ; import { launch } from 'puppeteer' ; import test from 'ava' ; import { Category } from '@hint/utils-types' ; import { readFileAsync } from '@hint/utils-fs' ; import { Server } from '@hint/utils-create-server' ; import { Events , InjectDetails , Results } from '../src/shared/types' ; type Listener = ( message : Events ) => void ; param results const mockExtensionAPIs = ( results ) => { ( window as any ) . __startPromise = new Promise < InjectDetails > ( ( resolve ) => { const listeners = new Set < Listener > ( ) ; ( window as any ) . browser = { devtools : { inspectedWindow : { eval ( ) { } , tabId : 1 } , network : { getHAR : ( ) => { } , onRequestFinished : { addListener : ( ) => { } , removeListener : ( ) => { } } } , panels : { themeName : 'dark' } } , i18n : { getMessage : ( ) => { return 'Localized string.' ; } } , runtime : { connect : ( ) => { return { onMessage : { addListener : ( fn ) => { listeners . add ( fn ) ; } , removeListener : ( fn ) => { listeners . delete ( fn ) ; } } } ; } , sendMessage : ( evt ) => { if ( evt . enable ) { resolve ( evt . enable ) ; setTimeout ( ( ) => { listeners . forEach ( ( listener ) => { listener ( { results } ) ; } ) ; } , 500 ) ; } } } } ; } ) ; } ; test ( 'It builds a configuration, starts a scan, and displays results' , async ( t ) => { const [ htmlSource , jsSource ] = await Promise . all ( [ readFileAsync ( path . resolve ( __dirname , '../bundle/devtools/panel.html' ) ) , readFileAsync ( path . resolve ( __dirname , '../bundle/devtools/panel.js' ) ) ] ) ; const server = await Server . create ( { configuration : { '/' : htmlSource , '/panel.js' : { content : jsSource , headers : { 'Content-Type' : 'text/javascript' } } } } ) ; const url = `http://localhost:${ server . port }/` ; const browser = await launch ( ) ; const page = ( await browser . pages ( ) ) [ 0 ] ; await page . evaluateOnNewDocument ( mockExtensionAPIs , { categories : [ { hints : [ ] , name : Category . compatibility , passed : 0 } ] , url : '' } as Results as any ) ; await page . goto ( url ) ; await page . click ( 'input[type="checkbox"][value="accessibility"]' ) ; await page . click ( 'button[type="submit"]' ) ; const injectDetails = await page . evaluate ( ( ) => { return ( window as any ) . __startPromise ; } ) ; t . deepEqual ( injectDetails . config , { disabledCategories : [ Category . accessibility ] } ) ; await page . waitForSelector ( '[class*="results_root"]' ) ; const categories = await page . $$ ( '[class*="category_root"]' ) ; t . is ( categories . length , 1 ) ; await browser . close ( ) ; await server . stop ( ) ; } ) ;