import unittest
import steam_relay as relay


class RelayTests(unittest.TestCase):
    def test_money(self):
        cases = [("$23\u202f351", "profit", 23351), ("-$660", "loss", -660), ("+$15,001", "profit", 15001), ("$0", "loss", 0), (None, "profit", None), ("$1.50", "profit", None), ("+$5", "loss", None), ("-$5", "profit", None), ("abc", "loss", None)]
        for raw, direction, expected in cases:
            self.assertEqual(relay.parse_profit(raw, direction), expected, (raw, direction))

    def test_match_and_missing(self):
        state = relay.RelayState()
        profile = {"steamid": "76561197960330724", "name": "Senka"}
        raw = {"game_state": "playing", "profit_loss": "$150", "in_profit_or_loss": "profit"}
        state.update(profile, raw)
        self.assertFalse(state.snapshot()["economy"]["available"])
        state.update(profile, raw)
        first = state.snapshot()
        self.assertEqual(first["economy"]["netProfit"], 150)
        state.update(profile, {**raw, "profit_loss": "$160"})
        self.assertEqual(state.snapshot()["economy"]["netProfit"], 160, "Must not add cumulative readings")
        state.update(profile, {})
        self.assertFalse(state.snapshot()["economy"]["available"])
        state.update(profile, raw)
        self.assertEqual(state.snapshot()["economy"]["matchSessionId"], first["economy"]["matchSessionId"])
        for _ in range(5):
            state.update(profile, {})
        state.update(profile, raw)
        state.update(profile, raw)
        self.assertNotEqual(state.snapshot()["economy"]["matchSessionId"], first["economy"]["matchSessionId"])
        state.fail("Steam closed")
        self.assertIsNone(state.snapshot()["economy"]["netProfit"])
        self.assertFalse(state.snapshot()["economy"]["available"])


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