Move testHelpers directory up one level

This commit is contained in:
2025-07-31 22:41:05 -04:00
parent b7299b8359
commit 14fbdc7408
28 changed files with 46 additions and 53 deletions

View File

@@ -0,0 +1,66 @@
import { readFileSync } from "fs";
import { ApolloServer } from "@apollo/server";
import { MergedResolvers } from "../src/MergedResolvers";
import { beforeEach } from "@jest/globals";
import { ServerContext } from "../src/ServerContext";
import { InterchangeSystem } from "../src/entities/InterchangeSystem";
import {
ChapmanApiBasedParkingRepositoryLoader
} from "../src/loaders/parking/ChapmanApiBasedParkingRepositoryLoader";
function setUpTestServer() {
// Leaving this separate from the main server in case
// configuration changes
const typeDefs = readFileSync("./schema.graphqls", "utf8");
return new ApolloServer({
typeDefs,
resolvers: MergedResolvers,
});
}
const systemInfoForTesting = {
id: "1",
name: "Chapman University",
passioSystemId: "263",
parkingSystemId: ChapmanApiBasedParkingRepositoryLoader.id,
};
export function buildSystemForTesting() {
return InterchangeSystem.buildForTesting(
systemInfoForTesting,
);
}
/**
* Returns a `ServerContext` object which can be passed to requests
* for testing.
*/
export function setupTestServerContext() {
const context: { [key: string] : any } = {};
beforeEach(() => {
context.systems = [
buildSystemForTesting(),
];
context.findSystemById = (_: string) => context.systems[0];
});
return context as ServerContext;
}
/**
* Returns an object which holds a test server.
* This server is reset before every test.
* Tests should keep a reference to the holder object,
* and not destructure it.
*/
export function setupTestServerHolder() {
const holder: { [key: string]: any } = {};
beforeEach(() => {
holder.testServer = setUpTestServer();
});
return holder as { testServer: ApolloServer };
}

View File

@@ -0,0 +1,6 @@
import { expect } from "@jest/globals";
import { ApiResponseError } from "../src/loaders/ApiResponseError";
export async function assertAsyncCallbackThrowsApiResponseError(callback: () => Promise<any>) {
await expect(callback).rejects.toThrow(ApiResponseError);
}

View File

@@ -0,0 +1,41 @@
import { jest } from "@jest/globals";
/**
* Function to update behavior of the global `fetch` function.
* Note that the Passio GO API returns status code 200 for failed responses.
* @param obj
* @param status
*/
export function updateGlobalFetchMockJson(
obj: any,
status: number = 200
) {
// @ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.resolve(obj),
status,
ok: status.toString().startsWith("2"), // 200-level codes are OK
})
});
}
/**
* Reset the global fetch function mock's JSON to return an empty object.
* @param obj
*/
export function resetGlobalFetchMockJson() {
updateGlobalFetchMockJson({});
}
export function updateGlobalFetchMockJsonToThrowSyntaxError() {
// @ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.reject(new SyntaxError("Unable to parse JSON")),
status: 200,
ok: true,
})
}) as jest.Mock;
}

View File

@@ -0,0 +1,92 @@
export const chapmanParkingStructureData = {
"CenterOnLatitude": 33.793379,
"CenterOnLongitude": -117.853099,
"LatitudeZoom": 0.0045,
"LongitudeZoom": 0.0045,
"Structures": [
{
"Address": "300 E Walnut, Orange, CA 92867",
"Capacity": 871,
"CurrentCount": 211,
"HdpiDetailImage": "https://webfarm.chapman.edu/ParkingService/lastinger-hdpi.png",
"Latitude": 33.7945513,
"LdpiDetailImage": "https://webfarm.chapman.edu/ParkingService/lastinger-ldpi.png",
"Levels": [
{
"Capacity": 871,
"CurrentCount": 211,
"FriendlyName": "All Levels",
"SystemName": "All"
},
{
"Capacity": 401,
"CurrentCount": 26,
"FriendlyName": "B1 Level",
"SystemName": "L1"
},
{
"Capacity": 470,
"CurrentCount": 185,
"FriendlyName": "B2 Level",
"SystemName": "L2"
}
],
"Longitude": -117.8518707,
"MdpiDetailImage": "https://webfarm.chapman.edu/ParkingService/lastinger-mdpi.png",
"Name": "Anderson Structure",
"Timestamp": "/Date(1744327687147-0700)/",
"XhdpiDetailImage": "https://webfarm.chapman.edu/ParkingService/lastinger-xhdpi.png"
},
{
"Address": "200 W Sycamore Ave, Orange, CA 92866-1053",
"Capacity": 692,
"CurrentCount": 282,
"HdpiDetailImage": "https://webfarm.chapman.edu/ParkingService/barrera-hdpi.png",
"Latitude": 33.792937,
"LdpiDetailImage": "https://webfarm.chapman.edu/ParkingService/barrera-ldpi.png",
"Levels": [
{
"Capacity": 692,
"CurrentCount": 282,
"FriendlyName": "All Levels",
"SystemName": "All"
},
{
"Capacity": 85,
"CurrentCount": 0,
"FriendlyName": "Level 1",
"SystemName": "L1"
},
{
"Capacity": 145,
"CurrentCount": 21,
"FriendlyName": "Level 2",
"SystemName": "L2"
},
{
"Capacity": 150,
"CurrentCount": 50,
"FriendlyName": "Level 3",
"SystemName": "L3"
},
{
"Capacity": 150,
"CurrentCount": 79,
"FriendlyName": "Level 4",
"SystemName": "L4"
},
{
"Capacity": 162,
"CurrentCount": 132,
"FriendlyName": "Level 5",
"SystemName": "L5"
}
],
"Longitude": -117.854782,
"MdpiDetailImage": "https://webfarm.chapman.edu/ParkingService/barrera-mdpi.png",
"Name": "Barrera",
"Timestamp": "/Date(1744327647113-0700)/",
"XhdpiDetailImage": "https://webfarm.chapman.edu/ParkingService/barrera-xhdpi.png"
}
]
}

View File

@@ -0,0 +1,115 @@
// Snapshot taken from the Passio GO! API
export const fetchEtaDataSuccessfulResponse = {
"ETAs": {
"177666": [
{
"OOS": 0,
"busName": "08",
"distance": 1,
"speed": 10.028535400123669,
"routeBlockId": "142270",
"actualRouteBlockId": "142270",
"arrived": null,
"eta": "10 min ",
"color": "#000000",
"bg": "#ffea3f",
"order": 0,
"dwell": null,
"stopsAmount": 2,
"secondsSpent": 587,
"etaR": "10",
"error": null,
"outdated": 0,
"routeId": "53966",
"serviceTime": "",
"scheduleTimes": [],
"goShowSchedule": 0,
"looping": "1",
"routeGroupId": "6703",
"busId": 5577,
"tripId": 751430,
"deviceId": 402840,
"created": "2025-01-07 15:00:09",
"routePointPosition": 6,
"routeStopPosition": 1,
"stopRoutePointPosition": 217,
"timezoneOffset": -10800,
"busLatLng": [
33.7933406,
-117.8539321
],
"busProjectionLatlng": {
"lat": 33.79331052666975,
"lng": -117.85392945849208
},
"busProjectionError": 3,
"stopId": "177666",
"theStop": {
"name": "Chapman Court",
"position": 3,
"userId": "263",
"routeStopId": "1348785",
"busId": 5577,
"routeName": "Red Route",
"shortName": null,
"routeId": "53966",
"stopId": "177666"
}
},
{
"OOS": 0,
"busName": "07",
"distance": 1,
"speed": 12.160256921380398,
"routeBlockId": "142270",
"actualRouteBlockId": "142270",
"arrived": null,
"eta": "11 min ",
"color": "#000000",
"bg": "#ffea3f",
"order": 0,
"dwell": null,
"stopsAmount": 2,
"secondsSpent": 635,
"etaR": "11",
"error": null,
"outdated": 0,
"routeId": "53966",
"serviceTime": "",
"scheduleTimes": [],
"goShowSchedule": 0,
"looping": "1",
"routeGroupId": "6703",
"busId": 5576,
"tripId": 751430,
"deviceId": 441065,
"created": "2025-01-07 15:00:10",
"routePointPosition": 448,
"routeStopPosition": 4,
"stopRoutePointPosition": 217,
"timezoneOffset": -10800,
"busLatLng": [
33.7933284,
-117.855132
],
"busProjectionLatlng": {
"lat": 33.79332033922653,
"lng": -117.85513217762522
},
"busProjectionError": 1,
"stopId": "177666",
"theStop": {
"name": "Chapman Court",
"position": 3,
"userId": "263",
"routeStopId": "1348785",
"busId": 5576,
"routeName": "Red Route",
"shortName": null,
"routeId": "53966",
"stopId": "177666"
}
},
]
},
}

View File

@@ -0,0 +1,153 @@
export const fetchRouteDataSuccessfulResponse = {
"all": [
{
"name": "Rinker Route",
"shortName": null,
"color": "#696969",
"userId": "263",
"myid": "53970",
"mapApp": "1",
"archive": "0",
"goPrefixRouteName": "1",
"goShowSchedule": 0,
"outdated": "0",
"id": "5902",
"distance": 1900,
"latitude": "33.656851000",
"longitude": "-117.733221000",
"timezone": "America/Los_Angeles",
"fullname": "Chapman University",
"nameOrig": "Rinker Route",
"groupId": "6706",
"groupColor": "#696969"
},
{
"name": "Red Route",
"shortName": null,
"color": "#d62728",
"userId": "263",
"myid": "53966",
"mapApp": "1",
"archive": "0",
"goPrefixRouteName": "1",
"goShowSchedule": 0,
"outdated": "0",
"id": "2032",
"distance": 1905,
"latitude": "33.793325000",
"longitude": "-117.852810000",
"timezone": "America/Los_Angeles",
"fullname": "Chapman University",
"nameOrig": "Red Route",
"groupId": "6703",
"groupColor": "#d62728"
},
{
"name": "Teal Route",
"shortName": null,
"color": "#096e90",
"userId": "263",
"myid": "54191",
"mapApp": "1",
"archive": "0",
"goPrefixRouteName": "1",
"goShowSchedule": 0,
"outdated": "0",
"id": "2032",
"distance": 1905,
"latitude": "33.793325000",
"longitude": "-117.852810000",
"timezone": "America/Los_Angeles",
"fullname": "Chapman University",
"nameOrig": "Teal Route",
"groupId": "6705",
"groupColor": "#096e90"
},
{
"name": "Gold Route",
"shortName": null,
"color": "#bd9e39",
"userId": "263",
"myid": "54256",
"mapApp": "1",
"archive": "0",
"goPrefixRouteName": "1",
"goShowSchedule": 0,
"outdated": "0",
"id": "2032",
"distance": 1905,
"latitude": "33.793325000",
"longitude": "-117.852810000",
"timezone": "America/Los_Angeles",
"fullname": "Chapman University",
"nameOrig": "Gold Route",
"groupId": "6707",
"groupColor": "#bd9e39"
},
{
"name": "Black Route",
"shortName": null,
"color": "#000000",
"userId": "263",
"myid": "54257",
"mapApp": "1",
"archive": "0",
"goPrefixRouteName": "1",
"goShowSchedule": 0,
"outdated": "0",
"id": "2032",
"distance": 1905,
"latitude": "33.793325000",
"longitude": "-117.852810000",
"timezone": "America/Los_Angeles",
"fullname": "Chapman University",
"nameOrig": "Black Route",
"groupId": "6704",
"groupColor": "#000000"
},
{
"name": "Weekend Route",
"shortName": null,
"color": "#510094",
"userId": "263",
"myid": "54282",
"mapApp": "1",
"archive": "0",
"goPrefixRouteName": "1",
"goShowSchedule": 0,
"outdated": "1",
"id": "2032",
"distance": 1905,
"latitude": "33.793325000",
"longitude": "-117.852810000",
"timezone": "America/Los_Angeles",
"fullname": "Chapman University",
"nameOrig": "Weekend Route",
"groupId": "6710",
"groupColor": "#510094",
"serviceTime": "is not provided: no bus on the route",
"serviceTimeShort": "No bus in service"
},
{
"name": "Parking Lot",
"shortName": null,
"color": "#000000",
"userId": "263",
"myid": "54529",
"mapApp": "1",
"archive": "0",
"goPrefixRouteName": "1",
"goShowSchedule": 0,
"outdated": "0",
"id": "2032",
"distance": 1905,
"latitude": "33.793325000",
"longitude": "-117.852810000",
"timezone": "America/Los_Angeles",
"fullname": "Chapman University",
"nameOrig": "Parking Lot",
"groupId": "6731",
"groupColor": "#393838"
}
]
}

View File

@@ -0,0 +1,106 @@
// Snapshot taken from the Passio GO! API
export const fetchShuttleDataSuccessfulResponse = {
"alertCRC": "23c1b91c",
"buses": {
"402840": [
{
"deviceId": 402840,
"created": "08:24 PM",
"createdTime": "08:24 PM",
"paxLoad": 0,
"bus": "08",
"busId": 5577,
"userId": "263",
"routeBlockId": "142270",
"latitude": "33.791781800",
"longitude": "-117.858964600",
"calculatedCourse": "351.796001302109",
"outOfService": 0,
"more": "102",
"totalCap": 20,
"color": "#d62728",
"busName": "08",
"busType": "",
"routeId": "53966",
"route": "Red Route",
"outdated": 0
}
],
"404873": [
{
"deviceId": 404873,
"created": "08:24 PM",
"createdTime": "08:24 PM",
"paxLoad": 0,
"bus": "10",
"busId": 7105,
"userId": "263",
"routeBlockId": "142270",
"latitude": "33.789331300",
"longitude": "-117.888790600",
"calculatedCourse": "76.005762226701",
"outOfService": 0,
"more": "101",
"totalCap": 20,
"color": "#d62728",
"busName": "10",
"busType": "",
"routeId": "53966",
"route": "Red Route",
"outdated": 0
}
],
"421421": [
{
"deviceId": 421421,
"created": "08:24 PM",
"createdTime": "08:24 PM",
"paxLoad": 0,
"bus": "17",
"busId": 12502,
"userId": "263",
"routeBlockId": "142660",
"latitude": "33.790699500",
"longitude": "-117.890385500",
"calculatedCourse": "10.65684824528148",
"outOfService": 0,
"more": "101",
"totalCap": 32,
"color": "#bd9e39",
"busName": "17",
"busType": "Shuttle Bus",
"routeId": "54256",
"route": "Gold Route",
"outdated": 0
}
],
"441065": [
{
"deviceId": 441065,
"created": "08:19 PM",
"createdTime": "08:19 PM",
"paxLoad": 0,
"bus": "07",
"busId": 5576,
"userId": "263",
"routeBlockId": "142270",
"latitude": "33.793278900",
"longitude": "-117.852629400",
"calculatedCourse": "299.74488110904485",
"outOfService": 0,
"more": "22",
"totalCap": 20,
"color": "#d62728",
"busName": "07",
"busType": "",
"routeId": "53966",
"route": "Red Route",
"outdated": 0
}
]
},
"microtime": 0.023222923278808594,
"time": {
"263": "08:24 PM"
}
};

View File

@@ -0,0 +1,5 @@
// Not an actual response snapshot
// Simulate a case where `all` property of response is unavailable
export const fetchSystemDataFailedResponse = {
"error": "no systems",
};

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,109 @@
import { IEta, IOrderedStop, IRoute, IShuttle, IStop } from "../src/entities/ShuttleRepositoryEntities";
import { IParkingStructure } from "../src/entities/ParkingRepositoryEntities";
// Use a single set of generators in case any of the
// interfaces change in the future
export function generateParkingStructures(): IParkingStructure[] {
// Copied from debugger
return [
{
"capacity": 871,
"coordinates": {
"latitude": 33.7945513,
"longitude": -117.8518707
},
"id": "b0723baf8a6b8bcc37c821473373049e",
"name": "Anderson Structure",
"spotsAvailable": 163,
"address": "300 E Walnut, Orange, CA 92867",
updatedTime: new Date(),
},
{
"capacity": 692,
"coordinates": {
"latitude": 33.792937,
"longitude": -117.854782
},
"id": "81b9e1ed004cf6def2e6c568aaf79ece",
"name": "Barrera",
"spotsAvailable": 179,
"address": "200 W Sycamore Ave, Orange, CA 92866-1053",
updatedTime: new Date(),
}
];
}
export function generateMockShuttles(): IShuttle[] {
return [
{
id: "sh1",
name: "Shuttle A",
routeId: "r1",
systemId: "sys1",
coordinates: {
latitude: 10,
longitude: 20
},
orientationInDegrees: 25.163,
updatedTime: new Date(),
},
{
id: "sh2",
name: "Shuttle B",
routeId: "r2",
systemId: "sys2",
coordinates: {
latitude: 15,
longitude: 25
},
orientationInDegrees: 50.912,
updatedTime: new Date(),
},
{
id: "sh3",
name: "Shuttle C",
routeId: "r3",
systemId: "sys3",
coordinates: {
latitude: 30,
longitude: 40
},
orientationInDegrees: 321.019,
updatedTime: new Date(),
},
];
}
export function generateMockRoutes(): IRoute[] {
return [
{ id: "r1", name: "Route 1", color: "red", systemId: "sys1", polylineCoordinates: [], updatedTime: new Date() },
{ id: "r2", name: "Route 2", color: "blue", systemId: "sys2", polylineCoordinates: [], updatedTime: new Date() },
{ id: "r3", name: "Route 3", color: "green", systemId: "sys3", polylineCoordinates: [], updatedTime: new Date() },
];
}
export function generateMockStops(): IStop[] {
return [
{ id: "st1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 }, updatedTime: new Date() },
{ id: "st2", name: "Stop B", systemId: "sys2", coordinates: { latitude: 15, longitude: 25 }, updatedTime: new Date() },
{ id: "st3", name: "Stop C", systemId: "sys3", coordinates: { latitude: 30, longitude: 40 }, updatedTime: new Date() },
];
}
export function generateMockOrderedStops(): IOrderedStop[] {
return [
{ stopId: "st1", routeId: "r1", position: 1, systemId: "sys1", updatedTime: new Date(), },
{ stopId: "st1", routeId: "r2", position: 2, systemId: "sys1", updatedTime: new Date(), },
{ stopId: "st2", routeId: "r1", position: 3, systemId: "sys1", updatedTime: new Date(), },
{ stopId: "st2", routeId: "r2", position: 4, systemId: "sys1", updatedTime: new Date(), },
];
}
export function generateMockEtas(): IEta[] {
return [
{ shuttleId: "sh1", stopId: "st1", secondsRemaining: 120, systemId: "sys1", updatedTime: new Date() },
{ shuttleId: "sh1", stopId: "st2", secondsRemaining: 180, systemId: "sys1", updatedTime: new Date() },
{ shuttleId: "sh2", stopId: "st3", secondsRemaining: 240, systemId: "sys1", updatedTime: new Date() },
];
}

View File

@@ -0,0 +1,44 @@
import {
generateMockEtas,
generateMockRoutes,
generateMockShuttles,
generateMockStops,
} from "./mockDataGenerators";
import { ShuttleGetterSetterRepository } from "../src/repositories/shuttle/ShuttleGetterSetterRepository";
export async function addMockRouteToRepository(repository: ShuttleGetterSetterRepository, systemId: string) {
const mockRoutes = generateMockRoutes();
const mockRoute = mockRoutes[0];
mockRoute.systemId = systemId;
await repository.addOrUpdateRoute(mockRoute);
return mockRoute;
}
export async function addMockStopToRepository(repository: ShuttleGetterSetterRepository, systemId: string) {
const mockStops = generateMockStops();
const mockStop = mockStops[0];
mockStop.systemId = systemId;
await repository.addOrUpdateStop(mockStop);
return mockStop;
}
export async function addMockShuttleToRepository(repository: ShuttleGetterSetterRepository, systemId: string) {
const mockShuttles = generateMockShuttles();
const mockShuttle = mockShuttles[0];
mockShuttle.systemId = systemId;
await repository.addOrUpdateShuttle(mockShuttle);
return mockShuttle;
}
export async function addMockEtaToRepository(repository: ShuttleGetterSetterRepository, stopId: string, shuttleId: string) {
const etas = generateMockEtas();
const expectedEta = etas[0];
expectedEta.stopId = stopId;
expectedEta.shuttleId = shuttleId;
await repository.addOrUpdateEta(expectedEta);
return expectedEta;
}