// Version: 1.0.0
import {jestState} from '../form.spec';
import {__runLogin} from './login.test';
import {test} from '@jest/globals';
/**
 * Run Critical Updates elements exist tests.
 * @return {Promise<void>}
 * @private
 */
async function __criticalUpdatesElementsExist() {
    await jestState.page.goto(
        jestState.envData.root + '/wp-admin/options-general.php?page=spbc&spbc_tab=critical_updates',
        {waitUntil: 'load'},
    );

    // Wait for the tab container
    await jestState.page.waitForSelector('[id=spbc_tab-critical_updates]');

    // Wait for the AJAX content to load by checking for the legend element
    try {
        await jestState.page.waitForSelector('.spbc_critical_updates-legend', { timeout: 10000 });
    } catch (e) {
        console.log('Critical updates legend element not found after AJAX load');
        throw e;
    }

    const criticalUpdatesElementsClasses = [
        '.spbc_tab-critical_updates',
        '.spbc_critical_updates-legend',
    ];
    
    for (let i = 0; i < criticalUpdatesElementsClasses.length; i++) {
        await jestState.page.$eval(criticalUpdatesElementsClasses[i], (el) => el.innerText);
    }
}

/**
 * Run Critical Updates tests.
 * This test is a wrapper for group of tests.
 * @param {boolean} doLogin
 * @return {void}
 */
export function groupCriticalUpdatesInfo(doLogin = true) {
    if (doLogin) {
        test('LOGIN', async () => {
            await __runLogin();
        }, jestState.globals.globalTimeout);
    }
    test('CRITICAL UPDATES ELEMENTS EXISTS', async () => {
        await __criticalUpdatesElementsExist();
    }, jestState.globals.globalTimeout);
    // more test there
}
