import 'reflect-metadata' ; import * as fs from 'fs' ; import { CrawlerRunOptions } from 'accessibility-insights-crawler' ; import { IMock , It , Mock , Times } from 'typemoq' ; import { ScanArguments } from '../scan-arguments' ; import { ConsolidatedReportGenerator } from '../report/consolidated-report-generator' ; import { CrawlerParametersBuilder } from '../crawler/crawler-parameters-builder' ; import { AICrawler , CombinedScanResult } from '../crawler/ai-crawler' ; import { OutputFileWriter } from '../files/output-file-writer' ; import { BaselineOptionsBuilder } from '../baseline/baseline-options-builder' ; import { BaselineOptions } from '../baseline/baseline-types' ; import { BaselineFileUpdater } from '../baseline/baseline-file-updater' ; import { CrawlerCommandRunner } from './crawler-command-runner' ; describe ( 'CrawlerCommandRunner' , ( ) => { const testUrl = 'http://localhost/' ; let scanArguments ; let crawlerOptions ; let baselineOptions ; let crawlerMock ; let crawlerParametersBuilderMock ; let outputFileWriterMock ; let consolidatedReportGeneratorMock ; let baselineOptionsBuilderMock ; let baselineFileUpdaterMock ; let fsMock ; let testSubject ; let stubCombinedScanResults ; let stdout ; beforeEach ( ( ) => { scanArguments = { url : testUrl , output : './dir' } ; crawlerOptions = { baseUrl : scanArguments . url , localOutputDir : scanArguments . output , inputUrls : undefined , discoveryPatterns : undefined , simulate : undefined , selectors : undefined , maxRequestsPerCrawl : undefined , restartCrawl : undefined , snapshot : undefined , memoryMBytes : undefined , silentMode : undefined , } ; baselineOptions = undefined ; stubCombinedScanResults = { } as CombinedScanResult ; crawlerMock = Mock . ofType < AICrawler > ( ) ; crawlerParametersBuilderMock = Mock . ofType < CrawlerParametersBuilder > ( ) ; outputFileWriterMock = Mock . ofType < OutputFileWriter > ( ) ; consolidatedReportGeneratorMock = Mock . ofType < ConsolidatedReportGenerator > ( ) ; baselineOptionsBuilderMock = Mock . ofType < BaselineOptionsBuilder > ( ) ; baselineFileUpdaterMock = Mock . ofType < BaselineFileUpdater > ( ) ; fsMock = Mock . ofInstance ( fs ) ; fsMock . setup ( ( o ) => o . existsSync ( scanArguments . output ) ) . returns ( ( ) => false ) . verifiable ( ) ; crawlerParametersBuilderMock . setup ( ( o ) => o . build ( It . isAny ( ) ) ) . returns ( ( ) => crawlerOptions ) . verifiable ( ) ; baselineOptionsBuilderMock . setup ( ( o ) => o . build ( It . isAny ( ) ) ) . returns ( ( ) => undefined ) . verifiable ( ) ; stdout = [ ] ; const stdoutWriter = ( s ) => stdout . push ( s ) ; testSubject = new CrawlerCommandRunner ( crawlerMock . object , crawlerParametersBuilderMock . object , consolidatedReportGeneratorMock . object , outputFileWriterMock . object , baselineOptionsBuilderMock . object , baselineFileUpdaterMock . object , fsMock . object , stdoutWriter ) ; } ) ; afterEach ( ( ) => { crawlerMock . verifyAll ( ) ; outputFileWriterMock . verifyAll ( ) ; consolidatedReportGeneratorMock . verifyAll ( ) ; fsMock . verifyAll ( ) ; } ) ; it ( 'skip run when last scan data persisted' , async ( ) => { fsMock . reset ( ) ; fsMock . setup ( ( o ) => o . existsSync ( scanArguments . output ) ) . returns ( ( ) => true ) . verifiable ( ) ; crawlerMock . setup ( ( o ) => o . crawl ( It . isAny ( ) , It . isAny ( ) ) ) . verifiable ( Times . never ( ) ) ; await testSubject . runCommand ( scanArguments ) ; expect ( stdout ) . toMatchInlineSnapshot ( `
            Array [
              "The last scan result was found on a disk. Use --continue option to continue scan for the last URL provided, or --restart option to delete the last scan result.",
            ]
        ` ) ; } ) ; it ( 'continue run with --restart when last scan data persisted' , async ( ) => { setupReportOutput ( '/path/to/report' ) ; scanArguments = { url : testUrl , output : './dir' , restart : true } ; crawlerOptions . restartCrawl = true ; fsMock . reset ( ) ; fsMock . setup ( ( o ) => o . existsSync ( scanArguments . output ) ) . returns ( ( ) => true ) . verifiable ( ) ; crawlerMock . setup ( ( o ) => o . crawl ( crawlerOptions , baselineOptions ) ) . returns ( async ( ) => stubCombinedScanResults ) . verifiable ( ) ; await testSubject . runCommand ( scanArguments ) ; expect ( stdout ) . toMatchInlineSnapshot ( `
Array [
  "Generating summary scan report...",
  "Summary report was saved as /path/to/report",
]
` ) ; } ) ; it ( 'continue run with --continue when last scan data persisted' , async ( ) => { setupReportOutput ( '/path/to/report' ) ; scanArguments = { url : testUrl , output : './dir' , continue : true } ; fsMock . reset ( ) ; fsMock . setup ( ( o ) => o . existsSync ( scanArguments . output ) ) . returns ( ( ) => true ) . verifiable ( ) ; crawlerMock . setup ( ( o ) => o . crawl ( crawlerOptions , baselineOptions ) ) . returns ( async ( ) => stubCombinedScanResults ) . verifiable ( ) ; await testSubject . runCommand ( scanArguments ) ; expect ( stdout ) . toMatchInlineSnapshot ( `
Array [
  "Generating summary scan report...",
  "Summary report was saved as /path/to/report",
]
` ) ; } ) ; it ( 'run crawler' , async ( ) => { setupReportOutput ( '/path/to/report' ) ; crawlerMock . setup ( ( o ) => o . crawl ( crawlerOptions , baselineOptions ) ) . returns ( async ( ) => stubCombinedScanResults ) . verifiable ( ) ; await testSubject . runCommand ( scanArguments ) ; expect ( stdout ) . toMatchInlineSnapshot ( `
            Array [
              "Generating summary scan report...",
              "Summary report was saved as /path/to/report",
            ]
        ` ) ; } ) ; it ( 'runs crawler with baseline options' , async ( ) => { setupReportOutput ( '/path/to/report' ) ; baselineOptions = { baselineContent : null , } ; baselineOptionsBuilderMock . reset ( ) ; baselineOptionsBuilderMock . setup ( ( o ) => o . build ( scanArguments ) ) . returns ( ( ) => baselineOptions ) . verifiable ( ) ; crawlerMock . setup ( ( o ) => o . crawl ( crawlerOptions , baselineOptions ) ) . returns ( async ( ) => stubCombinedScanResults ) . verifiable ( ) ; await testSubject . runCommand ( scanArguments ) ; expect ( stdout ) . toMatchInlineSnapshot ( `
            Array [
              "Generating summary scan report...",
              "Summary report was saved as /path/to/report",
            ]
        ` ) ; } ) ; function setupReportOutput ( outputPath ) { consolidatedReportGeneratorMock . setup ( async ( o ) => o . generateReport ( { } , It . isAny ( ) , It . isAny ( ) ) ) . returns ( ( ) => Promise . resolve ( 'report' ) ) . verifiable ( ) ; outputFileWriterMock . setup ( ( o ) => o . writeToDirectory ( scanArguments . output , 'index' , 'html' , 'report' ) ) . returns ( ( ) => '/path/to/report' ) . verifiable ( ) ; } } ) ;