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

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

    def test_output_init(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])[0]
        task_frame = self.mvs.execute("CN", [])[0]
        phase = self.mvs.execute("CNV", ["init"])[0]
        inst = self.mvs.execute("CNV", [{"value":"output"}])[0]
        value = self.mvs.execute("CN", [])[0]
        evalstack = self.mvs.execute("CN", [])[0]

        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", [inst, "value", value])

        # 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"])[0], task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"])[0], task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])[0]])[0], "init")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"])[0], value)
        new_evalstack = self.mvs.execute("RD", [task_frame, "evalstack"])[0]
        self.assertNotEquals(new_evalstack, evalstack)
        self.assertEquals(self.mvs.execute("RD", [new_evalstack, "prev"])[0], evalstack)
        self.assertEquals(self.mvs.execute("RD", [evalstack, "inst"])[0], inst)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [evalstack, "phase"])[0]])[0], "output")

    def test_output_output(self):
        root = self.root
        task_root = self.mvs.execute("CN", [])[0]
        task_frame = self.mvs.execute("CN", [])[0]
        phase = self.mvs.execute("CNV", ["output"])[0]
        inst = self.mvs.execute("CNV", [{"value":"output"}])[0]
        retval = self.mvs.execute("CN", [])[0]
        output_queue = self.mvs.execute("CN", [])[0]
        value = self.mvs.execute("CN", [])[0]

        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, "returnvalue", retval])
        self.mvs.execute("CD", [task_root, "last_output", output_queue])

        # 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"])[0], task_root)
        self.assertEquals(self.mvs.execute("RD", [task_root, "frame"])[0], task_frame)
        self.assertEquals(self.mvs.execute("RV", [self.mvs.execute("RD", [task_frame, "phase"])[0]])[0], "finish")
        self.assertEquals(self.mvs.execute("RD", [task_frame, "IP"])[0], inst)
        self.assertEquals(self.mvs.execute("RD", [task_frame, "returnvalue"])[0], retval)
        output_queue_next = self.mvs.execute("RD", [task_root, "last_output"])[0]
        self.assertNotEquals(output_queue_next, output_queue)
        self.assertEquals(self.mvs.execute("RD", [output_queue, "next"])[0], output_queue_next)
        self.assertEquals(self.mvs.execute("RD", [output_queue, "value"])[0], retval)
