Add test for previous day fallback calculation

This commit is contained in:
2025-11-13 16:41:51 -08:00
parent d302e8a2cb
commit 3f267be154

View File

@@ -8,6 +8,7 @@ import { RedisShuttleRepository } from "../../RedisShuttleRepository";
import { UnoptimizedInMemoryShuttleRepository } from "../../UnoptimizedInMemoryShuttleRepository";
import { setupRouteAndOrderedStopsForShuttleRepository } from "../../../../../testHelpers/setupRouteAndOrderedStopsForShuttleRepository";
import { ShuttleGetterSetterRepository } from "../../ShuttleGetterSetterRepository";
import { IRoute, IStop } from "../../../../entities/ShuttleRepositoryEntities";
class RedisSelfUpdatingETARepositoryHolder implements RepositoryHolder<SelfUpdatingETARepository> {
repo: RedisSelfUpdatingETARepository | undefined;
@@ -20,6 +21,7 @@ class RedisSelfUpdatingETARepositoryHolder implements RepositoryHolder<SelfUpdat
url: process.env.REDIS_URL,
});
await this.redisClient.connect();
await this.redisClient.flushAll();
this.shuttleRepo = new RedisShuttleRepository(this.redisClient);
this.repo = new RedisSelfUpdatingETARepository(
this.shuttleRepo,
@@ -109,16 +111,19 @@ describe.each(repositoryImplementations)('$name', (holder) => {
});
expect(travelTime).toEqual(15 * 60);
});
});
describe("handleShuttleUpdate", () =>{
describe("handleShuttleUpdate", () => {
test("adds an ETA entry based on historical data", async () => {
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops();
const firstStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 0, 0);
const secondStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 15, 0);
const shuttleSecondArrivalTimeAtFirstStop = new Date(2025, 0, 8, 12, 0, 0);
const currentTime = new Date(shuttleSecondArrivalTimeAtFirstStop.getTime() + 7 * 60 * 1000);
// "current time" is 7 minutes past arrival, expected eta = 8 minutes
repository.setReferenceTime(currentTime);
repository.setReferenceTime(currentTime);
repository.startListeningForUpdates();
const shuttle = {
@@ -131,23 +136,66 @@ describe.each(repositoryImplementations)('$name', (holder) => {
updatedTime: new Date(),
};
const firstStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 0, 0);
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopLastWeekArrivalTime.getTime());
shuttle.coordinates = stop2.coordinates;
const secondStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 15, 0);
await shuttleRepository.addOrUpdateShuttle(shuttle, secondStopLastWeekArrivalTime.getTime());
shuttle.coordinates = stop1.coordinates;
await shuttleRepository.addOrUpdateShuttle(
shuttle,
shuttleSecondArrivalTimeAtFirstStop.getTime(),
shuttleSecondArrivalTimeAtFirstStop.getTime()
);
shuttle.coordinates = { latitude: 12.5, longitude: 22.5 };
await shuttleRepository.addOrUpdateShuttle(
shuttle,
currentTime.getTime(),
currentTime.getTime()
);
await new Promise((resolve) => setTimeout(resolve, 1000));
const eta = await repository.getEtaForShuttleAndStopId(shuttle.id, stop2.id);
expect(eta?.secondsRemaining).toEqual(8 * 60);
}, 60000);
test("uses previous day fallback calculation when no data available from one week ago", async () => {
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops();
const firstStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 0, 0);
const secondStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 15, 0);
const shuttleSecondArrivalTimeAtFirstStop = new Date(2025, 0, 2, 12, 0, 0);
const currentTime = new Date(shuttleSecondArrivalTimeAtFirstStop.getTime() + 7 * 60 * 1000);
repository.setReferenceTime(currentTime);
repository.startListeningForUpdates();
const shuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopLastWeekArrivalTime.getTime());
shuttle.coordinates = stop2.coordinates;
await shuttleRepository.addOrUpdateShuttle(shuttle, secondStopLastWeekArrivalTime.getTime());
shuttle.coordinates = stop1.coordinates;
await shuttleRepository.addOrUpdateShuttle(
shuttle,
shuttleSecondArrivalTimeAtFirstStop.getTime()
);
shuttle.coordinates = { latitude: 12.5, longitude: 22.5 };
await shuttleRepository.addOrUpdateShuttle(
shuttle,
currentTime.getTime()
);
await new Promise((resolve) => setTimeout(resolve, 1000));