Implement updateEtasBasedOnHistoricalData and add a test for it

This commit is contained in:
2025-11-10 18:11:54 -08:00
parent d92db84738
commit 91517669f0
2 changed files with 78 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
import { beforeEach, describe, it, expect, afterEach } from "@jest/globals";
import { RedisShuttleRepository } from "../RedisShuttleRepository";
import { generateMockShuttles } from "../../../../testHelpers/mockDataGenerators";
import { IOrderedStop } from "../../../entities/ShuttleRepositoryEntities";
describe("RedisShuttleRepository", () => {
let repository: RedisShuttleRepository;
@@ -44,22 +45,27 @@ describe("RedisShuttleRepository", () => {
await repository.addOrUpdateStop(stop1);
await repository.addOrUpdateStop(stop2);
const orderedStop1 = {
const orderedStop1: IOrderedStop = {
routeId: route.id,
stopId: stop1.id,
position: 1,
systemId: systemId,
updatedTime: new Date(),
};
const orderedStop2 = {
const orderedStop2: IOrderedStop = {
routeId: route.id,
stopId: stop2.id,
position: 2,
systemId: systemId,
updatedTime: new Date(),
};
orderedStop1.nextStop = orderedStop2;
orderedStop1.previousStop = orderedStop2;
orderedStop2.nextStop = orderedStop1;
orderedStop2.previousStop = orderedStop1;
await repository.addOrUpdateOrderedStop(orderedStop1);
await repository.addOrUpdateOrderedStop(orderedStop2);
return {
route,
systemId,
@@ -90,7 +96,7 @@ describe("RedisShuttleRepository", () => {
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",
@@ -120,6 +126,39 @@ describe("RedisShuttleRepository", () => {
});
expect(travelTime).toEqual(15 * 60)
});
it("adds an ETA entry based on historical data", async () => {
const { route, systemId, stop1, stop2 } = 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());
shuttle.coordinates = stop1.coordinates;
await repository.addOrUpdateShuttle(
shuttle,
new Date(2025, 0, 8, 12, 0, 0).getTime(),
new Date(2025, 0, 8, 12, 7, 30),
);
const eta = await repository.getEtaForShuttleAndStopId(shuttle.id, stop2.id);
expect(eta?.secondsRemaining).toEqual(7 * 60 + 30);
});
});
describe("getArrivedStopIfExists", () => {