Implement travel time data point saving and loading, and tests

This commit is contained in:
2025-11-10 16:46:34 -08:00
parent bd1ae07662
commit d92db84738
2 changed files with 249 additions and 59 deletions

View File

@@ -15,57 +15,116 @@ describe("RedisShuttleRepository", () => {
await repository.disconnect();
});
async function setupRouteAndOrderedStops() {
const systemId = "sys1";
const route = {
id: "r1",
name: "Route 1",
color: "red",
systemId: systemId,
polylineCoordinates: [],
updatedTime: new Date(),
};
await repository.addOrUpdateRoute(route);
const stop1 = {
id: "st1",
name: "Stop 1",
systemId: systemId,
coordinates: { latitude: 10.0, longitude: 20.0 },
updatedTime: new Date(),
};
const stop2 = {
id: "st2",
name: "Stop 2",
systemId: systemId,
coordinates: { latitude: 15.0, longitude: 25.0 },
updatedTime: new Date(),
};
await repository.addOrUpdateStop(stop1);
await repository.addOrUpdateStop(stop2);
const orderedStop1 = {
routeId: route.id,
stopId: stop1.id,
position: 1,
systemId: systemId,
updatedTime: new Date(),
};
const orderedStop2 = {
routeId: route.id,
stopId: stop2.id,
position: 2,
systemId: systemId,
updatedTime: new Date(),
};
await repository.addOrUpdateOrderedStop(orderedStop1);
await repository.addOrUpdateOrderedStop(orderedStop2);
return {
route,
systemId,
stop1,
stop2,
};
}
describe("addOrUpdateShuttle", () => {
it("updates the shuttle's last stop arrival if shuttle is at a stop", async () => {
const { route, systemId, stop2 } = await setupRouteAndOrderedStops();
// Shuttle positioned at stop2
const shuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop2.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
await repository.addOrUpdateShuttle(shuttle);
const lastStop = await repository.getShuttleLastStopArrival(shuttle);
expect(lastStop?.stopId).toEqual(stop2.id)
});
it("updates how long the shuttle took to get from one stop to another", async () => {
const { route, systemId, stop2, stop1 } = await setupRouteAndOrderedStops();
// Start the shuttle at stop 1, then have it move to stop 2
const shuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
}
const firstStopArrivalTime = new Date(2025, 0, 1, 12, 0, 0);
await repository.addOrUpdateShuttle(shuttle, firstStopArrivalTime.getTime());
shuttle.coordinates = stop2.coordinates;
// 15-minute travel time
const secondStopArrivalTime = new Date(2025, 0, 1, 12, 15, 0);
await repository.addOrUpdateShuttle(shuttle, secondStopArrivalTime.getTime());
const travelTime = await repository.getAverageTravelTimeSeconds({
routeId: route.id,
fromStopId: stop1.id,
toStopId: stop2.id,
}, {
from: new Date(2025, 0, 1, 11, 0, 0),
to: new Date(2025, 0, 1, 13, 0, 0),
});
expect(travelTime).toEqual(15 * 60)
});
});
describe("getArrivedStopIfExists", () => {
async function setupRouteAndOrderedStops() {
const systemId = "sys1";
const route = {
id: "r1",
name: "Route 1",
color: "red",
systemId: systemId,
polylineCoordinates: [],
updatedTime: new Date(),
};
await repository.addOrUpdateRoute(route);
const stop1 = {
id: "st1",
name: "Stop 1",
systemId: systemId,
coordinates: { latitude: 10.0, longitude: 20.0 },
updatedTime: new Date(),
};
const stop2 = {
id: "st2",
name: "Stop 2",
systemId: systemId,
coordinates: { latitude: 15.0, longitude: 25.0 },
updatedTime: new Date(),
};
await repository.addOrUpdateStop(stop1);
await repository.addOrUpdateStop(stop2);
const orderedStop1 = {
routeId: route.id,
stopId: stop1.id,
position: 1,
systemId: systemId,
updatedTime: new Date(),
};
const orderedStop2 = {
routeId: route.id,
stopId: stop2.id,
position: 2,
systemId: systemId,
updatedTime: new Date(),
};
await repository.addOrUpdateOrderedStop(orderedStop1);
await repository.addOrUpdateOrderedStop(orderedStop2);
return { route, systemId };
}
it("gets the stop that the shuttle is currently at, if exists", async () => {
const { route, systemId } = await setupRouteAndOrderedStops();
const { route, systemId, stop2 } = await setupRouteAndOrderedStops();
// Create a shuttle positioned at stop2
const shuttle = {
@@ -73,7 +132,7 @@ describe("RedisShuttleRepository", () => {
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: { latitude: 15.0, longitude: 25.0 }, // Same as stop2
coordinates: stop2.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};