afterhours-shift-manager/tests/slack_bot/test_handle_drop.py
Adam Moussa a16bf6d370 Add pytest suite and wire it into CI
Stands up the first automated tests for the repo (151 tests) and turns on
the CI test step.

- Lift slack-bot handlers out of create_app() closures to module level so
  they're unit-testable; create_app is now a thin Bolt-wiring layer. No
  behavior change (handler entrypoints and create_app signature unchanged).
- tests/ mirrors src/: shared layer (schedule, blocks, 3CX client,
  ring_scheduler, secrets) + all four Lambdas (pay math, drop/swap/pick/
  admin/register/rate, pickup button, roster sync, queue scheduler).
- All boundaries mocked: DynamoDB/SES/Secrets via moto, 3CX HTTP via
  responses, Slack via fakes, time via freezegun. No real network/AWS.
- pyproject.toml pytest config (pythonpath=src/shared, importlib mode);
  per-package conftest loads each app.py under a unique name to avoid the
  four-app.py collision. tests/requirements.txt for test-only deps.
- ci.yaml: run-tests: true (reusable workflow auto-installs deps) and lint
  the tests dir too.
- README Testing section.

Closes #85
2026-06-01 18:50:27 -04:00

89 lines
2.9 KiB
Python

"""Tests for slack-bot _handle_drop."""
from freezegun import freeze_time
from shared.schedule import FALLBACK_EXTENSION
# Monday 2026-06-01 08:00 ET — weekday, active shift is night.
MON = "2026-06-01 12:00:00"
def _register_on_monday(seed):
"""Alice (114) is registered and assigned the Monday night shift."""
seed.roster("114", "Alice", slack_user_id="U_ALICE")
seed.weekly("Monday", "114", "Alice")
@freeze_time(MON)
def test_drop_today_marks_open_and_repoints_3cx(
slackbot_app, schedule, seed, respond, client, routing_spy, text_of
):
_register_on_monday(seed)
slackbot_app._handle_drop(
respond, schedule, "U_ALICE", "drop today", "C1", client, None
)
assert schedule.get_override("2026-06-01")["extension"] == "OPEN"
assert "dropped" in text_of(respond)
# Today + active night shift → queue falls back.
routing_spy.assert_called_once_with(FALLBACK_EXTENSION)
client.chat_postMessage.assert_called_once()
@freeze_time(MON)
def test_drop_future_does_not_repoint_3cx(
slackbot_app, schedule, seed, respond, client, routing_spy
):
seed.roster("114", "Alice", slack_user_id="U_ALICE")
seed.weekly("Tuesday", "114", "Alice")
slackbot_app._handle_drop(
respond, schedule, "U_ALICE", "drop tomorrow", "C1", client, None
)
assert schedule.get_override("2026-06-02")["extension"] == "OPEN"
routing_spy.assert_not_called()
@freeze_time(MON)
def test_drop_not_your_shift(
slackbot_app, schedule, seed, respond, client, routing_spy, text_of
):
seed.roster("114", "Alice", slack_user_id="U_ALICE")
seed.weekly("Monday", "115", "Bob") # Bob is on shift, not Alice
slackbot_app._handle_drop(
respond, schedule, "U_ALICE", "drop today", "C1", client, None
)
assert "not your shift" in text_of(respond).lower()
routing_spy.assert_not_called()
@freeze_time(MON)
def test_drop_past_date(slackbot_app, schedule, seed, respond, client, text_of):
_register_on_monday(seed)
slackbot_app._handle_drop(
respond, schedule, "U_ALICE", "drop 2026-05-01", "C1", client, None
)
assert "past" in text_of(respond).lower()
@freeze_time(MON)
def test_drop_unregistered(slackbot_app, schedule, respond, client, text_of):
slackbot_app._handle_drop(
respond, schedule, "U_NOBODY", "drop today", "C1", client, None
)
assert "not registered" in text_of(respond).lower()
@freeze_time(MON)
def test_drop_bad_date(slackbot_app, schedule, seed, respond, client, text_of):
_register_on_monday(seed)
slackbot_app._handle_drop(
respond, schedule, "U_ALICE", "drop notaday", "C1", client, None
)
assert "couldn't parse" in text_of(respond).lower()
@freeze_time(MON)
def test_drop_usage(slackbot_app, schedule, respond, client, text_of):
slackbot_app._handle_drop(respond, schedule, "U_ALICE", "drop", "C1", client, None)
assert "Usage" in text_of(respond)