import unittest
from modelverse_kernel.main import ModelverseKernel
from test.utils import execute_until_finished, MvSWrapper

class TestHelper(unittest.TestCase):
    def setUp(self):
        self.mvs = MvSWrapper()
        self.root = self.mvs.execute("RR", [])
        self.mvk = ModelverseKernel(self.root)

    def test_next(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])
        task_frame = self.mvs.execute("CN", [])
        phase = self.mvs.execute("CNV", ["finish"])
        inst = self.mvs.execute("CN", [])
        next_inst = self.mvs.execute("CNV", ["next"])

        self.mvs.execute("CD", [root, "task_1", task_root])
        self.mvs.execute("CD", [task_root, "frame", task_frame])
        self.mvs.execute("CD", [task_frame, "phase", phase])
        self.mvs.execute("CD", [task_frame, "IP", inst])
        self.mvs.execute("CD", [inst, "next", next_inst])

        # Do the invocation until it returns None
        execute_until_finished(self.mvk, self.mvs)

        # Execution of the command finished, so the MvS should be in the correct new state right now
        self.assertEquals(self.mvs.execute("RD", [root, "task_1"]), task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"]), task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])]), "init")
        self.assertEquals(self.mvs.execute("RD", [inst, "next"]), next_inst)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"]), next_inst)

    def test_evalstack(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])
        task_frame = self.mvs.execute("CN", [])
        phase = self.mvs.execute("CNV", ["finish"])
        inst = self.mvs.execute("CN", [])
        evalstack = self.mvs.execute("CN", [])
        prev_evalstack = self.mvs.execute("CN", [])
        evalstack_inst = self.mvs.execute("CN", [])
        evalstack_phase = self.mvs.execute("CN", [])

        self.mvs.execute("CD", [root, "task_1", task_root])
        self.mvs.execute("CD", [task_root, "frame", task_frame])
        self.mvs.execute("CD", [task_frame, "phase", phase])
        self.mvs.execute("CD", [task_frame, "IP", inst])
        self.mvs.execute("CD", [task_frame, "evalstack", evalstack])
        self.mvs.execute("CD", [evalstack, "prev", prev_evalstack])
        self.mvs.execute("CD", [prev_evalstack, "inst", evalstack_inst])
        self.mvs.execute("CD", [prev_evalstack, "phase", evalstack_phase])

        # Do the invocation until it returns None
        execute_until_finished(self.mvk, self.mvs)

        # Execution of the command finished, so the MvS should be in the correct new state right now
        self.assertEquals(self.mvs.execute("RD", [root, "task_1"]), task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"]), task_frame)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"]), evalstack_inst)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "phase"]), evalstack_phase)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "evalstack"]), prev_evalstack)
