import omni.kit.test
from .test_base import KitLaunchTest
from .scripts import extensions_load, extensions_tests

import inspect
import sys


class TestExtensions(KitLaunchTest):
    async def setUp(self):
        await super().setUp()

        self.app_to_test = "omni.app.full"
        # basic kit file with additional test dependencies
        # Note we need to manually add some [[test]] dependencies (could be automated)
        self.kit_file = f"""
[dependencies]
"{self.app_to_test}" = {{}}
"omni.kit.ui_test" = {{}}               # test dependency from omni.app.full.kit
"omni.kit.test_suite.helpers" = {{}}    # test dependency from omni.app.full.kit
"omni.kit.test_helpers_gfx" = {{}}      # test dependency from omni.renderer.core
"omni.kit.hydra_texture" = {{}}         # test dependency from omni.kit.viewport.legacy_gizmos
"omni.rtx.tests" = {{}}                 # test dependency from omni.hydra.scene_api
        """

    async def test_l1_extensions_have_tests(self):
        # This list should be empty or near empty ideally
        EXCLUSION_LIST = [
            "omni.physx.cct",
            "omni.kit.widget.live",
            "omni.mdl",
            "omni.mdl.neuraylib",
        ]
        # These extensions only run tests on win32 for now
        if sys.platform != "win32":
            EXCLUSION_LIST.append("omni.hydra.scene_api")
            EXCLUSION_LIST.append("omni.rtx.tests")

        script = inspect.getsource(extensions_tests)
        script = script.replace("EXCLUSION_LIST = []", f"EXCLUSION_LIST = {EXCLUSION_LIST}")

        # when it fails it will assert with the number of extensions that have no tests, scroll up in the log to find them
        await self._run_kit_with_script(
            script,
            args=[
                "--/app/extensions/disableStartup=1",   # extensions startup is disabled
                "--/app/extensions/registryEnabled=0",
                "--/app/quitAfter=10",
                "--/crashreporter/gatherUserStory=0", # don't pop up the GUI on crash
                "--no-window",
            ],
            kit_file=self.kit_file,
            timeout=60,
        )

    async def test_l1_extensions_load(self):
        script = inspect.getsource(extensions_load)
        await self._run_kit_with_script(
            script,
            args=[
                "--/app/extensions/disableStartup=1",   # extensions startup is disabled
                "--/app/extensions/registryEnabled=0",
                "--/app/quitAfter=10",
                "--/crashreporter/gatherUserStory=0", # don't pop up the GUI on crash
            ],
            kit_file=self.kit_file,
            timeout=60,
        )
