77 lines
2.7 KiB
Python
77 lines
2.7 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
from gateway.notification_preferences import (
|
|
MAILBOX_SUMMARY_CATEGORY,
|
|
add_hard_stop,
|
|
has_hard_stop,
|
|
job_targets_email,
|
|
matching_mailbox_summary_jobs,
|
|
text_requests_mailbox_summary_stop,
|
|
)
|
|
|
|
|
|
class DummyJob(dict):
|
|
pass
|
|
|
|
|
|
def test_text_requests_mailbox_summary_stop_matches_explicit_stop_phrases():
|
|
assert text_requests_mailbox_summary_stop("請不要再寄我信件摘要了") is True
|
|
assert text_requests_mailbox_summary_stop("stop summary") is True
|
|
assert text_requests_mailbox_summary_stop("unsubscribe yesterday digest") is True
|
|
assert text_requests_mailbox_summary_stop("停止提供昨天信件摘要") is True
|
|
assert text_requests_mailbox_summary_stop("請幫我整理昨天信件摘要") is False
|
|
assert text_requests_mailbox_summary_stop("停止提醒這個專案進度") is False
|
|
|
|
|
|
def test_add_hard_stop_round_trips(tmp_path, monkeypatch):
|
|
hermes_home = tmp_path / ".hermes"
|
|
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
|
|
|
|
assert add_hard_stop("Elena.Cheng@bremen.com.tw", note="user requested stop") is True
|
|
assert has_hard_stop("elena.cheng@bremen.com.tw") is True
|
|
|
|
prefs = json.loads((hermes_home / "state" / "notification_preferences.json").read_text(encoding="utf-8"))
|
|
row = prefs[MAILBOX_SUMMARY_CATEGORY]["elena.cheng@bremen.com.tw"]
|
|
assert row["enabled"] is True
|
|
assert row["note"] == "user requested stop"
|
|
|
|
|
|
def test_job_targets_email_and_matching_mailbox_summary_jobs(tmp_path, monkeypatch):
|
|
hermes_home = tmp_path / ".hermes"
|
|
scripts_dir = hermes_home / "scripts"
|
|
scripts_dir.mkdir(parents=True)
|
|
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
|
|
|
|
summary_script = scripts_dir / "daily_email_summary_dedupe.py"
|
|
summary_script.write_text(
|
|
"RECIPIENT = 'elena.cheng@bremen.com.tw'\n",
|
|
encoding="utf-8",
|
|
)
|
|
other_script = scripts_dir / "phoenix_daily_change_email.py"
|
|
other_script.write_text(
|
|
"RECIPIENT = 'someone@example.com'\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
matching = matching_mailbox_summary_jobs(
|
|
[
|
|
DummyJob(
|
|
id="job-1",
|
|
name="daily-email-summary-10am",
|
|
script=str(summary_script),
|
|
prompt_preview="昨天信件摘要",
|
|
),
|
|
DummyJob(
|
|
id="job-2",
|
|
name="phoenix-daily-change-email",
|
|
script=str(other_script),
|
|
prompt_preview="公槽異動摘要",
|
|
),
|
|
],
|
|
"elena.cheng@bremen.com.tw",
|
|
)
|
|
|
|
assert job_targets_email({"script": str(summary_script)}, "elena.cheng@bremen.com.tw") is True
|
|
assert [job["id"] for job in matching] == ["job-1"]
|