"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
const puppeteer_1 = __importDefault(require("puppeteer"));
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"));
describe('Sanity integration test', function () {
    this.timeout(ms_1.default('10 seconds'));
    let browser;
    let browserContext;
    let page;
    before(async () => {
        browser = await puppeteer_1.default.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
    });
    after(async () => {
        await browser.close();
    });
    beforeEach(async () => {
        browserContext = await browser.createIncognitoBrowserContext();
        page = await browserContext.newPage();
    });
    afterEach(async () => {
        await page.close();
        await browserContext.close();
    });
    it('Sanity integration test 1', async function () {
        this.timeout(ms_1.default('10 seconds'));
        utils_1.assertNotNullOrUndefined(this.test);
        const startTestParams = {
            runId: 'mock_invocation_id',
            projectRoot: path_1.default.resolve(__dirname, 'testsResults'),
            fullName: this.test.fullTitle(),
            description: this.test.title,
            fullSuitePath: 'FAKE_PATH',
        };
        const mockedDateConstructor = Object.create(Date);
        let nowCallsCounter = 1;
        mockedDateConstructor.now = function mockedNow() {
            return nowCallsCounter++;
        };
        const { page: playedPage, endTest } = await index_1.attach({
            page,
            startTestParams,
        }, mockedDateConstructor);
        await playedPage.goto('http://jsbin.testim.io/ner/1');
        await playedPage.click('#test');
        page.evaluate(() => {
            setTimeout(() => {
                throw new Error('injected error');
            }, 0);
        });
        await playedPage.click('#test');
        await playedPage.click('#test');
        page.evaluate(() => {
            setTimeout(() => {
                console.warn('injected console');
            }, 0);
        });
        await playedPage.click('#test');
        const counterEl = await playedPage.$('#counter');
        assert_1.default.notEqual(counterEl, null);
        utils_1.assertNotNullOrUndefined(counterEl);
        const text = await (await counterEl.getProperty('innerText')).jsonValue();
        assert_1.default.equal(text, '4');
        await playedPage.click('#reset');
        const text2 = await (await counterEl.getProperty('innerText')).jsonValue();
        assert_1.default.equal(text2, '0');
        const allButtons = await playedPage.$$('button');
        await allButtons[0].click();
        const text3 = await (await counterEl.getProperty('innerText')).jsonValue();
        assert_1.default.equal(text3, '1');
        assert_1.default.equal(allButtons.length, 2);
        await endTest({
            success: false,
            error: { message: 'mocked error test failed' },
        });
        const expectedResults = path_1.default.resolve(__dirname, 'testsResults', '.screenplay', 'runs', 'mock_invocation_id', utils_1.testUniqueIdentifierFromStartParams(startTestParams));
        const testResultsFixture = JSON.parse(await fs_extra_1.default.readFile(path_1.default.resolve(__dirname, 'fixtures', 'sanity', 'results.json'), 'utf-8'), utils_1.jsonReduceNoiseReviver);
        const testResults = JSON.parse(await fs_extra_1.default.readFile(path_1.default.resolve(expectedResults, 'results.json'), 'utf-8'), utils_1.jsonReduceNoiseReviver);
        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(expectedResults, stepResult.screenshot)), true, `${stepResult.screenshot} is missing`);
            }
        }
    });
});
