From 71ac18cb8d2e2f0f9bc40b7941064bd3b8c1062f Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Thu, 13 Nov 2025 16:43:53 -0800 Subject: [PATCH] Add a similar test for previous hour --- ...lfUpdatingETARepositorySharedTests.test.ts | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/repositories/shuttle/eta/__tests__/SelfUpdatingETARepositorySharedTests.test.ts b/src/repositories/shuttle/eta/__tests__/SelfUpdatingETARepositorySharedTests.test.ts index df1dfc7..7be9242 100644 --- a/src/repositories/shuttle/eta/__tests__/SelfUpdatingETARepositorySharedTests.test.ts +++ b/src/repositories/shuttle/eta/__tests__/SelfUpdatingETARepositorySharedTests.test.ts @@ -8,7 +8,6 @@ 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 { repo: RedisSelfUpdatingETARepository | undefined; @@ -203,6 +202,50 @@ describe.each(repositoryImplementations)('$name', (holder) => { const eta = await repository.getEtaForShuttleAndStopId(shuttle.id, stop2.id); expect(eta?.secondsRemaining).toEqual(8 * 60); }, 60000); + + test("uses previous hour fallback calculation when no data available from one day 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, 1, 13, 5, 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)); + + const eta = await repository.getEtaForShuttleAndStopId(shuttle.id, stop2.id); + expect(eta?.secondsRemaining).toEqual(8 * 60); + }); }); describe("getAverageTravelTimeSeconds", () => {