Initial commit for the rspamd spam/ham trainer

This commit is contained in:
2026-06-22 16:17:38 +02:00
commit 7bc9d7a901
18 changed files with 1071 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
from pathlib import Path
import tempfile
import unittest
from rspamd_mail_trainer.cli import SearchOptions, iter_candidates, maybe_compile, parse_user_date
def write_mail(path: Path, sender: str, subject: str, body: str = "hello") -> None:
path.write_text(
"\n".join([
f"From: {sender}",
"To: johannes@example.org",
f"Subject: {subject}",
"Date: Fri, 12 Jun 2026 13:36:00 +0000",
"Message-ID: <test@example.org>",
"",
body,
]),
encoding="utf-8",
)
class CliTests(unittest.TestCase):
def test_maildir_sender_match(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td) / "Maildir"
cur = root / "cur"
cur.mkdir(parents=True)
write_mail(cur / "1:2,S", "no-reply@example.test", "Monthly report")
opts = SearchOptions(
from_re=maybe_compile("example.test", True, True),
to_re=None,
subject_re=None,
any_re=None,
after=None,
before=None,
max_size=1024 * 1024,
include_seen_duplicates=False,
)
matches = list(iter_candidates([root], opts))
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0][0].sender, "no-reply@example.test")
def test_subject_match(self) -> None:
with tempfile.TemporaryDirectory() as td:
root = Path(td) / "Maildir"
cur = root / "cur"
cur.mkdir(parents=True)
write_mail(cur / "2:2,S", "sender@example.org", "Invoice 123")
opts = SearchOptions(
from_re=None,
to_re=None,
subject_re=maybe_compile("Invoice", True, True),
any_re=None,
after=None,
before=None,
max_size=1024 * 1024,
include_seen_duplicates=False,
)
matches = list(iter_candidates([root], opts))
self.assertEqual(len(matches), 1)
def test_parse_user_date_day_start(self) -> None:
parsed = parse_user_date("2026-06-13")
self.assertIsNotNone(parsed)
assert parsed is not None
self.assertEqual(parsed.hour, 0)
self.assertEqual(parsed.minute, 0)
if __name__ == "__main__":
unittest.main()