fix: address code review findings

- Fix type mismatch in search_cache.go: SearchWithCache now returns *yandex.Response instead of *Itinerary
- Fix token bucket refill logic in yandex/client.go to properly accumulate tokens based on refillPerSec
- Fix hardcoded API key in main.go to load from YANDEX_API_KEY environment variable
- Fix missing error handling for JSON encoding in handlers.go RouteGeoJSON function
- Fix incorrect redis.Nil handling in cache/store.go CacheAside.Exists method
- Fix incorrect error return type in station_status.go updateStationStatus to return newStatus instead of empty string
This commit is contained in:
2026-08-17 22:42:08 +03:00
parent 78f662c985
commit 6dcec6a7a5
17 changed files with 331 additions and 349 deletions

View File

@@ -18,25 +18,25 @@ func TestFindRouteMaxTransfers(t *testing.T) {
// Add direct edge s1 -> s6 (0 transfers)
graph.AddEdge(&Edge{
From: graph.Nodes()[0], // s1
To: graph.Nodes()[5], // s6
Kind: EdgeKindReal,
Duration: 3600,
Transport: "train",
From: graph.Nodes()[0], // s1
To: graph.Nodes()[5], // s6
Kind: EdgeKindReal,
Duration: 3600,
Transport: "train",
TransportType: TransportTypeTrain,
IsTransfer: false,
IsTransfer: false,
})
// Add chain edges s1->s2->s3->s4->s5->s6 (each is a transfer edge)
for i := 0; i < 5; i++ {
graph.AddEdge(&Edge{
From: graph.Nodes()[i],
To: graph.Nodes()[i+1],
Kind: EdgeKindReal,
Duration: 1000,
Transport: "train",
From: graph.Nodes()[i],
To: graph.Nodes()[i+1],
Kind: EdgeKindReal,
Duration: 1000,
Transport: "train",
TransportType: TransportTypeTrain,
IsTransfer: true,
IsTransfer: true,
})
}
@@ -105,68 +105,68 @@ func TestParetoFrontGeneration(t *testing.T) {
// Add direct edge s1 -> s8 (0 transfers, higher cost)
graph.AddEdge(&Edge{
From: graph.Nodes()[0], // s1
To: graph.Nodes()[7], // s8
Kind: EdgeKindReal,
Duration: 600, // 10 min
Transport: "train",
From: graph.Nodes()[0], // s1
To: graph.Nodes()[7], // s8
Kind: EdgeKindReal,
Duration: 600, // 10 min
Transport: "train",
TransportType: TransportTypeTrain,
IsTransfer: false,
Cost: 500, // expensive direct
IsTransfer: false,
Cost: 500, // expensive direct
})
// Add 1-transfer route s1->s3->s8 (lower cost, more time)
graph.AddEdge(&Edge{
From: graph.Nodes()[0], // s1
To: graph.Nodes()[2], // s3
Kind: EdgeKindReal,
Duration: 200, // 3 min
Transport: "train",
From: graph.Nodes()[0], // s1
To: graph.Nodes()[2], // s3
Kind: EdgeKindReal,
Duration: 200, // 3 min
Transport: "train",
TransportType: TransportTypeTrain,
IsTransfer: true,
Cost: 200,
IsTransfer: true,
Cost: 200,
})
graph.AddEdge(&Edge{
From: graph.Nodes()[2], // s3
To: graph.Nodes()[7], // s8
Kind: EdgeKindReal,
Duration: 300, // 5 min
Transport: "train",
From: graph.Nodes()[2], // s3
To: graph.Nodes()[7], // s8
Kind: EdgeKindReal,
Duration: 300, // 5 min
Transport: "train",
TransportType: TransportTypeTrain,
IsTransfer: true,
Cost: 100,
IsTransfer: true,
Cost: 100,
})
// Add 2-transfer route s1->s5->s6->s8 (even lower cost, more transfers)
graph.AddEdge(&Edge{
From: graph.Nodes()[0], // s1
To: graph.Nodes()[4], // s5
Kind: EdgeKindReal,
Duration: 100, // 2 min
Transport: "train",
From: graph.Nodes()[0], // s1
To: graph.Nodes()[4], // s5
Kind: EdgeKindReal,
Duration: 100, // 2 min
Transport: "train",
TransportType: TransportTypeTrain,
IsTransfer: true,
Cost: 100,
IsTransfer: true,
Cost: 100,
})
graph.AddEdge(&Edge{
From: graph.Nodes()[4], // s5
To: graph.Nodes()[5], // s6
Kind: EdgeKindReal,
Duration: 100, // 2 min
Transport: "train",
From: graph.Nodes()[4], // s5
To: graph.Nodes()[5], // s6
Kind: EdgeKindReal,
Duration: 100, // 2 min
Transport: "train",
TransportType: TransportTypeTrain,
IsTransfer: true,
Cost: 50,
IsTransfer: true,
Cost: 50,
})
graph.AddEdge(&Edge{
From: graph.Nodes()[5], // s6
To: graph.Nodes()[7], // s8
Kind: EdgeKindReal,
Duration: 200, // 3 min
Transport: "train",
From: graph.Nodes()[5], // s6
To: graph.Nodes()[7], // s8
Kind: EdgeKindReal,
Duration: 200, // 3 min
Transport: "train",
TransportType: TransportTypeTrain,
IsTransfer: true,
Cost: 50,
IsTransfer: true,
Cost: 50,
})
t.Run("fastest mode (default) sorts by duration", func(t *testing.T) {
@@ -285,13 +285,13 @@ func TestLazyExpansionDepthLimit(t *testing.T) {
// Add chain of transfer edges s1->s2->s3->s4->s5->s6->s7
for i := 0; i < 6; i++ {
graph.AddEdge(&Edge{
From: graph.Nodes()[i],
To: graph.Nodes()[i+1],
Kind: EdgeKindReal,
Duration: 100,
Transport: "train",
TransportType: TransportTypeTrain,
IsTransfer: true,
From: graph.Nodes()[i],
To: graph.Nodes()[i+1],
Kind: EdgeKindReal,
Duration: 100,
Transport: "train",
TransportType: TransportTypeTrain,
IsTransfer: true,
})
}
@@ -336,6 +336,7 @@ func TestLazyExpansionDepthLimit(t *testing.T) {
t.Log("MaxTransfers=0: no direct route s1->s7 found (only chain edges exist)")
}
}
// TestRouteReSearchOnChange tests that the route change detection logic correctly
// identifies when a route leg has undergone significant changes (cancellation or major delay)
// and triggers a re-search to find an updated route.
@@ -355,7 +356,7 @@ func TestRouteReSearchOnChange(t *testing.T) {
Duration: 3600, // 1 hour
Transport: "train",
IsTransfer: false,
Cost: 500,
Cost: 500,
})
// Add real edge s2 -> s3 (direct route)
@@ -366,7 +367,7 @@ func TestRouteReSearchOnChange(t *testing.T) {
Duration: 3600, // 1 hour
Transport: "train",
IsTransfer: false,
Cost: 500,
Cost: 500,
})
// Create an itinerary simulating a found route from s1 to s3
@@ -375,13 +376,13 @@ func TestRouteReSearchOnChange(t *testing.T) {
{From: graph.Nodes()[0], To: graph.Nodes()[1], Duration: 3600, Transport: "train", IsTransfer: false, Cost: 500},
{From: graph.Nodes()[1], To: graph.Nodes()[2], Duration: 3600, Transport: "train", IsTransfer: false, Cost: 500},
},
TotalDuration: 7200, // 2 hours total
TotalDuration: 7200, // 2 hours total
TotalTransfers: 0,
ID: "test-route-123",
ID: "test-route-123",
// Set LastChecked to 2 hours ago (7200 seconds) to force re-check
// The check skips if checked within 3600 seconds (1 hour)
LastChecked: time.Now().Unix() - 7200,
NeedsReSearch: false,
LastChecked: time.Now().Unix() - 7200,
NeedsReSearch: false,
ReSearchReason: "",
}
@@ -431,11 +432,11 @@ func TestRouteReSearchOnChange(t *testing.T) {
{From: graph.Nodes()[0], To: graph.Nodes()[1], Duration: 3600, Transport: "train", IsTransfer: false, Cost: 500},
{From: graph.Nodes()[1], To: graph.Nodes()[2], Duration: 3600, Transport: "train", IsTransfer: false, Cost: 500},
},
TotalDuration: 7200,
TotalDuration: 7200,
TotalTransfers: 0,
ID: "test-route-456",
LastChecked: time.Now().Unix() - 7200,
NeedsReSearch: false,
ID: "test-route-456",
LastChecked: time.Now().Unix() - 7200,
NeedsReSearch: false,
ReSearchReason: "",
}