Move all tests to subdirectories underneath code to be tested

This commit is contained in:
2025-07-31 22:35:49 -04:00
parent 0fd8de13f9
commit b7299b8359
20 changed files with 79 additions and 79 deletions

View File

@@ -0,0 +1,87 @@
import { beforeEach, describe, expect, it } from "@jest/globals";
import { setupTestServerContext, setupTestServerHolder } from "../../../test/testHelpers/apolloTestServerHelpers";
import { IEta, IShuttle, IStop } from "../../entities/ShuttleRepositoryEntities";
import {
addMockEtaToRepository,
addMockShuttleToRepository,
addMockStopToRepository,
} from "../../../test/testHelpers/repositorySetupHelpers";
import assert = require("node:assert");
describe("EtaResolvers", () => {
const holder = setupTestServerHolder();
const context = setupTestServerContext();
let mockShuttle: IShuttle;
let mockStop: IStop;
let expectedEta: IEta;
beforeEach(async () => {
mockShuttle = await addMockShuttleToRepository(context.systems[0].shuttleRepository, context.systems[0].id);
mockStop = await addMockStopToRepository(context.systems[0].shuttleRepository, context.systems[0].id);
expectedEta = await addMockEtaToRepository(context.systems[0].shuttleRepository, mockStop.id, mockShuttle.id);
});
async function getResponseForEtaQuery(query: string) {
return await holder.testServer.executeOperation({
query,
variables: {
systemId: context.systems[0].id,
shuttleId: mockShuttle.id,
},
}, {
contextValue: context
});
}
describe("stop", () => {
const query = `
query GetETAStop($systemId: ID!, $shuttleId: ID!) {
system(id: $systemId) {
shuttle(id: $shuttleId) {
etas {
stop {
id
}
}
}
}
}
`;
it("returns the associated stop if it exists", async () => {
const response = await getResponseForEtaQuery(query);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const eta = (response.body.singleResult.data?.system as any).shuttle.etas[0];
expect(eta.stop.id).toEqual(expectedEta.stopId);
});
});
describe("shuttle", () => {
const query = `
query GetETAShuttle($systemId: ID!, $shuttleId: ID!) {
system(id: $systemId) {
shuttle(id: $shuttleId) {
etas {
shuttle {
id
}
}
}
}
}
`;
it("returns the associated shuttle if it exists", async () => {
const response = await getResponseForEtaQuery(query);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const eta = (response.body.singleResult.data?.system as any).shuttle.etas[0];
expect(eta.shuttle.id).toEqual(expectedEta.shuttleId);
});
});
});

View File

@@ -0,0 +1,187 @@
import { describe, expect, it } from "@jest/globals";
import { setupTestServerContext, setupTestServerHolder } from "../../../test/testHelpers/apolloTestServerHelpers";
import {
addMockShuttleToRepository,
addMockStopToRepository,
} from "../../../test/testHelpers/repositorySetupHelpers";
import assert = require("node:assert");
import { NotificationInput } from "../../generated/graphql";
describe("MutationResolvers", () => {
const holder = setupTestServerHolder()
const context = setupTestServerContext();
async function getServerResponse(query: string, notificationInput: { deviceId: string; shuttleId: string; stopId: string }) {
return await holder.testServer.executeOperation({
query,
variables: {
input: notificationInput,
}
}, {
contextValue: context
});
}
describe("scheduleNotification", () => {
const query = `
mutation ScheduleNotification($input: NotificationInput!) {
scheduleNotification(input: $input) {
success
message
data {
deviceId
shuttleId
stopId
}
}
}
`
async function assertFailedResponse(response: any, notificationInput: NotificationInput) {
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const notificationResponse = response.body.singleResult.data?.scheduleNotification as any;
expect(notificationResponse.success).toBe(false);
expect(await context.systems[0].notificationRepository.isNotificationScheduled(notificationInput)).toBe(false);
}
it("adds a notification to the notification service", async () => {
const system = context.systems[0];
const shuttle = await addMockShuttleToRepository(context.systems[0].shuttleRepository, system.id);
const stop = await addMockStopToRepository(context.systems[0].shuttleRepository, system.id);
const notificationInput = {
deviceId: "1",
shuttleId: shuttle.id,
stopId: stop.id,
secondsThreshold: 240,
};
const response = await getServerResponse(query, notificationInput);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const expectedNotificationData: any = {
...notificationInput,
}
delete expectedNotificationData.secondsThreshold;
const notificationResponse = response.body.singleResult.data?.scheduleNotification as any;
expect(notificationResponse?.success).toBe(true);
expect(notificationResponse?.data).toEqual(expectedNotificationData);
expect(await context.systems[0].notificationRepository.getSecondsThresholdForNotificationIfExists(expectedNotificationData)).toBe(240);
});
it("adds a notification with the default seconds threshold if none is provided", async () => {
const system = context.systems[0];
const shuttle = await addMockShuttleToRepository(context.systems[0].shuttleRepository, system.id);
const stop = await addMockStopToRepository(context.systems[0].shuttleRepository, system.id);
const notificationInput = {
deviceId: "1",
shuttleId: shuttle.id,
stopId: stop.id,
};
const response = await getServerResponse(query, notificationInput);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const notificationResponse = response.body.singleResult.data?.scheduleNotification as any;
expect(notificationResponse?.success).toBe(true);
expect(await context.systems[0].notificationRepository.getSecondsThresholdForNotificationIfExists(notificationInput)).toBe(180);
});
it("fails if the shuttle ID doesn't exist", async () => {
const system = context.systems[0];
const stop = await addMockStopToRepository(context.systems[0].shuttleRepository, system.id);
const notificationInput = {
deviceId: "1",
shuttleId: "1",
stopId: stop.id,
}
const response = await getServerResponse(query, notificationInput);
await assertFailedResponse(response, notificationInput);
});
it("fails if the stop ID doesn't exist", async () => {
const system = context.systems[0];
const shuttle = await addMockShuttleToRepository(context.systems[0].shuttleRepository, system.id);
const notificationInput = {
deviceId: "1",
shuttleId: shuttle.id,
stopId: "1",
}
const response = await getServerResponse(query, notificationInput);
await assertFailedResponse(response, notificationInput);
});
});
describe("cancelNotification", () => {
const query = `
mutation CancelNotification($input: NotificationInput!) {
cancelNotification(input: $input) {
success
message
data {
deviceId
shuttleId
stopId
}
}
}
`
it("removes the notification from the notification service", async () => {
const system = context.systems[0];
const shuttle = await addMockShuttleToRepository(context.systems[0].shuttleRepository, system.id);
const stop = await addMockStopToRepository(context.systems[0].shuttleRepository, system.id);
const notificationInput: any = {
deviceId: "1",
shuttleId: shuttle.id,
stopId: stop.id,
secondsThreshold: 180,
}
await context.systems[0].notificationRepository.addOrUpdateNotification(notificationInput);
const notificationLookup = {
...notificationInput
}
delete notificationLookup.secondsThreshold;
const response = await getServerResponse(query, notificationLookup);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const notificationResponse = response.body.singleResult.data?.cancelNotification as any;
expect(notificationResponse.success).toBe(true);
expect(notificationResponse.data).toEqual(notificationLookup);
expect(await context.systems[0].notificationRepository.isNotificationScheduled(notificationLookup)).toBe(false);
});
it("fails if the notification doesn't exist", async () => {
const notificationInput = {
deviceId: "1",
shuttleId: "1",
stopId: "1",
}
const response = await getServerResponse(query, notificationInput);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const notificationResponse = response.body.singleResult.data?.cancelNotification as any;
expect(notificationResponse.success).toBe(false);
});
});
});

View File

@@ -0,0 +1,283 @@
import { beforeEach, describe, expect, it } from "@jest/globals";
import { setupTestServerContext, setupTestServerHolder } from "../../../test/testHelpers/apolloTestServerHelpers";
import { IRoute, IStop } from "../../entities/ShuttleRepositoryEntities";
import { generateMockOrderedStops, generateMockStops } from "../../../test/testHelpers/mockDataGenerators";
import { addMockRouteToRepository } from "../../../test/testHelpers/repositorySetupHelpers";
import assert = require("node:assert");
describe("OrderedStopResolvers", () => {
const holder = setupTestServerHolder();
const context = setupTestServerContext();
let mockRoute: IRoute;
let mockStops: IStop[];
beforeEach(async () => {
mockRoute = await addMockRouteToRepository(context.systems[0].shuttleRepository, context.systems[0].id);
mockStops = generateMockStops();
await Promise.all(mockStops.map(async (mockStop) => {
mockStop.systemId = context.systems[0].id;
await context.systems[0].shuttleRepository.addOrUpdateStop(mockStop);
}));
});
async function setUpOrderedStopsInRepository() {
const orderedStops = generateMockOrderedStops();
// Set up IDs and link stops together to work with the test query
orderedStops[0].routeId = mockRoute.id;
orderedStops[1].routeId = mockRoute.id;
// Ensure that there is no duplication
orderedStops[0].stopId = mockStops[0].id;
orderedStops[1].stopId = mockStops[1].id;
// Link the stops together
orderedStops[0].nextStop = orderedStops[1];
orderedStops[1].previousStop = orderedStops[0];
await context.systems[0].shuttleRepository.addOrUpdateOrderedStop(orderedStops[0]);
await context.systems[0].shuttleRepository.addOrUpdateOrderedStop(orderedStops[1]);
return orderedStops;
}
describe("nextStop", () => {
async function getResponseForNextStopQuery(stopId: string) {
const query = `
query GetNextStop($systemId: ID!, $routeId: ID!, $stopId: ID!) {
system(id: $systemId) {
route(id: $routeId) {
orderedStop(forStopId: $stopId) {
nextStop {
routeId
stopId
}
}
}
}
}
`;
return await holder.testServer.executeOperation({
query,
variables: {
systemId: context.systems[0].id,
routeId: mockRoute.id,
stopId,
},
}, {
contextValue: context
});
}
it("returns the next stop if it exists", async () => {
// Arrange
const orderedStops = await setUpOrderedStopsInRepository();
// Act
const response = await getResponseForNextStopQuery(orderedStops[0].stopId);
// Assert
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const nextStop = (response.body.singleResult.data?.system as any).route.orderedStop.nextStop;
expect(nextStop.stopId).toEqual(orderedStops[1].stopId);
expect(nextStop.routeId).toEqual(orderedStops[1].routeId);
});
it("returns null if there is no next stop in the repository", async () => {
const orderedStops = await setUpOrderedStopsInRepository();
orderedStops[0].nextStop = undefined;
await context.systems[0].shuttleRepository.addOrUpdateOrderedStop(orderedStops[0]);
const response = await getResponseForNextStopQuery(orderedStops[0].stopId);
assert(response.body.kind === "single");
const nonexistentNextStop = (response.body.singleResult.data?.system as any).route.orderedStop.nextStop;
expect(nonexistentNextStop).toBeNull();
});
it("returns null if the next stop object no longer exists", async () => {
const orderedStops = await setUpOrderedStopsInRepository();
await context.systems[0].shuttleRepository.removeStopIfExists(orderedStops[1].stopId);
const response = await getResponseForNextStopQuery(orderedStops[0].stopId);
assert(response.body.kind === "single");
const nonexistentNextStop = (response.body.singleResult.data?.system as any).route.orderedStop.nextStop;
expect(nonexistentNextStop).toBeNull();
});
});
describe("previousStop", () => {
async function getResponseForPreviousStopQuery(stopId: string) {
const query = `
query GetNextStop($systemId: ID!, $routeId: ID!, $stopId: ID!) {
system(id: $systemId) {
route(id: $routeId) {
orderedStop(forStopId: $stopId) {
previousStop {
routeId
stopId
}
}
}
}
}
`;
return await holder.testServer.executeOperation({
query,
variables: {
systemId: context.systems[0].id,
routeId: mockRoute.id,
stopId,
},
}, {
contextValue: context
});
}
it("returns the previous stop if it exists", async () => {
// Arrange
const orderedStops = await setUpOrderedStopsInRepository();
// Act
const response = await getResponseForPreviousStopQuery(orderedStops[1].stopId);
// Assert
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const previousStop = (response.body.singleResult.data?.system as any).route.orderedStop.previousStop;
expect(previousStop.stopId).toEqual(orderedStops[0].stopId);
expect(previousStop.routeId).toEqual(orderedStops[0].routeId);
});
it("returns null if there is no previous stop in the repository", async () => {
const orderedStops = await setUpOrderedStopsInRepository();
orderedStops[1].previousStop = undefined;
await context.systems[0].shuttleRepository.addOrUpdateOrderedStop(orderedStops[1]);
const response = await getResponseForPreviousStopQuery(orderedStops[1].stopId);
assert(response.body.kind === "single");
const nonexistentPreviousStop = (response.body.singleResult.data?.system as any).route.orderedStop.previousStop;
expect(nonexistentPreviousStop).toBeNull();
});
it("returns null if the current stop no longer exists", async () => {
const orderedStops = await setUpOrderedStopsInRepository();
await context.systems[0].shuttleRepository.removeStopIfExists(orderedStops[0].stopId);
const response = await getResponseForPreviousStopQuery(orderedStops[1].stopId);
assert(response.body.kind === "single");
const nonexistentPreviousStop = (response.body.singleResult.data?.system as any).route.orderedStop.previousStop;
expect(nonexistentPreviousStop).toBeNull();
});
});
describe("route", () => {
// Note that there is no `orderedStop(forRouteId)` resolver,
// so fetch all ordered stops for a stop instead.
// If we went through the route ID, it would've
// relied on the parent data within the route.orderedStop resolver
async function getResponseForRouteQuery(stopId: string) {
const query = `
query GetNextStop($systemId: ID!, $stopId: ID!) {
system(id: $systemId) {
stop(id: $stopId) {
orderedStops {
route {
id
name
}
}
}
}
}
`;
return await holder.testServer.executeOperation({
query,
variables: {
systemId: context.systems[0].id,
stopId,
}
}, {
contextValue: context
});
}
it("returns the associated route if it exists", async () => {
const orderedStops = generateMockOrderedStops();
orderedStops[0].routeId = mockRoute.id;
orderedStops[0].stopId = mockStops[0].id;
// Add one stop only
await context.systems[0].shuttleRepository.addOrUpdateOrderedStop(orderedStops[0]);
const response = await getResponseForRouteQuery(orderedStops[1].stopId);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const route = (response.body.singleResult.data?.system as any).stop.orderedStops[0].route
expect(route.id).toEqual(mockRoute.id);
expect(route.name).toEqual(mockRoute.name);
});
});
describe("stop", () => {
async function getResponseForStopQuery(stopId: string) {
const query = `
query GetNextStop($systemId: ID!, $routeId: ID!, $stopId: ID!) {
system(id: $systemId) {
route(id: $routeId) {
orderedStop(forStopId: $stopId) {
stop {
name
id
}
}
}
}
}
`;
return await holder.testServer.executeOperation({
query,
variables: {
systemId: context.systems[0].id,
routeId: mockRoute.id,
stopId,
}
}, {
contextValue: context
});
}
it("returns the associated stop if it exists", async () => {
const orderedStops = await setUpOrderedStopsInRepository();
orderedStops[0].stopId = mockStops[0].id;
await context.systems[0].shuttleRepository.addOrUpdateOrderedStop(orderedStops[0]);
const response = await getResponseForStopQuery(orderedStops[0].stopId);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const stop = (response.body.singleResult.data?.system as any).route.orderedStop.stop;
expect(stop.name).toEqual(mockStops[0].name);
expect(stop.id).toEqual(mockStops[0].id);
});
});
});

View File

@@ -0,0 +1,79 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { setupTestServerContext, setupTestServerHolder } from "../../../test/testHelpers/apolloTestServerHelpers";
import { InterchangeSystem } from "../../entities/InterchangeSystem";
import { generateParkingStructures } from "../../../test/testHelpers/mockDataGenerators";
import { HistoricalParkingAverageQueryInput } from "../../generated/graphql";
import assert = require("node:assert");
jest.mock("../../environment");
describe("ParkingStructureResolver", () => {
const holder = setupTestServerHolder();
const context = setupTestServerContext();
let mockSystem: InterchangeSystem;
beforeEach(async () => {
mockSystem = context.systems[0];
jest.useRealTimers();
});
describe("historicalAverages", () => {
const query = `
query GetParkingStructureHistoricalAverages(
$systemId: ID!,
$parkingStructureId: ID!,
$historicalAverageInput: HistoricalParkingAverageQueryInput!
) {
system(id: $systemId) {
parkingSystem {
parkingStructure(id: $parkingStructureId) {
historicalAverages(input: $historicalAverageInput) {
from
to
averageSpotsAvailable
}
}
}
}
}
`;
it("gets data for historical averages", async () => {
jest.useFakeTimers();
jest.setSystemTime(new Date());
const parkingStructure = generateParkingStructures()[0];
parkingStructure.spotsAvailable = parkingStructure.capacity;
mockSystem.parkingRepository?.setLoggingInterval(100);
// Simulate repeated updates
for (let i = 0; i < 6; i += 1) {
jest.setSystemTime(new Date(Date.now() + 1000));
parkingStructure.spotsAvailable = parkingStructure.spotsAvailable - 100;
await mockSystem.parkingRepository?.addOrUpdateParkingStructure(parkingStructure);
}
const historicalAverageInput: HistoricalParkingAverageQueryInput = {
from: new Date(Date.now() - 5000).getTime(),
intervalMs: 2000,
to: new Date().getTime(),
};
const response = await holder.testServer.executeOperation({
query,
variables: {
systemId: mockSystem.id,
parkingStructureId: parkingStructure.id,
historicalAverageInput,
},
}, {
contextValue: context,
});
assert(response.body.kind === 'single');
expect(response.body.singleResult.errors).toBeUndefined();
const historicalAverages = (response.body.singleResult.data as any).system.parkingSystem.parkingStructure.historicalAverages;
expect(historicalAverages).toHaveLength(3);
});
});
});

View File

@@ -0,0 +1,157 @@
import { beforeEach, describe, expect, it } from "@jest/globals";
import { generateParkingStructures } from "../../../test/testHelpers/mockDataGenerators";
import { setupTestServerContext, setupTestServerHolder } from "../../../test/testHelpers/apolloTestServerHelpers";
import { InterchangeSystem } from "../../entities/InterchangeSystem";
import assert = require("node:assert");
describe("ParkingSystemResolver", () => {
const holder = setupTestServerHolder();
const context = setupTestServerContext();
let mockSystem: InterchangeSystem;
beforeEach(async () => {
mockSystem = context.systems[0];
});
async function getResponseFromQueryNeedingSystemId(query: string) {
return await holder.testServer.executeOperation({
query,
variables: {
systemId: mockSystem.id,
},
}, {
contextValue: context,
});
}
describe("parkingStructures", () => {
const query = `
query GetParkingStructuresBySystem($systemId: ID!) {
system(id: $systemId) {
parkingSystem {
parkingStructures {
name
id
capacity
spotsAvailable
coordinates {
latitude
longitude
}
address
updatedTime
}
}
}
}
`
it("gets parking structures associated with the system id", async () => {
let expectedParkingStructures = generateParkingStructures();
await Promise.all(expectedParkingStructures.map(async (structure) => {
await context.systems[0].parkingRepository?.addOrUpdateParkingStructure(structure);
}));
// Dates are transformed into epoch timestamps when serialized
expectedParkingStructures = expectedParkingStructures.map((structure) => {
const newStructure = { ...structure };
// @ts-ignore
newStructure.updatedTime = newStructure.updatedTime.getTime();
return newStructure;
});
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.parkingSystem.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.parkingSystem.parkingStructures;
expect(parkingStructures).toHaveLength(0);
});
});
describe("parkingStructure", () => {
async function getResponseForParkingStructureQuery(parkingStructureId: string) {
const query = `
query GetParkingStructureBySystem($systemId: ID!, $parkingStructureId: ID!) {
system(id: $systemId) {
parkingSystem {
parkingStructure(id: $parkingStructureId) {
name
id
capacity
spotsAvailable
coordinates {
latitude
longitude
}
address
updatedTime
}
}
}
}
`;
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];
// @ts-ignore
expectedParkingStructure.updatedTime = expectedParkingStructure.updatedTime.getTime();
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.parkingSystem.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.parkingSystem.parkingStructure;
expect(parkingStructure).toBeNull();
});
});
});

View File

@@ -0,0 +1,165 @@
import { describe, expect, it } from "@jest/globals";
import {
buildSystemForTesting,
setupTestServerContext,
setupTestServerHolder
} from "../../../test/testHelpers/apolloTestServerHelpers";
import assert = require("node:assert");
import { addMockShuttleToRepository, addMockStopToRepository } from "../../../test/testHelpers/repositorySetupHelpers";
import { ScheduledNotification } from "../../repositories/notifications/NotificationRepository";
// See Apollo documentation for integration test guide
// https://www.apollographql.com/docs/apollo-server/testing/testing
describe("QueryResolvers", () => {
const holder = setupTestServerHolder();
const context = setupTestServerContext();
describe("systems", () => {
it("returns systems from the repository", async () => {
const systems = context.systems;
const query = `
query GetSystems
{
systems {
name
}
}
`;
const response = await holder.testServer.executeOperation({
query,
}, {
contextValue: context
});
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.systems).toHaveLength(systems.length);
});
});
describe("system", () => {
const query = `
query GetSystem($id: ID!)
{
system(id: $id) {
name
}
}
`;
it("returns a system for an ID from the repository", async () => {
context.systems = [
buildSystemForTesting(),
buildSystemForTesting(),
];
context.findSystemById = (_: string) => context.systems[1];
context.systems[1].id = "test-id";
const systems = context.systems;
const systemToGet = systems[1];
const response = await holder.testServer.executeOperation({
query,
variables: {
id: systemToGet.id,
}
}, {
contextValue: context
});
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.system).toBeDefined();
});
it("returns null if there is no system", async () => {
context.findSystemById = (_: string) => null;
const response = await holder.testServer.executeOperation({
query,
variables: {
id: "nonexistent-id",
}
}, {
contextValue: context
});
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.system).toBeNull();
});
});
describe("isNotificationScheduled and secondsThresholdForNotification", () => {
const query = `
query IsNotificationScheduled($input: NotificationInput!) {
isNotificationScheduled(input: $input)
secondsThresholdForNotification(input: $input)
}
`;
it("returns correct data if the notification is scheduled", async () => {
// Arrange
const shuttle = await addMockShuttleToRepository(context.systems[0].shuttleRepository, "1");
const stop = await addMockStopToRepository(context.systems[0].shuttleRepository, "1")
const notification: ScheduledNotification = {
shuttleId: shuttle.id,
stopId: stop.id,
deviceId: "1",
secondsThreshold: 240,
};
await context.systems[0].notificationRepository.addOrUpdateNotification(notification);
const notificationLookup: any = {
...notification,
}
delete notificationLookup.secondsThreshold;
// Act
const response = await holder.testServer.executeOperation({
query,
variables: {
input: notificationLookup,
}
}, {
contextValue: context,
});
// Assert
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.secondsThresholdForNotification).toEqual(240);
expect(response.body.singleResult.data?.isNotificationScheduled).toBe(true);
});
it("returns false/null data if the notification isn't scheduled", async () => {
// Act
const response = await holder.testServer.executeOperation({
query,
variables: {
input: {
shuttleId: "1",
stopId: "1",
deviceId: "1",
},
}
}, {
contextValue: context,
});
// Assert
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect(response.body.singleResult.data?.isNotificationScheduled).toBe(false);
expect(response.body.singleResult.data?.secondsThresholdForNotification).toBe(null);
});
});
});

View File

@@ -0,0 +1,147 @@
import { beforeEach, describe, expect, it } from "@jest/globals";
import { setupTestServerContext, setupTestServerHolder } from "../../../test/testHelpers/apolloTestServerHelpers";
import {
addMockRouteToRepository,
addMockStopToRepository
} from "../../../test/testHelpers/repositorySetupHelpers";
import { generateMockOrderedStops, generateMockShuttles } from "../../../test/testHelpers/mockDataGenerators";
import { IRoute, IStop } from "../../entities/ShuttleRepositoryEntities";
import assert = require("node:assert");
import { InterchangeSystem } from "../../entities/InterchangeSystem";
describe("RouteResolvers", () => {
const holder = setupTestServerHolder();
const context = setupTestServerContext();
let mockSystem: InterchangeSystem;
let mockRoute: IRoute;
let mockStop: IStop;
beforeEach(async () => {
mockSystem = context.systems[0];
const systemId = mockSystem.id;
mockRoute = await addMockRouteToRepository(context.systems[0].shuttleRepository, systemId);
mockStop = await addMockStopToRepository(context.systems[0].shuttleRepository, systemId);
});
describe("shuttles", () => {
async function getResponseForShuttlesQuery() {
const query = `
query GetRouteShuttles($systemId: ID!, $routeId: ID!) {
system(id: $systemId) {
route(id: $routeId) {
shuttles {
id
name
}
}
}
}
`;
return await holder.testServer.executeOperation({
query,
variables: {
systemId: mockSystem.id,
routeId: mockRoute.id,
},
}, {
contextValue: context
});
}
it("returns shuttle array if there are shuttles", async () => {
const expectedShuttles = generateMockShuttles();
const expectedShuttle = expectedShuttles[0];
expectedShuttle.systemId = mockSystem.id;
expectedShuttle.routeId = mockRoute.id;
await context.systems[0].shuttleRepository.addOrUpdateShuttle(expectedShuttle);
const response = await getResponseForShuttlesQuery();
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined()
const shuttle = (response.body.singleResult.data as
any).system.route.shuttles[0];
expect(shuttle.id).toEqual(expectedShuttle.id);
expect(shuttle.name).toEqual(expectedShuttle.name);
});
it("returns empty array if there are no shuttles", async () => {
const response = await getResponseForShuttlesQuery();
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined()
const shuttles = (response.body.singleResult.data as
any).system.route.shuttles;
expect(shuttles.length).toEqual(0);
});
});
describe("orderedStop", () => {
async function getResponseForOrderedStopQuery() {
const query = `
query GetRouteOrderedStop($systemId: ID!, $routeId: ID!, $stopId: ID!) {
system(id: $systemId) {
route(id: $routeId) {
orderedStop(forStopId: $stopId) {
stopId
}
}
}
}
`;
return await holder.testServer.executeOperation({
query,
variables: {
systemId: mockSystem.id,
routeId: mockRoute.id,
stopId: mockStop.id,
}
}, {
contextValue: context
});
}
it("returns ordered stop using provided data", async () => {
const orderedStops = generateMockOrderedStops();
const expectedOrderedStop = orderedStops[0];
expectedOrderedStop.stopId = mockStop.id;
expectedOrderedStop.routeId = mockRoute.id;
await context.systems[0].shuttleRepository.addOrUpdateOrderedStop(expectedOrderedStop);
const response = await getResponseForOrderedStopQuery();
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const orderedStop = (response.body.singleResult.data as
any).system.route.orderedStop;
expect(orderedStop.stopId).toEqual(expectedOrderedStop.stopId);
});
it("returns null if the stop doesn't exist", async () => {
const orderedStops = generateMockOrderedStops();
const expectedOrderedStop = orderedStops[0];
expectedOrderedStop.stopId = mockStop.id;
expectedOrderedStop.routeId = mockRoute.id;
await context.systems[0].shuttleRepository.addOrUpdateOrderedStop(expectedOrderedStop);
await context.systems[0].shuttleRepository.removeStopIfExists(mockStop.id);
const response = await getResponseForOrderedStopQuery();
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
const orderedStop = (response.body.singleResult.data as
any).system.route.orderedStop;
expect(orderedStop).toBeNull();
});
});
});

View File

@@ -0,0 +1,197 @@
import { beforeEach, describe, expect, it } from "@jest/globals";
import { generateMockEtas, generateMockRoutes } from "../../../test/testHelpers/mockDataGenerators";
import { IShuttle } from "../../entities/ShuttleRepositoryEntities";
import { setupTestServerContext, setupTestServerHolder } from "../../../test/testHelpers/apolloTestServerHelpers";
import { addMockShuttleToRepository } from "../../../test/testHelpers/repositorySetupHelpers";
import assert = require("node:assert");
import { InterchangeSystem } from "../../entities/InterchangeSystem";
describe("ShuttleResolvers", () => {
const holder = setupTestServerHolder();
const context = setupTestServerContext();
let mockSystem: InterchangeSystem;
let mockShuttle: IShuttle;
beforeEach(async () => {
mockSystem = context.systems[0];
mockShuttle = await addMockShuttleToRepository(context.systems[0].shuttleRepository,
mockSystem.id);
});
async function addMockEtas(shuttleId: string) {
const etas = generateMockEtas();
await Promise.all(etas.map(async (eta) => {
eta.shuttleId = shuttleId;
await context.systems[0].shuttleRepository.addOrUpdateEta(eta);
}));
return etas;
}
describe("eta", () => {
const query = `
query GetShuttleETAs($systemId: ID!, $shuttleId: ID!, $stopId: ID!)
{
system(id: $systemId) {
shuttle(id: $shuttleId) {
eta(forStopId: $stopId) {
secondsRemaining
}
}
}
}
`
it("returns ETA data for stop ID if exists", async () => {
const etas = await addMockEtas(mockShuttle.id);
const mockEta = etas[1];
// Act
const response = await holder.testServer.executeOperation({
query,
variables: {
systemId: mockSystem.id,
shuttleId: mockShuttle.id,
stopId: mockEta.stopId,
},
}, {
contextValue: context
});
// Assert
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect((response.body.singleResult.data as
any).system.shuttle.eta.secondsRemaining).toEqual(mockEta.secondsRemaining);
});
it("returns null if it doesn't exist", async () => {
const response = await holder.testServer.executeOperation({
query,
variables: {
systemId: mockSystem.id,
shuttleId: mockShuttle.id,
stopId: "nonexistent-stop",
}
}, {
contextValue: context
});
// Assert
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect((response.body.singleResult.data as
any).system.shuttle.eta).toBeNull();
});
});
describe("etas", () => {
const query = `
query GetShuttleETAs($systemId: ID!, $shuttleId: ID!)
{
system(id: $systemId) {
shuttle(id: $shuttleId) {
etas {
secondsRemaining
}
}
}
}
`
it("returns associated ETAs if they exist for the shuttle", async () => {
const etas = await addMockEtas(mockShuttle.id);
const response = await holder.testServer.executeOperation({
query,
variables: {
systemId: mockSystem.id,
shuttleId: mockShuttle.id,
},
}, {
contextValue: context
});
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect((response.body.singleResult.data as
any).system.shuttle.etas).toHaveLength(etas.length);
});
it("returns empty array if no ETAs exist", async () => {
const response = await holder.testServer.executeOperation({
query,
variables: {
systemId: mockSystem.id,
shuttleId: mockShuttle.id,
},
}, {
contextValue: context
});
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect((response.body.singleResult.data as
any).system.shuttle.etas).toHaveLength(0);
});
});
describe("route", () => {
const query = `
query GetShuttleRoute($systemId: ID!, $shuttleId: ID!) {
system(id: $systemId) {
shuttle(id: $shuttleId) {
route {
color
id
name
polylineCoordinates {
latitude
longitude
}
}
}
}
}
`
async function getResponseForQuery() {
return await holder.testServer.executeOperation({
query,
variables: {
systemId: mockSystem.id,
shuttleId: mockShuttle.id,
}
}, {
contextValue: context
});
}
it("returns the route if it exists", async () => {
const mockRoute = generateMockRoutes()[0];
await context.systems[0].shuttleRepository.addOrUpdateRoute(mockRoute);
const response = await getResponseForQuery();
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect((response.body.singleResult.data as any).system.shuttle.route.id).toEqual(mockRoute.id);
});
it("returns null if there is no route", async () => {
const response = await getResponseForQuery();
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect((response.body.singleResult.data as any).system.shuttle.route).toBeNull();
});
});
});

View File

@@ -0,0 +1,108 @@
import { beforeEach, describe, expect, it } from "@jest/globals";
import {
setupTestServerContext,
setupTestServerHolder
} from "../../../test/testHelpers/apolloTestServerHelpers";
import { generateMockEtas, generateMockOrderedStops } from "../../../test/testHelpers/mockDataGenerators";
import { IStop } from "../../entities/ShuttleRepositoryEntities";
import { addMockStopToRepository } from "../../../test/testHelpers/repositorySetupHelpers";
import assert = require("node:assert");
describe("StopResolvers", () => {
const holder = setupTestServerHolder();
const context = setupTestServerContext();
let mockStop: IStop;
beforeEach(async () => {
mockStop = await addMockStopToRepository(context.systems[0].shuttleRepository, context.systems[0].id);
})
async function getResponseForQuery(query: string) {
return await holder.testServer.executeOperation({
query,
variables: {
systemId: context.systems[0].id,
stopId: mockStop.id,
},
}, {
contextValue: context,
});
}
describe("orderedStops", () => {
const query = `
query GetOrderedStops($systemId: ID!, $stopId: ID!) {
system(id: $systemId) {
stop(id: $stopId) {
orderedStops {
routeId
stopId
}
}
}
}
`
it("returns ordered stops if they exist for the stop ID", async () => {
let mockOrderedStops = generateMockOrderedStops();
mockOrderedStops = mockOrderedStops.filter((orderedStop) => orderedStop.stopId === mockOrderedStops[0].stopId);
await Promise.all(mockOrderedStops.map(async orderedStop => {
orderedStop.stopId = mockStop.id;
await context.systems[0].shuttleRepository.addOrUpdateOrderedStop(orderedStop);
}));
const response = await getResponseForQuery(query);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect((response.body.singleResult.data as any).system.stop.orderedStops).toHaveLength(mockOrderedStops.length);
});
it("returns empty array if no ordered stops exist", async () => {
const response = await getResponseForQuery(query);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect((response.body.singleResult.data as any).system.stop.orderedStops).toHaveLength(0);
});
});
describe("etas", () => {
const query = `
query GetEtas($systemId: ID!, $stopId: ID!) {
system(id: $systemId) {
stop(id: $stopId) {
etas {
secondsRemaining
}
}
}
}
`
it("returns ETAs if they exist for the stop ID", async () => {
let mockEtas = generateMockEtas();
mockEtas = mockEtas.filter((eta) => eta.stopId === mockEtas[0].stopId);
await Promise.all(mockEtas.map(async eta => {
eta.stopId = mockStop.id;
await context.systems[0].shuttleRepository.addOrUpdateEta(eta);
}));
const response = await getResponseForQuery(query);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect((response.body.singleResult.data as any).system.stop.etas).toHaveLength(mockEtas.length);
});
it("returns empty array if no ETAs exist", async () => {
const response = await getResponseForQuery(query);
assert(response.body.kind === "single");
expect(response.body.singleResult.errors).toBeUndefined();
expect((response.body.singleResult.data as any).system.stop.etas).toHaveLength(0);
});
});
});

View File

@@ -0,0 +1,317 @@
import { beforeEach, describe, expect, it } from "@jest/globals";
import { setupTestServerContext, setupTestServerHolder } from "../../../test/testHelpers/apolloTestServerHelpers";
import {
generateMockRoutes,
generateMockShuttles,
generateMockStops,
generateParkingStructures
} from "../../../test/testHelpers/mockDataGenerators";
import {
addMockRouteToRepository,
addMockShuttleToRepository,
addMockStopToRepository,
} from "../../../test/testHelpers/repositorySetupHelpers";
import assert = require("node:assert");
import { InterchangeSystem } from "../../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);
});
});
});