diff --git a/cheddahbot/tools/autocora.py b/cheddahbot/tools/autocora.py index 62ecd1b..7818335 100644 --- a/cheddahbot/tools/autocora.py +++ b/cheddahbot/tools/autocora.py @@ -11,7 +11,7 @@ import json import logging import re import time -from datetime import UTC, datetime +from datetime import UTC, datetime, timedelta from pathlib import Path from . import tool @@ -117,11 +117,8 @@ def _find_qualifying_tasks_sweep(client, config, categories: list[str]): # Current and last month tags (e.g. "feb26", "jan26") current_month_tag = now.strftime("%b%y").lower() - # Go back one month - if now.month == 1: - last_month = now.replace(year=now.year - 1, month=12) - else: - last_month = now.replace(month=now.month - 1) + # Go back one month (use 1st-of-current-month minus 1 day to avoid day-out-of-range) + last_month = now.replace(day=1) - timedelta(days=1) last_month_tag = last_month.strftime("%b%y").lower() # Fetch all "to do" tasks with due dates up to lookahead diff --git a/tests/test_autocora.py b/tests/test_autocora.py index bc0f6a2..259a164 100644 --- a/tests/test_autocora.py +++ b/tests/test_autocora.py @@ -442,13 +442,10 @@ class TestFindQualifyingTasksSweep: assert any(t.id == "t2" for t in result) def test_finds_last_month_tagged(self): - from datetime import UTC, datetime + from datetime import UTC, datetime, timedelta now = datetime.now(UTC) - if now.month == 1: - last = now.replace(year=now.year - 1, month=12) - else: - last = now.replace(month=now.month - 1) + last = now.replace(day=1) - timedelta(days=1) last_tag = last.strftime("%b%y").lower() # No due date needed for month-tag pass task = FakeTask(id="t3", name="Last Month", tags=[last_tag])