import unittest

from fgai.logs import local_in_failures, parse_log_line, top_field_values


class DiagnosticTests(unittest.TestCase):
    def test_local_in_failures_group_by_source_service_policy(self):
        events = [
            parse_log_line(
                'type=traffic policytype="local-in-policy6" policyid=2 srcip=192.168.1.20 '
                'service="udp/5353" msg="Connection Failed"'
            ),
            parse_log_line(
                'type=traffic policytype="local-in-policy6" policyid=2 srcip=192.168.1.20 '
                'service="udp/5353" msg="Connection Failed"'
            ),
        ]

        self.assertEqual(
            local_in_failures(events),
            [{"src_ip": "192.168.1.20", "service": "udp/5353", "policy": "2", "count": 2}],
        )

    def test_top_field_values_counts_values(self):
        events = [
            parse_log_line("srcip=1.1.1.1 service=https"),
            parse_log_line("srcip=1.1.1.1 service=http"),
            parse_log_line("srcip=8.8.8.8 service=http"),
        ]

        self.assertEqual(top_field_values(events, "srcip", limit=1), [{"value": "1.1.1.1", "count": 2}])


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