From 47708d050e79440282a1f94a6b9f281a5ebd13ec Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 18 Nov 2025 19:50:07 -0800 Subject: [PATCH] Move duplicate shuttle declarations in tests to a "sample" shuttle generated by the setup function --- .../ShuttleRepositorySharedTests.test.ts | 99 +++---------------- ...lfUpdatingETARepositorySharedTests.test.ts | 60 ++--------- ...outeAndOrderedStopsForShuttleRepository.ts | 13 ++- 3 files changed, 36 insertions(+), 136 deletions(-) diff --git a/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts b/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts index 4d5a059..94dd9e8 100644 --- a/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts +++ b/src/repositories/shuttle/__tests__/ShuttleRepositorySharedTests.test.ts @@ -565,17 +565,7 @@ describe.each(repositoryImplementations)('$name', (holder) => { describe("addOrUpdateShuttle with shuttle tracking", () => { test("updates the shuttle's last stop arrival if shuttle is at a stop", async () => { - const { route, systemId, stop2 } = await setupRouteAndOrderedStops(); - - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop2.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + const { stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); await repository.addOrUpdateShuttle(shuttle); const lastStop = await repository.getShuttleLastStopArrival(shuttle.id); @@ -585,17 +575,7 @@ describe.each(repositoryImplementations)('$name', (holder) => { describe("getArrivedStopIfExists", () => { test("gets the stop that the shuttle is currently at, if exists", async () => { - const { route, systemId, stop2 } = await setupRouteAndOrderedStops(); - - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop2.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + const { sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); 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 () => { - const { route, systemId } = await setupRouteAndOrderedStops(); - - 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 { sampleShuttleNotInRepository } = await setupRouteAndOrderedStops(); + const shuttle = { ...sampleShuttleNotInRepository, coordinates: { latitude: 12.5, longitude: 22.5 } }; // Not at any stop const result = await repository.getArrivedStopIfExists(shuttle); @@ -625,17 +596,8 @@ describe.each(repositoryImplementations)('$name', (holder) => { describe("getShuttleLastStopArrival", () => { test("gets the shuttle's last stop if existing in the data", async () => { - const { route, systemId, stop1 } = await setupRouteAndOrderedStops(); - - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop1.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + const { stop1, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); + shuttle.coordinates = stop1.coordinates; const stopArrivalTime = new Date("2024-01-15T10:30:00Z"); 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 () => { - const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops(); - - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop1.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + const { stop1, stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); + shuttle.coordinates = stop1.coordinates; const firstArrivalTime = new Date("2024-01-15T10:30:00Z"); await repository.addOrUpdateShuttle(shuttle, firstArrivalTime.getTime()); @@ -750,20 +703,12 @@ describe.each(repositoryImplementations)('$name', (holder) => { describe("SHUTTLE_WILL_ARRIVE_AT_STOP event", () => { 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(); repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener); - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop1.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + shuttle.coordinates = stop1.coordinates; const arrivalTime = new Date("2024-01-15T10:30:00Z"); 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 () => { - const { route, systemId } = await setupRouteAndOrderedStops(); + const { sampleShuttleNotInRepository } = await setupRouteAndOrderedStops(); const listener = jest.fn(); repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener); - const shuttle = { - 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(), - }; + const shuttle = { ...sampleShuttleNotInRepository, coordinates: { latitude: 12.5, longitude: 22.5 } }; // Not at any stop await repository.addOrUpdateShuttle(shuttle); @@ -799,20 +736,12 @@ describe.each(repositoryImplementations)('$name', (holder) => { }); 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(); repository.on(ShuttleRepositoryEvent.SHUTTLE_WILL_ARRIVE_AT_STOP, listener); - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop1.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + shuttle.coordinates = stop1.coordinates; const firstArrivalTime = new Date("2024-01-15T10:30:00Z"); await repository.addOrUpdateShuttle(shuttle, firstArrivalTime.getTime()); diff --git a/src/repositories/shuttle/eta/__tests__/SelfUpdatingETARepositorySharedTests.test.ts b/src/repositories/shuttle/eta/__tests__/SelfUpdatingETARepositorySharedTests.test.ts index b260e3e..8460724 100644 --- a/src/repositories/shuttle/eta/__tests__/SelfUpdatingETARepositorySharedTests.test.ts +++ b/src/repositories/shuttle/eta/__tests__/SelfUpdatingETARepositorySharedTests.test.ts @@ -76,19 +76,11 @@ describe.each(repositoryImplementations)('$name', (holder) => { describe("handleShuttleWillArriveAtStop", () => { 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(); - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop1.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + shuttle.coordinates = stop1.coordinates; const firstStopArrivalTime = new Date(2025, 0, 1, 12, 0, 0); await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopArrivalTime.getTime()); @@ -118,7 +110,7 @@ describe.each(repositoryImplementations)('$name', (holder) => { currentTime: Date, shuttleSecondArrivalTimeAtFirstStop: Date ) { - const { route, systemId, stop1, stop2, stop3 } = await setupRouteAndOrderedStops(); + const { stop1, stop2, stop3, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); // Populating travel time data const firstStopArrivalTime = new Date(2025, 0, 1, 11, 0, 0); @@ -128,15 +120,7 @@ describe.each(repositoryImplementations)('$name', (holder) => { repository.setReferenceTime(currentTime); repository.startListeningForUpdates(); - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop1.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + shuttle.coordinates = stop1.coordinates; await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopArrivalTime.getTime()); @@ -196,19 +180,11 @@ describe.each(repositoryImplementations)('$name', (holder) => { describe("getAverageTravelTimeSeconds", () => { 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(); - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop1.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + shuttle.coordinates = stop1.coordinates; const firstStopTime = new Date(2025, 0, 1, 12, 0, 0); await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopTime.getTime()); @@ -232,19 +208,11 @@ describe.each(repositoryImplementations)('$name', (holder) => { }); 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(); - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop1.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + shuttle.coordinates = stop1.coordinates; // First trip: 10 minutes travel time 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 () => { - const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops(); + const { route, stop1, stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops(); repository.startListeningForUpdates(); - const shuttle = { - id: "sh1", - name: "Shuttle 1", - routeId: route.id, - systemId: systemId, - coordinates: stop1.coordinates, - orientationInDegrees: 0, - updatedTime: new Date(), - }; + shuttle.coordinates = stop1.coordinates; await shuttleRepository.addOrUpdateShuttle(shuttle, new Date(2025, 0, 1, 12, 0, 0).getTime()); shuttle.coordinates = stop2.coordinates; diff --git a/testHelpers/setupRouteAndOrderedStopsForShuttleRepository.ts b/testHelpers/setupRouteAndOrderedStopsForShuttleRepository.ts index 7dd80e9..65e81ba 100644 --- a/testHelpers/setupRouteAndOrderedStopsForShuttleRepository.ts +++ b/testHelpers/setupRouteAndOrderedStopsForShuttleRepository.ts @@ -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"; export async function setupRouteAndOrderedStopsForShuttleRepository( @@ -71,11 +71,22 @@ export async function setupRouteAndOrderedStopsForShuttleRepository( await shuttleRepository.addOrUpdateOrderedStop(orderedStop2); 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 { route, systemId, stop1, stop2, stop3, + sampleShuttleNotInRepository, }; }