Move duplicate shuttle declarations in tests to a "sample" shuttle generated by the setup function

This commit is contained in:
2025-11-18 19:50:07 -08:00
parent f334054b5e
commit 47708d050e
3 changed files with 36 additions and 136 deletions

View File

@@ -565,17 +565,7 @@ describe.each(repositoryImplementations)('$name', (holder) => {
describe("addOrUpdateShuttle with shuttle tracking", () => { describe("addOrUpdateShuttle with shuttle tracking", () => {
test("updates the shuttle's last stop arrival if shuttle is at a stop", async () => { test("updates the shuttle's last stop arrival if shuttle is at a stop", async () => {
const { route, systemId, stop2 } = await setupRouteAndOrderedStops(); const { stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
const shuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop2.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
await repository.addOrUpdateShuttle(shuttle); await repository.addOrUpdateShuttle(shuttle);
const lastStop = await repository.getShuttleLastStopArrival(shuttle.id); const lastStop = await repository.getShuttleLastStopArrival(shuttle.id);
@@ -585,17 +575,7 @@ describe.each(repositoryImplementations)('$name', (holder) => {
describe("getArrivedStopIfExists", () => { describe("getArrivedStopIfExists", () => {
test("gets the stop that the shuttle is currently at, if exists", async () => { test("gets the stop that the shuttle is currently at, if exists", async () => {
const { route, systemId, stop2 } = await setupRouteAndOrderedStops(); const { sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
const shuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop2.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
const result = await repository.getArrivedStopIfExists(shuttle); const result = await repository.getArrivedStopIfExists(shuttle);
@@ -605,17 +585,8 @@ describe.each(repositoryImplementations)('$name', (holder) => {
}); });
test("returns undefined if shuttle is not currently at a stop", async () => { test("returns undefined if shuttle is not currently at a stop", async () => {
const { route, systemId } = await setupRouteAndOrderedStops(); const { sampleShuttleNotInRepository } = await setupRouteAndOrderedStops();
const shuttle = { ...sampleShuttleNotInRepository, coordinates: { latitude: 12.5, longitude: 22.5 } }; // Not at any stop
const shuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: { latitude: 12.5, longitude: 22.5 },
orientationInDegrees: 0,
updatedTime: new Date(),
};
const result = await repository.getArrivedStopIfExists(shuttle); const result = await repository.getArrivedStopIfExists(shuttle);
@@ -625,17 +596,8 @@ describe.each(repositoryImplementations)('$name', (holder) => {
describe("getShuttleLastStopArrival", () => { describe("getShuttleLastStopArrival", () => {
test("gets the shuttle's last stop if existing in the data", async () => { test("gets the shuttle's last stop if existing in the data", async () => {
const { route, systemId, stop1 } = await setupRouteAndOrderedStops(); const { stop1, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
shuttle.coordinates = stop1.coordinates;
const shuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
const stopArrivalTime = new Date("2024-01-15T10:30:00Z"); const stopArrivalTime = new Date("2024-01-15T10:30:00Z");
await repository.addOrUpdateShuttle(shuttle, stopArrivalTime.getTime()); await repository.addOrUpdateShuttle(shuttle, stopArrivalTime.getTime());
@@ -657,17 +619,8 @@ describe.each(repositoryImplementations)('$name', (holder) => {
}); });
test("returns the most recent stop arrival when updated multiple times", async () => { test("returns the most recent stop arrival when updated multiple times", async () => {
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops(); const { stop1, stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
shuttle.coordinates = stop1.coordinates;
const shuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
const firstArrivalTime = new Date("2024-01-15T10:30:00Z"); const firstArrivalTime = new Date("2024-01-15T10:30:00Z");
await repository.addOrUpdateShuttle(shuttle, firstArrivalTime.getTime()); await repository.addOrUpdateShuttle(shuttle, firstArrivalTime.getTime());
@@ -750,20 +703,12 @@ describe.each(repositoryImplementations)('$name', (holder) => {
describe("SHUTTLE_WILL_ARRIVE_AT_STOP event", () => { describe("SHUTTLE_WILL_ARRIVE_AT_STOP event", () => {
test("emits SHUTTLE_WILL_ARRIVE_AT_STOP event before shuttle arrives at a stop", async () => { test("emits SHUTTLE_WILL_ARRIVE_AT_STOP event before shuttle arrives at a stop", async () => {
const { route, systemId, stop1 } = await setupRouteAndOrderedStops(); const { stop1, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
const listener = jest.fn(); const listener = jest.fn();
repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener); repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener);
const shuttle = { shuttle.coordinates = stop1.coordinates;
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
const arrivalTime = new Date("2024-01-15T10:30:00Z"); const arrivalTime = new Date("2024-01-15T10:30:00Z");
await repository.addOrUpdateShuttle(shuttle, arrivalTime.getTime()); await repository.addOrUpdateShuttle(shuttle, arrivalTime.getTime());
@@ -778,20 +723,12 @@ describe.each(repositoryImplementations)('$name', (holder) => {
}); });
test("does not emit event when shuttle is not at a stop", async () => { test("does not emit event when shuttle is not at a stop", async () => {
const { route, systemId } = await setupRouteAndOrderedStops(); const { sampleShuttleNotInRepository } = await setupRouteAndOrderedStops();
const listener = jest.fn(); const listener = jest.fn();
repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener); repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener);
const shuttle = { const shuttle = { ...sampleShuttleNotInRepository, coordinates: { latitude: 12.5, longitude: 22.5 } }; // Not at any stop
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: { latitude: 12.5, longitude: 22.5 }, // Not at any stop
orientationInDegrees: 0,
updatedTime: new Date(),
};
await repository.addOrUpdateShuttle(shuttle); await repository.addOrUpdateShuttle(shuttle);
@@ -799,20 +736,12 @@ describe.each(repositoryImplementations)('$name', (holder) => {
}); });
test("emits multiple events as shuttle visits multiple stops", async () => { test("emits multiple events as shuttle visits multiple stops", async () => {
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops(); const { stop1, stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
const listener = jest.fn(); const listener = jest.fn();
repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener); repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener);
const shuttle = { shuttle.coordinates = stop1.coordinates;
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
const firstArrivalTime = new Date("2024-01-15T10:30:00Z"); const firstArrivalTime = new Date("2024-01-15T10:30:00Z");
await repository.addOrUpdateShuttle(shuttle, firstArrivalTime.getTime()); await repository.addOrUpdateShuttle(shuttle, firstArrivalTime.getTime());

View File

@@ -76,19 +76,11 @@ describe.each(repositoryImplementations)('$name', (holder) => {
describe("handleShuttleWillArriveAtStop", () => { describe("handleShuttleWillArriveAtStop", () => {
test("updates how long the shuttle took to get from one stop to another", async () => { test("updates how long the shuttle took to get from one stop to another", async () => {
const { route, systemId, stop2, stop1 } = await setupRouteAndOrderedStops(); const { route, stop2, stop1, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
repository.startListeningForUpdates(); repository.startListeningForUpdates();
const shuttle = { shuttle.coordinates = stop1.coordinates;
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
const firstStopArrivalTime = new Date(2025, 0, 1, 12, 0, 0); const firstStopArrivalTime = new Date(2025, 0, 1, 12, 0, 0);
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopArrivalTime.getTime()); await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopArrivalTime.getTime());
@@ -118,7 +110,7 @@ describe.each(repositoryImplementations)('$name', (holder) => {
currentTime: Date, currentTime: Date,
shuttleSecondArrivalTimeAtFirstStop: Date shuttleSecondArrivalTimeAtFirstStop: Date
) { ) {
const { route, systemId, stop1, stop2, stop3 } = await setupRouteAndOrderedStops(); const { stop1, stop2, stop3, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
// Populating travel time data // Populating travel time data
const firstStopArrivalTime = new Date(2025, 0, 1, 11, 0, 0); const firstStopArrivalTime = new Date(2025, 0, 1, 11, 0, 0);
@@ -128,15 +120,7 @@ describe.each(repositoryImplementations)('$name', (holder) => {
repository.setReferenceTime(currentTime); repository.setReferenceTime(currentTime);
repository.startListeningForUpdates(); repository.startListeningForUpdates();
const shuttle = { shuttle.coordinates = stop1.coordinates;
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopArrivalTime.getTime()); await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopArrivalTime.getTime());
@@ -196,19 +180,11 @@ describe.each(repositoryImplementations)('$name', (holder) => {
describe("getAverageTravelTimeSeconds", () => { describe("getAverageTravelTimeSeconds", () => {
test("returns the average travel time when historical data exists", async () => { test("returns the average travel time when historical data exists", async () => {
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops(); const { route, stop1, stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
repository.startListeningForUpdates(); repository.startListeningForUpdates();
const shuttle = { shuttle.coordinates = stop1.coordinates;
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
const firstStopTime = new Date(2025, 0, 1, 12, 0, 0); const firstStopTime = new Date(2025, 0, 1, 12, 0, 0);
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopTime.getTime()); await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopTime.getTime());
@@ -232,19 +208,11 @@ describe.each(repositoryImplementations)('$name', (holder) => {
}); });
test("returns average of multiple data points", async () => { test("returns average of multiple data points", async () => {
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops(); const { route, stop1, stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
repository.startListeningForUpdates(); repository.startListeningForUpdates();
const shuttle = { shuttle.coordinates = stop1.coordinates;
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
// First trip: 10 minutes travel time // First trip: 10 minutes travel time
await shuttleRepository.addOrUpdateShuttle(shuttle, new Date(2025, 0, 1, 12, 0, 0).getTime()); await shuttleRepository.addOrUpdateShuttle(shuttle, new Date(2025, 0, 1, 12, 0, 0).getTime());
@@ -288,19 +256,11 @@ describe.each(repositoryImplementations)('$name', (holder) => {
}); });
test("returns undefined when querying outside the time range of data", async () => { test("returns undefined when querying outside the time range of data", async () => {
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops(); const { route, stop1, stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
repository.startListeningForUpdates(); repository.startListeningForUpdates();
const shuttle = { shuttle.coordinates = stop1.coordinates;
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop1.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
await shuttleRepository.addOrUpdateShuttle(shuttle, new Date(2025, 0, 1, 12, 0, 0).getTime()); await shuttleRepository.addOrUpdateShuttle(shuttle, new Date(2025, 0, 1, 12, 0, 0).getTime());
shuttle.coordinates = stop2.coordinates; shuttle.coordinates = stop2.coordinates;

View File

@@ -1,4 +1,4 @@
import { IOrderedStop, IStop } from "../src/entities/ShuttleRepositoryEntities"; import { IOrderedStop, IShuttle, IStop } from "../src/entities/ShuttleRepositoryEntities";
import { ShuttleGetterSetterRepository } from "../src/repositories/shuttle/ShuttleGetterSetterRepository"; import { ShuttleGetterSetterRepository } from "../src/repositories/shuttle/ShuttleGetterSetterRepository";
export async function setupRouteAndOrderedStopsForShuttleRepository( export async function setupRouteAndOrderedStopsForShuttleRepository(
@@ -71,11 +71,22 @@ export async function setupRouteAndOrderedStopsForShuttleRepository(
await shuttleRepository.addOrUpdateOrderedStop(orderedStop2); await shuttleRepository.addOrUpdateOrderedStop(orderedStop2);
await shuttleRepository.addOrUpdateOrderedStop(orderedStop3); await shuttleRepository.addOrUpdateOrderedStop(orderedStop3);
const sampleShuttleNotInRepository: IShuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: stop2.coordinates,
orientationInDegrees: 0,
updatedTime: new Date(),
};
return { return {
route, route,
systemId, systemId,
stop1, stop1,
stop2, stop2,
stop3, stop3,
sampleShuttleNotInRepository,
}; };
} }