Update test cases to test updating shuttle data depending on proximity

This commit is contained in:
2025-09-26 16:12:01 -07:00
parent 155ab2b4d2
commit 67a4b3fc8e

View File

@@ -16,6 +16,7 @@ import {
updateGlobalFetchMockJsonToThrowSyntaxError updateGlobalFetchMockJsonToThrowSyntaxError
} from "../../../../testHelpers/fetchMockHelpers"; } from "../../../../testHelpers/fetchMockHelpers";
import { assertAsyncCallbackThrowsApiResponseError } from "../../../../testHelpers/assertAsyncCallbackThrowsApiResponseError"; import { assertAsyncCallbackThrowsApiResponseError } from "../../../../testHelpers/assertAsyncCallbackThrowsApiResponseError";
import { IRoute } from "../../../entities/ShuttleRepositoryEntities";
describe("ApiBasedShuttleRepositoryLoader", () => { describe("ApiBasedShuttleRepositoryLoader", () => {
let loader: ApiBasedShuttleRepositoryLoader; let loader: ApiBasedShuttleRepositoryLoader;
@@ -125,47 +126,86 @@ describe("ApiBasedShuttleRepositoryLoader", () => {
}); });
describe("updateShuttleDataForSystemBasedOnProximityToRoutes", () => { describe("updateShuttleDataForSystemBasedOnProximityToRoutes", () => {
it("updates shuttle data in repository from API if shuttles close enough to route", async () => { function generateMockRoutesWithPolylineCoordinates() {
const distance = 1;
loader = new ApiBasedShuttleRepositoryLoader(
"263",
"1",
new UnoptimizedInMemoryShuttleRepository(),
distance,
);
const routes = generateMockRoutes(); const routes = generateMockRoutes();
routes[0].polylineCoordinates = [ routes[0].polylineCoordinates = [
{ latitude: 33.78792, longitude: -117.86187 }, {latitude: 33.78792, longitude: -117.86187},
{ latitude: 33.78792, longitude: -117.86200 }, {latitude: 33.78792, longitude: -117.86200},
{ latitude: 33.78792, longitude: -117.86245 } {latitude: 33.78792, longitude: -117.86245}
]; ];
return routes;
}
await Promise.all(routes.map(async (route) => { function getMockJsonResponseMatchingRouteAndCoordinates(route: IRoute, longitude: string, latitude: string) {
await loader.repository.addOrUpdateRoute(route);
}));
const modifiedSuccessfulResponse = { const modifiedSuccessfulResponse = {
...fetchShuttleDataSuccessfulResponse, ...fetchShuttleDataSuccessfulResponse,
}; };
Object.keys(modifiedSuccessfulResponse.buses).forEach((busId) => { Object.keys(modifiedSuccessfulResponse.buses).forEach((busId) => {
const bus = (modifiedSuccessfulResponse.buses as any)[busId][0]; const bus = (modifiedSuccessfulResponse.buses as any)[busId][0];
bus.latitude = "33.78792"; bus.latitude = latitude;
bus.longitude = "-117.86187"; bus.longitude = longitude;
bus.routeId = routes[0].id; 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); updateGlobalFetchMockJson(modifiedSuccessfulResponse);
const busesInResponse = Object.values(modifiedSuccessfulResponse.buses); const busesInResponse = Object.values(modifiedSuccessfulResponse.buses);
await loader.updateShuttleDataForSystemBasedOnProximityToRoutes(); await loader.updateShuttleDataForSystemBasedOnProximityToRoutes();
const shuttles = await loader.repository.getShuttles(); const shuttles = await loader.repository.getShuttles();
expect(shuttles.length).toEqual(busesInResponse.length); 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 () => { it("throws the correct error if the API response contains no data", async () => {
updateGlobalFetchMockJsonToThrowSyntaxError(); updateGlobalFetchMockJsonToThrowSyntaxError();