From 67a4b3fc8eaa9bc4b7520e497dd783f82fab8b1a Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Fri, 26 Sep 2025 16:12:01 -0700 Subject: [PATCH] Update test cases to test updating shuttle data depending on proximity --- ...iBasedShuttleRepositoryLoaderTests.test.ts | 80 ++++++++++++++----- 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/src/loaders/shuttle/__tests__/ApiBasedShuttleRepositoryLoaderTests.test.ts b/src/loaders/shuttle/__tests__/ApiBasedShuttleRepositoryLoaderTests.test.ts index 7aeb9ff..3f0ff94 100644 --- a/src/loaders/shuttle/__tests__/ApiBasedShuttleRepositoryLoaderTests.test.ts +++ b/src/loaders/shuttle/__tests__/ApiBasedShuttleRepositoryLoaderTests.test.ts @@ -16,6 +16,7 @@ import { updateGlobalFetchMockJsonToThrowSyntaxError } from "../../../../testHelpers/fetchMockHelpers"; import { assertAsyncCallbackThrowsApiResponseError } from "../../../../testHelpers/assertAsyncCallbackThrowsApiResponseError"; +import { IRoute } from "../../../entities/ShuttleRepositoryEntities"; describe("ApiBasedShuttleRepositoryLoader", () => { let loader: ApiBasedShuttleRepositoryLoader; @@ -125,47 +126,86 @@ describe("ApiBasedShuttleRepositoryLoader", () => { }); describe("updateShuttleDataForSystemBasedOnProximityToRoutes", () => { - it("updates shuttle data in repository from API if shuttles close enough to route", async () => { - const distance = 1; - loader = new ApiBasedShuttleRepositoryLoader( - "263", - "1", - new UnoptimizedInMemoryShuttleRepository(), - distance, - ); - + function generateMockRoutesWithPolylineCoordinates() { const routes = generateMockRoutes(); routes[0].polylineCoordinates = [ - { latitude: 33.78792, longitude: -117.86187 }, - { latitude: 33.78792, longitude: -117.86200 }, - { latitude: 33.78792, longitude: -117.86245 } + {latitude: 33.78792, longitude: -117.86187}, + {latitude: 33.78792, longitude: -117.86200}, + {latitude: 33.78792, longitude: -117.86245} ]; + return routes; + } - await Promise.all(routes.map(async (route) => { - await loader.repository.addOrUpdateRoute(route); - })); - + function getMockJsonResponseMatchingRouteAndCoordinates(route: IRoute, longitude: string, latitude: string) { const modifiedSuccessfulResponse = { ...fetchShuttleDataSuccessfulResponse, }; Object.keys(modifiedSuccessfulResponse.buses).forEach((busId) => { const bus = (modifiedSuccessfulResponse.buses as any)[busId][0]; - bus.latitude = "33.78792"; - bus.longitude = "-117.86187"; - bus.routeId = routes[0].id; + bus.latitude = latitude; + bus.longitude = longitude; + bus.routeId = route.id; }); + return modifiedSuccessfulResponse; + } + async function addMockRoutes(routes: IRoute[]) { + await Promise.all(routes.map(async (route) => { + await loader.repository.addOrUpdateRoute(route); + })); + } + + it("updates shuttle data in repository from API if shuttles close enough to route", async () => { + const distanceMiles = 1; + loader = new ApiBasedShuttleRepositoryLoader( + "263", + "1", + new UnoptimizedInMemoryShuttleRepository(), + distanceMiles, + ); + + const routes = generateMockRoutesWithPolylineCoordinates(); + await addMockRoutes(routes); + const modifiedSuccessfulResponse = getMockJsonResponseMatchingRouteAndCoordinates( + routes[0], + "-117.86187", + "33.78792" + ); updateGlobalFetchMockJson(modifiedSuccessfulResponse); const busesInResponse = Object.values(modifiedSuccessfulResponse.buses); await loader.updateShuttleDataForSystemBasedOnProximityToRoutes(); const shuttles = await loader.repository.getShuttles(); - expect(shuttles.length).toEqual(busesInResponse.length); }); + it("does not update shuttle data in repository from API if shuttles are not close enough to route", async () => { + const distanceMiles = 1; + loader = new ApiBasedShuttleRepositoryLoader( + "263", + "1", + new UnoptimizedInMemoryShuttleRepository(), + distanceMiles, + ); + + const routes = generateMockRoutesWithPolylineCoordinates(); + await addMockRoutes(routes); + + const modifiedSuccessfulResponse = getMockJsonResponseMatchingRouteAndCoordinates( + routes[0], + "-116.86187", + "32.78792" + ); + updateGlobalFetchMockJson(modifiedSuccessfulResponse); + + await loader.updateShuttleDataForSystemBasedOnProximityToRoutes(); + + const shuttles = await loader.repository.getShuttles(); + expect(shuttles.length).toEqual(0); + }); + it("throws the correct error if the API response contains no data", async () => { updateGlobalFetchMockJsonToThrowSyntaxError();