Address PR feedback: remove comments, old logic, and unnecessary tests
Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com>
This commit is contained in:
parent
881c0f4c84
commit
75ed873a44
4 changed files with 2 additions and 188 deletions
|
|
@ -4,36 +4,6 @@ namespace ShiftScheduler.Services;
|
|||
|
||||
public static class TransportConnectionCalculator
|
||||
{
|
||||
public static TransportConnection? FindBestConnection(IReadOnlyList<TransportConnection> connections, DateTime latestArrivalTime)
|
||||
{
|
||||
var validConnections = new List<TransportConnection>();
|
||||
|
||||
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<TransportConnection> connections,
|
||||
DateTime shiftStartTime,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -6,80 +6,6 @@ namespace ShiftScheduler.Services.Tests;
|
|||
|
||||
public class TransportConnectionCalculatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void FindBestConnection_WithValidConnections_ShouldReturnLatestValidConnection()
|
||||
{
|
||||
// Arrange
|
||||
var connections = new List<TransportConnection>
|
||||
{
|
||||
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<TransportConnection>
|
||||
{
|
||||
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<TransportConnection>();
|
||||
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<TransportConnection>
|
||||
{
|
||||
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()
|
||||
|
|
|
|||
|
|
@ -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<TransportApiConnection>
|
||||
{
|
||||
// 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"
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue