afterhours-shift-manager/tests/slack_bot/test_handle_swap.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

98 lines
3.1 KiB
Python

"""Tests for slack-bot _handle_swap."""
from freezegun import freeze_time
MON = "2026-06-01 12:00:00"
def _setup(seed):
seed.roster("114", "Alice", slack_user_id="U_ALICE")
seed.roster("115", "Bob", slack_user_id="U_BOB")
seed.weekly("Monday", "114", "Alice") # Alice is on the Monday shift
@freeze_time(MON)
def test_swap_to_mention(
slackbot_app, schedule, seed, respond, client, routing_spy, text_of
):
_setup(seed)
slackbot_app._handle_swap(
respond, schedule, "U_ALICE", "swap today <@U_BOB>", "C1", client, None
)
override = schedule.get_override("2026-06-01")
assert override["extension"] == "115" and override["name"] == "Bob"
assert "Swapped" in text_of(respond)
routing_spy.assert_called_once_with("115")
client.chat_postMessage.assert_called_once()
@freeze_time(MON)
def test_swap_to_extension(slackbot_app, schedule, seed, respond, client, routing_spy):
_setup(seed)
slackbot_app._handle_swap(
respond, schedule, "U_ALICE", "swap today 115", "C1", client, None
)
assert schedule.get_override("2026-06-01")["extension"] == "115"
@freeze_time(MON)
def test_swap_not_your_shift(
slackbot_app, schedule, seed, respond, client, routing_spy, text_of
):
seed.roster("114", "Alice", slack_user_id="U_ALICE")
seed.roster("115", "Bob", slack_user_id="U_BOB")
seed.weekly("Monday", "115", "Bob") # Bob's shift, not Alice's
slackbot_app._handle_swap(
respond, schedule, "U_ALICE", "swap today <@U_BOB>", "C1", client, None
)
assert "not your shift" in text_of(respond).lower()
routing_spy.assert_not_called()
@freeze_time(MON)
def test_swap_target_mention_unregistered(
slackbot_app, schedule, seed, respond, client, text_of
):
seed.roster("114", "Alice", slack_user_id="U_ALICE")
seed.weekly("Monday", "114", "Alice")
slackbot_app._handle_swap(
respond, schedule, "U_ALICE", "swap today <@U_GHOST>", "C1", client, None
)
assert "isn't registered" in text_of(respond)
@freeze_time(MON)
def test_swap_target_extension_not_found(
slackbot_app, schedule, seed, respond, client, text_of
):
seed.roster("114", "Alice", slack_user_id="U_ALICE")
seed.weekly("Monday", "114", "Alice")
slackbot_app._handle_swap(
respond, schedule, "U_ALICE", "swap today 999", "C1", client, None
)
assert "not found" in text_of(respond).lower()
@freeze_time(MON)
def test_swap_past_date(slackbot_app, schedule, seed, respond, client, text_of):
_setup(seed)
slackbot_app._handle_swap(
respond, schedule, "U_ALICE", "swap 2026-05-01 <@U_BOB>", "C1", client, None
)
assert "past" in text_of(respond).lower()
@freeze_time(MON)
def test_swap_unregistered_requester(slackbot_app, schedule, respond, client, text_of):
slackbot_app._handle_swap(
respond, schedule, "U_NOBODY", "swap today 115", "C1", client, None
)
assert "not registered" in text_of(respond).lower()
@freeze_time(MON)
def test_swap_usage(slackbot_app, schedule, respond, client, text_of):
slackbot_app._handle_swap(
respond, schedule, "U_ALICE", "swap today", "C1", client, None
)
assert "Usage" in text_of(respond)