|
| 1 | +"""End-to-end validation of the two cross-day HOS invariants that the |
| 2 | +TimelineView UI shows: |
| 3 | +
|
| 4 | +1. For every day, max(driving minutes accumulated since the last contiguous |
| 5 | + >= 10h off-duty/sleeper run) must be <= 660 (FMCSA 11h cap). |
| 6 | +2. For every pair of adjacent days, the boundary-pill "continuous rest" |
| 7 | + calculation must AGREE with the DOT 11h check — both derived from the |
| 8 | + same single-pass rest-run analysis. |
| 9 | +
|
| 10 | +Scenario: Chicago, IL -> Denver, CO -> Los Angeles, CA, cycle hours used 65. |
| 11 | +This is the exact input that produced the contradictory UI in the bug |
| 12 | +report. |
| 13 | +""" |
| 14 | + |
| 15 | +from unittest.mock import patch |
| 16 | + |
| 17 | +from simulator.models import DutyStatus, TripInput |
| 18 | +from simulator.engine import simulate_trip |
| 19 | + |
| 20 | + |
| 21 | +CHICAGO_DENVER_LA = TripInput( |
| 22 | + current_location="Chicago, IL", |
| 23 | + pickup_location="Denver, CO", |
| 24 | + dropoff_location="Los Angeles, CA", |
| 25 | + cycle_hours_used=65.0, |
| 26 | +) |
| 27 | + |
| 28 | +REST_STATUSES = {DutyStatus.OFF_DUTY, DutyStatus.SLEEPER_BERTH} |
| 29 | +RESET_10H_MIN = 10 * 60 |
| 30 | +DOT_11H_MIN = 11 * 60 |
| 31 | + |
| 32 | + |
| 33 | +def _detect_rest_runs(events): |
| 34 | + """Mirror of the frontend detectRestRuns: maximal contiguous rest runs |
| 35 | + where each event's end_time exactly equals the next event's start_time.""" |
| 36 | + runs = [] |
| 37 | + run_start = None |
| 38 | + run_end = None |
| 39 | + run_minutes = 0.0 |
| 40 | + |
| 41 | + def flush(): |
| 42 | + nonlocal run_start, run_end, run_minutes |
| 43 | + if run_start is not None: |
| 44 | + runs.append((run_start, run_end, run_minutes)) |
| 45 | + run_start = None |
| 46 | + run_end = None |
| 47 | + run_minutes = 0.0 |
| 48 | + |
| 49 | + for event in events: |
| 50 | + if event.status not in REST_STATUSES: |
| 51 | + flush() |
| 52 | + continue |
| 53 | + duration = (event.end_time - event.start_time).total_seconds() / 60.0 |
| 54 | + if run_start is not None and event.start_time == run_end: |
| 55 | + run_end = event.end_time |
| 56 | + run_minutes += duration |
| 57 | + else: |
| 58 | + flush() |
| 59 | + run_start = event.start_time |
| 60 | + run_end = event.end_time |
| 61 | + run_minutes = duration |
| 62 | + flush() |
| 63 | + return runs |
| 64 | + |
| 65 | + |
| 66 | +def _compute_driving_max_by_day(events, runs): |
| 67 | + """Mirror of the frontend driving-max-by-day walk.""" |
| 68 | + reset_moments = sorted( |
| 69 | + run_start.timestamp() + RESET_10H_MIN * 60 |
| 70 | + for run_start, _, total in runs |
| 71 | + if total >= RESET_10H_MIN |
| 72 | + ) |
| 73 | + |
| 74 | + driving_max = {} |
| 75 | + driving_since_reset = 0.0 |
| 76 | + next_reset = 0 |
| 77 | + |
| 78 | + for event in events: |
| 79 | + start_ts = event.start_time.timestamp() |
| 80 | + while next_reset < len(reset_moments) and reset_moments[next_reset] <= start_ts: |
| 81 | + driving_since_reset = 0.0 |
| 82 | + next_reset += 1 |
| 83 | + |
| 84 | + if event.status == DutyStatus.DRIVING: |
| 85 | + duration = (event.end_time - event.start_time).total_seconds() / 60.0 |
| 86 | + driving_since_reset += duration |
| 87 | + |
| 88 | + day = event.start_time.astimezone().date().isoformat() |
| 89 | + driving_max[day] = max(driving_max.get(day, 0.0), driving_since_reset) |
| 90 | + |
| 91 | + return driving_max |
| 92 | + |
| 93 | + |
| 94 | +@patch("simulator.engine.get_route") |
| 95 | +@patch("simulator.engine.geocode_address") |
| 96 | +def test_chicago_denver_la_cycle65_no_11h_violation(mock_geocode, mock_route): |
| 97 | + mock_geocode.side_effect = [ |
| 98 | + (41.8781, -87.6298), |
| 99 | + (39.7392, -104.9903), |
| 100 | + (34.0522, -118.2437), |
| 101 | + ] |
| 102 | + mock_route.side_effect = [ |
| 103 | + {"distance_miles": 1000.0, "duration_hours": 15.0}, |
| 104 | + {"distance_miles": 1000.0, "duration_hours": 17.0}, |
| 105 | + ] |
| 106 | + result = simulate_trip(CHICAGO_DENVER_LA) |
| 107 | + |
| 108 | + runs = _detect_rest_runs(result.timeline) |
| 109 | + driving_max = _compute_driving_max_by_day(result.timeline, runs) |
| 110 | + |
| 111 | + offenders = {day: mins for day, mins in driving_max.items() if mins > DOT_11H_MIN} |
| 112 | + assert not offenders, ( |
| 113 | + "Simulator produced driving runs exceeding 11h between 10h resets: " |
| 114 | + f"{offenders}. Full per-day max: {driving_max}" |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +@patch("simulator.engine.get_route") |
| 119 | +@patch("simulator.engine.geocode_address") |
| 120 | +def test_chicago_denver_la_cycle65_pill_and_check_agree(mock_geocode, mock_route): |
| 121 | + """For every adjacent day pair where a >=10h rest run spans the boundary, |
| 122 | + the next day's DOT 11h check MUST show 'since last reset' below the |
| 123 | + cumulative that led to the reset. Specifically: the run's start-of-day |
| 124 | + carry-over must be 0 immediately after the reset (<= DOT_11H_MIN trivially) |
| 125 | + and the next-day max must not exceed DOT_11H_MIN.""" |
| 126 | + mock_geocode.side_effect = [ |
| 127 | + (41.8781, -87.6298), |
| 128 | + (39.7392, -104.9903), |
| 129 | + (34.0522, -118.2437), |
| 130 | + ] |
| 131 | + mock_route.side_effect = [ |
| 132 | + {"distance_miles": 1000.0, "duration_hours": 15.0}, |
| 133 | + {"distance_miles": 1000.0, "duration_hours": 17.0}, |
| 134 | + ] |
| 135 | + result = simulate_trip(CHICAGO_DENVER_LA) |
| 136 | + |
| 137 | + runs = _detect_rest_runs(result.timeline) |
| 138 | + driving_max = _compute_driving_max_by_day(result.timeline, runs) |
| 139 | + |
| 140 | + # Boundary rest per day, same rule as the frontend pill. |
| 141 | + sheets = result.log_sheets |
| 142 | + for i in range(len(sheets) - 1): |
| 143 | + curr, nxt = sheets[i], sheets[i + 1] |
| 144 | + if not curr.events or not nxt.events: |
| 145 | + continue |
| 146 | + last, first = curr.events[-1], nxt.events[0] |
| 147 | + if ( |
| 148 | + last.end_time != first.start_time |
| 149 | + or last.status not in REST_STATUSES |
| 150 | + or first.status not in REST_STATUSES |
| 151 | + ): |
| 152 | + continue |
| 153 | + boundary_ts = last.end_time.timestamp() |
| 154 | + containing = next( |
| 155 | + ( |
| 156 | + total |
| 157 | + for run_start, run_end, total in runs |
| 158 | + if run_start.timestamp() < boundary_ts < run_end.timestamp() |
| 159 | + ), |
| 160 | + None, |
| 161 | + ) |
| 162 | + assert containing is not None, ( |
| 163 | + f"Boundary between {curr.date} and {nxt.date} has contiguous rest " |
| 164 | + "on both sides but no rest run contains it — pill/check will disagree." |
| 165 | + ) |
| 166 | + if containing >= RESET_10H_MIN: |
| 167 | + next_day_max = driving_max.get(nxt.date.isoformat(), 0.0) |
| 168 | + assert next_day_max <= DOT_11H_MIN, ( |
| 169 | + f"Pill says 10h reset ✓ for {curr.date}->{nxt.date} but DOT 11h " |
| 170 | + f"check on {nxt.date} reports {next_day_max} min > {DOT_11H_MIN}." |
| 171 | + ) |
0 commit comments