"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const http_1 = require("http");
const resolve_1 = require("resolve");
const findOpenPort_1 = require("../browser/findOpenPort");
const openBrowser_1 = require("../browser/openBrowser");
const server_1 = require("../browser/server");
const setupBrowser_1 = require("../browser/setupBrowser");
function runBrowserTests(options, cwd, logger, testMetadata) {
    return __awaiter(this, void 0, void 0, function* () {
        const { timeout, webpackConfiguration } = options;
        const { port, outputDirectory } = yield setup(logger, cwd, timeout, testMetadata, webpackConfiguration);
        return new Promise(runTests(cwd, options, logger, outputDirectory, port)).catch(() => runBrowserTests(options, cwd, logger, testMetadata));
    });
}
exports.runBrowserTests = runBrowserTests;
function setup(logger, cwd, timeout, testMetadata, webpackConfiguration) {
    return __awaiter(this, void 0, void 0, function* () {
        yield logger.log('Bundling tests...');
        const port = yield findOpenPort_1.findOpenPort();
        const { outputDirectory } = yield setupBrowser_1.setupBrowser(cwd, port, timeout, logger, testMetadata, webpackConfiguration);
        yield logger.log('Spinning up server...');
        return { port, outputDirectory };
    });
}
function runTests(cwd, options, logger, outputDirectory, port) {
    const { keepAlive } = options;
    return (resolve) => {
        let server;
        let dispose;
        const app = server_1.setupServer(logger, outputDirectory, (results, stats) => {
            if (server) {
                server.close();
            }
            if (!keepAlive && dispose) {
                dispose();
            }
            resolve({ results, stats });
        });
        server = http_1.createServer(app);
        server.listen(port, '0.0.0.0', () => openTestBrowser(cwd, logger, options, port, d => (dispose = d)));
    };
}
function openTestBrowser(cwd, logger, options, port, setDispose) {
    return __awaiter(this, void 0, void 0, function* () {
        const { browser, keepAlive } = options;
        const url = `http://localhost:${port}`;
        if (browser !== 'chrome-headless') {
            yield logger.log('Opening browser...');
            const instance = yield openBrowser_1.openBrowser(browser, url, keepAlive, yield openBrowser_1.getLauncher());
            setDispose(() => instance.stop());
            return;
        }
        // tslint:disable-next-line:no-var-requires
        const { launch } = require(resolve_1.sync('chrome-launcher', { basedir: cwd }));
        const chrome = yield launch({ startingUrl: url });
        setDispose(() => chrome.kill());
    });
}
//# sourceMappingURL=runBrowserTests.js.map