Skip to content

fix: correct serviceDate for past-midnight trips - #1286

Open
3rabiii wants to merge 2 commits into
OneBusAway:mainfrom
3rabiii:fix-trips-for-route-gap9
Open

fix: correct serviceDate for past-midnight trips#1286
3rabiii wants to merge 2 commits into
OneBusAway:mainfrom
3rabiii:fix-trips-for-route-gap9

Conversation

@3rabiii

@3rabiii 3rabiii commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Description

This PR fixes a logical bug in the trips-for-route handler where the serviceDate for active trips was being unconditionally set to the current calendar day's midnight (todayMidnight).

According to the GTFS API specification, serviceDate should represent the midnight at the start of the service day for the trip. For trips running past midnight (e.g., departing at 23:00 on Thursday and still active at 00:30 on Friday), the previous implementation incorrectly returned Friday's midnight instead of Thursday's.

Changes Made

  • Dynamic Service Date Tracking: Introduced a tripServiceDay map to track the correct midnight timestamp for each active trip at the time it is resolved.
  • Previous-Day Resolution: Trips resolved via the prevServiceIDs path (previous-day null-block trips) and block-resolved active trips now correctly carry the midnight of their respective service day (prevDayMidnight).
  • Response Builder Update: The entry builder now fetches the exact serviceDate for each trip from the map instead of falling back to the current day's midnight.

Testing

  • Added a new custom fixture createTestApiWithOvernightFixture containing a Thursday-only service with a trip running from 23:00 to 24:45.
  • Added TestTripsForRouteHandler_PastMidnightServiceDate: Uses a mock clock set to Friday 00:30 UTC to verify that the returned serviceDate correctly reflects Thursday's midnight. All assertions pass cleanly.

Closes: #1285

Summary by CodeRabbit

  • Bug Fixes

    • Corrected trip service dates for routes operating across midnight.
    • Previous-day trips now report the appropriate calendar date instead of the current day.
    • Improved service-date accuracy for trips selected through both scheduled and block-based paths.
  • Tests

    • Added coverage for overnight trips and service periods spanning midnight.

- Map active trips to their actual service day's midnight instead of unconditionally using today's midnight.
- Ensure trips resolved via previous-day service IDs correctly report yesterday's midnight.
- Add test fixture and unit test to verify past-midnight service date assignment.
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@3rabiii, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 33 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 1a6b54dc-1dd0-4209-9423-03790102b47c

📥 Commits

Reviewing files that changed from the base of the PR and between b688f8a and 60cc46c.

📒 Files selected for processing (1)
  • internal/restapi/trips_for_route_handler_test.go
📝 Walkthrough

Walkthrough

The trips-for-route handler now tracks the service-day midnight for each active trip. Responses use that trip-specific value, including trips from the previous service day that remain active after midnight. Tests add an overnight GTFS case and verify the returned ServiceDate.

Changes

Service-date resolution

Layer / File(s) Summary
Track service-day assignments
internal/restapi/trips_for_route_handler.go
The handler records current and previous service-day midnights for null-block and block-selected trips.
Return and validate service dates
internal/restapi/trips_for_route_handler.go, internal/restapi/trips_for_route_handler_test.go
The response uses the recorded trip service date, with today’s midnight as fallback. An overnight fixture and regression test verify previous-day service dates.

Estimated code review effort: 3 (Moderate) | ~15–30 minutes

Possibly related PRs

Suggested labels: LGTM

Suggested reviewers: ahmedhossamdev, arcoder181105, fletcherw

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the fix for incorrect serviceDate values on past-midnight trips.
Linked Issues check ✅ Passed The handler now returns the previous service day's midnight for past-midnight trips, with regression coverage for the prevServiceIDs path [#1285].
Out of Scope Changes check ✅ Passed The handler changes and regression test directly support the linked issue and PR objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/restapi/trips_for_route_handler_test.go`:
- Around line 130-135: Add a separate previous-service-day fixture in the
trips-for-route tests with a non-empty block_id so it exercises the
block-selected assignment path in the trips-for-route handler rather than the
null-block path. Assert that the returned trip’s ServiceDate equals the previous
service-day midnight, and ensure the new fixture covers the newly introduced
branch or condition.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 010ebb25-cf37-473b-9572-ece873163ac8

📥 Commits

Reviewing files that changed from the base of the PR and between 3b73fd9 and b688f8a.

📒 Files selected for processing (2)
  • internal/restapi/trips_for_route_handler.go
  • internal/restapi/trips_for_route_handler_test.go

Comment thread internal/restapi/trips_for_route_handler_test.go
Add a test fixture and unit test to ensure that past-midnight trips
selected via the block path correctly report the previous service day's
midnight as their ServiceDate.
@sonarqubecloud

sonarqubecloud Bot commented Aug 3, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect serviceDate for past-midnight trips in TripsForRoute API

1 participant