Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions internal/restapi/trips_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,11 @@ func findNextStop(

for i, st := range stopTimes {
if uint32(st.StopSequence) == *vehicleCurrentStopSequence {
if len(stopTimes) > 0 {
nextIdx := (i + 1) % len(stopTimes)
return stopTimes[nextIdx].StopID, 0
nextIdx := i + 1
if nextIdx >= len(stopTimes) {
return "", 0 // Vehicle is at the last stop; no next stop
}
return stopTimes[nextIdx].StopID, 0
}
}

Expand Down
29 changes: 29 additions & 0 deletions internal/restapi/trips_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@
"maglev.onebusaway.org/internal/utils"
)

// TestFindNextStop tests the findNextStop helper for correct next-stop lookup and edge cases.
func TestFindNextStop(t *testing.T) {
seq2 := uint32(2)
seq3 := uint32(3)

stopTimes := []*gtfsdb.StopTime{
{StopID: "stop_1", StopSequence: 1},
{StopID: "stop_2", StopSequence: 2},
{StopID: "stop_3", StopSequence: 3},
}

t.Run("Returns the correct next stop when not at the last stop", func(t *testing.T) {
vehicle := &gtfs.Vehicle{CurrentStopSequence: &seq2}
stopID, _ := findNextStop(stopTimes, vehicle)

Check failure on line 29 in internal/restapi/trips_helper_test.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

not enough arguments in call to findNextStop

Check failure on line 29 in internal/restapi/trips_helper_test.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

not enough arguments in call to findNextStop

Check failure on line 29 in internal/restapi/trips_helper_test.go

View workflow job for this annotation

GitHub Actions / Lint

not enough arguments in call to findNextStop
assert.Equal(t, "stop_3", stopID)
})

t.Run("Returns empty string when at the last stop", func(t *testing.T) {
vehicle := &gtfs.Vehicle{CurrentStopSequence: &seq3}
stopID, _ := findNextStop(stopTimes, vehicle)

Check failure on line 35 in internal/restapi/trips_helper_test.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

not enough arguments in call to findNextStop

Check failure on line 35 in internal/restapi/trips_helper_test.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

not enough arguments in call to findNextStop

Check failure on line 35 in internal/restapi/trips_helper_test.go

View workflow job for this annotation

GitHub Actions / Lint

not enough arguments in call to findNextStop
assert.Equal(t, "", stopID)
})

t.Run("Returns empty string when vehicle is nil", func(t *testing.T) {
stopID, _ := findNextStop(stopTimes, nil)

Check failure on line 40 in internal/restapi/trips_helper_test.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

not enough arguments in call to findNextStop

Check failure on line 40 in internal/restapi/trips_helper_test.go

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

not enough arguments in call to findNextStop

Check failure on line 40 in internal/restapi/trips_helper_test.go

View workflow job for this annotation

GitHub Actions / Lint

not enough arguments in call to findNextStop
assert.Equal(t, "", stopID)
})
}

// TestDistanceToLineSegment tests the helper function that calculates distance from a point to a line segment
func TestDistanceToLineSegment(t *testing.T) {
tests := []struct {
Expand Down
Loading