Skip to content

Commit 2d53882

Browse files
fix: compute time values per-request in check-schedule endpoint
Move current_time_local and current_day from module-level constants into should_post_to_slack() so they reflect request time instead of server startup time. Remove unused current_time_utc. Add test that mocks arrow.now to verify the endpoint uses request-time values. Closes TASK-020 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 896dfbe commit 2d53882

3 files changed

Lines changed: 49 additions & 8 deletions

File tree

app/main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@
4545
bypass_schedule = config("OVERRIDE", default=False, cast=bool)
4646
DEV = config("DEV", default=False, cast=bool)
4747

48-
# time
49-
current_time_local = arrow.now(tz)
50-
current_time_utc = arrow.utcnow()
51-
current_day = current_time_local.format("dddd") # Monday, Tuesday, etc.
5248
time.tzset()
5349

5450
# pandas don't truncate output
@@ -438,6 +434,9 @@ def should_post_to_slack(auth: dict = Depends(ip_whitelist_or_auth), request: Re
438434
Check if it's time to post to Slack based on the schedule
439435
"""
440436

437+
current_time_local = arrow.now(tz)
438+
current_day = current_time_local.format("dddd")
439+
441440
with db_session:
442441
check_and_revert_snooze() # Check and revert any expired snoozes
443442
schedule = get_schedule(current_day)

backlog/tasks/task-020 - Fix-stale-module-level-current_time_local-in-main.py.md renamed to backlog/completed/task-020 - Fix-stale-module-level-current_time_local-in-main.py.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
id: TASK-020
33
title: Fix stale module-level current_time_local in main.py
4-
status: To Do
4+
status: Done
55
assignee: []
66
created_date: '2026-03-21 00:58'
7+
updated_date: '2026-03-21 01:12'
78
labels:
89
- bug
910
dependencies: []
@@ -21,7 +22,13 @@ priority: medium
2122

2223
## Acceptance Criteria
2324
<!-- AC:BEGIN -->
24-
- [ ] #1 current_time_local, current_time_utc, and current_day are computed per-request in endpoints that use them
25-
- [ ] #2 Existing tests continue to pass
26-
- [ ] #3 Add test verifying time values reflect request time, not server startup time
25+
- [x] #1 current_time_local, current_time_utc, and current_day are computed per-request in endpoints that use them
26+
- [x] #2 Existing tests continue to pass
27+
- [x] #3 Add test verifying time values reflect request time, not server startup time
2728
<!-- AC:END -->
29+
30+
## Final Summary
31+
32+
<!-- SECTION:FINAL_SUMMARY:BEGIN -->
33+
Moved `current_time_local`, `current_time_utc`, and `current_day` from module-level constants to per-request computation inside `should_post_to_slack()`. Removed `current_time_utc` entirely (unused). Added `test_check_schedule_uses_request_time` to verify the endpoint reflects request time, not server startup time.
34+
<!-- SECTION:FINAL_SUMMARY:END -->

tests/test_unit.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,41 @@ def db_session_passthrough(f=None, *a, **kw):
341341
assert "should_post" in data
342342

343343

344+
@pytest.mark.unit
345+
def test_check_schedule_uses_request_time(test_client, auth_headers):
346+
"""Time values should be computed per-request, not at module load."""
347+
mock_schedule_obj = MagicMock()
348+
mock_schedule_obj.enabled = True
349+
mock_schedule_obj.schedule_time = "14:00"
350+
351+
mock_db_ctx = MagicMock()
352+
mock_db_ctx.__enter__ = MagicMock()
353+
mock_db_ctx.__exit__ = MagicMock(return_value=False)
354+
355+
def db_session_passthrough(f=None, *a, **kw):
356+
if f is not None and callable(f):
357+
return f
358+
return mock_db_ctx
359+
360+
request_time = arrow.Arrow(2026, 7, 15, 14, 5, tzinfo="America/Chicago")
361+
362+
with (
363+
patch('pony.orm.db_session', side_effect=db_session_passthrough),
364+
patch('main.db_session', side_effect=db_session_passthrough),
365+
patch('schedule.db_session', side_effect=db_session_passthrough),
366+
patch('main.check_and_revert_snooze'),
367+
patch('main.get_schedule', return_value=mock_schedule_obj),
368+
patch('main.get_current_schedule_time', return_value=("14:00 UTC", "14:00 CDT")),
369+
patch('main.arrow') as mock_arrow,
370+
):
371+
mock_arrow.now.return_value = request_time
372+
mock_arrow.get = arrow.get
373+
response = test_client.get("/api/check-schedule", headers=auth_headers)
374+
assert response.status_code == 200
375+
data = response.json()
376+
assert "Wednesday" in data["current_time"], f"Expected Wednesday (request time) but got: {data['current_time']}"
377+
378+
344379
@pytest.mark.unit
345380
def test_post_slack(test_client, auth_headers):
346381
mock_message = ["Test message"]

0 commit comments

Comments
 (0)