import omni.kit.test
import omni.kit.app

from pathlib import Path

from .test_base import KitLaunchTest


class TestCoreOnlyConfiguration(KitLaunchTest):
    async def test_kit_app_with_only_core_exts(self):
        manager = omni.kit.app.get_app().get_extension_manager()
        core_exts = []
        for ext in manager.get_extensions():
            info = manager.get_extension_dict(ext["id"])
            ext_folder_name = Path(info["path"]).parts[-2]
            if ext_folder_name == "extscore":
                core_exts.append(ext["name"])
                self.assertTrue(info["isCore"])

        self.assertGreater(len(core_exts), 0)

        # Enable only core exts:
        kit_file = "[dependencies]\n"
        for ext in core_exts:
            kit_file += '"{0}" = {{}}\n'.format(ext)
        kit_file += "\n[settings.app.exts]\n"
        kit_file += r'folders = ["${kit}/extscore"]'

        # Check all extensions enabled
        script = f"""
import omni.kit.app
manager = omni.kit.app.get_app().get_extension_manager()
core_exts = {core_exts}
for ext in core_exts:
    if not manager.is_extension_enabled(ext):
        print("failed to enable extension: " + ext)
        omni.kit.app.get_app().post_quit(-1)
        """

        await self._run_kit_with_script(script, args=["--/app/quitAfter=10"], kit_file=kit_file)
