-
Notifications
You must be signed in to change notification settings - Fork 50
Email demo mbox #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Email demo mbox #198
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5d0a61d
- added mbox handling
bmerkle 47d34cb
- major refactoring
bmerkle 853d1b9
fix and update docs
bmerkle 89dc931
fixed format
bmerkle 38e6282
Update tools/ingest_email.py
bmerkle a3d2002
Update src/typeagent/emails/email_import.py
bmerkle 85b14b1
added test data for small mbox
bmerkle b2028a2
- fixed copilot reviews
bmerkle 8947349
semantic of before is usually inclusive, so >= is ok
bmerkle c4a7e4d
fix: update charset fallback in email payload decoding to latin-1
bmerkle 2a0335e
removed 2 mbox data files
bmerkle 969da13
- implemented review feedback from Guido
bmerkle 360f7d7
fix: update email preview length in verbose output
bmerkle 9aa193b
Refactor email ingestion tools and update documentation
bmerkle 62f7cb9
moved email date filtering logic and update related tests
bmerkle 56184d7
fix: remove carriage returns from email test cases for consistency
bmerkle eefc8d7
fix: adjust naive timestamp handling to ensure consistent UTC offset …
bmerkle 892480d
- enhance email header handling
bmerkle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Tests for email filtering logic and email parsing edge cases.""" | ||
|
|
||
| from datetime import datetime, timezone | ||
|
|
||
| # Import the filtering helpers from the tool module. | ||
bmerkle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import importlib | ||
| import importlib.util | ||
| from pathlib import Path | ||
| import sys | ||
|
|
||
| from typeagent.emails.email_import import import_email_string | ||
|
|
||
| _tools_dir = str(Path(__file__).resolve().parent.parent / "tools") | ||
| _spec = importlib.util.spec_from_file_location( | ||
| "ingest_email", Path(_tools_dir) / "ingest_email.py" | ||
| ) | ||
| assert _spec and _spec.loader | ||
| _ingest_mod = importlib.util.module_from_spec(_spec) | ||
| sys.modules["ingest_email"] = _ingest_mod | ||
| _spec.loader.exec_module(_ingest_mod) | ||
|
|
||
| from ingest_email import ( # type: ignore[import-untyped] | ||
| _email_matches_date_filter, | ||
| ) | ||
|
|
||
| # =========================================================================== | ||
| # Tests for _email_matches_date_filter | ||
| # =========================================================================== | ||
|
|
||
|
|
||
| class TestEmailMatchesDateFilter: | ||
| """Tests for the _email_matches_date_filter helper in ingest_email.py.""" | ||
|
|
||
| def _utc(self, year: int, month: int, day: int) -> datetime: | ||
| return datetime(year, month, day, tzinfo=timezone.utc) | ||
|
|
||
| def test_no_filters(self) -> None: | ||
| """All emails pass when no filters are set.""" | ||
| assert _email_matches_date_filter("2024-01-15T10:00:00+00:00", None, None) | ||
|
|
||
| def test_none_timestamp_always_passes(self) -> None: | ||
| """Emails without a timestamp are always included.""" | ||
| assert _email_matches_date_filter( | ||
| None, self._utc(2024, 1, 1), self._utc(2024, 12, 31) | ||
| ) | ||
|
|
||
| def test_invalid_timestamp_always_passes(self) -> None: | ||
| """Emails with unparseable timestamps are always included.""" | ||
| assert _email_matches_date_filter( | ||
| "not-a-date", self._utc(2024, 1, 1), self._utc(2024, 12, 31) | ||
| ) | ||
|
|
||
| def test_after_filter_includes(self) -> None: | ||
| """Email on or after the 'after' date passes.""" | ||
| after = self._utc(2024, 1, 15) | ||
| assert _email_matches_date_filter("2024-01-15T00:00:00+00:00", after, None) | ||
| assert _email_matches_date_filter("2024-01-16T00:00:00+00:00", after, None) | ||
|
|
||
| def test_after_filter_excludes(self) -> None: | ||
| """Email before the 'after' date is excluded.""" | ||
| after = self._utc(2024, 1, 15) | ||
| assert not _email_matches_date_filter("2024-01-14T23:59:59+00:00", after, None) | ||
|
|
||
| def test_before_filter_includes(self) -> None: | ||
| """Email before the 'before' date passes.""" | ||
| before = self._utc(2024, 2, 1) | ||
| assert _email_matches_date_filter("2024-01-31T23:59:59+00:00", None, before) | ||
|
|
||
| def test_before_filter_excludes(self) -> None: | ||
| """Email on or after the 'before' date is excluded (exclusive upper bound).""" | ||
| before = self._utc(2024, 2, 1) | ||
| assert not _email_matches_date_filter("2024-02-01T00:00:00+00:00", None, before) | ||
|
|
||
| def test_date_range(self) -> None: | ||
| """Email within [after, before) passes; outside fails.""" | ||
| after = self._utc(2024, 1, 1) | ||
| before = self._utc(2024, 2, 1) | ||
| # Inside | ||
| assert _email_matches_date_filter("2024-01-15T12:00:00+00:00", after, before) | ||
| # Before range | ||
| assert not _email_matches_date_filter( | ||
| "2023-12-31T23:59:59+00:00", after, before | ||
| ) | ||
| # At upper bound (exclusive) | ||
| assert not _email_matches_date_filter( | ||
| "2024-02-01T00:00:00+00:00", after, before | ||
| ) | ||
|
|
||
| def test_naive_timestamp_treated_as_local(self) -> None: | ||
| """Offset-naive timestamps should be treated as local time.""" | ||
| from datetime import datetime as dt | ||
|
|
||
| # Build the filter boundary in local time so the test is TZ-independent | ||
bmerkle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| local_tz = dt.now().astimezone().tzinfo | ||
| after = datetime(2024, 1, 15, tzinfo=local_tz) | ||
| assert _email_matches_date_filter("2024-01-15T00:00:00", after, None) | ||
| assert not _email_matches_date_filter("2024-01-14T23:59:59", after, None) | ||
|
|
||
| def test_different_timezone(self) -> None: | ||
| """Timestamps with non-UTC offsets are compared correctly.""" | ||
| # 2024-01-15T00:00:00+05:00 is 2024-01-14T19:00:00 UTC | ||
| after = self._utc(2024, 1, 15) | ||
| assert not _email_matches_date_filter("2024-01-15T00:00:00+05:00", after, None) | ||
| # 2024-01-15T10:00:00+05:00 is 2024-01-15T05:00:00 UTC | ||
| assert _email_matches_date_filter("2024-01-15T10:00:00+05:00", after, None) | ||
|
|
||
|
|
||
| # =========================================================================== | ||
| # Tests for email encoding edge cases | ||
| # =========================================================================== | ||
|
|
||
|
|
||
| _EMAIL_WITH_ENCODED_HEADER = """\ | ||
| From: =?utf-8?b?SsO8cmdlbg==?= <juergen@example.com>\r | ||
bmerkle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| To: recipient@example.com\r | ||
| Subject: =?utf-8?q?M=C3=BCnchen_weather?=\r | ||
| Date: Mon, 01 Jan 2024 10:00:00 +0000\r | ||
| Message-ID: <encoded@example.com>\r | ||
| \r | ||
| Hello from Munich!\r | ||
| """ | ||
|
|
||
|
|
||
| class TestEncodingEdgeCases: | ||
| def test_encoded_header_sender(self) -> None: | ||
| """RFC 2047 encoded sender should be decoded to a string, not raise.""" | ||
| email = import_email_string(_EMAIL_WITH_ENCODED_HEADER) | ||
| assert isinstance(email.metadata.sender, str) | ||
|
|
||
| def test_encoded_header_subject(self) -> None: | ||
| """RFC 2047 encoded subject should be decoded to a string.""" | ||
| email = import_email_string(_EMAIL_WITH_ENCODED_HEADER) | ||
| assert isinstance(email.metadata.subject, str) | ||
|
|
||
|
|
||
| _EMAIL_WITH_UNKNOWN_CHARSET = """\ | ||
| From: test@example.com\r | ||
| To: recipient@example.com\r | ||
| Subject: Unknown charset test\r | ||
| Date: Mon, 01 Jan 2024 10:00:00 +0000\r | ||
| Message-ID: <charset@example.com>\r | ||
| MIME-Version: 1.0\r | ||
| Content-Type: text/plain; charset="iso-8859-8-i"\r | ||
| Content-Transfer-Encoding: base64\r | ||
| \r | ||
| SGVsbG8gV29ybGQ=\r | ||
| """ | ||
|
|
||
|
|
||
| class TestUnknownCharset: | ||
| def test_unknown_charset_does_not_crash(self) -> None: | ||
| """An email with an unknown charset should be decoded without raising.""" | ||
| email = import_email_string(_EMAIL_WITH_UNKNOWN_CHARSET) | ||
| body = " ".join(email.text_chunks) | ||
| assert "Hello World" in body or len(body) > 0 | ||
|
|
||
|
|
||
| # =========================================================================== | ||
| # Tests for mbox with missing / malformed date | ||
| # =========================================================================== | ||
|
|
||
| _EMAIL_NO_DATE = """\ | ||
| From: test@example.com\r | ||
| To: recipient@example.com\r | ||
| Subject: No date header\r | ||
| Message-ID: <nodate@example.com>\r | ||
| \r | ||
| This email has no Date header.\r | ||
| """ | ||
|
|
||
|
|
||
| class TestMissingDate: | ||
| def test_email_without_date_has_none_timestamp(self) -> None: | ||
| email = import_email_string(_EMAIL_NO_DATE) | ||
| assert email.timestamp is None | ||
|
|
||
| def test_email_without_date_passes_date_filter(self) -> None: | ||
| """Emails without timestamps should always pass the date filter.""" | ||
| assert _email_matches_date_filter( | ||
| None, datetime(2024, 1, 1, tzinfo=timezone.utc), None | ||
| ) | ||
|
|
||
|
|
||
| # =========================================================================== | ||
| # Tests for import_email_string (also exercised by mbox, but directly tested) | ||
bmerkle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # =========================================================================== | ||
|
|
||
| _SIMPLE_EMAIL = """\ | ||
| From: alice@example.com\r | ||
| To: bob@example.com\r | ||
| Subject: Test\r | ||
| Date: Mon, 01 Jan 2024 10:00:00 +0000\r | ||
| Message-ID: <simple@example.com>\r | ||
| \r | ||
| Hello Bob!\r | ||
| """ | ||
|
|
||
| _MULTIPART_EMAIL = """\ | ||
| From: alice@example.com\r | ||
| To: bob@example.com\r | ||
| Subject: Multipart\r | ||
| Date: Mon, 01 Jan 2024 10:00:00 +0000\r | ||
| MIME-Version: 1.0\r | ||
| Content-Type: multipart/alternative; boundary="boundary"\r | ||
| \r | ||
| --boundary\r | ||
| Content-Type: text/plain\r | ||
| \r | ||
| Plain text body\r | ||
| --boundary\r | ||
| Content-Type: text/html\r | ||
| \r | ||
| <p>HTML body</p>\r | ||
| --boundary--\r | ||
| """ | ||
|
|
||
|
|
||
| class TestImportEmailString: | ||
| def test_simple_email(self) -> None: | ||
| email = import_email_string(_SIMPLE_EMAIL) | ||
| assert "alice@example.com" in email.metadata.sender | ||
| assert email.metadata.subject is not None | ||
| assert "Test" in email.metadata.subject | ||
| assert email.metadata.id == "<simple@example.com>" | ||
| assert email.timestamp is not None | ||
| assert len(email.text_chunks) > 0 | ||
|
|
||
| def test_multipart_email(self) -> None: | ||
| email = import_email_string(_MULTIPART_EMAIL) | ||
| # Should extract the plain text part | ||
| body = " ".join(email.text_chunks) | ||
| assert "Plain text body" in body | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.