fix: address code review findings

This commit is contained in:
2026-08-17 21:50:37 +03:00
parent 9c402c0086
commit 777fda95a3
6 changed files with 76 additions and 23 deletions

View File

@@ -418,7 +418,7 @@ func (g *Graph) FindRoute(originID, destID string, opts SearchOptions, closedSta
newDurationWithMCT := newDuration + transferTime
// Check if we've visited this node with fewer transfers
visKey := current.nodeID
visKey := nextNode.ID
if existingTransfers, ok := visited[visKey]; ok {
if current.transfers+1 > existingTransfers {
// Already visited this node with fewer transfers, skip
@@ -880,7 +880,7 @@ type SearchResult struct {
// route in all three metrics simultaneously. Routes are sorted according to the RankingMode
// in SearchOptions: "fastest" (default, by duration), "fewest_transfers" (by transfers),
// or "cheapest" (by cost).
func (g *Graph) FindRoutesPareto(originID, destID string, opts SearchOptions) []*Itinerary {
func (g *Graph) FindRoutesPareto(originID, destID string, opts SearchOptions, closedStations map[string]bool, neighbors map[string][]storage.StationNeighbor) []*Itinerary {
// Run multiple searches with different strategies to find diverse routes
var allItineraries []*Itinerary
@@ -889,7 +889,7 @@ func (g *Graph) FindRoutesPareto(originID, destID string, opts SearchOptions) []
optsCopy := opts
optsCopy.MaxTransfers = maxTransfers
result := g.FindRoute(originID, destID, optsCopy, nil, nil)
result := g.FindRoute(originID, destID, optsCopy, closedStations, neighbors)
if result != nil && result.TotalDuration > 0 {
allItineraries = append(allItineraries, result)
}

View File

@@ -1,9 +1,11 @@
package routing
import (
"time"
"fmt"
"testing"
"time"
"trip-planner/internal/storage"
)
func TestFindRouteMaxTransfers(t *testing.T) {
@@ -40,7 +42,9 @@ func TestFindRouteMaxTransfers(t *testing.T) {
// Test with MaxTransfers=0: should only find the direct route (0 transfers)
opts0 := SearchOptions{MaxTransfers: 0}
results0 := graph.FindRoutesPareto("s1", "s6", opts0)
closedStations0 := make(map[string]bool)
neighborsMap0 := make(map[string][]storage.StationNeighbor)
results0 := graph.FindRoutesPareto("s1", "s6", opts0, closedStations0, neighborsMap0)
t.Logf("MaxTransfers=0: found %d route(s)", len(results0))
for _, r := range results0 {
t.Logf(" Route: duration=%d, transfers=%d", r.TotalDuration, r.TotalTransfers)
@@ -60,7 +64,9 @@ func TestFindRouteMaxTransfers(t *testing.T) {
// Test with MaxTransfers=1: should find direct route + 1-transfer route if any
opts1 := SearchOptions{MaxTransfers: 1}
results1 := graph.FindRoutesPareto("s1", "s6", opts1)
closedStations1 := make(map[string]bool)
neighborsMap1 := make(map[string][]storage.StationNeighbor)
results1 := graph.FindRoutesPareto("s1", "s6", opts1, closedStations1, neighborsMap1)
t.Logf("MaxTransfers=1: found %d route(s)", len(results1))
for _, r := range results1 {
t.Logf(" Route: duration=%d, transfers=%d", r.TotalDuration, r.TotalTransfers)
@@ -74,7 +80,9 @@ func TestFindRouteMaxTransfers(t *testing.T) {
// Test with MaxTransfers=2: should find more routes
opts2 := SearchOptions{MaxTransfers: 2}
results2 := graph.FindRoutesPareto("s1", "s6", opts2)
closedStations2 := make(map[string]bool)
neighborsMap2 := make(map[string][]storage.StationNeighbor)
results2 := graph.FindRoutesPareto("s1", "s6", opts2, closedStations2, neighborsMap2)
t.Logf("MaxTransfers=2: found %d route(s)", len(results2))
for _, r := range results2 {
t.Logf(" Route: duration=%d, transfers=%d", r.TotalDuration, r.TotalTransfers)
@@ -163,7 +171,9 @@ func TestParetoFrontGeneration(t *testing.T) {
t.Run("fastest mode (default) sorts by duration", func(t *testing.T) {
opts := SearchOptions{MaxTransfers: 3}
results := graph.FindRoutesPareto("s1", "s8", opts)
closedStations := make(map[string]bool)
neighborsMap := make(map[string][]storage.StationNeighbor)
results := graph.FindRoutesPareto("s1", "s8", opts, closedStations, neighborsMap)
// Should find at least some Pareto-optimal routes
if len(results) == 0 {
@@ -199,7 +209,9 @@ func TestParetoFrontGeneration(t *testing.T) {
t.Run("fewest_transfers mode sorts by transfers first", func(t *testing.T) {
opts := SearchOptions{MaxTransfers: 3, RankingMode: "fewest_transfers"}
results := graph.FindRoutesPareto("s1", "s8", opts)
closedStations := make(map[string]bool)
neighborsMap := make(map[string][]storage.StationNeighbor)
results := graph.FindRoutesPareto("s1", "s8", opts, closedStations, neighborsMap)
if len(results) == 0 {
t.Fatal("expected at least one Pareto-optimal route with fewest_transfers mode")
@@ -230,7 +242,9 @@ func TestParetoFrontGeneration(t *testing.T) {
t.Run("cheapest mode sorts by cost first", func(t *testing.T) {
opts := SearchOptions{MaxTransfers: 3, RankingMode: "cheapest"}
results := graph.FindRoutesPareto("s1", "s8", opts)
closedStations := make(map[string]bool)
neighborsMap := make(map[string][]storage.StationNeighbor)
results := graph.FindRoutesPareto("s1", "s8", opts, closedStations, neighborsMap)
if len(results) == 0 {
t.Fatal("expected at least one Pareto-optimal route with cheapest mode")