import {test} from '@jest/globals';
import {__runLogin} from './login.test';
import {jestState} from '../form.spec';

export async function __scanWork() {
    await jestState.page.goto(
        jestState.envData.root + '/wp-admin/options-general.php?page=spbc&spbc_tab=scanner',
        {waitUntil: 'load'},
    );

    let field = '#spbc_perform_scan';
    await jestState.page.waitForSelector(field);
    await jestState.page.click(field);

    await jestState.page.waitForSelector('#spbc_scan_accordion');

    await jestState.page.waitForSelector('div[refresh_control_tab="critical"]');
    await jestState.page.click('div[refresh_control_tab="critical"]');
    await jestState.page.waitForSelector('div[refresh_control_tab="cure_log"]');
    await jestState.page.click('div[refresh_control_tab="cure_log"]');
    await jestState.page.waitForSelector('div[refresh_control_tab="unsafe_permissions"]');
    await jestState.page.click('div[refresh_control_tab="unsafe_permissions"]');
    await jestState.page.waitForSelector('div[refresh_control_tab="files_listing"]');
    await jestState.page.click('div[refresh_control_tab="files_listing"]');
    await jestState.page.waitForSelector('div[refresh_control_tab="oscron"]');
    await jestState.page.click('div[refresh_control_tab="oscron"]');

    await new Promise(resolve => setTimeout(resolve, 1000));
}

/**
 * Run tests.
 * This test is a wrapper for group of tests.
 * @param {boolean} doLogin
 * @return {void}
 */
export function groupScanWork(doLogin = true) {
    if (doLogin) {
        test('LOGIN', async () => {
            await __runLogin();
        }, jestState.globals.globalTimeout);
    }

    test('SCAN WORK', async () => {
        await __scanWork();
    }, jestState.globals.globalTimeout);
}
