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,213 @@
import { afterEach, beforeEach, describe, expect, it, jest } from "@jest/globals";
import { InMemoryNotificationRepository } from "../InMemoryNotificationRepository";
import { NotificationEvent, NotificationRepository } from "../NotificationRepository";
import { RedisNotificationRepository } from "../RedisNotificationRepository";
interface RepositoryHolder {
name: string;
factory(): Promise<NotificationRepository>,
teardown(): Promise<void>,
}
class InMemoryRepositoryHolder implements RepositoryHolder {
name = 'InMemoryNotificationRepository';
factory = async () => {
return new InMemoryNotificationRepository();
}
teardown = async () => {}
}
class RedisNotificationRepositoryHolder implements RepositoryHolder {
repo: RedisNotificationRepository | undefined;
name = 'RedisNotificationRepository';
factory = async () => {
this.repo = new RedisNotificationRepository();
await this.repo.connect();
return this.repo;
}
teardown = async () => {
if (this.repo) {
await this.repo.clearAllData();
await this.repo.disconnect();
}
}
}
const repositoryImplementations = [
new InMemoryRepositoryHolder(),
new RedisNotificationRepositoryHolder(),
]
describe.each(repositoryImplementations)('$name', (holder) => {
let repo: NotificationRepository;
beforeEach(async () => {
repo = await holder.factory();
});
afterEach(async () => {
await holder.teardown();
})
const notification = {
deviceId: "device1",
shuttleId: "shuttle1",
stopId: "stop1",
secondsThreshold: 180
};
describe("getAllNotificationsForShuttleAndStopId", () => {
it("gets notifications correctly", async () => {
await repo.addOrUpdateNotification(notification);
const result = await repo.getAllNotificationsForShuttleAndStopId("shuttle1", "stop1");
expect(result).toHaveLength(1);
expect(result[0]).toEqual(notification);
});
it("returns empty array if no notifications", async () => {
const result = await repo.getAllNotificationsForShuttleAndStopId("shuttle1", "stop1");
expect(result).toEqual([]);
});
});
describe("getSecondsThresholdForNotificationIfExists", () => {
it("gets the seconds threshold if exists", async () => {
await repo.addOrUpdateNotification(notification);
const result = await repo.getSecondsThresholdForNotificationIfExists({
deviceId: "device1",
shuttleId: "shuttle1",
stopId: "stop1"
});
expect(result).toBe(180);
});
it("returns null if there is no seconds threshold", async () => {
const result = await repo.getSecondsThresholdForNotificationIfExists({
deviceId: "device1",
shuttleId: "shuttle1",
stopId: "stop1"
});
expect(result).toBeNull();
});
});
describe("addOrUpdateNotification", () => {
// Add/get flow is covered in getAllNotificationsForShuttleAndStopId
it("updates the seconds threshold if the notification exists already", async () => {
await repo.addOrUpdateNotification(notification);
await repo.addOrUpdateNotification({...notification, secondsThreshold: 300});
const result = await repo.getSecondsThresholdForNotificationIfExists({
deviceId: "device1",
shuttleId: "shuttle1",
stopId: "stop1"
});
expect(result).toBe(300);
});
});
describe("deleteNotificationIfExists", () => {
it("deletes the notification", async () => {
await repo.addOrUpdateNotification(notification);
await repo.deleteNotificationIfExists(notification);
const result = await repo.getAllNotificationsForShuttleAndStopId("shuttle1", "stop1");
expect(result).toHaveLength(0);
});
it("does nothing if there's no notification", async () => {
await expect(repo.deleteNotificationIfExists({
deviceId: "device1",
shuttleId: "shuttle1",
stopId: "stop1"
})).resolves.not.toThrow();
});
});
describe("subscribeToNotificationChanges", () => {
it("calls subscribers when something is added", async () => {
const mockCallback = jest.fn();
repo.subscribeToNotificationChanges(mockCallback);
await repo.addOrUpdateNotification(notification);
const expectedEvent: NotificationEvent = {
event: 'addOrUpdate',
notification,
}
expect(mockCallback).toHaveBeenCalledTimes(1);
expect(mockCallback).toHaveBeenCalledWith(expectedEvent);
});
it("calls subscribers when something is updated", async () => {
const mockCallback = jest.fn();
repo.subscribeToNotificationChanges(mockCallback);
await repo.addOrUpdateNotification(notification);
const updatedNotification = {
...notification,
secondsThreshold: notification.secondsThreshold + 60,
};
await repo.addOrUpdateNotification(updatedNotification);
const expectedEvent: NotificationEvent = {
event: 'addOrUpdate',
notification,
}
expect(mockCallback).toHaveBeenCalledTimes(2);
expect(mockCallback).toHaveBeenCalledWith(expectedEvent);
});
it("calls subscribers when something is deleted", async () => {
await repo.addOrUpdateNotification(notification);
const mockCallback = jest.fn();
repo.subscribeToNotificationChanges(mockCallback);
await repo.deleteNotificationIfExists(notification);
expect(mockCallback).toHaveBeenCalledTimes(1);
const expectedEvent: NotificationEvent = {
event: 'delete',
notification,
};
expect(mockCallback).toHaveBeenCalledWith(expectedEvent);
});
});
describe("unsubscribeFromNotificationChanges", () => {
it("stops calling subscribers when unsubscribed", async () => {
const mockCallback = jest.fn();
repo.subscribeToNotificationChanges(mockCallback);
await repo.addOrUpdateNotification(notification);
repo.unsubscribeFromNotificationChanges(mockCallback);
await repo.deleteNotificationIfExists(notification);
expect(mockCallback).toHaveBeenCalledTimes(1);
});
});
describe("isNotificationScheduled", () => {
it("returns true if the notification is in the repo", async () => {
await repo.addOrUpdateNotification(notification);
const result = await repo.isNotificationScheduled(notification);
expect(result).toBe(true);
});
it("returns false if the notification isn't in the repo", async () => {
const result = await repo.isNotificationScheduled(notification);
expect(result).toBe(false);
})
});
});

View File

@@ -0,0 +1,205 @@
import { afterEach, beforeEach, describe, expect, it, jest } from "@jest/globals";
import { InMemoryParkingRepository, } from "../InMemoryParkingRepository";
import { IParkingStructure } from "../../../entities/ParkingRepositoryEntities";
import { HistoricalParkingAverageQueryArguments } from "../ParkingGetterRepository";
import { ParkingGetterSetterRepository } from "../ParkingGetterSetterRepository";
import { RedisParkingRepository } from "../RedisParkingRepository";
interface RepositoryHolder {
name: string;
factory(): Promise<ParkingGetterSetterRepository>;
teardown(): Promise<void>;
}
class InMemoryParkingRepositoryHolder implements RepositoryHolder {
name = 'InMemoryParkingRepository';
factory = async () => {
return new InMemoryParkingRepository();
};
teardown = async () => {};
}
class RedisParkingRepositoryHolder implements RepositoryHolder {
repo: RedisParkingRepository | undefined;
name = 'RedisParkingRepository';
factory = async () => {
this.repo = new RedisParkingRepository();
await this.repo.connect();
return this.repo;
};
teardown = async () => {
if (this.repo) {
await this.repo.clearAllData();
await this.repo.disconnect();
}
};
}
const repositoryImplementations = [
new InMemoryParkingRepositoryHolder(),
new RedisParkingRepositoryHolder(),
];
describe.each(repositoryImplementations)('$name', (holder) => {
let repository: ParkingGetterSetterRepository;
const testStructure: IParkingStructure = {
coordinates: {
latitude: 33.794795,
longitude: -117.850807,
},
spotsAvailable: 0,
id: "1",
name: "Anderson Parking Structure",
capacity: 100,
address: "300 E Walnut Ave, Orange, CA 92867",
updatedTime: new Date(),
};
beforeEach(async () => {
repository = await holder.factory();
jest.useRealTimers();
});
afterEach(async () => {
await holder.teardown();
jest.useRealTimers();
});
describe("addOrUpdateParkingStructure", () => {
it("should add a new parking structure", async () => {
await repository.addOrUpdateParkingStructure(testStructure);
const result = await repository.getParkingStructureById(testStructure.id);
expect(result).toEqual(testStructure);
});
it("should update existing parking structure", async () => {
await repository.addOrUpdateParkingStructure(testStructure);
const updatedStructure = { ...testStructure, name: "Updated Garage" };
await repository.addOrUpdateParkingStructure(updatedStructure);
const result = await repository.getParkingStructureById(testStructure.id);
expect(result).toEqual(updatedStructure);
});
});
describe("removeParkingStructureIfExists", () => {
it("should remove existing parking structure and return it", async () => {
await repository.addOrUpdateParkingStructure(testStructure);
const removed = await repository.removeParkingStructureIfExists(testStructure.id);
expect(removed).toEqual(testStructure);
const result = await repository.getParkingStructureById(testStructure.id);
expect(result).toBeNull();
});
it("should return null when removing non-existent structure", async () => {
const result = await repository.removeParkingStructureIfExists("non-existent");
expect(result).toBeNull();
});
});
describe("clearParkingStructureData", () => {
it("should remove all parking structures", async () => {
const structures = [
testStructure,
{ ...testStructure, id: "test-id-2", name: "Second Garage" }
];
for (const structure of structures) {
await repository.addOrUpdateParkingStructure(structure);
}
await repository.clearParkingStructureData();
const result = await repository.getParkingStructures();
expect(result).toHaveLength(0);
});
});
describe("getParkingStructures", () => {
it("should return empty array when no structures exist", async () => {
const result = await repository.getParkingStructures();
expect(result).toEqual([]);
});
it("should return all added structures", async () => {
const structures = [
testStructure,
{ ...testStructure, id: "test-id-2", name: "Second Garage" }
];
for (const structure of structures) {
await repository.addOrUpdateParkingStructure(structure);
}
const result = await repository.getParkingStructures();
expect(result).toHaveLength(2);
expect(result).toEqual(expect.arrayContaining(structures));
});
});
describe("getParkingStructureById", () => {
it("should return null for non-existent structure", async () => {
const result = await repository.getParkingStructureById("non-existent");
expect(result).toBeNull();
});
it("should return structure by id", async () => {
await repository.addOrUpdateParkingStructure(testStructure);
const result = await repository.getParkingStructureById(testStructure.id);
expect(result).toEqual(testStructure);
});
});
describe("getHistoricalAveragesOfParkingStructureCounts", () => {
it("should return empty array for non-existent structure or no data", async () => {
const options: HistoricalParkingAverageQueryArguments = {
from: new Date(1000),
to: new Date(2000),
intervalMs: 500
};
expect(await repository.getHistoricalAveragesOfParkingStructureCounts("non-existent", options)).toEqual([]);
await repository.addOrUpdateParkingStructure(testStructure);
expect(await repository.getHistoricalAveragesOfParkingStructureCounts(testStructure.id, options)).toEqual([]);
});
it("should calculate average for one single large interval", async () => {
// Set logging interval to 0 so every update creates historical data
repository.setLoggingInterval(0);
await repository.addOrUpdateParkingStructure(testStructure);
const updates = [
{ ...testStructure, spotsAvailable: 80, updatedTime: new Date() },
{ ...testStructure, spotsAvailable: 70, updatedTime: new Date() },
{ ...testStructure, spotsAvailable: 60, updatedTime: new Date() },
];
for (let i = 0; i < updates.length; i++) {
// Ensure that different timestamps are created, even after adding the first test structure
await new Promise((resolve) => setTimeout(resolve, 200));
await repository.addOrUpdateParkingStructure(updates[i]);
}
const now = Date.now();
const options: HistoricalParkingAverageQueryArguments = {
from: new Date(now - 10000), // Look back 10 seconds
to: new Date(now + 10000), // Look forward 10 seconds
intervalMs: 20000 // Single large interval
};
const result = await repository.getHistoricalAveragesOfParkingStructureCounts(testStructure.id, options);
// Should have at least some historical data
expect(result.length).toEqual(1);
if (result.length > 0) {
expect(result[0]).toHaveProperty('from');
expect(result[0]).toHaveProperty('to');
expect(result[0].from).toBeInstanceOf(Date);
expect(result[0].to).toBeInstanceOf(Date);
expect(result[0]).toHaveProperty('averageSpotsAvailable');
expect(result[0].averageSpotsAvailable).toBeCloseTo(52.5);
}
});
});
});

View File

@@ -0,0 +1,643 @@
import { beforeEach, describe, expect, jest, test } from "@jest/globals";
import { UnoptimizedInMemoryShuttleRepository } from "../UnoptimizedInMemoryShuttleRepository";
import {
generateMockEtas,
generateMockOrderedStops,
generateMockRoutes,
generateMockShuttles,
generateMockStops,
} from "../../../../test/testHelpers/mockDataGenerators";
// For repositories created in the future, reuse core testing
// logic from here and differentiate setup (e.g. creating mocks)
// Do this by creating a function which takes a ShuttleGetterRepository
// or ShuttleGetterSetterRepository instance
describe("UnoptimizedInMemoryRepository", () => {
let repository: UnoptimizedInMemoryShuttleRepository;
beforeEach(() => {
repository = new UnoptimizedInMemoryShuttleRepository();
});
describe("getStops", () => {
test("gets all stops in the repository", async () => {
const mockStops = generateMockStops();
for (const stop of mockStops) {
await repository.addOrUpdateStop(stop);
}
const result = await repository.getStops();
expect(result).toEqual(mockStops);
});
test("returns an empty list if there are no stops for the given system ID", async () => {
const result = await repository.getStops();
expect(result).toEqual([]);
});
});
describe("getStopById", () => {
test("gets a stop by ID if it exists", async () => {
const mockStops = generateMockStops();
const mockStop = mockStops[0];
await repository.addOrUpdateStop(mockStop);
const result = await repository.getStopById("st1");
expect(result).toEqual(mockStop);
});
test("returns null if the stop does not exist", async () => {
const result = await repository.getStopById("nonexistent-stop");
expect(result).toBeNull();
});
});
describe("getRoutes", () => {
test("gets all routes for a specific system ID", async () => {
const mockRoutes = generateMockRoutes();
for (const route of mockRoutes) {
await repository.addOrUpdateRoute(route);
}
const result = await repository.getRoutes();
expect(result).toEqual(mockRoutes);
});
test("returns an empty list if there are no routes for the system ID", async () => {
const result = await repository.getRoutes();
expect(result).toEqual([]);
});
});
describe("getRouteById", () => {
test("gets a route by ID if it exists", async () => {
const mockRoutes = generateMockRoutes();
const mockRoute = mockRoutes[0];
await repository.addOrUpdateRoute(mockRoute);
const result = await repository.getRouteById("r1");
expect(result).toEqual(mockRoute);
});
test("returns null if the route does not exist", async () => {
const result = await repository.getRouteById("nonexistent-route");
expect(result).toBeNull();
});
});
describe("getShuttles", () => {
test("gets all shuttles for a specific system ID", async () => {
const mockShuttles = generateMockShuttles();
for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle);
}
const result = await repository.getShuttles();
expect(result).toEqual(mockShuttles);
});
test("returns an empty list if there are no shuttles for the system ID", async () => {
const result = await repository.getShuttles();
expect(result).toEqual([]);
});
});
describe("getShuttlesByRouteId", () => {
test("gets all shuttles for a specific route ID", async () => {
const mockShuttles = generateMockShuttles();
for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle);
}
const result = await repository.getShuttlesByRouteId("r1");
expect(result).toEqual(mockShuttles.filter((sh) => sh.routeId === "r1"));
});
test("returns an empty list if there are no shuttles for the route ID", async () => {
const result = await repository.getShuttlesByRouteId("nonexistent-route");
expect(result).toEqual([]);
});
});
describe("getShuttleById", () => {
test("gets a shuttle by ID if it exists", async () => {
const mockShuttles = generateMockShuttles();
for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle);
}
const result = await repository.getShuttleById("sh2");
expect(result).toEqual(mockShuttles[1]);
});
test("returns null if the shuttle doesn't exist", async () => {
const result = await repository.getShuttleById("nonexistent-id");
expect(result).toBeNull();
});
});
describe("getEtasForShuttleId", () => {
test("gets ETAs for a specific shuttle ID", async () => {
const mockEtas = generateMockEtas();
for (const eta of mockEtas) {
await repository.addOrUpdateEta(eta);
}
const result = await repository.getEtasForShuttleId("sh1");
expect(result).toEqual(mockEtas.filter((eta) => eta.shuttleId === "sh1"));
});
test("returns an empty list if there are no ETAs for the shuttle ID", async () => {
const result = await repository.getEtasForShuttleId("nonexistent-shuttle");
expect(result).toEqual([]);
});
});
describe("getEtasForStopId", () => {
test("gets ETAs for a specific stop ID", async () => {
const mockEtas = generateMockEtas();
for (const eta of mockEtas) {
await repository.addOrUpdateEta(eta);
}
const result = await repository.getEtasForStopId("st1");
expect(result).toEqual(mockEtas.filter((eta) => eta.stopId === "st1"));
});
test("returns an empty list if there are no ETAs for the stop ID", async () => {
const result = await repository.getEtasForStopId("nonexistent-stop");
expect(result).toEqual([]);
});
});
describe("getEtaForShuttleAndStopId", () => {
test("gets a single ETA for a specific shuttle and stop ID", async () => {
const mockEtas = generateMockEtas();
const mockEta = mockEtas[0];
await repository.addOrUpdateEta(mockEta);
const result = await repository.getEtaForShuttleAndStopId("sh1", "st1");
expect(result).toEqual(mockEta);
});
test("returns null if no ETA matches the shuttle and stop ID", async () => {
const result = await repository.getEtaForShuttleAndStopId("nonexistent-shuttle", "nonexistent-stop");
expect(result).toBeNull();
});
});
describe("subscribeToEtaChanges", () => {
test("notifies listeners if etas have been added or changed", async () => {
const mockCallback = jest.fn(); // Jest mock function to simulate a listener
repository.subscribeToEtaUpdates(mockCallback);
const mockEtas = generateMockEtas();
for (const eta of mockEtas) {
await repository.addOrUpdateEta(eta); // Trigger changes in ETAs
}
expect(mockCallback).toHaveBeenCalledTimes(mockEtas.length);
expect(mockCallback).toHaveBeenCalledWith(mockEtas[0]); // First notification
expect(mockCallback).toHaveBeenCalledWith(mockEtas[mockEtas.length - 1]); // Last notification
});
});
describe("unsubscribeFromEtaChanges", () => {
test("stops notifying listeners after etas have stopped changing", async () => {
const mockCallback = jest.fn(); // Jest mock function to simulate a listener
repository.subscribeToEtaUpdates(mockCallback);
const mockEtas = generateMockEtas();
await repository.addOrUpdateEta(mockEtas[0]);
repository.unsubscribeFromEtaUpdates(mockCallback);
await repository.addOrUpdateEta(mockEtas[mockEtas.length - 1]);
expect(mockCallback).toHaveBeenCalledTimes(1);
expect(mockCallback).toHaveBeenCalledWith(mockEtas[0]); // First notification
expect(mockCallback).not.toHaveBeenCalledWith(mockEtas[mockEtas.length - 1]); // Last notification
});
test("does nothing if the listener doesn't exist", async () => {
const mockCallback = jest.fn();
repository.subscribeToEtaUpdates(mockCallback);
const mockEtas = generateMockEtas();
repository.unsubscribeFromEtaUpdates(() => {});
await repository.addOrUpdateEta(mockEtas[0]);
expect(mockCallback).toHaveBeenCalledTimes(1);
});
});
describe("getOrderedStopByRouteAndStopId", () => {
test("gets an ordered stop by route ID and stop ID", async () => {
const mockOrderedStops = generateMockOrderedStops();
for (const orderedStop of mockOrderedStops) {
await repository.addOrUpdateOrderedStop(orderedStop);
}
const mockOrderedStop = mockOrderedStops[0];
const { routeId, stopId } = mockOrderedStop;
const result = await repository.getOrderedStopByRouteAndStopId(routeId, stopId);
expect(result).toEqual(mockOrderedStop);
});
test("returns null if no ordered stop matches the given route ID and stop ID", async () => {
const result = await repository.getOrderedStopByRouteAndStopId("nonexistent-route", "nonexistent-stop");
expect(result).toBeNull();
});
});
describe("getOrderedStopsByStopId", () => {
test("gets all ordered stops for a specific stop ID", async () => {
const mockOrderedStops = generateMockOrderedStops();
for (const orderedStop of mockOrderedStops) {
await repository.addOrUpdateOrderedStop(orderedStop);
}
const result = await repository.getOrderedStopsByStopId("st1");
expect(result).toEqual(mockOrderedStops.filter((os) => os.stopId === "st1"));
});
test("returns an empty list if there are no ordered stops for the stop ID", async () => {
const result = await repository.getOrderedStopsByStopId("nonexistent-stop");
expect(result).toEqual([]);
});
});
describe("getOrderedStopsByRouteId", () => {
test("gets all ordered stops for a specific route ID", async () => {
const mockOrderedStops = generateMockOrderedStops();
for (const orderedStop of mockOrderedStops) {
await repository.addOrUpdateOrderedStop(orderedStop);
}
const result = await repository.getOrderedStopsByRouteId("r1");
expect(result).toEqual(mockOrderedStops.filter((os) => os.routeId === "r1"));
});
test("returns an empty list if there are no ordered stops for the route ID", async () => {
const result = await repository.getOrderedStopsByRouteId("nonexistent-route");
expect(result).toEqual([]);
});
});
describe("addOrUpdateRoute", () => {
test("adds a new route if nonexistent", async () => {
const mockRoutes = generateMockRoutes();
const newRoute = mockRoutes[0];
await repository.addOrUpdateRoute(newRoute);
const result = await repository.getRoutes();
expect(result).toEqual([newRoute]);
});
test("updates an existing route if it exists", async () => {
const mockRoutes = generateMockRoutes();
const existingRoute = mockRoutes[0];
const updatedRoute = structuredClone(existingRoute);
updatedRoute.name = "Updated Route";
await repository.addOrUpdateRoute(existingRoute);
await repository.addOrUpdateRoute(updatedRoute);
const result = await repository.getRoutes();
expect(result).toEqual([updatedRoute]);
});
});
describe("addOrUpdateShuttle", () => {
test("adds a new shuttle if nonexistent", async () => {
const mockShuttles = generateMockShuttles();
const newShuttle = mockShuttles[0];
await repository.addOrUpdateShuttle(newShuttle);
const result = await repository.getShuttles();
expect(result).toEqual([newShuttle]);
});
test("updates an existing shuttle if it exists", async () => {
const mockShuttles = generateMockShuttles();
const existingShuttle = mockShuttles[0];
const updatedShuttle = structuredClone(existingShuttle);
updatedShuttle.name = "Updated Shuttle";
await repository.addOrUpdateShuttle(existingShuttle);
await repository.addOrUpdateShuttle(updatedShuttle);
const result = await repository.getShuttles();
expect(result).toEqual([updatedShuttle]);
});
});
describe("addOrUpdateStop", () => {
test("adds a new stop if nonexistent", async () => {
const mockStops = generateMockStops();
const newStop = mockStops[0];
await repository.addOrUpdateStop(newStop);
const result = await repository.getStops();
expect(result).toEqual([newStop]);
});
test("updates an existing stop if it exists", async () => {
const mockStops = generateMockStops();
const existingStop = mockStops[0];
const updatedStop = structuredClone(existingStop);
updatedStop.name = "Updated Stop";
await repository.addOrUpdateStop(existingStop);
await repository.addOrUpdateStop(updatedStop);
const result = await repository.getStops();
expect(result).toEqual([updatedStop]);
});
});
describe("addOrUpdateOrderedStop", () => {
test("adds a new ordered stop if nonexistent", async () => {
const mockOrderedStops = generateMockOrderedStops();
const newOrderedStop = mockOrderedStops[0];
await repository.addOrUpdateOrderedStop(newOrderedStop);
const result = await repository.getOrderedStopsByRouteId(newOrderedStop.routeId);
expect(result).toEqual([newOrderedStop]);
});
test("updates an existing ordered stop if it exists", async () => {
const mockOrderedStops = generateMockOrderedStops();
const existingOrderedStop = mockOrderedStops[0];
const updatedOrderedStop = structuredClone(existingOrderedStop);
updatedOrderedStop.position = 5;
await repository.addOrUpdateOrderedStop(existingOrderedStop);
await repository.addOrUpdateOrderedStop(updatedOrderedStop);
const result = await repository.getOrderedStopsByRouteId(existingOrderedStop.routeId);
expect(result).toEqual([updatedOrderedStop]);
});
});
describe("addOrUpdateEta", () => {
test("adds a new ETA if nonexistent", async () => {
const mockEtas = generateMockEtas();
const newEta = mockEtas[0];
await repository.addOrUpdateEta(newEta);
const result = await repository.getEtasForShuttleId(newEta.shuttleId);
expect(result).toEqual([newEta]);
});
test("updates an existing ETA if it exists", async () => {
const mockEtas = generateMockEtas();
const existingEta = mockEtas[0];
const updatedEta = structuredClone(existingEta);
updatedEta.secondsRemaining = existingEta.secondsRemaining + 60;
await repository.addOrUpdateEta(existingEta);
await repository.addOrUpdateEta(updatedEta);
const result = await repository.getEtasForShuttleId(existingEta.shuttleId);
expect(result).toEqual([updatedEta]);
});
});
describe("removeRouteIfExists", () => {
test("removes route given ID", async () => {
const systemId = "1";
const mockRoutes = generateMockRoutes();
await Promise.all(mockRoutes.map(async (route) => {
route.systemId = systemId;
await repository.addOrUpdateRoute(route);
}));
const routeToRemove = mockRoutes[0];
await repository.removeRouteIfExists(routeToRemove.id);
const remainingRoutes = await repository.getRoutes();
expect(remainingRoutes).toHaveLength(mockRoutes.length - 1);
});
test("does nothing if route doesn't exist", async () => {
const systemId = "1";
const mockRoutes = generateMockRoutes();
await Promise.all(mockRoutes.map(async (route) => {
route.systemId = systemId;
await repository.addOrUpdateRoute(route);
}));
await repository.removeRouteIfExists("nonexistent-id");
const remainingRoutes = await repository.getRoutes();
expect(remainingRoutes).toHaveLength(mockRoutes.length);
});
});
describe("removeShuttleIfExists", () => {
test("removes shuttle given ID", async () => {
const systemId = "1";
const mockShuttles = generateMockShuttles();
await Promise.all(mockShuttles.map(async (shuttle) => {
shuttle.systemId = systemId;
await repository.addOrUpdateShuttle(shuttle);
}));
const shuttleToRemove = mockShuttles[0];
await repository.removeShuttleIfExists(shuttleToRemove.id);
const remainingShuttles = await repository.getShuttles();
expect(remainingShuttles).toHaveLength(mockShuttles.length - 1);
});
test("does nothing if shuttle doesn't exist", async () => {
const systemId = "1";
const mockShuttles = generateMockShuttles();
await Promise.all(mockShuttles.map(async (shuttle) => {
shuttle.systemId = systemId;
await repository.addOrUpdateShuttle(shuttle);
}));
await repository.removeShuttleIfExists("nonexistent-id");
const remainingShuttles = await repository.getShuttles();
expect(remainingShuttles).toHaveLength(mockShuttles.length);
});
});
describe("removeStopIfExists", () => {
test("removes stop given ID", async () => {
const systemId = "1";
const mockStops = generateMockStops();
await Promise.all(mockStops.map(async (stop) => {
stop.systemId = systemId;
await repository.addOrUpdateStop(stop);
}));
const stopToRemove = mockStops[0];
await repository.removeStopIfExists(stopToRemove.id);
const remainingStops = await repository.getStops();
expect(remainingStops).toHaveLength(mockStops.length - 1);
});
test("does nothing if stop doesn't exist", async () => {
const systemId = "1";
const mockStops = generateMockStops();
await Promise.all(mockStops.map(async (stop) => {
stop.systemId = systemId;
await repository.addOrUpdateStop(stop);
}));
await repository.removeStopIfExists("nonexistent-id");
const remainingStops = await repository.getStops();
expect(remainingStops).toHaveLength(mockStops.length);
});
});
describe("removeOrderedStopIfExists", () => {
test("removes ordered stop given stop ID and route ID", async () => {
let mockOrderedStops = generateMockOrderedStops();
const routeId = mockOrderedStops[0].routeId;
mockOrderedStops = mockOrderedStops.filter((orderedStop) => orderedStop.routeId === routeId);
await Promise.all(mockOrderedStops.map(async (stop) => {
stop.routeId = routeId;
await repository.addOrUpdateOrderedStop(stop);
}));
const orderedStopToRemove = mockOrderedStops[0];
await repository.removeOrderedStopIfExists(orderedStopToRemove.stopId, orderedStopToRemove.routeId);
const remainingOrderedStops = await repository.getOrderedStopsByRouteId(routeId);
expect(remainingOrderedStops).toHaveLength(mockOrderedStops.length - 1);
});
test("does nothing if ordered stop doesn't exist", async () => {
let mockOrderedStops = generateMockOrderedStops();
const routeId = mockOrderedStops[0].routeId;
mockOrderedStops = mockOrderedStops.filter((orderedStop) => orderedStop.routeId === routeId);
await Promise.all(mockOrderedStops.map(async (stop) => {
stop.routeId = routeId;
await repository.addOrUpdateOrderedStop(stop);
}));
await repository.removeOrderedStopIfExists("nonexistent-stop-id", "nonexistent-route-id");
const remainingOrderedStops = await repository.getOrderedStopsByRouteId(routeId);
expect(remainingOrderedStops).toHaveLength(mockOrderedStops.length);
});
});
describe("removeEtaIfExists", () => {
test("removes eta given shuttle ID and stop ID", async () => {
let mockEtas = generateMockEtas();
const stopId = mockEtas[0].stopId;
mockEtas = mockEtas.filter((eta) => eta.stopId === stopId);
await Promise.all(mockEtas.map(async (eta) => {
eta.stopId = stopId;
await repository.addOrUpdateEta(eta);
}));
const etaToRemove = mockEtas[0];
await repository.removeEtaIfExists(etaToRemove.shuttleId, etaToRemove.stopId);
const remainingEtas = await repository.getEtasForStopId(stopId);
expect(remainingEtas).toHaveLength(mockEtas.length - 1);
});
test("does nothing if eta doesn't exist", async () => {
let mockEtas = generateMockEtas();
const stopId = mockEtas[0].stopId;
mockEtas = mockEtas.filter((eta) => eta.stopId === stopId);
await Promise.all(mockEtas.map(async (eta) => {
eta.stopId = stopId;
await repository.addOrUpdateEta(eta);
}));
await repository.removeEtaIfExists("nonexistent-shuttle-id", "nonexistent-stop-id");
const remainingEtas = await repository.getEtasForStopId(stopId);
expect(remainingEtas).toHaveLength(mockEtas.length);
});
});
describe("clearShuttleData", () => {
test("clears all shuttles from the repository", async () => {
const mockShuttles = generateMockShuttles();
for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle);
}
await repository.clearShuttleData();
const result = await repository.getShuttles();
expect(result).toEqual([]);
});
});
describe("clearEtaData", () => {
test("clears all ETAs from the repository", async () => {
const mockEtas = generateMockEtas();
for (const eta of mockEtas) {
await repository.addOrUpdateEta(eta);
}
await repository.clearEtaData();
const result = await repository.getEtasForShuttleId("shuttle1");
expect(result).toEqual([]);
});
});
describe("clearOrderedStopData", () => {
test("clears all ordered stops from the repository", async () => {
const mockOrderedStops = await generateMockOrderedStops();
for (const system of mockOrderedStops) {
await repository.addOrUpdateOrderedStop(system);
}
await repository.clearOrderedStopData();
const result = await repository.getOrderedStopsByRouteId("route1");
expect(result).toEqual([]);
});
});
describe("clearRouteData", () => {
test("clears all routes from the repository", async () => {
const mockRoutes = generateMockRoutes();
for (const route of mockRoutes) {
await repository.addOrUpdateRoute(route);
}
await repository.clearRouteData();
const result = await repository.getRoutes();
expect(result).toEqual([]);
});
});
describe("clearStopData", () => {
test("clears all stops from the repository", async () => {
const mockStops = generateMockStops();
for (const stop of mockStops) {
await repository.addOrUpdateStop(stop);
}
await repository.clearStopData();
const result = await repository.getStops();
expect(result).toEqual([]);
});
});
});