import json import mailbox from email.message import EmailMessage from typer.testing import CliRunner from reyna_cli.cli import app from reyna_cli.email_local import ThunderbirdEmailClient runner = CliRunner() def make_profile(tmp_path): profile = tmp_path / "profile" inbox = profile / "ImapMail" / "imap.example.com" / "INBOX" inbox.parent.mkdir(parents=True) mbox = mailbox.mbox(str(inbox)) msg = EmailMessage() msg["From"] = "The Home Depot Protection Plan Team " msg["To"] = "adolfo@example.com" msg["Subject"] = "Thank You for Your The Home Depot Protection Plan Purchase" msg["Date"] = "Mon, 01 Sep 2025 04:20:20 +0000" msg["Message-ID"] = "" msg.set_content("Thank you for purchasing your new Whirlpool Refrigerator WRS321SDHZ Protection Plan.") mbox.add(msg) mbox.flush() mbox.close() return profile, inbox def test_email_client_searches_local_thunderbird_mbox(tmp_path, monkeypatch): profile, _ = make_profile(tmp_path) monkeypatch.setenv("REYNA_THUNDERBIRD_PROFILE", str(profile)) result = ThunderbirdEmailClient().search("Whirlpool Refrigerator", limit=5) assert result["results"] first = result["results"][0] assert first["subject"] == "Thank You for Your The Home Depot Protection Plan Purchase" assert "Whirlpool Refrigerator" in " ".join(first["snippets"]) def test_email_cli_has_subcommands(): result = runner.invoke(app, ["email", "--help"]) assert result.exit_code == 0 assert "search" in result.stdout assert "latest" in result.stdout assert "find-receipts" in result.stdout def test_email_cli_search_json(tmp_path, monkeypatch): profile, _ = make_profile(tmp_path) monkeypatch.setenv("REYNA_THUNDERBIRD_PROFILE", str(profile)) result = runner.invoke(app, ["email", "search", "Whirlpool Refrigerator", "--json"]) assert result.exit_code == 0 payload = json.loads(result.stdout) assert payload["ok"] is True assert payload["source"] == "local_thunderbird" assert payload["result"]["results"][0]["message_id"] == "" def test_email_cli_show_by_id(tmp_path, monkeypatch): profile, _ = make_profile(tmp_path) monkeypatch.setenv("REYNA_THUNDERBIRD_PROFILE", str(profile)) search = runner.invoke(app, ["email", "search", "WRS321SDHZ", "--json"]) message_id = json.loads(search.stdout)["result"]["results"][0]["id"] result = runner.invoke(app, ["email", "show", message_id, "--body", "--json"]) assert result.exit_code == 0 payload = json.loads(result.stdout) assert "Whirlpool Refrigerator" in payload["result"]["body"]