import carb.tokens
import omni.kit.test

import os

from .test_base import run_process

class TestPythonBat(omni.kit.test.AsyncTestCase):
    async def test_python_bat(self):
        resolve = carb.tokens.get_tokens_interface().resolve

        python_path = resolve("${kit}/python${shell_ext}")
        self.assertTrue(os.path.exists(python_path))

        async def _run_python_and_check(args, timeout=30):
            launch_args = [python_path] + args
            code, err_messages, *_ = await run_process(launch_args, timeout)
            if len(err_messages) > 0:
                print(err_messages)
            self.assertEqual(len(err_messages), 0, msg=err_messages)
            self.assertEqual(code, 0)

        # smoke test and it even runs python ok
        await _run_python_and_check(["-c", "import sys"])

        # check that we can import a few popular modules
        await _run_python_and_check(["-c", "import carb; import omni.kit.app"])
        await _run_python_and_check(["-c", "import omni.usd"])
        await _run_python_and_check(["-c", "import omni.ui"])
