|
| 1 | +"""Which forwarded emails are about a job application (pure, no database). |
| 2 | +
|
| 3 | +The first block is the mail that actually filled a user's inbox in production. |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from app.services.inbound_email.classifier import classify |
| 13 | +from app.services.inbound_email.relevance import is_job_related, looks_bulk |
| 14 | + |
| 15 | + |
| 16 | +def judge( |
| 17 | + *, sender: str, subject: str, body: str = "", headers: dict[str, str] | None = None, matched: bool = False |
| 18 | +) -> bool: |
| 19 | + keep, _ = is_job_related( |
| 20 | + subject=subject, |
| 21 | + snippet=body, |
| 22 | + from_domain=sender.rpartition("@")[2], |
| 23 | + classification=classify(subject, body), |
| 24 | + matched=matched, |
| 25 | + bulk=looks_bulk(headers=headers or {}, from_address=sender, snippet=body), |
| 26 | + ) |
| 27 | + return keep |
| 28 | + |
| 29 | + |
| 30 | +UNSUB = {"List-Unsubscribe": "<https://example.com/u>"} |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize( |
| 34 | + "mail", |
| 35 | + [ |
| 36 | + dict(sender="noreply@glassdoor.com", subject="At 30 years old, with $600,000 in saving an annual...", headers=UNSUB), |
| 37 | + dict(sender="dan@tldrnewsletter.com", subject="Gemini Hydrogen arrives, Resend moves to ...", body="Sponsor: we are hiring engineers."), |
| 38 | + dict(sender="team@cs.resumeworded.com", subject="CS#376: Terrible cooks, the two-skip rule, and how to interview", headers=UNSUB), |
| 39 | + dict(sender="dan@tldrnewsletter.com", subject="Nvidia's CEO search, AWS emulator", headers=UNSUB), |
| 40 | + dict(sender="info@lagenhetsbyte.se", subject="Du har nya bytesförslag att ta ställning till"), |
| 41 | + # A parcel notice from a company the user applied to must not pass as |
| 42 | + # mail about that application. |
| 43 | + dict(sender="shipment-tracking@amazon.se", subject='Skickade: "Amazon Basics HDMI Splitter..." och 1 till', matched=True), |
| 44 | + dict(sender="orders@spotify.com", subject="Your receipt from Spotify", matched=True), |
| 45 | + dict(sender="jobs-noreply@linkedin.com", subject="30 new jobs for Backend Developer", body="Apply now. Unsubscribe"), |
| 46 | + dict(sender="friend@example.com", subject="Dinner on Friday?", body="Are you free?"), |
| 47 | + ], |
| 48 | +) |
| 49 | +def test_noise_is_not_kept(mail: dict[str, Any]) -> None: |
| 50 | + assert judge(**mail) is False |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "mail", |
| 55 | + [ |
| 56 | + dict(sender="no-reply@teamtailor.com", subject="Thank you for your application to Klarna", body="We have received your application."), |
| 57 | + dict(sender="rekrytering@kommun.se", subject="Tack för din ansökan", body="Vi har tagit emot din ansökan till tjänsten."), |
| 58 | + dict(sender="anna@klarna.com", subject="Quick question", body="Hi Sam, are you free on Tuesday at 10?", matched=True), |
| 59 | + dict(sender="hr@company.se", subject="Regarding the position", body="Unfortunately we have decided to move forward with other candidates."), |
| 60 | + dict(sender="talent@startup.io", subject="Interview invitation", body="We would like to invite you to an interview."), |
| 61 | + dict(sender="no-reply@greenhouse.io", subject="An update from Acme", body="Please see the message below."), |
| 62 | + # A status mail sent through a mailing system is still kept when it is |
| 63 | + # tied to something the user applied for. |
| 64 | + dict(sender="careers@klarna.com", subject="Your application at Klarna", body="Thanks for applying.", headers=UNSUB, matched=True), |
| 65 | + dict(sender="noreply@varbi.com", subject="Bekräftelse på din ansökan", body="Din ansökan är mottagen.", headers=UNSUB), |
| 66 | + ], |
| 67 | +) |
| 68 | +def test_job_mail_is_kept(mail: dict[str, Any]) -> None: |
| 69 | + assert judge(**mail) is True |
| 70 | + |
| 71 | + |
| 72 | +def test_a_clear_decision_is_kept_even_from_a_bulk_sender() -> None: |
| 73 | + assert judge( |
| 74 | + sender="newsletter@bigcorp.com", |
| 75 | + subject="Your application", |
| 76 | + body="Unfortunately we will not be moving forward with your application.", |
| 77 | + headers=UNSUB, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize( |
| 82 | + ("headers", "sender", "body", "expected"), |
| 83 | + [ |
| 84 | + ({"List-Unsubscribe": "<mailto:x>"}, "a@b.com", "", True), |
| 85 | + ({"Precedence": "bulk"}, "a@b.com", "", True), |
| 86 | + ({}, "dan@tldrnewsletter.com", "", True), |
| 87 | + ({}, "a@b.com", "Click here to unsubscribe.", True), |
| 88 | + ({}, "a@b.com", "Klicka här för att avregistrera dig.", True), |
| 89 | + ({}, "anna@klarna.com", "Hi Sam, are you free on Tuesday?", False), |
| 90 | + ], |
| 91 | +) |
| 92 | +def test_bulk_detection(headers: dict[str, str], sender: str, body: str, expected: bool) -> None: |
| 93 | + assert looks_bulk(headers=headers, from_address=sender, snippet=body) is expected |
0 commit comments