Files
Scrobbler/tests/test_federation_schedule.py
T
mattandClaude Sonnet 5 860d4d0f93
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m8s
CI / test (push) Successful in 2m56s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
Admin post-weekly command and GoToSocial post interop checks (phase 3, step 10)
`flask federation post-weekly --user <name> --now` posts a user's
summary for the week still in progress immediately, ignoring the
Monday-09:00 gate - for operators, and so the interop test can produce
a real post without waiting on calendar time. Backed by a new
weekly.post_now(), sharing _post_for_window() with the normal
check_and_post() path so both go through the same has_post/empty-week/
publish logic; only the window (this week so far, vs. the last gated
one) differs.

Extends the GoToSocial interop driver to a second phase: run.sh seeds
one scrobble directly (the driver container has no way to sign a
Last.fm-API request, and doesn't need one just to seed data), triggers
the CLI command via `docker compose exec api`, then re-runs the driver
with --check-posts to confirm the post reaches the already-following
GoToSocial account's home timeline as `private` (followers-only, our
default) with the expected content, deletes it via the existing
DELETE /posts/{id} API, and confirms it disappears from GoToSocial's
timeline too. The main flow now ends by re-following instead of the
previous test's decline, so the post-check phase has a follower to
check against.

Verified end to end against a real GoToSocial 0.22.1 container locally
before pushing (scripts/interop/run.sh), not just left to CI:
    private post: '<p><strong>My week in music: 1 play</strong></p>...'
    All post interop checks passed

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-26 16:40:34 +01:00

99 lines
4.0 KiB
Python

"""Pure week-boundary math for scheduled posts (protocol/schedule.py)."""
from datetime import UTC, datetime, timedelta
from zoneinfo import ZoneInfo
import pytest
from scrobbler.federation.protocol import schedule
# Confirmed with date().isocalendar(): 2025-12-29, 2026-01-05 and 2026-01-12 are Mondays,
# ISO weeks 2026-W01, 2026-W02 and 2026-W03 respectively (the first spans the year
# boundary: Dec 29 2025 is in ISO week 2026-W01).
STABLE_ZONES = ["UTC", "Europe/London", "Pacific/Auckland", "America/Los_Angeles"]
def local(zone: str, *args) -> datetime:
return datetime(*args, tzinfo=ZoneInfo(zone))
@pytest.mark.parametrize("zone", STABLE_ZONES)
def test_just_before_the_gate_targets_the_week_before_last(zone):
now = local(zone, 2026, 1, 12, 8, 59).astimezone(UTC)
start, end, key = schedule.target_week(now, ZoneInfo(zone))
assert (start, end, key) == (
local(zone, 2025, 12, 29, 0, 0).astimezone(UTC),
local(zone, 2026, 1, 5, 0, 0).astimezone(UTC),
"2026-W01",
)
@pytest.mark.parametrize("zone", STABLE_ZONES)
def test_at_the_gate_targets_the_week_that_just_ended(zone):
now = local(zone, 2026, 1, 12, 9, 0).astimezone(UTC)
start, end, key = schedule.target_week(now, ZoneInfo(zone))
assert (start, end, key) == (
local(zone, 2026, 1, 5, 0, 0).astimezone(UTC),
local(zone, 2026, 1, 12, 0, 0).astimezone(UTC),
"2026-W02",
)
@pytest.mark.parametrize("zone", STABLE_ZONES)
def test_any_time_until_the_next_gate_targets_the_same_week(zone):
"""The whole week between two Monday-09:00 gates targets the same week just ended."""
midweek = local(zone, 2026, 1, 14, 15, 30).astimezone(UTC) # a Wednesday afternoon
_, _, key = schedule.target_week(midweek, ZoneInfo(zone))
assert key == "2026-W02"
def test_target_week_across_a_daylight_saving_change():
"""Europe/London springs forward on 2026-03-29: the week Mon 23 Mar - Mon 30 Mar is
only 6 days 23 hours of real (UTC) time, even though it's a full 7 local days."""
tz = ZoneInfo("Europe/London")
gate = datetime(2026, 3, 30, 9, 0, tzinfo=tz).astimezone(UTC) # 08:00 UTC (BST = UTC+1)
start, end, key = schedule.target_week(gate, tz)
assert key == "2026-W13"
assert start == datetime(2026, 3, 23, 0, 0, tzinfo=UTC) # still GMT
assert end == datetime(2026, 3, 29, 23, 0, tzinfo=UTC) # BST has started: UTC+1
assert end - start == timedelta(days=6, hours=23)
just_before = gate - timedelta(minutes=1)
_, _, earlier_key = schedule.target_week(just_before, tz)
assert earlier_key == "2026-W12"
def test_a_worker_down_for_weeks_only_targets_the_most_recent_one():
"""target_week is a pure function of `now`: however much time has passed, it always
names exactly one week (the latest eligible one), never a backlog of several."""
tz = ZoneInfo("UTC")
# A month after 2026-W02 (confirmed above); Feb 2 2026 is the Monday of ISO week 6.
right_after_an_outage = datetime(2026, 2, 2, 10, 0, tzinfo=UTC)
_, _, key = schedule.target_week(right_after_an_outage, tz)
assert key == "2026-W05" # the week that ended just before "now" (Jan 26 - Feb 2)
@pytest.mark.parametrize("name", ["not/a/zone", "", "Mars/Phobos", "utc but wrong"])
def test_resolve_timezone_falls_back_to_utc_for_bad_names(name):
assert schedule.resolve_timezone(name) == ZoneInfo("UTC")
def test_resolve_timezone_passes_through_valid_names():
assert schedule.resolve_timezone("Europe/London") == ZoneInfo("Europe/London")
assert schedule.resolve_timezone("UTC") == ZoneInfo("UTC")
def test_week_key():
assert schedule.week_key(datetime(2025, 12, 29).date()) == "2026-W01"
assert schedule.week_key(datetime(2026, 1, 5).date()) == "2026-W02"
assert schedule.week_key(datetime(2026, 1, 12).date()) == "2026-W03"
def test_current_week_start_is_this_weeks_monday_local():
tz = ZoneInfo("Europe/London")
# A Wednesday, mid-morning UTC
now = local("UTC", 2026, 1, 7, 10)
start = schedule.current_week_start(now, tz)
assert start.astimezone(tz) == local("Europe/London", 2026, 1, 5)