diff options
| author | Danilo M. <danix@danix.xyz> | 2026-11-16 12:24:22 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-11-16 12:24:22 +0100 |
| commit | 227258a199283089ec16e4798988277cf5062e67 (patch) | |
| tree | 66500eb7a9f84b2ab85c3bbf1ab1c771c4eb48dd /assets/hooks/test_mailrules.py | |
| parent | 21da4ee6c225d2b0b04fc51e17f39f446740a35a (diff) | |
| download | qtmaildir-227258a199283089ec16e4798988277cf5062e67.tar.gz qtmaildir-227258a199283089ec16e4798988277cf5062e67.zip | |
feat(hooks): let a rule move new mail to spam or trash
A rule may carry "move": "spam" | "trash", an account role rather than a
folder name. The post-new hook moves each matching tag:new message into
its own account's configured folder under a fresh Maildir name in cur/,
records the renames with one notmuch new --no-hooks, then tags +spam or
+deleted, -inbox and the account-relative moved-from: origin. unread is
never touched. A message with several files, in no account, or whose
account has no folder for the role is logged and left alone, and a
configured folder that does not exist is never created.
Also fixes _quote, which escaped quotes with a backslash notmuch does not
honour, so an id holding a quote matched nothing on the carve-out path.
Item 214.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'assets/hooks/test_mailrules.py')
| -rwxr-xr-x | assets/hooks/test_mailrules.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/assets/hooks/test_mailrules.py b/assets/hooks/test_mailrules.py index b1f31c9..6613fc0 100755 --- a/assets/hooks/test_mailrules.py +++ b/assets/hooks/test_mailrules.py @@ -266,6 +266,53 @@ def test_tag_arguments(): assert mailrules.tag_arguments(rule) == ["+one", "+two", "-three"] +def test_a_move_rule_loads_and_round_trips(): + with tempfile.TemporaryDirectory() as tmp: + path = write_rules(tmp, {"version": 1, "rules": [ + {"id": "junk", "query": "from:spammer@example.net", + "move": "spam"}]}) + store = mailrules.load(path) + assert store.warnings == [], store.warnings + # A move-only rule is a rule: it adds and removes nothing. + assert [r.id for r in store.rules] == ["junk"] + assert store.rules[0].move == "spam" + assert "move" not in store.rules[0].unknown + mailrules.save(store, path) + assert json.loads(path.read_text())["rules"][0]["move"] == "spam" + + +def test_a_rule_without_a_move_writes_no_move_key(): + """Absent stays absent. Writing `"move": ""` on every rule would rewrite + every file either reader saves, a diff nobody made.""" + with tempfile.TemporaryDirectory() as tmp: + path = write_rules(tmp, {"version": 1, "rules": [ + {"id": "x", "add": ["x"], "query": "from:a@example.com"}]}) + mailrules.save(mailrules.load(path), path) + assert "move" not in json.loads(path.read_text())["rules"][0] + + +def test_an_unknown_move_role_is_ignored_and_the_tags_still_apply(): + """The hook must never guess a destination, and must not lose the rule's + harmless tagging over it either.""" + with tempfile.TemporaryDirectory() as tmp: + path = write_rules(tmp, {"version": 1, "rules": [ + {"id": "odd", "add": ["x"], "query": "from:a@example.com", + "move": "Archive"}]}) + store = mailrules.load(path) + assert store.rules[0].move == "" + assert store.rules[0].add == ["x"] + assert len(store.warnings) == 1 and "Archive" in store.warnings[0] + + +def test_a_rule_whose_only_action_is_a_bad_move_is_dropped(): + with tempfile.TemporaryDirectory() as tmp: + path = write_rules(tmp, {"version": 1, "rules": [ + {"id": "odd", "query": "from:a@example.com", "move": "archive"}]}) + store = mailrules.load(path) + assert store.rules == [] + assert len(store.warnings) == 2, store.warnings + + def run_all(): for name, fn in sorted(globals().items()): if name.startswith("test_") and callable(fn): |
