feat: list next departures across stops with day rollover

Co-Authored-By: Claude Haiku 4.5 <[email protected]>
This commit is contained in:
rickzkmandClaude Haiku 4.5 committed 2026-10-07 00:34:16 +02:00
1 parent 2eecbab365
commit ce52e38f66
2 files changed
+142 -5

No files matched your search

+105 -3
View File
@@ -1,7 +1,8 @@
from datetime import date
from datetime import date, datetime, time
from schedule import is_rest_day, select_column
from timetable import KIND_REST_DAY, KIND_SPECIAL, KIND_WORKDAY, Column
from schedule import is_rest_day, next_departures, select_column
from tests.helpers import load_timetables
from timetable import KIND_REST_DAY, KIND_SPECIAL, KIND_WORKDAY, Column, Departure, Timetable
def col(kind, valid_from, dates=()):
@@ -62,3 +63,104 @@ def test_special_date_beats_rest_day():
def test_no_column_of_the_needed_kind_returns_none():
assert select_column([WORK_NEW], date(2026, 10, 10), rest_day=True) is None
assert select_column([], date(2026, 10, 6), rest_day=False) is None
def dep(line, stop, h, m):
return Departure(line, stop, time(h, m))
def column(kind, valid_from, deps, dates=()):
return Column(kind, kind, valid_from, tuple(dates), tuple(deps))
V = date(2026, 10, 1)
STOP_A, STOP_B = "U potoka", "Družstvo"
A = Timetable(
"4",
STOP_A,
(
column(KIND_WORKDAY, V, [dep("4", STOP_A, 10, 0), dep("4", STOP_A, 10, 30), dep("4", STOP_A, 23, 6)]),
column(KIND_REST_DAY, V, [dep("4", STOP_A, 9, 15)]),
column(KIND_SPECIAL, V, [dep("4", STOP_A, 6, 45)], dates=[date(2026, 10, 29)]),
),
)
B = Timetable("35", STOP_B, (column(KIND_WORKDAY, V, [dep("35", STOP_B, 10, 10)]),))
TUE_0950 = datetime(2026, 10, 6, 9, 50, 0)
def lines_and_times(upcoming):
return [(u.departure.line, u.when.strftime("%H:%M")) for u in upcoming]
def test_merges_timetables_earliest_first(cz):
up = next_departures([A, B], TUE_0950, 3, cz)
assert lines_and_times(up) == [("4", "10:00"), ("35", "10:10"), ("4", "10:30")]
def test_minutes_until_departure(cz):
first = next_departures([A, B], TUE_0950, 1, cz)[0]
assert first.minutes == 10 and first.next_day is False
def test_departure_in_the_current_minute_is_now(cz):
first = next_departures([A], datetime(2026, 10, 6, 10, 0, 40), 1, cz)[0]
assert first.when == datetime(2026, 10, 6, 10, 0) and first.minutes == 0
def test_departed_bus_is_dropped(cz):
first = next_departures([A], datetime(2026, 10, 6, 10, 1, 0), 1, cz)[0]
assert first.when == datetime(2026, 10, 6, 10, 30)
def test_rolls_into_next_day_after_last_departure(cz):
up = next_departures([A], datetime(2026, 10, 6, 23, 30), 2, cz) # last bus today was 23:06
assert [(u.when, u.next_day) for u in up] == [
(datetime(2026, 10, 7, 10, 0), True),
(datetime(2026, 10, 7, 10, 30), True),
]
assert up[0].minutes == 630
def test_state_holiday_on_weekday_uses_rest_day_column(cz):
first = next_departures([A], datetime(2026, 10, 28, 8, 0), 1, cz)[0] # Wednesday, Statehood Day
assert first.when == datetime(2026, 10, 28, 9, 15)
def test_saturday_uses_rest_day_column(cz):
first = next_departures([A], datetime(2026, 10, 10, 8, 0), 1, cz)[0]
assert first.when == datetime(2026, 10, 10, 9, 15)
def test_special_date_uses_special_column(cz):
first = next_departures([A], datetime(2026, 10, 29, 6, 0), 1, cz)[0]
assert first.when == datetime(2026, 10, 29, 6, 45)
def test_nothing_usable_returns_empty_list(cz):
empty = Timetable("9", "X", ())
assert next_departures([empty], TUE_0950, 3, cz) == []
assert next_departures([], TUE_0950, 3, cz) == []
def test_real_timetables_in_the_afternoon(cz):
up = next_departures(load_timetables(), datetime(2026, 10, 6, 13, 28, 20), 5, cz)
assert lines_and_times(up) == [
("4", "13:33"),
("4", "13:53"),
("4", "14:08"),
("5", "14:25"),
("36", "14:27"),
]
assert up[0].minutes == 5
def test_real_special_date_includes_line35(cz):
first = next_departures(load_timetables(), datetime(2026, 10, 29, 5, 0), 1, cz)[0]
assert (first.departure.line, first.departure.mark) == ("35", "v")
assert first.when == datetime(2026, 10, 29, 5, 16)
def test_real_timetables_late_evening_show_tomorrow(cz):
up = next_departures(load_timetables(), datetime(2026, 10, 6, 23, 52, 30), 3, cz)
assert all(u.next_day for u in up)
assert up[0].when == datetime(2026, 10, 7, 4, 16)