import unittest

from core.quote_state_machine import QuoteStateMachine


class _State:
    def __init__(self):
        self.pending_image_urls = []
        self.pending_requirements = []
        self.quote_phase = "idle"
        self.quote_ready_turns = 0


class GoldenReplayTests(unittest.TestCase):
    def test_replay_collect_then_ready_then_quote(self):
        sm = QuoteStateMachine(delay_turns=1)
        st = _State()

        replay = [
            {"event": "image", "url": "a.jpg", "want_phase": "collecting"},
            {"event": "image", "url": "b.jpg", "want_phase": "collecting"},
            {"event": "finish", "want_phase": "ready_to_quote", "want_defer": True},
            {"event": "progress", "want_phase": "ready_to_quote", "want_defer": False},
        ]

        for step in replay:
            if step["event"] == "image":
                st.pending_image_urls.append(step["url"])
                sm.refresh(st)
                self.assertEqual(st.quote_phase, step["want_phase"])
            elif step["event"] == "finish":
                deferred = sm.should_defer_batch_quote(st, mark_ready=True)
                self.assertEqual(st.quote_phase, step["want_phase"])
                self.assertEqual(deferred, step["want_defer"])
            elif step["event"] == "progress":
                deferred = sm.should_defer_batch_quote(st, mark_ready=False)
                self.assertEqual(st.quote_phase, step["want_phase"])
                self.assertEqual(deferred, step["want_defer"])


if __name__ == "__main__":
    unittest.main(verbosity=2)
