'use strict';

/**
 * How to run tests:
 * 1. Run all tests: `jest --testPathPattern form.spec -t runAll --watch`
 * 2. Run standalone test: `jest --testPathPattern form.spec -t groupNavBar --watch`
 * 3. If you use IDE like PHPStorm, you can run tests from the IDE.
 * Also, running is available by prepared npm run scripts.
 */

import puppeteer from 'puppeteer';
import {afterAll, beforeAll, describe, test} from '@jest/globals';
// utils
import {getEnvData, getGlobals} from './utils';
// use login handler for all tests case
import {__runLogin} from './__tests__/login.test';

// use group tests
import {groupTopInfo} from './__tests__/topinfo.test';
import {groupSettingsTab} from './__tests__/settings_tab.test';
import {groupNavBar} from './__tests__/navbar.test';
import {groupCriticalUpdatesInfo} from './__tests__/critical_upd_tab.test';
import {groupSettingsSetDirExclusion} from './__tests__/settings_set_dir_exclusion.test';
import {groupScanWork} from './__tests__/scan_work.test';

/**
 * State global object. It contains all necessary data for tests. Should be available in all tests via import.
 */
let jestState = {
    page: null,
    browser: null,
    globals: getGlobals(),
    envData: getEnvData(),
};


/**
 * This code must be executed before all tests to run puppeteer.
 */
beforeAll(async () => {
    // init browser
    jestState.browser = await puppeteer.launch({
        // where is chrome executable
        executablePath: jestState.envData.chrome_executable,
        // is headless
        headless: jestState.envData.headless,
        slowMo: 30,
        ignoreDefaultArgs: ['--disable-extensions'],
        args: [
            '--no-sandbox',
            '--disable-setuid-sandbox',
            `--window-size=${jestState.globals.browserWidth},${jestState.globals.browserHeight}`,
        ],
    });
    // prepare browser page
    jestState.page = await jestState.browser.newPage();
    await jestState.page.setUserAgent(jestState.envData.chrome_ua);
    await jestState.page.setViewport({width: jestState.globals.browserWidth, height: jestState.globals.browserHeight});
}, 16000);

// close browser after all tests
afterAll(() => {
    jestState.browser.close();
});

// export jestState for all tests
export {jestState};

/**
 * All tests flow.
 * Make note, that login test is obligatory before running the tests.
 * */
describe('runAll', () => {
    test('LOGIN', async () => {
        await __runLogin();
    }, jestState.globals.globalTimeout);
    groupSettingsTab(false);
    groupTopInfo(false);
    groupNavBar(false);
    groupCriticalUpdatesInfo(false);
    groupSettingsSetDirExclusion(false);
    groupScanWork(false);
});

/**
 * Standalone tests. Run them separately if you need.
 * By default, any test tun login process before running the tests.
 */

describe('groupSettingsTab', () => {
    groupSettingsTab();
});

describe('groupTopInfo', () => {
    groupTopInfo();
});

describe('groupNavBar', () => {
    groupNavBar();
});

describe('groupCriticalUpdatesInfo', () => {
    groupCriticalUpdatesInfo();
});

describe('groupSettingsSetDirExclusion', () => {
    groupSettingsSetDirExclusion();
});

describe('groupScanWork', () => {
    groupScanWork();
});
