import random

from browsergym.workarena import get_all_tasks_humans

tasks_l2 = get_all_tasks_humans(filter="l2", meta_seed=42)
tasks_l3 = get_all_tasks_humans(filter="l3", meta_seed=42)

tasks = tasks_l2 + tasks_l3
random.shuffle(tasks)

annotators = [
    "darwiche",
    "parikh",
    "marchand",
    "paquet",
    "nayal",
    "huang",
    "subbaraj",
    "williams",
    "li",
    "marcotte",
    "rancourt",
    "prince_tremblay",
    "ashok",
    "bajaj",
]
random.shuffle(annotators)

print("Number of tasks: ", len(tasks))
print("Number of annotators: ", len(annotators))

tasks_by_annotator = {}
n_base_assignment = len(tasks) // len(annotators)
n_extra_assignment = len(tasks) % len(annotators)
for i, annotator in enumerate(annotators):
    n_assignments = n_base_assignment + (1 if i < n_extra_assignment else 0)
    tasks_by_annotator[annotator] = tasks[:n_assignments]
    tasks = tasks[n_assignments:]
    print(f"{annotator}: {n_assignments}")

    with open(f"{annotator}.tasks", "w") as f:
        for task in tasks_by_annotator[annotator]:
            f.write(f"{task[0].__name__},{task[1]}\n")


import pytest

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright

from playwright.sync_api import Page

from browsergym.workarena.instance import SNowInstance
from browsergym.workarena.utils import ui_login, url_login

@pytest.mark.parametrize("login_func", [ui_login, url_login])
def test_login_correct_credentials(login_func, page: Page):
    """
    Test logging into the instance with the correct credentials

    """
    # Log in with correct credentials
    instance = SNowInstance()
    login_func(instance=instance, page=page)

@pytest.mark.parametrize("login_func", [ui_login, url_login])
def test_login_wrong_credentials(login_func, page: Page):
    """
    Test logging into the instance with the wrong credentials

    """
    # Log in with wrong credentials
    instance = SNowInstance(snow_credentials=("wrong", "wrong"))
    with pytest.raises(RuntimeError):
        login_func(instance=instance, page=page)


from utils import setup_playwright

from playwright.sync_api import Page

from browsergym.workarena.instance import SNowInstance
from browsergym.workarena.utils import ui_login, url_login

@pytest.mark.parametrize("login_func", [ui_login, url_login])
def test_login_correct_credentials(login_func, page: Page):
    """
    Test logging into the instance with the correct credentials

    """
    # Log in with correct credentials
    instance = SNowInstance()
    login_func(instance=instance, page=page)

@pytest.mark.parametrize("login_func", [ui_login, url_login])
def test_login_wrong_credentials(login_func, page: Page):
    """
    Test logging into the instance with the wrong credentials

    """
    # Log in with wrong credentials
    instance = SNowInstance(snow_credentials=("wrong", "wrong"))
    with pytest.raises(RuntimeError):
        login_func(instance=instance, page=page)


from copy import deepcopy
import json
import numpy as np
import pytest

from browsergym.workarena.tasks.compositional.utils.knapsack import KnapsackInstanceGenarator
from browsergym.workarena.tasks.compositional.utils.infeasible_configs import (
    get_infeasible_form_config,
    get_infeasible_service_catalog_config,
    get_infeasible_filter_config,
    get_infeasible_sort_config,
)
from browsergym.workarena.config import (
    CREATE_USER_CONFIG_PATH,
    ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH,
    FILTER_USER_LIST_CONFIG_PATH,
    SORT_USER_LIST_CONFIG_PATH,
)

@pytest.mark.parametrize(
    "mode", ["random", "trivial", "single_item", "single_item_uniform", "n_items"]
)
def test_knapsack(mode: str, num_items_in_solution: int = 2):
    num_items_in_solution = 2 if mode == "n_items" else 1
    knapsack = KnapsackInstanceGenarator(
        random=np.random,
        num_items=3,
        max_capacity=150000,
        mode=mode,
        num_items_in_solution=num_items_in_solution,
    )
    investments, max_return, selected_indices = knapsack.get_instance()

    # In these modes, all items are identical, so the optimal solution can be any
    if mode in ["n_items", "single_item_uniform"]:
        selected_indices = [i for i in range(num_items_in_solution)]

    assert len(investments) == 3
    assert sum(investments[i][0] for i in selected_indices) <= 150000
    assert max_return == sum(investments[i][1] for i in selected_indices)

    if mode != "trivial":
        unselected_index = [i for i in range(3) if i not in selected_indices][0]
        assert (
            sum(investments[i][0] for i in selected_indices) + investments[unselected_index][0]
            > 150000
        )
    else:
        assert len(selected_indices) == len(investments)

config_generator_and_config_path = [
    [get_infeasible_form_config, CREATE_USER_CONFIG_PATH],
    [get_infeasible_service_catalog_config, ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH],
    [get_infeasible_filter_config, FILTER_USER_LIST_CONFIG_PATH],
    [get_infeasible_sort_config, SORT_USER_LIST_CONFIG_PATH],
]

@pytest.mark.parametrize("function_to_path", config_generator_and_config_path)
def test_invalid_config_generator(function_to_path):
    def parse_nested_dict(nested_dict, keywords):
        """Look for keywords in a nested dictionary.
        Return True if any keyword is found, False otherwise.
        """
        for key, value in nested_dict.items():
            if key in keywords or value in keywords:
                return True
            if isinstance(value, dict):
                if parse_nested_dict(value, keywords):
                    return True
            elif isinstance(value, list):
                for item in value:
                    if isinstance(item, dict):
                        if parse_nested_dict(item, keywords):
                            return True
            elif isinstance(value, str):
                for keyword in keywords:
                    if keyword in value:
                        return True
        return False

    config_generator, config_path = function_to_path
    with open(config_path, "r") as f:
        config = json.load(f)[0]
    base_config = deepcopy(config)

    invalid_config, infeasible_reasons = config_generator(random=np.random, config=config)
    assert invalid_config != base_config
    assert parse_nested_dict(invalid_config, infeasible_reasons)
    assert parse_nested_dict(base_config, infeasible_reasons) == False


import pytest

from browsergym.workarena.tasks.compositional.utils.knapsack import KnapsackInstanceGenarator
from browsergym.workarena.tasks.compositional.utils.infeasible_configs import (
    get_infeasible_form_config,
    get_infeasible_service_catalog_config,
    get_infeasible_filter_config,
    get_infeasible_sort_config,
)
from browsergym.workarena.config import (
    CREATE_USER_CONFIG_PATH,
    ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH,
    FILTER_USER_LIST_CONFIG_PATH,
    SORT_USER_LIST_CONFIG_PATH,
)

@pytest.mark.parametrize(
    "mode", ["random", "trivial", "single_item", "single_item_uniform", "n_items"]
)
def test_knapsack(mode: str, num_items_in_solution: int = 2):
    num_items_in_solution = 2 if mode == "n_items" else 1
    knapsack = KnapsackInstanceGenarator(
        random=np.random,
        num_items=3,
        max_capacity=150000,
        mode=mode,
        num_items_in_solution=num_items_in_solution,
    )
    investments, max_return, selected_indices = knapsack.get_instance()

    # In these modes, all items are identical, so the optimal solution can be any
    if mode in ["n_items", "single_item_uniform"]:
        selected_indices = [i for i in range(num_items_in_solution)]

    assert len(investments) == 3
    assert sum(investments[i][0] for i in selected_indices) <= 150000
    assert max_return == sum(investments[i][1] for i in selected_indices)

    if mode != "trivial":
        unselected_index = [i for i in range(3) if i not in selected_indices][0]
        assert (
            sum(investments[i][0] for i in selected_indices) + investments[unselected_index][0]
            > 150000
        )
    else:
        assert len(selected_indices) == len(investments)

config_generator_and_config_path = [
    [get_infeasible_form_config, CREATE_USER_CONFIG_PATH],
    [get_infeasible_service_catalog_config, ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH],
    [get_infeasible_filter_config, FILTER_USER_LIST_CONFIG_PATH],
    [get_infeasible_sort_config, SORT_USER_LIST_CONFIG_PATH],
]

@pytest.mark.parametrize("function_to_path", config_generator_and_config_path)
def test_invalid_config_generator(function_to_path):
    def parse_nested_dict(nested_dict, keywords):
        """Look for keywords in a nested dictionary.
        Return True if any keyword is found, False otherwise.
        """
        for key, value in nested_dict.items():
            if key in keywords or value in keywords:
                return True
            if isinstance(value, dict):


    if mode != "trivial":
        unselected_index = [i for i in range(3) if i not in selected_indices][0]
        assert (
            sum(investments[i][0] for i in selected_indices) + investments[unselected_index][0]
            > 150000
        )
    else:
        assert len(selected_indices) == len(investments)

config_generator_and_config_path = [
    [get_infeasible_form_config, CREATE_USER_CONFIG_PATH],
    [get_infeasible_service_catalog_config, ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH],
    [get_infeasible_filter_config, FILTER_USER_LIST_CONFIG_PATH],
    [get_infeasible_sort_config, SORT_USER_LIST_CONFIG_PATH],
]

@pytest.mark.parametrize("function_to_path", config_generator_and_config_path)
def test_invalid_config_generator(function_to_path):
    def parse_nested_dict(nested_dict, keywords):
        """Look for keywords in a nested dictionary.
        Return True if any keyword is found, False otherwise.
        """
        for key, value in nested_dict.items():
            if key in keywords or value in keywords:
                return True
            if isinstance(value, dict):
                if parse_nested_dict(value, keywords):
                    return True
            elif isinstance(value, list):
                for item in value:
                    if isinstance(item, dict):
                        if parse_nested_dict(item, keywords):
                            return True
            elif isinstance(value, str):
                for keyword in keywords:
                    if keyword in value:
                        return True
        return False

    config_generator, config_path = function_to_path
    with open(config_path, "r") as f:
        config = json.load(f)[0]
    base_config = deepcopy(config)

    invalid_config, infeasible_reasons = config_generator(random=np.random, config=config)
    assert invalid_config != base_config
    assert parse_nested_dict(invalid_config, infeasible_reasons)
    assert parse_nested_dict(base_config, infeasible_reasons) == False


"""
Tests that are not specific to any particular kind of task.

"""

import json
import logging
import pickle
import pytest

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright

from playwright.sync_api import Page, TimeoutError
from tenacity import retry, stop_after_attempt, retry_if_exception_type

from browsergym.workarena import ATOMIC_TASKS

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint", ATOMIC_TASKS)
@pytest.mark.parametrize("random_seed", range(1))
@pytest.mark.slow
def test_cheat(task_entrypoint, random_seed: int, page: Page):
    task = task_entrypoint(seed=random_seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    reward, done, message, info = task.validate(page, chat_messages)
    assert done is False and reward == 0.0
    assert type(message) == str and type(info) == dict
    task.cheat(page=page, chat_messages=chat_messages)
    reward, done, message, info = task.validate(page, chat_messages)
    task.teardown()
    assert done is True and reward == 1.0


import pytest

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright

from playwright.sync_api import Page, TimeoutError
from tenacity import retry, stop_after_attempt, retry_if_exception_type

from browsergym.workarena import ATOMIC_TASKS

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint", ATOMIC_TASKS)
@pytest.mark.parametrize("random_seed", range(1))
@pytest.mark.slow
def test_cheat(task_entrypoint, random_seed: int, page: Page):
    task = task_entrypoint(seed=random_seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    reward, done, message, info = task.validate(page, chat_messages)
    assert done is False and reward == 0.0
    assert type(message) == str and type(info) == dict
    task.cheat(page=page, chat_messages=chat_messages)
    reward, done, message, info = task.validate(page, chat_messages)
    task.teardown()
    assert done is True and reward == 1.0


"""
Tests that are not specific to any particular kind of task.

"""

import pytest
import json
import logging
import random

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright
from playwright.sync_api import Page, TimeoutError
from tenacity import retry, stop_after_attempt, retry_if_exception_type
from browsergym.workarena.config import ORDER_APPLE_WATCH_TASK_CONFIG_PATH

from browsergym.workarena.tasks.navigation import AllMenuTask
from browsergym.workarena.tasks.service_catalog import OrderAppleWatchTask

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.slow
def test_add_to_cart_disabled(page: Page):
    task_config = json.load(open(ORDER_APPLE_WATCH_TASK_CONFIG_PATH, "r"))[0]
    task = OrderAppleWatchTask(seed=1, fixed_config=task_config)
    # setup the task and try clicking on the "Add to cart button"
    task.setup(page=page)
    order_apple_watch_page = (
        task.instance.snow_url
        + "/now/nav/ui/classic/params/target/com.glideapp.servicecatalog_cat_item_view.do%3Fv%3D1%26sysparm_id%3D774906834fbb4200086eeed18110c737%26sysparm_link_parent%3Dd258b953c611227a0146101fb1be7c31%26sysparm_catalog%3De0d08b13c3330100c8b837659bba8fb4%26sysparm_catalog_view%3Dcatalog_default%26sysparm_view%3Dcatalog_default"
    )
    task.page.goto(order_apple_watch_page)
    task.page.wait_for_timeout(1000)
    iframe_element = task.page.wait_for_selector("#gsft_main")
    iframe = iframe_element.content_frame()
    task.teardown()
    # verify that Add to cart is disabled and order now is enabled
    assert iframe.locator('button[aria-label="Add to Cart"]').is_disabled()
    assert iframe.locator('button[aria-label="Order Now"]').is_enabled()

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.slow
def test_top_items_panel_removed(page: Page):
    def check_top_items_panel(page: Page) -> bool:
        """Checks if the 'top items' panel exists on the landing page"""
        frame = page.wait_for_selector("iframe#gsft_main").content_frame()

        # Use evaluate to find divs containing an element with role="heading" and the text "Top Requests"
        panel_exists = frame.evaluate(
            """() => {
                const headings = Array.from(document.querySelectorAll('[role="heading"]'));
                let panelExists = false;
                headings.forEach((heading) => {
                    if (heading.textContent.includes("Top Requests")) {
                        panelExists = true;
                    }
                });
                return panelExists;
            }"""
        )

        return panel_exists

    # # Create a new task outside the service catalog and check if the Top Items panel exists
    # TODO: Uncomment this code and fix the test; it is currently failing, but the functionality is optional
    menu_task = AllMenuTask(seed=1)
    menu_task.setup(page=page)
    menu_task.page.goto(
        menu_task.instance.snow_url
        + r"/now/nav/ui/classic/params/target/catalog_home.do%3Fsysparm_view%3Dcatalog_default"
    )
    menu_task.page.wait_for_timeout(2000)
    panel_exists = check_top_items_panel(page=menu_task.page)
    menu_task.teardown()
    assert panel_exists is True

    service_catalog_task = OrderAppleWatchTask(seed=1)
    # Setup the task and check if the Top Items panel exists
    service_catalog_task.setup(page=page)
    service_catalog_task.page.wait_for_timeout(2000)
    panel_exists = check_top_items_panel(page=service_catalog_task.page)
    service_catalog_task.teardown()
    assert panel_exists is False


import logging
import random

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright
from playwright.sync_api import Page, TimeoutError
from tenacity import retry, stop_after_attempt, retry_if_exception_type
from browsergym.workarena.config import ORDER_APPLE_WATCH_TASK_CONFIG_PATH

from browsergym.workarena.tasks.navigation import AllMenuTask
from browsergym.workarena.tasks.service_catalog import OrderAppleWatchTask

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.slow
def test_add_to_cart_disabled(page: Page):
    task_config = json.load(open(ORDER_APPLE_WATCH_TASK_CONFIG_PATH, "r"))[0]
    task = OrderAppleWatchTask(seed=1, fixed_config=task_config)
    # setup the task and try clicking on the "Add to cart button"
    task.setup(page=page)
    order_apple_watch_page = (
        task.instance.snow_url
        + "/now/nav/ui/classic/params/target/com.glideapp.servicecatalog_cat_item_view.do%3Fv%3D1%26sysparm_id%3D774906834fbb4200086eeed18110c737%26sysparm_link_parent%3Dd258b953c611227a0146101fb1be7c31%26sysparm_catalog%3De0d08b13c3330100c8b837659bba8fb4%26sysparm_catalog_view%3Dcatalog_default%26sysparm_view%3Dcatalog_default"
    )
    task.page.goto(order_apple_watch_page)
    task.page.wait_for_timeout(1000)
    iframe_element = task.page.wait_for_selector("#gsft_main")
    iframe = iframe_element.content_frame()
    task.teardown()
    # verify that Add to cart is disabled and order now is enabled
    assert iframe.locator('button[aria-label="Add to Cart"]').is_disabled()
    assert iframe.locator('button[aria-label="Order Now"]').is_enabled()

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.slow
def test_top_items_panel_removed(page: Page):
    def check_top_items_panel(page: Page) -> bool:
        """Checks if the 'top items' panel exists on the landing page"""
        frame = page.wait_for_selector("iframe#gsft_main").content_frame()

        # Use evaluate to find divs containing an element with role="heading" and the text "Top Requests"
        panel_exists = frame.evaluate(
            """() => {
                const headings = Array.from(document.querySelectorAll('[role="heading"]'));
                let panelExists = false;
                headings.forEach((heading) => {


        task.instance.snow_url
        + "/now/nav/ui/classic/params/target/com.glideapp.servicecatalog_cat_item_view.do%3Fv%3D1%26sysparm_id%3D774906834fbb4200086eeed18110c737%26sysparm_link_parent%3Dd258b953c611227a0146101fb1be7c31%26sysparm_catalog%3De0d08b13c3330100c8b837659bba8fb4%26sysparm_catalog_view%3Dcatalog_default%26sysparm_view%3Dcatalog_default"
    )
    task.page.goto(order_apple_watch_page)
    task.page.wait_for_timeout(1000)
    iframe_element = task.page.wait_for_selector("#gsft_main")
    iframe = iframe_element.content_frame()
    task.teardown()
    # verify that Add to cart is disabled and order now is enabled
    assert iframe.locator('button[aria-label="Add to Cart"]').is_disabled()
    assert iframe.locator('button[aria-label="Order Now"]').is_enabled()

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.slow
def test_top_items_panel_removed(page: Page):
    def check_top_items_panel(page: Page) -> bool:
        """Checks if the 'top items' panel exists on the landing page"""
        frame = page.wait_for_selector("iframe#gsft_main").content_frame()

        # Use evaluate to find divs containing an element with role="heading" and the text "Top Requests"
        panel_exists = frame.evaluate(
            """() => {
                const headings = Array.from(document.querySelectorAll('[role="heading"]'));
                let panelExists = false;
                headings.forEach((heading) => {
                    if (heading.textContent.includes("Top Requests")) {
                        panelExists = true;
                    }
                });
                return panelExists;
            }"""
        )

        return panel_exists

    # # Create a new task outside the service catalog and check if the Top Items panel exists
    # TODO: Uncomment this code and fix the test; it is currently failing, but the functionality is optional
    menu_task = AllMenuTask(seed=1)
    menu_task.setup(page=page)
    menu_task.page.goto(
        menu_task.instance.snow_url
        + r"/now/nav/ui/classic/params/target/catalog_home.do%3Fsysparm_view%3Dcatalog_default"
    )
    menu_task.page.wait_for_timeout(2000)
    panel_exists = check_top_items_panel(page=menu_task.page)
    menu_task.teardown()
    assert panel_exists is True

    service_catalog_task = OrderAppleWatchTask(seed=1)
    # Setup the task and check if the Top Items panel exists
    service_catalog_task.setup(page=page)
    service_catalog_task.page.wait_for_timeout(2000)
    panel_exists = check_top_items_panel(page=service_catalog_task.page)
    service_catalog_task.teardown()
    assert panel_exists is False


        + "/now/nav/ui/classic/params/target/com.glideapp.servicecatalog_cat_item_view.do%3Fv%3D1%26sysparm_id%3D774906834fbb4200086eeed18110c737%26sysparm_link_parent%3Dd258b953c611227a0146101fb1be7c31%26sysparm_catalog%3De0d08b13c3330100c8b837659bba8fb4%26sysparm_catalog_view%3Dcatalog_default%26sysparm_view%3Dcatalog_default"
    )
    task.page.goto(order_apple_watch_page)
    task.page.wait_for_timeout(1000)
    iframe_element = task.page.wait_for_selector("#gsft_main")
    iframe = iframe_element.content_frame()
    task.teardown()
    # verify that Add to cart is disabled and order now is enabled
    assert iframe.locator('button[aria-label="Add to Cart"]').is_disabled()
    assert iframe.locator('button[aria-label="Order Now"]').is_enabled()

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.slow
def test_top_items_panel_removed(page: Page):
    def check_top_items_panel(page: Page) -> bool:
        """Checks if the 'top items' panel exists on the landing page"""
        frame = page.wait_for_selector("iframe#gsft_main").content_frame()

        # Use evaluate to find divs containing an element with role="heading" and the text "Top Requests"
        panel_exists = frame.evaluate(
            """() => {
                const headings = Array.from(document.querySelectorAll('[role="heading"]'));
                let panelExists = false;
                headings.forEach((heading) => {
                    if (heading.textContent.includes("Top Requests")) {
                        panelExists = true;
                    }
                });
                return panelExists;
            }"""
        )

        return panel_exists

    # # Create a new task outside the service catalog and check if the Top Items panel exists
    # TODO: Uncomment this code and fix the test; it is currently failing, but the functionality is optional
    menu_task = AllMenuTask(seed=1)
    menu_task.setup(page=page)
    menu_task.page.goto(
        menu_task.instance.snow_url
        + r"/now/nav/ui/classic/params/target/catalog_home.do%3Fsysparm_view%3Dcatalog_default"
    )
    menu_task.page.wait_for_timeout(2000)
    panel_exists = check_top_items_panel(page=menu_task.page)
    menu_task.teardown()
    assert panel_exists is True

    service_catalog_task = OrderAppleWatchTask(seed=1)
    # Setup the task and check if the Top Items panel exists
    service_catalog_task.setup(page=page)
    service_catalog_task.page.wait_for_timeout(2000)
    panel_exists = check_top_items_panel(page=service_catalog_task.page)
    service_catalog_task.teardown()


"""
Tests that are not specific to any particular kind of task.

"""

import logging

import pytest

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright

from playwright.sync_api import Page, TimeoutError
from tenacity import retry, stop_after_attempt, retry_if_exception_type
from browsergym.workarena import ALL_COMPOSITIONAL_TASKS, get_all_tasks_agents

AGENT_L2_SAMPLED_SET = get_all_tasks_agents(filter="l2")

AGENT_L2_SAMPLED_TASKS, AGENT_L2_SEEDS = [sampled_set[0] for sampled_set in AGENT_L2_SAMPLED_SET], [
    sampled_set[1] for sampled_set in AGENT_L2_SAMPLED_SET
]

AGENT_L3_SAMPLED_SET = get_all_tasks_agents(filter="l3")

AGENT_L3_SAMPLED_TASKS, AGENT_L3_SEEDS = [sampled_set[0] for sampled_set in AGENT_L3_SAMPLED_SET], [
    sampled_set[1] for sampled_set in AGENT_L3_SAMPLED_SET
]

HUMAN_L2_SAMPLED_SET = get_all_tasks_agents(filter="l2", is_agent_curriculum=False)

HUMAN_L2_SAMPLED_TASKS, HUMAN_L2_SEEDS = [sampled_set[0] for sampled_set in HUMAN_L2_SAMPLED_SET], [
    sampled_set[1] for sampled_set in HUMAN_L2_SAMPLED_SET
]

HUMAN_L3_SAMPLED_SET = get_all_tasks_agents(filter="l3", is_agent_curriculum=False)

HUMAN_L3_SAMPLED_TASKS, HUMAN_L3_SEEDS = [sampled_set[0] for sampled_set in HUMAN_L3_SAMPLED_SET], [
    sampled_set[1] for sampled_set in HUMAN_L3_SAMPLED_SET
]

@retry(
    stop=stop_after_attempt(5),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint", ALL_COMPOSITIONAL_TASKS)
@pytest.mark.parametrize("random_seed", range(1))
@pytest.mark.parametrize("level", range(2, 4))
@pytest.mark.pricy
def test_cheat_compositional(task_entrypoint, random_seed, level, page: Page):
    task = task_entrypoint(seed=random_seed, level=level)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(AGENT_L2_SAMPLED_TASKS, AGENT_L2_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_agent_set_l2(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(AGENT_L3_SAMPLED_TASKS, AGENT_L3_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_agent_set_l3(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(HUMAN_L2_SAMPLED_TASKS, HUMAN_L2_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_human_set_l2(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(HUMAN_L3_SAMPLED_TASKS, HUMAN_L3_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_human_set_l3(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0


HUMAN_L2_SAMPLED_TASKS, HUMAN_L2_SEEDS = [sampled_set[0] for sampled_set in HUMAN_L2_SAMPLED_SET], [
    sampled_set[1] for sampled_set in HUMAN_L2_SAMPLED_SET
]

HUMAN_L3_SAMPLED_SET = get_all_tasks_agents(filter="l3", is_agent_curriculum=False)

HUMAN_L3_SAMPLED_TASKS, HUMAN_L3_SEEDS = [sampled_set[0] for sampled_set in HUMAN_L3_SAMPLED_SET], [
    sampled_set[1] for sampled_set in HUMAN_L3_SAMPLED_SET
]

@retry(
    stop=stop_after_attempt(5),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint", ALL_COMPOSITIONAL_TASKS)
@pytest.mark.parametrize("random_seed", range(1))
@pytest.mark.parametrize("level", range(2, 4))
@pytest.mark.pricy
def test_cheat_compositional(task_entrypoint, random_seed, level, page: Page):
    task = task_entrypoint(seed=random_seed, level=level)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(AGENT_L2_SAMPLED_TASKS, AGENT_L2_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_agent_set_l2(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)


        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(AGENT_L2_SAMPLED_TASKS, AGENT_L2_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_agent_set_l2(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(AGENT_L3_SAMPLED_TASKS, AGENT_L3_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_agent_set_l3(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)


        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(AGENT_L3_SAMPLED_TASKS, AGENT_L3_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_agent_set_l3(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(HUMAN_L2_SAMPLED_TASKS, HUMAN_L2_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_human_set_l2(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)


        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(HUMAN_L2_SAMPLED_TASKS, HUMAN_L2_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_human_set_l2(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(HUMAN_L3_SAMPLED_TASKS, HUMAN_L3_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_human_set_l3(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)


        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint, seed", zip(HUMAN_L3_SAMPLED_TASKS, HUMAN_L3_SEEDS))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_compositional_sampled_human_set_l3(task_entrypoint, seed, page: Page):
    task = task_entrypoint(seed=seed)
    goal, info = task.setup(page=page)
    chat_messages = []
    for i in range(len(task)):
        page.wait_for_timeout(1000)
        task.cheat(page=page, chat_messages=chat_messages, subtask_idx=i)
        page.wait_for_timeout(1000)
        reward, done, message, info = task.validate(page=page, chat_messages=chat_messages)
        if i < len(task) - 1:
            assert done is False and reward == 0.0

    task.teardown()

    assert done is True and reward == 1.0


"""
Tests that are not specific to any particular kind of task.

"""

import pytest
import json
import logging
import random

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright
from playwright.sync_api import Page, TimeoutError
from tenacity import retry, stop_after_attempt, retry_if_exception_type
from browsergym.workarena.config import ORDER_APPLE_WATCH_TASK_CONFIG_PATH

from browsergym.workarena.tasks.service_catalog import OrderAppleWatchTask
from browsergym.workarena.tasks.scripts.validate import validate_configs

@retry(
    stop=stop_after_attempt(2),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
def test_validate_configs(page: Page):
    failed_tasks = validate_configs(
        OrderAppleWatchTask,
        ORDER_APPLE_WATCH_TASK_CONFIG_PATH,
        num_tasks=2,
        save_failed_tasks=False,
        page=page,
    )
    # assert that there are no failed tasks
    assert len(failed_tasks["cheat"]) == 0
    assert len(failed_tasks["no_reward"]) == 0
    assert len(failed_tasks["exception"]) == 0
    assert len(failed_tasks["not_done"]) == 0


import json
import logging
import random

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright
from playwright.sync_api import Page, TimeoutError
from tenacity import retry, stop_after_attempt, retry_if_exception_type
from browsergym.workarena.config import ORDER_APPLE_WATCH_TASK_CONFIG_PATH

from browsergym.workarena.tasks.service_catalog import OrderAppleWatchTask
from browsergym.workarena.tasks.scripts.validate import validate_configs

@retry(
    stop=stop_after_attempt(2),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
def test_validate_configs(page: Page):
    failed_tasks = validate_configs(
        OrderAppleWatchTask,
        ORDER_APPLE_WATCH_TASK_CONFIG_PATH,
        num_tasks=2,
        save_failed_tasks=False,
        page=page,
    )
    # assert that there are no failed tasks
    assert len(failed_tasks["cheat"]) == 0
    assert len(failed_tasks["no_reward"]) == 0
    assert len(failed_tasks["exception"]) == 0
    assert len(failed_tasks["not_done"]) == 0


"""
Tests that are not specific to any particular kind of task.

"""

import json
import logging
import pickle
import pytest

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright

from playwright.sync_api import Page, TimeoutError
from tenacity import retry, stop_after_attempt, retry_if_exception_type

from browsergym.workarena.tasks.form import (
    CreateChangeRequestTask,
    CreateHardwareAssetTask,
    CreateIncidentTask,
    CreateProblemTask,
    CreateUserTask,
)
from browsergym.workarena.tasks.list import (
    FilterAssetListTask,
    FilterChangeRequestListTask,
    FilterHardwareListTask,
    FilterIncidentListTask,
    FilterServiceCatalogItemListTask,
    FilterUserListTask,
    SortAssetListTask,
    SortChangeRequestListTask,
    SortHardwareListTask,
    SortIncidentListTask,
    SortServiceCatalogItemListTask,
    SortUserListTask,
)
from browsergym.workarena.tasks.service_catalog import (
    OrderDeveloperLaptopTask,
    OrderIpadMiniTask,
    OrderIpadProTask,
    OrderSalesLaptopTask,
    OrderStandardLaptopTask,
    OrderAppleWatchTask,
    OrderAppleMacBookPro15Task,
    OrderDevelopmentLaptopPCTask,
    OrderLoanerLaptopTask,
)

RANDOMLY_CONFIGURALBE_TASKS = [
    CreateChangeRequestTask,
    CreateHardwareAssetTask,
    CreateIncidentTask,
    CreateProblemTask,
    CreateUserTask,
    FilterAssetListTask,
    FilterChangeRequestListTask,
    FilterHardwareListTask,
    FilterIncidentListTask,
    FilterServiceCatalogItemListTask,
    FilterUserListTask,
    SortAssetListTask,
    SortChangeRequestListTask,
    SortHardwareListTask,
    SortIncidentListTask,
    SortServiceCatalogItemListTask,
    SortUserListTask,
    OrderDeveloperLaptopTask,
    OrderIpadMiniTask,
    OrderIpadProTask,
    OrderSalesLaptopTask,
    OrderStandardLaptopTask,
    OrderAppleWatchTask,
    OrderAppleMacBookPro15Task,
    OrderDevelopmentLaptopPCTask,
    OrderLoanerLaptopTask,
]

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint", RANDOMLY_CONFIGURALBE_TASKS)
@pytest.mark.parametrize("random_seed", range(1))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_from_random_config(task_entrypoint, random_seed: int, page: Page):
    task = task_entrypoint(seed=random_seed)
    task._generate_random_config(page=page)
    chat_messages = []
    reward, done, message, info = task.validate(page, chat_messages)
    assert done is False and reward == 0.0
    task.cheat(page=page, chat_messages=chat_messages)
    reward, done, message, info = task.validate(page, chat_messages)
    assert done is True and reward == 1.0
    task.teardown()


    OrderIpadProTask,
    OrderSalesLaptopTask,
    OrderStandardLaptopTask,
    OrderAppleWatchTask,
    OrderAppleMacBookPro15Task,
    OrderDevelopmentLaptopPCTask,
    OrderLoanerLaptopTask,
]

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
@pytest.mark.parametrize("task_entrypoint", RANDOMLY_CONFIGURALBE_TASKS)
@pytest.mark.parametrize("random_seed", range(1))
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_cheat_from_random_config(task_entrypoint, random_seed: int, page: Page):
    task = task_entrypoint(seed=random_seed)
    task._generate_random_config(page=page)
    chat_messages = []
    reward, done, message, info = task.validate(page, chat_messages)
    assert done is False and reward == 0.0
    task.cheat(page=page, chat_messages=chat_messages)
    reward, done, message, info = task.validate(page, chat_messages)
    assert done is True and reward == 1.0
    task.teardown()


"""
Test that the ServiceNow instance is reachable and that the credentials are correct

"""

import pytest

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright

from playwright.sync_api import Page

from browsergym.workarena.instance import SNowInstance
from browsergym.workarena.utils import ui_login

def test_check_is_reachable():
    """
    Test that the ServiceNow instance is reachable

    """
    # This tests that the user's config is correct
    # If it is not, the creation of the instance object
    # will simply return an exception
    instance = SNowInstance()

    # We modify the URL and ensure that the instance is not reachable
    instance.snow_url = "https://invalid.url"
    # We check that this raises an exception
    with pytest.raises(RuntimeError):
        instance._check_is_reachable()

def test_instance_active(page: Page):
    """
    Test that the ServiceNow instance is active (not hibernating)

    """
    instance = SNowInstance()
    page.goto(instance.snow_url)
    assert (
        "hibernating" not in page.title().lower()
    ), f"Instance is not active. Wake it up by navigating to {instance.snow_url} in a browser."

def test_credentials(page: Page):
    """
    Test that the credentials are correct

    """
    instance = SNowInstance()
    ui_login(instance=instance, page=page)  # Raises exception if login fails


"""
Test that the ServiceNow instance is reachable and that the credentials are correct

"""

import pytest

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright

from playwright.sync_api import Page

from browsergym.workarena.instance import SNowInstance
from browsergym.workarena.utils import ui_login

def test_check_is_reachable():
    """
    Test that the ServiceNow instance is reachable

    """
    # This tests that the user's config is correct
    # If it is not, the creation of the instance object
    # will simply return an exception
    instance = SNowInstance()

    # We modify the URL and ensure that the instance is not reachable
    instance.snow_url = "https://invalid.url"
    # We check that this raises an exception
    with pytest.raises(RuntimeError):
        instance._check_is_reachable()

def test_instance_active(page: Page):
    """
    Test that the ServiceNow instance is active (not hibernating)

    """
    instance = SNowInstance()
    page.goto(instance.snow_url)
    assert (
        "hibernating" not in page.title().lower()
    ), f"Instance is not active. Wake it up by navigating to {instance.snow_url} in a browser."

def test_credentials(page: Page):
    """
    Test that the credentials are correct

    """
    instance = SNowInstance()


from browsergym.workarena.utils import ui_login

def test_check_is_reachable():
    """
    Test that the ServiceNow instance is reachable

    """
    # This tests that the user's config is correct
    # If it is not, the creation of the instance object
    # will simply return an exception
    instance = SNowInstance()

    # We modify the URL and ensure that the instance is not reachable
    instance.snow_url = "https://invalid.url"
    # We check that this raises an exception
    with pytest.raises(RuntimeError):
        instance._check_is_reachable()

def test_instance_active(page: Page):
    """
    Test that the ServiceNow instance is active (not hibernating)

    """
    instance = SNowInstance()
    page.goto(instance.snow_url)
    assert (
        "hibernating" not in page.title().lower()
    ), f"Instance is not active. Wake it up by navigating to {instance.snow_url} in a browser."

def test_credentials(page: Page):
    """
    Test that the credentials are correct

    """
    instance = SNowInstance()
    ui_login(instance=instance, page=page)  # Raises exception if login fails


    # We modify the URL and ensure that the instance is not reachable
    instance.snow_url = "https://invalid.url"
    # We check that this raises an exception
    with pytest.raises(RuntimeError):
        instance._check_is_reachable()

def test_instance_active(page: Page):
    """
    Test that the ServiceNow instance is active (not hibernating)

    """
    instance = SNowInstance()
    page.goto(instance.snow_url)
    assert (
        "hibernating" not in page.title().lower()
    ), f"Instance is not active. Wake it up by navigating to {instance.snow_url} in a browser."

def test_credentials(page: Page):
    """
    Test that the credentials are correct

    """
    instance = SNowInstance()
    ui_login(instance=instance, page=page)  # Raises exception if login fails


tests.utils

import browsergym.core
import logging
import playwright.sync_api
import pytest

# setup code, executed ahead of first test
@pytest.fixture(scope="session", autouse=True)
def setup_playwright(playwright: playwright.sync_api.Playwright):
    # bugfix: re-use pytest-playwright's playwright instance in browsergym
    # https://github.com/microsoft/playwright-python/issues/2053
    browsergym.core._set_global_playwright(playwright)
    logging.info("Browsergym is using the playwright instance provided by pytest-playwright.")


import numpy as np
import pytest
import random

from time import sleep

from browsergym.workarena.instance import SNowInstance
from browsergym.workarena.api.utils import table_api_call
from browsergym.workarena.api.user import create_user, set_user_preference

@pytest.mark.parametrize("system", [True, False])
def test_set_user_preference(system):
    admin_instance = SNowInstance()

    # Create a user to get a sys_id
    if not system:
        uname, pwd, sysid = create_user(SNowInstance())
        user_instance = SNowInstance(snow_credentials=(uname, pwd))
        user = sysid
    else:
        user_instance = admin_instance

    # Do it twice to test updating existing preference
    pref_key = f"workarena.unittest.{random.randint(1, 100000000)}"
    for _ in range(2):
        pref = set_user_preference(
            user_instance, key=pref_key, value="1234", user=None if system else user
        )
        assert pref["name"] == pref_key
        assert pref["value"] == "1234"
        assert pref["user"] == "" if system else user
        assert str(pref["system"]).lower() == str(system).lower()
        assert pref["description"] == "Updated by WorkArena"

    # Delete the preference
    table_api_call(admin_instance, table=f"sys_user_preference/{pref['sys_id']}", method="DELETE")

    # Delete the user
    if not system:
        table_api_call(admin_instance, table=f"sys_user/{user}", method="DELETE")


"""
Tests that are not specific to any particular kind of task.

"""

import pytest
import json
import logging
import random

# bugfix: use same playwright instance in browsergym and pytest
from utils import setup_playwright
from playwright.sync_api import Page, TimeoutError
from tenacity import retry, stop_after_attempt, retry_if_exception_type
from browsergym.workarena.config import (
    # navigation tasks
    ALL_MENU_PATH,
    IMPERSONATION_CONFIG_PATH,
    # form tasks
    CREATE_CHANGE_REQUEST_CONFIG_PATH,
    CREATE_HARDWARE_CONFIG_PATH,
    CREATE_INCIDENT_CONFIG_PATH,
    CREATE_PROBLEM_CONFIG_PATH,
    CREATE_USER_CONFIG_PATH,
    # list tasks
    FILTER_ASSET_LIST_CONFIG_PATH,
    FILTER_CHANGE_REQUEST_LIST_CONFIG_PATH,
    FILTER_HARDWARE_LIST_CONFIG_PATH,
    FILTER_INCIDENT_LIST_CONFIG_PATH,
    FILTER_SERVICE_CATALOG_ITEM_LIST_CONFIG_PATH,
    FILTER_USER_LIST_CONFIG_PATH,
    SORT_ASSET_LIST_CONFIG_PATH,
    SORT_CHANGE_REQUEST_LIST_CONFIG_PATH,
    SORT_HARDWARE_LIST_CONFIG_PATH,
    SORT_INCIDENT_LIST_CONFIG_PATH,
    SORT_SERVICE_CATALOG_ITEM_LIST_CONFIG_PATH,
    SORT_USER_LIST_CONFIG_PATH,
    # knowledge tasks
    KB_CONFIG_PATH,
    # Service Catalog tasks
    ORDER_DEVELOPER_LAPTOP_TASK_CONFIG_PATH,
    ORDER_IPAD_MINI_TASK_CONFIG_PATH,
    ORDER_IPAD_PRO_TASK_CONFIG_PATH,
    ORDER_SALES_LAPTOP_TASK_CONFIG_PATH,
    ORDER_STANDARD_LAPTOP_TASK_CONFIG_PATH,
    ORDER_APPLE_WATCH_TASK_CONFIG_PATH,
    ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH,
    ORDER_DEVELOPMENT_LAPTOP_PC_TASK_CONFIG_PATH,
    ORDER_LOANER_LAPTOP_TASK_CONFIG_PATH,
)
from browsergym.workarena.tasks.form import (
    CreateChangeRequestTask,
    CreateHardwareAssetTask,
    CreateIncidentTask,
    CreateProblemTask,
    CreateUserTask,
)
from browsergym.workarena.tasks.knowledge import KnowledgeBaseSearchTask
from browsergym.workarena.tasks.list import (
    FilterAssetListTask,
    FilterChangeRequestListTask,
    FilterHardwareListTask,
    FilterIncidentListTask,
    FilterServiceCatalogItemListTask,
    FilterUserListTask,
    SortAssetListTask,
    SortChangeRequestListTask,
    SortHardwareListTask,
    SortIncidentListTask,
    SortServiceCatalogItemListTask,
    SortUserListTask,
)
from browsergym.workarena.tasks.navigation import AllMenuTask, ImpersonationTask
from browsergym.workarena.tasks.service_catalog import (
    OrderDeveloperLaptopTask,
    OrderIpadMiniTask,
    OrderIpadProTask,
    OrderSalesLaptopTask,
    OrderStandardLaptopTask,
    OrderAppleWatchTask,
    OrderAppleMacBookPro15Task,
    OrderDevelopmentLaptopPCTask,
    OrderLoanerLaptopTask,
)

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
def generic_task_cheat_test(task_class, config_path, page: Page, expected_goal: str = None):
    task_config = json.load(open(config_path, "r"))[0]
    task = task_class(seed=1, fixed_config=task_config)
    goal, _ = task.setup(page=page)
    if expected_goal:
        assert goal == expected_goal
    chat_messages = []
    reward, done, message, info = task.validate(page, chat_messages)
    assert (
        isinstance(reward, (int, float))
        and type(done) == bool
        and type(message) == str
        and type(info) == dict
    )
    assert done is False and reward == 0.0
    task.cheat(page=page, chat_messages=chat_messages)
    reward, done, message, info = task.validate(page, chat_messages)
    assert done is True and reward == 1.0
    task.teardown()

# Navigation tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_menu_task_from_config(page: Page):
    expected_goal = 'Navigate to the "AI Search for Next Experience > Guided Setup for Zing to AI Search Migration" module of the "AI Search" application.'
    generic_task_cheat_test(AllMenuTask, ALL_MENU_PATH, page, expected_goal=expected_goal)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_impersonation_from_config(page: Page):
    expected_goal = "Impersonate the user ATF Change Management."
    generic_task_cheat_test(
        ImpersonationTask, IMPERSONATION_CONFIG_PATH, page, expected_goal=expected_goal
    )

# Service Catalog tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_developer_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderDeveloperLaptopTask, ORDER_DEVELOPER_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_mini_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadMiniTask, ORDER_IPAD_MINI_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_pro_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadProTask, ORDER_IPAD_PRO_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_sales_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderSalesLaptopTask, ORDER_SALES_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_standard_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderStandardLaptopTask, ORDER_STANDARD_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_watch_task_from_config(page: Page):
    expected_goal = 'Go to the hardware store and order 1 "Apple Watch"'
    generic_task_cheat_test(
        OrderAppleWatchTask,
        ORDER_APPLE_WATCH_TASK_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_macbook_pro15_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderAppleMacBookPro15Task, ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_development_laptop_pc_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderDevelopmentLaptopPCTask, ORDER_DEVELOPMENT_LAPTOP_PC_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_loaner_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderLoanerLaptopTask, ORDER_LOANER_LAPTOP_TASK_CONFIG_PATH, page)

# form tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_change_request_task_from_config(page: Page):
    generic_task_cheat_test(CreateChangeRequestTask, CREATE_CHANGE_REQUEST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_hardware_asset_task_from_config(page: Page):
    generic_task_cheat_test(CreateHardwareAssetTask, CREATE_HARDWARE_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_incident_task_from_config(page: Page):
    generic_task_cheat_test(CreateIncidentTask, CREATE_INCIDENT_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_problem_task_from_config(page: Page):
    expected_goal = 'Create a new problem with a value of "Request for a Blackberry" for field "Problem statement", a value of "3 - Low" for field "Impact", a value of "" for field "Service", a value of "Hardware" for field "Category", a value of "bizonal wateringly nonsuccessful checkerberry abridgeable" for field "Description", and a value of "" for field "Configuration item".'
    generic_task_cheat_test(
        CreateProblemTask, CREATE_PROBLEM_CONFIG_PATH, page, expected_goal=expected_goal
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_user_task_from_config(page: Page):
    generic_task_cheat_test(CreateUserTask, CREATE_USER_CONFIG_PATH, page)

# knowledge tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_knowledge_base_from_config(page: Page):
    expected_goal = "Answer the following question using the knowledge base: \"Can you provide the direct contact number for the CEO? Answer with the full phone number starting with the '+' sign.\""
    generic_task_cheat_test(KnowledgeBaseSearchTask, KB_CONFIG_PATH, page)

# list tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_asset_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterAssetListTask, FILTER_ASSET_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_change_request_list_task_from_config(page: Page):
    expected_goal = 'Create a filter for the list to extract all entries where "Assignment group" is "Hardware" and "Assigned to" is "Bow Ruggeri".'
    generic_task_cheat_test(
        FilterChangeRequestListTask,
        FILTER_CHANGE_REQUEST_LIST_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_hardware_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterHardwareListTask, FILTER_HARDWARE_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_incident_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterIncidentListTask, FILTER_INCIDENT_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_service_catalog_item_list_task_from_config(page: Page):
    generic_task_cheat_test(
        FilterServiceCatalogItemListTask,
        FILTER_SERVICE_CATALOG_ITEM_LIST_CONFIG_PATH,
        page,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_user_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterUserListTask, FILTER_USER_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_sort_asset_list_task_from_config(page: Page):
    generic_task_cheat_test(SortAssetListTask, SORT_ASSET_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_sort_change_request_list_task_from_config(page: Page):
    generic_task_cheat_test(SortChangeRequestListTask, SORT_CHANGE_REQUEST_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_sort_hardware_list_task_from_config(page: Page):
    generic_task_cheat_test(SortHardwareListTask, SORT_HARDWARE_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_sort_incident_list_task_from_config(page: Page):
    generic_task_cheat_test(SortIncidentListTask, SORT_INCIDENT_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_sort_service_catalog_item_list_task_from_config(page: Page):
    generic_task_cheat_test(
        SortServiceCatalogItemListTask, SORT_SERVICE_CATALOG_ITEM_LIST_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_sort_user_list_task_from_config(page: Page):
    expected_goal = 'Sort the "users" list by the following fields:\n - Active (descending)'
    generic_task_cheat_test(
        SortUserListTask, SORT_USER_LIST_CONFIG_PATH, page, expected_goal=expected_goal
    )


from browsergym.workarena.tasks.navigation import AllMenuTask, ImpersonationTask
from browsergym.workarena.tasks.service_catalog import (
    OrderDeveloperLaptopTask,
    OrderIpadMiniTask,
    OrderIpadProTask,
    OrderSalesLaptopTask,
    OrderStandardLaptopTask,
    OrderAppleWatchTask,
    OrderAppleMacBookPro15Task,
    OrderDevelopmentLaptopPCTask,
    OrderLoanerLaptopTask,
)

@retry(
    stop=stop_after_attempt(5),
    retry=retry_if_exception_type(TimeoutError),
    reraise=True,
    before_sleep=lambda _: logging.info("Retrying due to a TimeoutError..."),
)
def generic_task_cheat_test(task_class, config_path, page: Page, expected_goal: str = None):
    task_config = json.load(open(config_path, "r"))[0]
    task = task_class(seed=1, fixed_config=task_config)
    goal, _ = task.setup(page=page)
    if expected_goal:
        assert goal == expected_goal
    chat_messages = []
    reward, done, message, info = task.validate(page, chat_messages)
    assert (
        isinstance(reward, (int, float))
        and type(done) == bool
        and type(message) == str
        and type(info) == dict
    )
    assert done is False and reward == 0.0
    task.cheat(page=page, chat_messages=chat_messages)
    reward, done, message, info = task.validate(page, chat_messages)
    assert done is True and reward == 1.0
    task.teardown()

# Navigation tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_menu_task_from_config(page: Page):
    expected_goal = 'Navigate to the "AI Search for Next Experience > Guided Setup for Zing to AI Search Migration" module of the "AI Search" application.'
    generic_task_cheat_test(AllMenuTask, ALL_MENU_PATH, page, expected_goal=expected_goal)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_impersonation_from_config(page: Page):
    expected_goal = "Impersonate the user ATF Change Management."
    generic_task_cheat_test(
        ImpersonationTask, IMPERSONATION_CONFIG_PATH, page, expected_goal=expected_goal
    )

# Service Catalog tasks


    if expected_goal:
        assert goal == expected_goal
    chat_messages = []
    reward, done, message, info = task.validate(page, chat_messages)
    assert (
        isinstance(reward, (int, float))
        and type(done) == bool
        and type(message) == str
        and type(info) == dict
    )
    assert done is False and reward == 0.0
    task.cheat(page=page, chat_messages=chat_messages)
    reward, done, message, info = task.validate(page, chat_messages)
    assert done is True and reward == 1.0
    task.teardown()

# Navigation tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_menu_task_from_config(page: Page):
    expected_goal = 'Navigate to the "AI Search for Next Experience > Guided Setup for Zing to AI Search Migration" module of the "AI Search" application.'
    generic_task_cheat_test(AllMenuTask, ALL_MENU_PATH, page, expected_goal=expected_goal)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_impersonation_from_config(page: Page):
    expected_goal = "Impersonate the user ATF Change Management."
    generic_task_cheat_test(
        ImpersonationTask, IMPERSONATION_CONFIG_PATH, page, expected_goal=expected_goal
    )

# Service Catalog tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_developer_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderDeveloperLaptopTask, ORDER_DEVELOPER_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")


        and type(message) == str
        and type(info) == dict
    )
    assert done is False and reward == 0.0
    task.cheat(page=page, chat_messages=chat_messages)
    reward, done, message, info = task.validate(page, chat_messages)
    assert done is True and reward == 1.0
    task.teardown()

# Navigation tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_menu_task_from_config(page: Page):
    expected_goal = 'Navigate to the "AI Search for Next Experience > Guided Setup for Zing to AI Search Migration" module of the "AI Search" application.'
    generic_task_cheat_test(AllMenuTask, ALL_MENU_PATH, page, expected_goal=expected_goal)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_impersonation_from_config(page: Page):
    expected_goal = "Impersonate the user ATF Change Management."
    generic_task_cheat_test(
        ImpersonationTask, IMPERSONATION_CONFIG_PATH, page, expected_goal=expected_goal
    )

# Service Catalog tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_developer_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderDeveloperLaptopTask, ORDER_DEVELOPER_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_mini_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadMiniTask, ORDER_IPAD_MINI_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_pro_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadProTask, ORDER_IPAD_PRO_TASK_CONFIG_PATH, page)


# Navigation tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_menu_task_from_config(page: Page):
    expected_goal = 'Navigate to the "AI Search for Next Experience > Guided Setup for Zing to AI Search Migration" module of the "AI Search" application.'
    generic_task_cheat_test(AllMenuTask, ALL_MENU_PATH, page, expected_goal=expected_goal)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_impersonation_from_config(page: Page):
    expected_goal = "Impersonate the user ATF Change Management."
    generic_task_cheat_test(
        ImpersonationTask, IMPERSONATION_CONFIG_PATH, page, expected_goal=expected_goal
    )

# Service Catalog tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_developer_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderDeveloperLaptopTask, ORDER_DEVELOPER_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_mini_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadMiniTask, ORDER_IPAD_MINI_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_pro_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadProTask, ORDER_IPAD_PRO_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_sales_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderSalesLaptopTask, ORDER_SALES_LAPTOP_TASK_CONFIG_PATH, page)


@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_impersonation_from_config(page: Page):
    expected_goal = "Impersonate the user ATF Change Management."
    generic_task_cheat_test(
        ImpersonationTask, IMPERSONATION_CONFIG_PATH, page, expected_goal=expected_goal
    )

# Service Catalog tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_developer_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderDeveloperLaptopTask, ORDER_DEVELOPER_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_mini_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadMiniTask, ORDER_IPAD_MINI_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_pro_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadProTask, ORDER_IPAD_PRO_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_sales_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderSalesLaptopTask, ORDER_SALES_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_standard_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderStandardLaptopTask, ORDER_STANDARD_LAPTOP_TASK_CONFIG_PATH, page)


    generic_task_cheat_test(
        ImpersonationTask, IMPERSONATION_CONFIG_PATH, page, expected_goal=expected_goal
    )

# Service Catalog tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_developer_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderDeveloperLaptopTask, ORDER_DEVELOPER_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_mini_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadMiniTask, ORDER_IPAD_MINI_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_pro_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadProTask, ORDER_IPAD_PRO_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_sales_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderSalesLaptopTask, ORDER_SALES_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_standard_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderStandardLaptopTask, ORDER_STANDARD_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_watch_task_from_config(page: Page):
    expected_goal = 'Go to the hardware store and order 1 "Apple Watch"'
    generic_task_cheat_test(
        OrderAppleWatchTask,


@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_developer_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderDeveloperLaptopTask, ORDER_DEVELOPER_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_mini_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadMiniTask, ORDER_IPAD_MINI_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_pro_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadProTask, ORDER_IPAD_PRO_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_sales_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderSalesLaptopTask, ORDER_SALES_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_standard_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderStandardLaptopTask, ORDER_STANDARD_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_watch_task_from_config(page: Page):
    expected_goal = 'Go to the hardware store and order 1 "Apple Watch"'
    generic_task_cheat_test(
        OrderAppleWatchTask,
        ORDER_APPLE_WATCH_TASK_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )


@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_mini_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadMiniTask, ORDER_IPAD_MINI_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_pro_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadProTask, ORDER_IPAD_PRO_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_sales_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderSalesLaptopTask, ORDER_SALES_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_standard_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderStandardLaptopTask, ORDER_STANDARD_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_watch_task_from_config(page: Page):
    expected_goal = 'Go to the hardware store and order 1 "Apple Watch"'
    generic_task_cheat_test(
        OrderAppleWatchTask,
        ORDER_APPLE_WATCH_TASK_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_macbook_pro15_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderAppleMacBookPro15Task, ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH, page
    )


@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_ipad_pro_task_from_config(page: Page):
    generic_task_cheat_test(OrderIpadProTask, ORDER_IPAD_PRO_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_sales_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderSalesLaptopTask, ORDER_SALES_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_standard_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderStandardLaptopTask, ORDER_STANDARD_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_watch_task_from_config(page: Page):
    expected_goal = 'Go to the hardware store and order 1 "Apple Watch"'
    generic_task_cheat_test(
        OrderAppleWatchTask,
        ORDER_APPLE_WATCH_TASK_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_macbook_pro15_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderAppleMacBookPro15Task, ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_development_laptop_pc_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderDevelopmentLaptopPCTask, ORDER_DEVELOPMENT_LAPTOP_PC_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")


@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_standard_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderStandardLaptopTask, ORDER_STANDARD_LAPTOP_TASK_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_watch_task_from_config(page: Page):
    expected_goal = 'Go to the hardware store and order 1 "Apple Watch"'
    generic_task_cheat_test(
        OrderAppleWatchTask,
        ORDER_APPLE_WATCH_TASK_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_macbook_pro15_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderAppleMacBookPro15Task, ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_development_laptop_pc_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderDevelopmentLaptopPCTask, ORDER_DEVELOPMENT_LAPTOP_PC_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_loaner_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderLoanerLaptopTask, ORDER_LOANER_LAPTOP_TASK_CONFIG_PATH, page)

# form tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_change_request_task_from_config(page: Page):


def test_order_apple_watch_task_from_config(page: Page):
    expected_goal = 'Go to the hardware store and order 1 "Apple Watch"'
    generic_task_cheat_test(
        OrderAppleWatchTask,
        ORDER_APPLE_WATCH_TASK_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_macbook_pro15_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderAppleMacBookPro15Task, ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_development_laptop_pc_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderDevelopmentLaptopPCTask, ORDER_DEVELOPMENT_LAPTOP_PC_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_loaner_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderLoanerLaptopTask, ORDER_LOANER_LAPTOP_TASK_CONFIG_PATH, page)

# form tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_change_request_task_from_config(page: Page):
    generic_task_cheat_test(CreateChangeRequestTask, CREATE_CHANGE_REQUEST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_hardware_asset_task_from_config(page: Page):
    generic_task_cheat_test(CreateHardwareAssetTask, CREATE_HARDWARE_CONFIG_PATH, page)


@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_apple_macbook_pro15_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderAppleMacBookPro15Task, ORDER_APPLE_MAC_BOOK_PRO15_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_development_laptop_pc_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderDevelopmentLaptopPCTask, ORDER_DEVELOPMENT_LAPTOP_PC_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_loaner_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderLoanerLaptopTask, ORDER_LOANER_LAPTOP_TASK_CONFIG_PATH, page)

# form tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_change_request_task_from_config(page: Page):
    generic_task_cheat_test(CreateChangeRequestTask, CREATE_CHANGE_REQUEST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_hardware_asset_task_from_config(page: Page):
    generic_task_cheat_test(CreateHardwareAssetTask, CREATE_HARDWARE_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_incident_task_from_config(page: Page):
    generic_task_cheat_test(CreateIncidentTask, CREATE_INCIDENT_CONFIG_PATH, page)


    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_development_laptop_pc_task_from_config(page: Page):
    generic_task_cheat_test(
        OrderDevelopmentLaptopPCTask, ORDER_DEVELOPMENT_LAPTOP_PC_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_loaner_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderLoanerLaptopTask, ORDER_LOANER_LAPTOP_TASK_CONFIG_PATH, page)

# form tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_change_request_task_from_config(page: Page):
    generic_task_cheat_test(CreateChangeRequestTask, CREATE_CHANGE_REQUEST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_hardware_asset_task_from_config(page: Page):
    generic_task_cheat_test(CreateHardwareAssetTask, CREATE_HARDWARE_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_incident_task_from_config(page: Page):
    generic_task_cheat_test(CreateIncidentTask, CREATE_INCIDENT_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_problem_task_from_config(page: Page):
    expected_goal = 'Create a new problem with a value of "Request for a Blackberry" for field "Problem statement", a value of "3 - Low" for field "Impact", a value of "" for field "Service", a value of "Hardware" for field "Category", a value of "bizonal wateringly nonsuccessful checkerberry abridgeable" for field "Description", and a value of "" for field "Configuration item".'
    generic_task_cheat_test(
        CreateProblemTask, CREATE_PROBLEM_CONFIG_PATH, page, expected_goal=expected_goal


    generic_task_cheat_test(
        OrderDevelopmentLaptopPCTask, ORDER_DEVELOPMENT_LAPTOP_PC_TASK_CONFIG_PATH, page
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_order_loaner_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderLoanerLaptopTask, ORDER_LOANER_LAPTOP_TASK_CONFIG_PATH, page)

# form tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_change_request_task_from_config(page: Page):
    generic_task_cheat_test(CreateChangeRequestTask, CREATE_CHANGE_REQUEST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_hardware_asset_task_from_config(page: Page):
    generic_task_cheat_test(CreateHardwareAssetTask, CREATE_HARDWARE_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_incident_task_from_config(page: Page):
    generic_task_cheat_test(CreateIncidentTask, CREATE_INCIDENT_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_problem_task_from_config(page: Page):
    expected_goal = 'Create a new problem with a value of "Request for a Blackberry" for field "Problem statement", a value of "3 - Low" for field "Impact", a value of "" for field "Service", a value of "Hardware" for field "Category", a value of "bizonal wateringly nonsuccessful checkerberry abridgeable" for field "Description", and a value of "" for field "Configuration item".'
    generic_task_cheat_test(
        CreateProblemTask, CREATE_PROBLEM_CONFIG_PATH, page, expected_goal=expected_goal
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_user_task_from_config(page: Page):


@pytest.mark.skip(reason="Tests are too slow")
def test_order_loaner_laptop_task_from_config(page: Page):
    generic_task_cheat_test(OrderLoanerLaptopTask, ORDER_LOANER_LAPTOP_TASK_CONFIG_PATH, page)

# form tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_change_request_task_from_config(page: Page):
    generic_task_cheat_test(CreateChangeRequestTask, CREATE_CHANGE_REQUEST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_hardware_asset_task_from_config(page: Page):
    generic_task_cheat_test(CreateHardwareAssetTask, CREATE_HARDWARE_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_incident_task_from_config(page: Page):
    generic_task_cheat_test(CreateIncidentTask, CREATE_INCIDENT_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_problem_task_from_config(page: Page):
    expected_goal = 'Create a new problem with a value of "Request for a Blackberry" for field "Problem statement", a value of "3 - Low" for field "Impact", a value of "" for field "Service", a value of "Hardware" for field "Category", a value of "bizonal wateringly nonsuccessful checkerberry abridgeable" for field "Description", and a value of "" for field "Configuration item".'
    generic_task_cheat_test(
        CreateProblemTask, CREATE_PROBLEM_CONFIG_PATH, page, expected_goal=expected_goal
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_user_task_from_config(page: Page):
    generic_task_cheat_test(CreateUserTask, CREATE_USER_CONFIG_PATH, page)

# knowledge tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")


@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_change_request_task_from_config(page: Page):
    generic_task_cheat_test(CreateChangeRequestTask, CREATE_CHANGE_REQUEST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_hardware_asset_task_from_config(page: Page):
    generic_task_cheat_test(CreateHardwareAssetTask, CREATE_HARDWARE_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_incident_task_from_config(page: Page):
    generic_task_cheat_test(CreateIncidentTask, CREATE_INCIDENT_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_problem_task_from_config(page: Page):
    expected_goal = 'Create a new problem with a value of "Request for a Blackberry" for field "Problem statement", a value of "3 - Low" for field "Impact", a value of "" for field "Service", a value of "Hardware" for field "Category", a value of "bizonal wateringly nonsuccessful checkerberry abridgeable" for field "Description", and a value of "" for field "Configuration item".'
    generic_task_cheat_test(
        CreateProblemTask, CREATE_PROBLEM_CONFIG_PATH, page, expected_goal=expected_goal
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_user_task_from_config(page: Page):
    generic_task_cheat_test(CreateUserTask, CREATE_USER_CONFIG_PATH, page)

# knowledge tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_knowledge_base_from_config(page: Page):
    expected_goal = "Answer the following question using the knowledge base: \"Can you provide the direct contact number for the CEO? Answer with the full phone number starting with the '+' sign.\""
    generic_task_cheat_test(KnowledgeBaseSearchTask, KB_CONFIG_PATH, page)

# list tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_asset_list_task_from_config(page: Page):


    generic_task_cheat_test(CreateHardwareAssetTask, CREATE_HARDWARE_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_incident_task_from_config(page: Page):
    generic_task_cheat_test(CreateIncidentTask, CREATE_INCIDENT_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_problem_task_from_config(page: Page):
    expected_goal = 'Create a new problem with a value of "Request for a Blackberry" for field "Problem statement", a value of "3 - Low" for field "Impact", a value of "" for field "Service", a value of "Hardware" for field "Category", a value of "bizonal wateringly nonsuccessful checkerberry abridgeable" for field "Description", and a value of "" for field "Configuration item".'
    generic_task_cheat_test(
        CreateProblemTask, CREATE_PROBLEM_CONFIG_PATH, page, expected_goal=expected_goal
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_user_task_from_config(page: Page):
    generic_task_cheat_test(CreateUserTask, CREATE_USER_CONFIG_PATH, page)

# knowledge tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_knowledge_base_from_config(page: Page):
    expected_goal = "Answer the following question using the knowledge base: \"Can you provide the direct contact number for the CEO? Answer with the full phone number starting with the '+' sign.\""
    generic_task_cheat_test(KnowledgeBaseSearchTask, KB_CONFIG_PATH, page)

# list tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_asset_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterAssetListTask, FILTER_ASSET_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_change_request_list_task_from_config(page: Page):


@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_problem_task_from_config(page: Page):
    expected_goal = 'Create a new problem with a value of "Request for a Blackberry" for field "Problem statement", a value of "3 - Low" for field "Impact", a value of "" for field "Service", a value of "Hardware" for field "Category", a value of "bizonal wateringly nonsuccessful checkerberry abridgeable" for field "Description", and a value of "" for field "Configuration item".'
    generic_task_cheat_test(
        CreateProblemTask, CREATE_PROBLEM_CONFIG_PATH, page, expected_goal=expected_goal
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_user_task_from_config(page: Page):
    generic_task_cheat_test(CreateUserTask, CREATE_USER_CONFIG_PATH, page)

# knowledge tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_knowledge_base_from_config(page: Page):
    expected_goal = "Answer the following question using the knowledge base: \"Can you provide the direct contact number for the CEO? Answer with the full phone number starting with the '+' sign.\""
    generic_task_cheat_test(KnowledgeBaseSearchTask, KB_CONFIG_PATH, page)

# list tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_asset_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterAssetListTask, FILTER_ASSET_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_change_request_list_task_from_config(page: Page):
    expected_goal = 'Create a filter for the list to extract all entries where "Assignment group" is "Hardware" and "Assigned to" is "Bow Ruggeri".'
    generic_task_cheat_test(
        FilterChangeRequestListTask,
        FILTER_CHANGE_REQUEST_LIST_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )


    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_create_user_task_from_config(page: Page):
    generic_task_cheat_test(CreateUserTask, CREATE_USER_CONFIG_PATH, page)

# knowledge tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_knowledge_base_from_config(page: Page):
    expected_goal = "Answer the following question using the knowledge base: \"Can you provide the direct contact number for the CEO? Answer with the full phone number starting with the '+' sign.\""
    generic_task_cheat_test(KnowledgeBaseSearchTask, KB_CONFIG_PATH, page)

# list tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_asset_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterAssetListTask, FILTER_ASSET_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_change_request_list_task_from_config(page: Page):
    expected_goal = 'Create a filter for the list to extract all entries where "Assignment group" is "Hardware" and "Assigned to" is "Bow Ruggeri".'
    generic_task_cheat_test(
        FilterChangeRequestListTask,
        FILTER_CHANGE_REQUEST_LIST_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_hardware_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterHardwareListTask, FILTER_HARDWARE_LIST_CONFIG_PATH, page)


    generic_task_cheat_test(CreateUserTask, CREATE_USER_CONFIG_PATH, page)

# knowledge tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_knowledge_base_from_config(page: Page):
    expected_goal = "Answer the following question using the knowledge base: \"Can you provide the direct contact number for the CEO? Answer with the full phone number starting with the '+' sign.\""
    generic_task_cheat_test(KnowledgeBaseSearchTask, KB_CONFIG_PATH, page)

# list tasks
@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_asset_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterAssetListTask, FILTER_ASSET_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_change_request_list_task_from_config(page: Page):
    expected_goal = 'Create a filter for the list to extract all entries where "Assignment group" is "Hardware" and "Assigned to" is "Bow Ruggeri".'
    generic_task_cheat_test(
        FilterChangeRequestListTask,
        FILTER_CHANGE_REQUEST_LIST_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_hardware_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterHardwareListTask, FILTER_HARDWARE_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_incident_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterIncidentListTask, FILTER_INCIDENT_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_service_catalog_item_list_task_from_config(page: Page):
    generic_task_cheat_test(
        FilterServiceCatalogItemListTask,
        FILTER_SERVICE_CATALOG_ITEM_LIST_CONFIG_PATH,


@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_asset_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterAssetListTask, FILTER_ASSET_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_change_request_list_task_from_config(page: Page):
    expected_goal = 'Create a filter for the list to extract all entries where "Assignment group" is "Hardware" and "Assigned to" is "Bow Ruggeri".'
    generic_task_cheat_test(
        FilterChangeRequestListTask,
        FILTER_CHANGE_REQUEST_LIST_CONFIG_PATH,
        page,
        expected_goal=expected_goal,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_hardware_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterHardwareListTask, FILTER_HARDWARE_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_incident_list_task_from_config(page: Page):
    generic_task_cheat_test(FilterIncidentListTask, FILTER_INCIDENT_LIST_CONFIG_PATH, page)

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")
def test_filter_service_catalog_item_list_task_from_config(page: Page):
    generic_task_cheat_test(
        FilterServiceCatalogItemListTask,
        FILTER_SERVICE_CATALOG_ITEM_LIST_CONFIG_PATH,
        page,
    )

@pytest.mark.slow
@pytest.mark.skip(reason="Tests are too slow")