diff --git a/Services/TransportConnectionCalculator.cs b/Services/TransportConnectionCalculator.cs index d42bdd6..ee0364a 100644 --- a/Services/TransportConnectionCalculator.cs +++ b/Services/TransportConnectionCalculator.cs @@ -4,36 +4,6 @@ namespace ShiftScheduler.Services; public static class TransportConnectionCalculator { - public static TransportConnection? FindBestConnection(IReadOnlyList connections, DateTime latestArrivalTime) - { - var validConnections = new List(); - - foreach (var connection in connections) - { - if (DateTime.TryParse(connection.ArrivalTime, out var arrivalTime)) - { - if (arrivalTime <= latestArrivalTime) - { - validConnections.Add(connection); - } - } - } - - if (validConnections.Count > 0) - { - var sortedValid = validConnections - .OrderBy(c => DateTime.Parse(c.ArrivalTime ?? "00:00")) - .ToList(); - - return sortedValid.Last(); - } - - // If no valid connections, return the earliest available - return connections - .OrderBy(c => DateTime.Parse(c.ArrivalTime ?? "23:59")) - .FirstOrDefault(); - } - public static TransportConnection? FindBestConnection( IReadOnlyList connections, DateTime shiftStartTime, diff --git a/Shared/TransportModels.cs b/Shared/TransportModels.cs index e374a26..b93f83c 100644 --- a/Shared/TransportModels.cs +++ b/Shared/TransportModels.cs @@ -7,8 +7,8 @@ namespace ShiftScheduler.Shared public string ApiBaseUrl { get; set; } = string.Empty; public int SafetyBufferMinutes { get; set; } = 30; public int MinBreakMinutes { get; set; } = 60; - public int MaxEarlyArrivalMinutes { get; set; } = 60; // Y parameter - public int MaxLateArrivalMinutes { get; set; } = 15; // X parameter + public int MaxEarlyArrivalMinutes { get; set; } = 60; + public int MaxLateArrivalMinutes { get; set; } = 15; } public class TransportConnection diff --git a/ShiftScheduler.Services.Tests/TransportConnectionCalculatorTests.cs b/ShiftScheduler.Services.Tests/TransportConnectionCalculatorTests.cs index 7b34d1e..d5c9a42 100644 --- a/ShiftScheduler.Services.Tests/TransportConnectionCalculatorTests.cs +++ b/ShiftScheduler.Services.Tests/TransportConnectionCalculatorTests.cs @@ -6,80 +6,6 @@ namespace ShiftScheduler.Services.Tests; public class TransportConnectionCalculatorTests { - [Fact] - public void FindBestConnection_WithValidConnections_ShouldReturnLatestValidConnection() - { - // Arrange - var connections = new List - { - new TransportConnection { ArrivalTime = "2023-12-15T07:15:00" }, - new TransportConnection { ArrivalTime = "2023-12-15T07:25:00" }, - new TransportConnection { ArrivalTime = "2023-12-15T07:35:00" } // This one is too late - }; - var latestArrivalTime = new DateTime(2023, 12, 15, 7, 30, 0); - - // Act - var result = TransportConnectionCalculator.FindBestConnection(connections, latestArrivalTime); - - // Assert - result.ShouldNotBeNull(); - result.ArrivalTime.ShouldBe("2023-12-15T07:25:00"); - } - - [Fact] - public void FindBestConnection_WithNoValidConnections_ShouldReturnEarliestConnection() - { - // Arrange - var connections = new List - { - new TransportConnection { ArrivalTime = "2023-12-15T07:35:00" }, - new TransportConnection { ArrivalTime = "2023-12-15T07:40:00" }, - new TransportConnection { ArrivalTime = "2023-12-15T07:32:00" } - }; - var latestArrivalTime = new DateTime(2023, 12, 15, 7, 30, 0); - - // Act - var result = TransportConnectionCalculator.FindBestConnection(connections, latestArrivalTime); - - // Assert - result.ShouldNotBeNull(); - result.ArrivalTime.ShouldBe("2023-12-15T07:32:00"); // Earliest of the invalid ones - } - - [Fact] - public void FindBestConnection_WithEmptyConnections_ShouldReturnNull() - { - // Arrange - var connections = new List(); - var latestArrivalTime = new DateTime(2023, 12, 15, 7, 30, 0); - - // Act - var result = TransportConnectionCalculator.FindBestConnection(connections, latestArrivalTime); - - // Assert - result.ShouldBeNull(); - } - - [Fact] - public void FindBestConnection_WithInvalidArrivalTimes_ShouldIgnoreInvalidEntries() - { - // Arrange - var connections = new List - { - new TransportConnection { ArrivalTime = "invalid-time" }, - new TransportConnection { ArrivalTime = "2023-12-15T07:25:00" }, - new TransportConnection { ArrivalTime = "" } - }; - var latestArrivalTime = new DateTime(2023, 12, 15, 7, 30, 0); - - // Act - var result = TransportConnectionCalculator.FindBestConnection(connections, latestArrivalTime); - - // Assert - result.ShouldNotBeNull(); - result.ArrivalTime.ShouldBe("2023-12-15T07:25:00"); - } - // Tests for new enhanced logic [Fact] public void FindBestConnectionEnhanced_WithGoodTimingConnection_ShouldReturnLatestValidConnection() diff --git a/ShiftScheduler.Services.Tests/TransportServiceTests.cs b/ShiftScheduler.Services.Tests/TransportServiceTests.cs index a4a77f6..63c2c19 100644 --- a/ShiftScheduler.Services.Tests/TransportServiceTests.cs +++ b/ShiftScheduler.Services.Tests/TransportServiceTests.cs @@ -240,86 +240,4 @@ public class TransportServiceTests } }; } - - [Fact] - public async Task GetConnectionAsync_WithNewLogic_ShouldHandleEarlyAndLateConnections() - { - // Arrange - Shift starts at 8:00, test new logic with late-arriving connections - var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0); - var apiResponse = CreateConnectionsForNewLogicTest(); - var jsonResponse = JsonSerializer.Serialize(apiResponse); - - SetupHttpMockResponse(HttpStatusCode.OK, jsonResponse); - - // Act - var result = await _transportService.GetConnectionAsync(shiftStartTime); - - // Assert - result.ShouldNotBeNull(); - // With MaxEarlyArrivalMinutes = 60, connections arriving before 7:00 are too early - // The connection arriving at 6:30 (90 min before shift) should be considered too early - // So it should prefer the connection at 8:05 (5 min after shift, within MaxLateArrivalMinutes = 15) - result.ArrivalTime.ShouldBe("2023-12-15T08:05:00"); - } - - private static TransportApiResponse CreateConnectionsForNewLogicTest() - { - return new TransportApiResponse - { - Connections = new List - { - // Too early connection (90 min before shift start) - new TransportApiConnection - { - From = new TransportApiCheckpoint - { - Station = new TransportApiStation { Name = "Zurich HB", Id = "zurich" }, - Departure = "2023-12-15T05:30:00", - Platform = "5" - }, - To = new TransportApiCheckpoint - { - Station = new TransportApiStation { Name = "Bern", Id = "bern" }, - Arrival = "2023-12-15T06:30:00", - Platform = "3" - }, - Duration = "01:00:00" - }, - // Late but acceptable connection (5 min after shift start) - new TransportApiConnection - { - From = new TransportApiCheckpoint - { - Station = new TransportApiStation { Name = "Zurich HB", Id = "zurich" }, - Departure = "2023-12-15T07:20:00", - Platform = "4" - }, - To = new TransportApiCheckpoint - { - Station = new TransportApiStation { Name = "Bern", Id = "bern" }, - Arrival = "2023-12-15T08:05:00", - Platform = "2" - }, - Duration = "00:45:00" - }, - // Too late connection (20 min after shift start, beyond MaxLateArrivalMinutes) - new TransportApiConnection - { - From = new TransportApiCheckpoint - { - Station = new TransportApiStation { Name = "Zurich HB", Id = "zurich" }, - Departure = "2023-12-15T07:35:00", - Platform = "6" - }, - To = new TransportApiCheckpoint - { - Station = new TransportApiStation { Name = "Bern", Id = "bern" }, - Arrival = "2023-12-15T08:20:00", - Platform = "1" - }, - Duration = "00:45:00" - } - } - }; - } } \ No newline at end of file