import unittest

from phs.modules.mail import MailModule


class MailParserTests(unittest.TestCase):
    def test_authenticated_arrival(self):
        line = (
            "2026-07-23 10:00:00 1abc-000001-2XY <= sender@example.com "
            "H=client.example [203.0.113.10]:55000 P=esmtpsa "
            "A=dovecot_login:sender@example.com S=1234"
        )
        match = MailModule.ARRIVAL.match(line)
        self.assertIsNotNone(match)
        rest = match.group("rest")
        self.assertEqual(MailModule._field(rest, "A"), "dovecot_login:sender@example.com")
        self.assertEqual(MailModule._ip(rest), "203.0.113.10")

    def test_local_arrival(self):
        line = (
            "2026-07-23 10:00:00 1abc-000001-2XZ <= user@host.example "
            "U=cpuser P=local S=1234"
        )
        match = MailModule.ARRIVAL.match(line)
        self.assertIsNotNone(match)
        self.assertEqual(MailModule._field(match.group("rest"), "U"), "cpuser")


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