|
| 1 | +"""Unit tests for the glue's time-window logic. |
| 2 | +
|
| 3 | +This is the code where subtle, real-world bugs have repeatedly lived: |
| 4 | + * 2026-07-14 a stale enable flag + an unchanged end time silently reopened a |
| 5 | + ~24h dispatch -> guarded by _minutes_remaining returning 0 for a |
| 6 | + same-day window whose end has already passed. |
| 7 | + * 2026-09-02 export ran ~30 min early because the glue dispatched the moment |
| 8 | + the enable flag was armed, ignoring the window start time |
| 9 | + -> guarded by _in_window(). |
| 10 | +
|
| 11 | +The tests below lock in both behaviours plus the ordinary and midnight-crossing |
| 12 | +cases. Everything else in the glue (dispatch commit, triggers, hardware writes) |
| 13 | +is integration territory - validated with real manual slots, not mocked here. |
| 14 | +""" |
| 15 | + |
| 16 | +import datetime as dt |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +S, E = "test.start", "test.end" |
| 21 | + |
| 22 | + |
| 23 | +def _at(glue, hh, mm, start=None, end=None): |
| 24 | + """Set the clock to today at hh:mm and (optionally) the window entities.""" |
| 25 | + glue["set_now"](dt.datetime(2026, 9, 3, hh, mm, 0)) |
| 26 | + if start is not None: |
| 27 | + glue["S"].put(S, start) |
| 28 | + if end is not None: |
| 29 | + glue["S"].put(E, end) |
| 30 | + |
| 31 | + |
| 32 | +# -------------------------------------------------------------------------- |
| 33 | +# _parse_time_of_day |
| 34 | +# -------------------------------------------------------------------------- |
| 35 | + |
| 36 | +@pytest.mark.parametrize("raw,expected", [ |
| 37 | + ("20:00", dt.time(20, 0)), |
| 38 | + ("20:00:00", dt.time(20, 0, 0)), |
| 39 | + ("05:30:00", dt.time(5, 30, 0)), |
| 40 | + ("00:00", dt.time(0, 0)), |
| 41 | +]) |
| 42 | +def test_parse_time_valid(glue, raw, expected): |
| 43 | + assert glue["_parse_time_of_day"](raw) == expected |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize("raw", ["garbage", "", "25:99", None, "unknown", "unavailable"]) |
| 47 | +def test_parse_time_invalid_returns_none(glue, raw): |
| 48 | + assert glue["_parse_time_of_day"](raw) is None |
| 49 | + |
| 50 | + |
| 51 | +# -------------------------------------------------------------------------- |
| 52 | +# _in_window - same-day window (this is the 2026-09-02 regression) |
| 53 | +# -------------------------------------------------------------------------- |
| 54 | + |
| 55 | +@pytest.mark.parametrize("hh,mm,expected", [ |
| 56 | + (19, 30, False), # <-- armed 30 min early: MUST NOT dispatch (the fix) |
| 57 | + (19, 59, False), |
| 58 | + (20, 0, True), # start boundary inclusive |
| 59 | + (20, 30, True), # squarely inside |
| 60 | + (21, 0, True), # end boundary inclusive |
| 61 | + (21, 1, False), # just after end |
| 62 | + (23, 0, False), |
| 63 | +]) |
| 64 | +def test_in_window_same_day(glue, hh, mm, expected): |
| 65 | + _at(glue, hh, mm, start="20:00:00", end="21:00:00") |
| 66 | + assert glue["_in_window"](S, E) is expected |
| 67 | + |
| 68 | + |
| 69 | +# -------------------------------------------------------------------------- |
| 70 | +# _in_window - window crossing midnight (e.g. the overnight charge 23:30->05:30) |
| 71 | +# -------------------------------------------------------------------------- |
| 72 | + |
| 73 | +@pytest.mark.parametrize("hh,mm,expected", [ |
| 74 | + (22, 0, False), # before start |
| 75 | + (23, 29, False), |
| 76 | + (23, 30, True), # start boundary |
| 77 | + (23, 59, True), |
| 78 | + (0, 30, True), # after midnight, still in window |
| 79 | + (5, 0, True), |
| 80 | + (5, 30, True), # end boundary |
| 81 | + (5, 31, False), |
| 82 | + (6, 0, False), # after end |
| 83 | + (12, 0, False), # midday, nowhere near |
| 84 | +]) |
| 85 | +def test_in_window_crosses_midnight(glue, hh, mm, expected): |
| 86 | + _at(glue, hh, mm, start="23:30:00", end="05:30:00") |
| 87 | + assert glue["_in_window"](S, E) is expected |
| 88 | + |
| 89 | + |
| 90 | +# -------------------------------------------------------------------------- |
| 91 | +# _in_window - fails permissive when the start can't be read |
| 92 | +# -------------------------------------------------------------------------- |
| 93 | + |
| 94 | +def test_in_window_unreadable_start_is_permissive(glue): |
| 95 | + _at(glue, 3, 0, start="unknown", end="21:00:00") |
| 96 | + assert glue["_in_window"](S, E) is True |
| 97 | + |
| 98 | + |
| 99 | +def test_in_window_unreadable_end_is_permissive(glue): |
| 100 | + _at(glue, 3, 0, start="20:00:00", end=None) |
| 101 | + assert glue["_in_window"](S, E) is True |
| 102 | + |
| 103 | + |
| 104 | +# -------------------------------------------------------------------------- |
| 105 | +# _minutes_remaining |
| 106 | +# -------------------------------------------------------------------------- |
| 107 | + |
| 108 | +def test_minutes_remaining_same_day_counts_to_end(glue): |
| 109 | + _at(glue, 20, 0, start="20:00:00", end="21:00:00") |
| 110 | + assert glue["_minutes_remaining"](S, E) == 60 |
| 111 | + |
| 112 | + |
| 113 | +def test_minutes_remaining_same_day_passed_end_returns_zero(glue): |
| 114 | + # 2026-07-14 regression: a same-day window whose end has passed is OVER, |
| 115 | + # not "see you tomorrow" - otherwise a stale flag reopens a ~24h dispatch. |
| 116 | + _at(glue, 22, 0, start="20:00:00", end="21:00:00") |
| 117 | + assert glue["_minutes_remaining"](S, E) == 0 |
| 118 | + |
| 119 | + |
| 120 | +def test_minutes_remaining_midnight_before_midnight(glue): |
| 121 | + # 23:00, window 23:30->05:30 -> end rolls to tomorrow 05:30 = 390 min away. |
| 122 | + _at(glue, 23, 0, start="23:30:00", end="05:30:00") |
| 123 | + assert glue["_minutes_remaining"](S, E) == 390 |
| 124 | + |
| 125 | + |
| 126 | +def test_minutes_remaining_midnight_after_midnight(glue): |
| 127 | + # 02:00, window 23:30->05:30 -> end is today 05:30 = 210 min away. |
| 128 | + _at(glue, 2, 0, start="23:30:00", end="05:30:00") |
| 129 | + assert glue["_minutes_remaining"](S, E) == 210 |
| 130 | + |
| 131 | + |
| 132 | +def test_minutes_remaining_unparseable_end_returns_zero_and_logs(glue): |
| 133 | + _at(glue, 20, 0, start="20:00:00", end="garbage") |
| 134 | + assert glue["_minutes_remaining"](S, E) == 0 |
| 135 | + assert glue["log"].messages("error"), "expected an error log for unparseable end" |
| 136 | + |
| 137 | + |
| 138 | +# -------------------------------------------------------------------------- |
| 139 | +# _clamp_power - the 2026-08-05 out-of-range guard |
| 140 | +# -------------------------------------------------------------------------- |
| 141 | + |
| 142 | +def test_clamp_power_within_range_unchanged(glue): |
| 143 | + glue["S"].put_attrs(glue["DISPATCH_POWER"], {"min": -5.0, "max": 5.0}) |
| 144 | + assert glue["_clamp_power"](3.0) == 3.0 |
| 145 | + assert glue["_clamp_power"](-5.0) == -5.0 |
| 146 | + |
| 147 | + |
| 148 | +def test_clamp_power_clamps_both_ends(glue): |
| 149 | + glue["S"].put_attrs(glue["DISPATCH_POWER"], {"min": -5.0, "max": 5.0}) |
| 150 | + assert glue["_clamp_power"](6.0) == 5.0 |
| 151 | + assert glue["_clamp_power"](-6.0) == -5.0 |
| 152 | + |
| 153 | + |
| 154 | +def test_clamp_power_falls_back_to_conservative_limit(glue): |
| 155 | + # No attrs available -> _dispatch_power_limits falls back to +/-3.0. |
| 156 | + assert glue["_clamp_power"](5.0) == 3.0 |
| 157 | + assert glue["_clamp_power"](-5.0) == -3.0 |
| 158 | + assert glue["log"].messages("warning"), "expected a CLAMPED warning" |
0 commit comments