- package src/signal-cal-bot -> src/signal_cal_bot - distribution and CLI entry point: signal-cal-bot - env vars CALBOT_* -> SIGNAL_CAL_BOT_* - database file, container names, user agents and logs renamed - docs and compose updated
112 lines
3.5 KiB
Python
112 lines
3.5 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
from zoneinfo import ZoneInfo
|
|
|
|
from signal_cal_bot.config import FormatConfig
|
|
from signal_cal_bot.expand import Event
|
|
from signal_cal_bot.format import (
|
|
find_postcode,
|
|
format_hashtags,
|
|
format_time_range,
|
|
render_post,
|
|
venue_from_description,
|
|
venue_from_location,
|
|
)
|
|
|
|
LONDON = ZoneInfo("Europe/London")
|
|
|
|
|
|
def make_event(**overrides) -> Event:
|
|
defaults = dict(
|
|
key="uid|2026-11-14T16:30:00+00:00",
|
|
uid="uid",
|
|
series_id="tec:1",
|
|
summary="Folk Night",
|
|
start=datetime(2026, 11, 14, 16, 30, tzinfo=LONDON).astimezone(timezone.utc),
|
|
end=datetime(2026, 11, 14, 18, 30, tzinfo=LONDON).astimezone(timezone.utc),
|
|
all_day=False,
|
|
location="Old Print Works\n12 High St\nBirmingham\nB12 9AH",
|
|
description="",
|
|
url="https://commonscalendar.uk/event/folk-night/",
|
|
image_url="",
|
|
tz="Europe/London",
|
|
)
|
|
defaults.update(overrides)
|
|
return Event(**defaults)
|
|
|
|
|
|
def test_render_matches_agreed_format():
|
|
text = render_post(
|
|
make_event(),
|
|
FormatConfig(),
|
|
LONDON,
|
|
venue="Old Print Works",
|
|
postcode="B12 9AH",
|
|
link="https://commonscalendar.uk/event/folk-night/",
|
|
)
|
|
assert text == (
|
|
"Folk Night\n"
|
|
"#November #November14\n"
|
|
"4.30-6.30pm\n"
|
|
"Old Print Works B12 9AH\n"
|
|
"https://commonscalendar.uk/event/folk-night/"
|
|
)
|
|
|
|
|
|
def test_time_range_same_meridiem():
|
|
start = datetime(2026, 11, 14, 16, 30, tzinfo=LONDON)
|
|
end = datetime(2026, 11, 14, 18, 30, tzinfo=LONDON)
|
|
assert format_time_range(start, end) == "4.30-6.30pm"
|
|
|
|
|
|
def test_time_range_crosses_meridiem():
|
|
start = datetime(2026, 11, 14, 11, 30, tzinfo=LONDON)
|
|
end = datetime(2026, 11, 14, 13, 30, tzinfo=LONDON)
|
|
assert format_time_range(start, end) == "11.30am-1.30pm"
|
|
|
|
|
|
def test_hashtags():
|
|
assert format_hashtags(datetime(2026, 11, 14, 10, 0, tzinfo=LONDON)) == (
|
|
"#November #November14"
|
|
)
|
|
assert format_hashtags(datetime(2026, 1, 1, 10, 0, tzinfo=LONDON)) == "#January #January1"
|
|
|
|
|
|
def test_all_day_event_says_all_day():
|
|
event = make_event(
|
|
all_day=True,
|
|
start=datetime(2026, 11, 21, tzinfo=LONDON).astimezone(timezone.utc),
|
|
end=datetime(2026, 11, 22, tzinfo=LONDON).astimezone(timezone.utc),
|
|
)
|
|
text = render_post(event, FormatConfig(), LONDON, venue="", postcode="", link="")
|
|
assert "All day" in text
|
|
assert "#November #November21" in text
|
|
|
|
|
|
def test_postcode_and_venue_helpers():
|
|
assert find_postcode("Old Print Works, B12 9AH") == "B12 9AH"
|
|
assert find_postcode("no code here") == ""
|
|
assert venue_from_location("Old Print Works\nB12 9AH") == "Old Print Works"
|
|
assert venue_from_description("Collier Room, Methodist Central Hall, M1 1JQ") == "Collier Room"
|
|
|
|
|
|
def test_missing_fields_are_omitted():
|
|
event = make_event(location="", url="")
|
|
text = render_post(event, FormatConfig(), LONDON, venue="", postcode="", link="")
|
|
lines = text.splitlines()
|
|
assert lines == ["Folk Night", "#November #November14", "4.30-6.30pm"]
|
|
|
|
|
|
def test_postcode_is_not_duplicated_when_part_of_venue():
|
|
event = make_event(location="Old Print Works B12 9AH", url="")
|
|
text = render_post(
|
|
event,
|
|
FormatConfig(),
|
|
LONDON,
|
|
venue="Old Print Works B12 9AH",
|
|
postcode="B12 9AH",
|
|
link="",
|
|
)
|
|
assert text.splitlines()[3] == "Old Print Works B12 9AH"
|