"""Tests reparenting workflows"""

import omni.graph.core as og
import omni.graph.core.tests as ogts
import omni.kit.test
import omni.usd


# ======================================================================
class TestReparentGraph(ogts.OmniGraphTestCase):
    """Run tests that exercises the reparenting of graph and checks that fabric is cleaned up properly
    this is separate from test_rename_and_reparent.py tests because of different settings need
    via OmniGraphDefaultTestCase in order for legacy prims to be added to fabric.
    """

    async def test_reparent_fabric(self):
        """Test basic reparenting a graph back away and then back to original parent."""

        flags = []
        world_graph_key = '"/World/ActionGraph"'
        xform_graph_key = '"/World/Xform/ActionGraph"'

        # Load the test scene which has /World/ActionGraph and /World/Xform
        (result, error) = await ogts.load_test_file("TestReparentGraph.usda", use_caller_subdirectory=True)
        self.assertTrue(result, error)

        # Verify that fabric has "/World/ActionGraph" and not "/World/Xform/ActionGraph"
        self.assertEqual(len(og.get_compute_graph_contexts()), 1)
        ctx = og.get_compute_graph_contexts()[0]
        post_load_fabric = og.OmniGraphInspector().as_json(ctx, flags=flags)
        self.assertTrue(post_load_fabric.find(world_graph_key) >= 0)
        self.assertTrue(post_load_fabric.find(xform_graph_key) == -1)

        # Reparent the graph to the Xform
        omni.kit.commands.execute("MovePrim", path_from="/World/ActionGraph", path_to="/World/Xform/ActionGraph")
        await omni.kit.app.get_app().next_update_async()

        # Verify that fabric no longer has "/World/ActionGraph" and now has "/World/Xform/ActionGraph"
        self.assertEqual(len(og.get_compute_graph_contexts()), 1)
        ctx = og.get_compute_graph_contexts()[0]
        post_reparent_to_xform_fabric = og.OmniGraphInspector().as_json(ctx, flags=flags)
        self.assertTrue(post_reparent_to_xform_fabric.find(world_graph_key) == -1)
        self.assertTrue(post_reparent_to_xform_fabric.find(xform_graph_key) >= 0)
