<commit_before>import * as puppeteer from 'puppeteer';
import * as process from 'process';
import * as path from 'path';
import { ErrorReporter, ReportType } from './shared/error-reporter';
import { Configuration } from './shared/configuration';
// ts-node required for runtime typescript compilation of sandboxes.ts
require('ts-node/register');


interface ScenarioSummary {
    url: string;
    name: string;
    description: string;
}

let browser: any;
let currentScenario = '';
const reporter = new ErrorReporter();

// Ensure Chromium instances are destroyed on err
process.on('unhandledRejection', () => {
    if (browser) browser.close();
});

export async function verifySandboxes(configuration: Configuration, sandboxesPath: string) {
    await main(configuration, sandboxesPath);
}

/////////////////////////////////

async function main (configuration: Configuration, sandboxesPath: string) {
    let timeoutAttempts = configuration.timeoutAttempts;
    browser = await puppeteer.launch({
        headless: true,
        handleSIGINT: false,
        args: configuration.chromeArguments
    });

    const scenarios = getSandboxMetadata(configuration.baseUrl, configuration.randomScenario, sandboxesPath);
    console.log(`Retrieved ${scenarios.length} scenarios.\n`);
    for (let i = 0; i < scenarios.length; i++) {
        await openScenarioInNewPage(scenarios[i], configuration.timeoutAttempts);
    }

    browser.close();

    if (reporter.errors.length > 0) {
        reporter.compileReport();
        process.exit(1);
    } else {
        process.exit(0);
    }
}

/**
 * Creates a Chromium page and navigates to a scenario (URL).
 * If Chromium is not able to connect to the provided page, it will issue a series
 * of retries before it finally fails.
 * @param scenario - Scenario to visit
 */
async function openScenarioInNewPage(scenario: ScenarioSummary, timeoutAttempts: number) {
    if (timeoutAttempts === 0) {
        await browser.close();
        process.exit(1);
    }

    const page = await browser.newPage();
    page.on('console', (msg: any) => onConsoleErr(msg));
    currentScenario = scenario.name;

    try {
        await page.goto(scenario.url);
    } catch (e) {
        await delay(5000);
        console.log(`Attempting to connect. (Attempts Remaining: ${timeoutAttempts})`);
        await openScenarioInNewPage(scenario, timeoutAttempts - 1);
    }

    console.log(`Checking: ${currentScenario}: ${scenario.description}`);
    await page.close();
}

/**
 * Retrieves Sandbox scenario URLs, descriptions, and names
 * @param baseUrl - Base URL of scenario path e.g. http://localhost:4201
 * @param selectRandomScenario - Whether or not to select one random scenario of all availalble scenarios for a component
 * @param path - Path to sandboxes.ts
 */
function getSandboxMetadata(baseUrl: string, selectRandomScenario: boolean, path: string): ScenarioSummary[] {
    const scenarios: ScenarioSummary[] = [];

    loadSandboxMenuItems(path).forEach((scenario: any) => {
        if (selectRandomScenario) {
            const randomItemKey = getRandomKey(scenario.scenarioMenuItems.length);
            scenario.scenarioMenuItems<commit_msg>ensure pages are closed during polling process<commit_after>import * as puppeteer from 'puppeteer';
import * as process from 'process';
import * as path from 'path';
import { ErrorReporter, ReportType } from './shared/error-reporter';
import { Configuration } from './shared/configuration';
// ts-node required for runtime typescript compilation of sandboxes.ts
require('ts-node/register');


interface ScenarioSummary {
    url: string;
    name: string;
    description: string;
}

let browser: any;
let currentScenario = '';
const reporter = new ErrorReporter();

// Ensure Chromium instances are destroyed on err
process.on('unhandledRejection', () => {
    if (browser) browser.close();
});

export async function verifySandboxes(configuration: Configuration, sandboxesPath: string) {
    await main(configuration, sandboxesPath);
}

/////////////////////////////////

async function main (configuration: Configuration, sandboxesPath: string) {
    let timeoutAttempts = configuration.timeoutAttempts;
    browser = await puppeteer.launch({
        headless: true,
        handleSIGINT: false,
        args: configuration.chromeArguments
    });

    const scenarios = getSandboxMetadata(configuration.baseUrl, configuration.randomScenario, sandboxesPath);
    console.log(`Retrieved ${scenarios.length} scenarios.\n`);
    for (let i = 0; i < scenarios.length; i++) {
        await openScenarioInNewPage(scenarios[i], configuration.timeoutAttempts);
    }

    browser.close();

    if (reporter.errors.length > 0) {
        reporter.compileReport();
        process.exit(1);
    } else {
        process.exit(0);
    }
}

/**
 * Creates a Chromium page and navigates to a scenario (URL).
 * If Chromium is not able to connect to the provided page, it will issue a series
 * of retries before it finally fails.
 * @param scenario - Scenario to visit
 */
async function openScenarioInNewPage(scenario: ScenarioSummary, timeoutAttempts: number) {
    if (timeoutAttempts === 0) {
        await browser.close();
        process.exit(1);
    }

    const page = await browser.newPage();
    page.on('console', (msg: any) => onConsoleErr(msg));
    currentScenario = scenario.name;

    try {
        console.log(`Checking: ${currentScenario}: ${scenario.description}`);
        await page.goto(scenario.url);
    } catch (e) {
        await page.close();
        await delay(5000);
        console.log(`Attempting to connect. (Attempts Remaining: ${timeoutAttempts})`);
        await openScenarioInNewPage(scenario, timeoutAttempts - 1);
    }
}

/**
 * Retrieves Sandbox scenario URLs, descriptions, and names
 * @param baseUrl - Base URL of scenario path e.g. http://localhost:4201
 * @param selectRandomScenario - Whether or not to select one random scenario of all availalble scenarios for a component
 * @param path - Path to sandboxes.ts
 */
function getSandboxMetadata(baseUrl: string, selectRandomScenario: boolean, path: string): ScenarioSummary[] {
    const scenarios: ScenarioSummary[] = [];

    loadSandboxMenuItems(path).forEach((scenario: any) => {
        if (selectRandomScenario) {
            const randomItemKey = getRandomKey(scenario.scenarioMenuItems.length);
            scenario.scenarioMenuItems
{'fraction_non_alphanumeric': 0.12047440699126093, 'fraction_numerical': 0.0046816479400749065, 'mean_word_length': 3.545390070921986, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7432114', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1754, 'n_words': 513}