"""
Tests for the omnigraph.tutorial.state node
"""
import omni.graph.core as og
import omni.graph.core.tests as ogts


class TestTutorialState(ogts.OmniGraphTestCase):
    """.ogn tests only run once while state tests require multiple evaluations, handled here"""

    # ----------------------------------------------------------------------
    async def test_tutorial_state_node(self):
        """Test basic operation of the tutorial node containing internal node state"""
        self.assertTrue(og.get_node_type("omni.graph.tutorials.State").has_state(), "Tutorial state node has state")

        # Create a set of state nodes, updating after each one so that the state information is properly initialized.
        # If that requirement weren't there this would just be done in one call.
        state_nodes = []
        for index in range(5):
            (graph, new_nodes, _, _) = og.Controller.edit(
                "/StateGraph",
                {
                    og.Controller.Keys.CREATE_NODES: [
                        (f"State{index}", "omni.graph.tutorials.State"),
                    ]
                },
            )
            state_nodes.append(new_nodes[0])
            await og.Controller.evaluate(graph)

        output_attrs = [state_node.get_attribute("outputs:monotonic") for state_node in state_nodes]

        output_values = [og.Controller(output_attr).get() for output_attr in output_attrs]
        for i in range(len(output_values) - 1):
            self.assertLess(output_values[i], output_values[i + 1], "Comparing state of other nodes")

        await og.Controller.evaluate(graph)
        new_values = [og.Controller(output_attr).get() for output_attr in output_attrs]
        for i in range(len(new_values) - 1):
            self.assertLess(new_values[i], new_values[i + 1], "Comparing second state of other nodes")
        for i, new_value in enumerate(new_values):
            self.assertLess(output_values[i], new_value, "Comparing node state updates")
