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());
    });
};
import { createServer } from 'http';
import { sync } from 'resolve';
import { findOpenPort } from '../browser/findOpenPort';
import { getLauncher, openBrowser } from '../browser/openBrowser';
import { setupServer } from '../browser/server';
import { setupBrowser } from '../browser/setupBrowser';
export 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));
    });
}
function setup(logger, cwd, timeout, testMetadata, webpackConfiguration) {
    return __awaiter(this, void 0, void 0, function* () {
        yield logger.log('Bundling tests...');
        const port = yield findOpenPort();
        const { outputDirectory } = yield 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 = setupServer(logger, outputDirectory, (results, stats) => {
            if (server) {
                server.close();
            }
            if (!keepAlive && dispose) {
                dispose();
            }
            resolve({ results, stats });
        });
        server = 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(browser, url, keepAlive, yield getLauncher());
            setDispose(() => instance.stop());
            return;
        }
        // tslint:disable-next-line:no-var-requires
        const { launch } = require(sync('chrome-launcher', { basedir: cwd }));
        const chrome = yield launch({ startingUrl: url });
        setDispose(() => chrome.kill());
    });
}
//# sourceMappingURL=runBrowserTests.js.map