"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const assert_1 = __importDefault(require("assert"));
const utils_1 = require("./utils");
const ms_1 = __importDefault(require("ms"));
const launch_1 = require("./launch");
const consts_1 = require("./consts");
describe('Launch api test', function () {
    this.timeout(ms_1.default('10 seconds'));
    beforeEach(async () => {
        await fs_extra_1.default.emptyDir(path_1.default.resolve(__dirname, '..', '.screenplay'));
    });
    it('Launch api test passing', async function () {
        this.timeout(ms_1.default('10 seconds'));
        utils_1.assertNotNullOrUndefined(this.test);
        const mockedDateConstructor = Object.create(Date);
        let nowCallsCounter = 1;
        mockedDateConstructor.now = function mockedNow() {
            return nowCallsCounter++;
        };
        await launch_1.launchImpl({
            testName: this.test.fullTitle(), noServer: true, automationLibrary: 'puppeteer', browserOptions: { headless: true, browserArgs: ['--no-sandbox', '--disable-setuid-sandbox'] },
        }, async (page) => {
            await page.goto('http://jsbin.testim.io/ner/1');
            await page.click('#test');
            await page.click('#test');
            await page.click('#test');
            await page.click('#test');
            await page.click('#test');
            await page.click('#test');
            await page.click('#reset');
            await page.click('#test');
            const counter = await page.$('#counter');
            if (!counter) {
                throw new Error('counter not found');
            }
            const textProp = await counter.getProperty('innerText');
            const text = await textProp.jsonValue();
            assert_1.default.equal(text, '1');
        }, mockedDateConstructor);
        const runResultsDir = path_1.default.resolve(__dirname, '..', '.screenplay', 'runs', consts_1.FALLBACK_RUN_ID);
        const allResultsInDir = (await fs_extra_1.default.readdir(runResultsDir)).sort();
        const testResultsFixture = await utils_1.readJsonTestSnapshotFile(path_1.default.resolve(__dirname, 'fixtures', 'launch-api-test-passing', 'results.json'));
        const testResults = await utils_1.readJsonTestSnapshotFile(path_1.default.resolve(runResultsDir, allResultsInDir[0], 'results.json'));
        assert_1.default.deepStrictEqual(testResults, testResultsFixture);
        for (const stepResult of testResults.steps) {
            if (stepResult.screenshot) {
                assert_1.default.equal(await fs_extra_1.default.pathExists(path_1.default.resolve(runResultsDir, allResultsInDir[0], stepResult.screenshot)), true, `${stepResult.screenshot} is missing`);
            }
        }
    });
    it('Launch api test = failing', async function () {
        this.timeout(ms_1.default('10 seconds'));
        utils_1.assertNotNullOrUndefined(this.test);
        const mockedDateConstructor = Object.create(Date);
        let nowCallsCounter = 1;
        mockedDateConstructor.now = function mockedNow() {
            return nowCallsCounter++;
        };
        const testDetails = this.test;
        await assert_1.default.rejects(() => launch_1.launchImpl({
            testName: testDetails.fullTitle(), noServer: true, automationLibrary: 'puppeteer', browserOptions: { headless: true, browserArgs: ['--no-sandbox', '--disable-setuid-sandbox'] },
        }, async (page) => {
            await page.goto('http://jsbin.testim.io/ner/1');
            await page.click('#test');
            await page.click('#test');
            await page.click('#test');
            await page.click('#test');
            await page.click('#test');
            await page.click('#test');
            await page.click('#reset');
            await page.click('#test');
            const counter = await page.$('#counter');
            if (!counter) {
                throw new Error('counter not found');
            }
            const textProp = await counter.getProperty('innerText');
            const text = await textProp.jsonValue();
            assert_1.default.equal(text, '2');
        }, mockedDateConstructor));
        const runResultsDir = path_1.default.resolve(__dirname, '..', '.screenplay', 'runs', consts_1.FALLBACK_RUN_ID);
        const allResultsInDir = await fs_extra_1.default.readdir(runResultsDir);
        const testResultsFixture = await utils_1.readJsonTestSnapshotFile(path_1.default.resolve(__dirname, 'fixtures', 'launch-api-test-failing', 'results.json'));
        const testResults = await utils_1.readJsonTestSnapshotFile(path_1.default.resolve(runResultsDir, allResultsInDir[0], 'results.json'));
        assert_1.default.deepStrictEqual(testResults, testResultsFixture);
        for (const stepResult of testResults.steps) {
            if (stepResult.screenshot) {
                assert_1.default.equal(await fs_extra_1.default.pathExists(path_1.default.resolve(runResultsDir, allResultsInDir[0], stepResult.screenshot)), true, `${stepResult.screenshot} is missing`);
            }
        }
    });
});
