import { beforeEach, describe, expect, it } from "@jest/globals"; import { setupTestServerContext, setupTestServerHolder } from "../testHelpers/apolloTestServerHelpers"; import { generateMockRoutes, generateMockShuttles, generateMockStops, generateParkingStructures } from "../testHelpers/mockDataGenerators"; import { addMockRouteToRepository, addMockShuttleToRepository, addMockStopToRepository, } from "../testHelpers/repositorySetupHelpers"; import assert = require("node:assert"); import { InterchangeSystem } from "../../src/entities/InterchangeSystem"; describe("SystemResolvers", () => { const holder = setupTestServerHolder(); const context = setupTestServerContext(); let mockSystem: InterchangeSystem; beforeEach(async () => { mockSystem = context.systems[0]; }); // TODO: Consolidate these into one single method taking an object async function getResponseFromQueryNeedingSystemId(query: string) { return await holder.testServer.executeOperation({ query, variables: { systemId: mockSystem.id, }, }, { contextValue: context, }); } describe("routes", () => { const query = ` query GetSystemRoutes($systemId: ID!) { system(id: $systemId) { routes { id name } } } `; it("gets routes associated with system id", async () => { const expectedRoutes = generateMockRoutes(); await Promise.all(expectedRoutes.map(async (route) => { route.systemId = mockSystem.id; await context.systems[0].shuttleRepository.addOrUpdateRoute(route); })); const response = await getResponseFromQueryNeedingSystemId(query); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined() const routes = (response.body.singleResult.data as any).system.routes; expect(routes.length === expectedRoutes.length); }); }); describe("stops", () => { const query = ` query GetSystemStops($systemId: ID!) { system(id: $systemId) { stops { id name } } } `; it("gets stops associated with system id", async () => { const expectedStops = generateMockStops(); await Promise.all(expectedStops.map(async (stop) => { stop.systemId = mockSystem.id; await context.systems[0].shuttleRepository.addOrUpdateStop(stop); })); const response = await getResponseFromQueryNeedingSystemId(query); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined() const stops = (response.body.singleResult.data as any).system.stops; expect(stops.length === expectedStops.length); }); }); describe("stop", () => { async function getResponseForStopQuery(stopId: string) { const query = ` query GetSystemStop($systemId: ID!, $stopId: ID!) { system(id: $systemId) { stop(id: $stopId) { id name } } } `; return await holder.testServer.executeOperation({ query, variables: { systemId: mockSystem.id, stopId: stopId, }, }, { contextValue: context, }); } it("gets the stop with the correct id", async () => { const mockStop = await addMockStopToRepository(context.systems[0].shuttleRepository, mockSystem.id); const response = await getResponseForStopQuery(mockStop.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const stop = (response.body.singleResult.data as any).system.stop; expect(stop.id).toEqual(mockStop.id); expect(stop.name).toEqual(mockStop.name); }); it("returns null if the stop isn't associated with the system", async () => { const updatedSystem = { ...mockSystem, id: "2", } const mockStop = await addMockStopToRepository(context.systems[0].shuttleRepository, updatedSystem.id); const response = await getResponseForStopQuery(mockStop.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const stop = (response.body.singleResult.data as any).system.stop; expect(stop).toBeNull(); }); it("returns null if there is no stop", async () => { const response = await getResponseForStopQuery("1"); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const stop = (response.body.singleResult.data as any).system.stop; expect(stop).toBeNull(); }); }); describe("route", () => { async function getResponseForRouteQuery(routeId: string) { const query = ` query GetSystemRoute($systemId: ID!, $routeId: ID!) { system(id: $systemId) { route(id: $routeId) { id name } } } `; return await holder.testServer.executeOperation({ query, variables: { systemId: mockSystem.id, routeId, }, }, { contextValue: context }); } it("gets the route with the correct id", async () => { const mockRoute = await addMockRouteToRepository(context.systems[0].shuttleRepository, mockSystem.id); const response = await getResponseForRouteQuery(mockRoute.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const route = (response.body.singleResult.data as any).system.route; expect(route.id).toEqual(mockRoute.id); expect(route.name).toEqual(mockRoute.name); }); it("returns null if the route isn't associated with the system", async () => { const updatedSystem = { ...mockSystem, id: "2", } const mockRoute = await addMockRouteToRepository(context.systems[0].shuttleRepository, updatedSystem.id); const response = await getResponseForRouteQuery(mockRoute.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const route = (response.body.singleResult.data as any).system.route; expect(route).toBeNull(); }); it("returns null if there is no route", async () => { const response = await getResponseForRouteQuery("nonexistent-route-id"); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const route = (response.body.singleResult.data as any).system.route; expect(route).toBeNull(); }); }); describe("shuttle", () => { async function getResponseForShuttleQuery(shuttleId: string) { const query = ` query GetSystemShuttle($systemId: ID!, $shuttleId: ID!) { system(id: $systemId) { shuttle(id: $shuttleId) { id name } } } `; return await holder.testServer.executeOperation({ query, variables: { systemId: mockSystem.id, shuttleId: shuttleId, } }, { contextValue: context }); } it("gets the shuttle with the correct id", async () => { const mockShuttle = await addMockShuttleToRepository(context.systems[0].shuttleRepository, mockSystem.id); const response = await getResponseForShuttleQuery(mockShuttle.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const shuttle = (response.body.singleResult.data as any).system.shuttle; expect(shuttle.id).toEqual(mockShuttle.id); expect(shuttle.name).toEqual(mockShuttle.name); }); it("returns null if the shuttle isn't associated with the system", async () => { const updatedSystem = { ...mockSystem, id: "2", } const mockShuttle = await addMockShuttleToRepository(context.systems[0].shuttleRepository, updatedSystem.id); const response = await getResponseForShuttleQuery(mockShuttle.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const shuttle = (response.body.singleResult.data as any).system.shuttle; expect(shuttle).toBeNull(); }); it("returns null if there is no shuttle", async () => { const response = await getResponseForShuttleQuery("nonexistent-shuttle-id"); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const shuttle = (response.body.singleResult.data as any).system.shuttle; expect(shuttle).toBeNull(); }); }); describe("shuttles", () => { const query = ` query GetSystemShuttle($systemId: ID!) { system(id: $systemId) { shuttles { id name } } } `; it("gets shuttles associated with system id", async () => { const expectedShuttles = generateMockShuttles(); await Promise.all(expectedShuttles.map(async (shuttle) => { shuttle.systemId = mockSystem.id; await context.systems[0].shuttleRepository.addOrUpdateShuttle(shuttle); })); const response = await getResponseFromQueryNeedingSystemId(query); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined() const shuttles = (response.body.singleResult.data as any).system.shuttles; expect(shuttles.length === expectedShuttles.length); }); }); describe("parkingStructures", () => { const query = ` query GetParkingStructuresBySystem($systemId: ID!) { system(id: $systemId) { parkingStructures { name id capacity spotsAvailable coordinates { latitude longitude } address } } } ` it("gets parking structures associated with the system id", async () => { const expectedParkingStructures = generateParkingStructures(); await Promise.all(expectedParkingStructures.map(async (structure) => { await context.systems[0].parkingRepository?.addOrUpdateParkingStructure(structure); })); const response = await getResponseFromQueryNeedingSystemId(query); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const parkingStructures = (response.body.singleResult.data as any).system.parkingStructures; expect(parkingStructures).toEqual(expectedParkingStructures); }); it("returns a blank array if there are no parking structures", async () => { const response = await getResponseFromQueryNeedingSystemId(query); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const parkingStructures = (response.body.singleResult.data as any).system.parkingStructures; expect(parkingStructures).toHaveLength(0); }); it("returns a blank array if there is no parking data available", async () => { context.systems[0].parkingTimedDataLoader = null; context.systems[0].parkingRepository = null; const response = await getResponseFromQueryNeedingSystemId(query); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const parkingStructures = (response.body.singleResult.data as any).system.parkingStructures; expect(parkingStructures).toHaveLength(0); }); }); describe("parkingStructure", () => { async function getResponseForParkingStructureQuery(parkingStructureId: string) { const query = ` query GetParkingStructureBySystem($systemId: ID!, $parkingStructureId: ID!) { system(id: $systemId) { parkingStructure(id: $parkingStructureId) { name id capacity spotsAvailable coordinates { latitude longitude } address } } } `; return await holder.testServer.executeOperation({ query, variables: { systemId: mockSystem.id, parkingStructureId, } }, { contextValue: context }); } it("returns the correct parking structure given the id", async () => { const generatedParkingStructures = generateParkingStructures(); await Promise.all(generatedParkingStructures.map(async (structure) => { await context.systems[0].parkingRepository?.addOrUpdateParkingStructure(structure); })); const expectedParkingStructure = generatedParkingStructures[1]; const response = await getResponseForParkingStructureQuery(expectedParkingStructure.id); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const parkingStructure = (response.body.singleResult.data as any).system.parkingStructure; expect(parkingStructure).toEqual(expectedParkingStructure); }); it("returns null if there is no matching parking structure", async () => { const generatedParkingStructures = generateParkingStructures(); await Promise.all(generatedParkingStructures.map(async (structure) => { await context.systems[0].parkingRepository?.addOrUpdateParkingStructure(structure); })); const nonexistentId = generatedParkingStructures[0].id + "12345"; const response = await getResponseForParkingStructureQuery(nonexistentId); assert(response.body.kind === "single"); expect(response.body.singleResult.errors).toBeUndefined(); const parkingStructure = (response.body.singleResult.data as any).system.parkingStructure; expect(parkingStructure).toBeNull(); }); }); });