mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-19 08:50:29 +00:00
Extract setup and assertion procedure into method for all three tests to use
This commit is contained in:
@@ -114,137 +114,74 @@ describe.each(repositoryImplementations)('$name', (holder) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("handleShuttleUpdate", () => {
|
describe("handleShuttleUpdate", () => {
|
||||||
test("adds an ETA entry based on historical data", async () => {
|
async function assertEtaIsValidGivenCurrentTimeAndSecondArrivalTime(
|
||||||
|
currentTime: Date,
|
||||||
|
shuttleSecondArrivalTimeAtFirstStop: Date
|
||||||
|
) {
|
||||||
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops();
|
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops();
|
||||||
|
|
||||||
const firstStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 0, 0);
|
const firstStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 0, 0);
|
||||||
const secondStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 15, 0);
|
const secondStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 15, 0);
|
||||||
|
|
||||||
|
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(),
|
||||||
|
};
|
||||||
|
|
||||||
|
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopLastWeekArrivalTime.getTime());
|
||||||
|
|
||||||
|
shuttle.coordinates = stop2.coordinates;
|
||||||
|
await shuttleRepository.addOrUpdateShuttle(shuttle, secondStopLastWeekArrivalTime.getTime());
|
||||||
|
|
||||||
|
shuttle.coordinates = stop1.coordinates;
|
||||||
|
await shuttleRepository.addOrUpdateShuttle(
|
||||||
|
shuttle,
|
||||||
|
shuttleSecondArrivalTimeAtFirstStop.getTime()
|
||||||
|
);
|
||||||
|
|
||||||
|
shuttle.coordinates = { latitude: 12.5, longitude: 22.5 };
|
||||||
|
await shuttleRepository.addOrUpdateShuttle(
|
||||||
|
shuttle,
|
||||||
|
currentTime.getTime()
|
||||||
|
);
|
||||||
|
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||||
|
|
||||||
|
const eta = await repository.getEtaForShuttleAndStopId(shuttle.id, stop2.id);
|
||||||
|
expect(eta?.secondsRemaining).toEqual(8 * 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
test("adds an ETA entry based on historical data", async () => {
|
||||||
const shuttleSecondArrivalTimeAtFirstStop = new Date(2025, 0, 8, 12, 0, 0);
|
const shuttleSecondArrivalTimeAtFirstStop = new Date(2025, 0, 8, 12, 0, 0);
|
||||||
const currentTime = new Date(shuttleSecondArrivalTimeAtFirstStop.getTime() + 7 * 60 * 1000);
|
const currentTime = new Date(shuttleSecondArrivalTimeAtFirstStop.getTime() + 7 * 60 * 1000);
|
||||||
|
|
||||||
repository.setReferenceTime(currentTime);
|
await assertEtaIsValidGivenCurrentTimeAndSecondArrivalTime(
|
||||||
repository.startListeningForUpdates();
|
currentTime, shuttleSecondArrivalTimeAtFirstStop
|
||||||
|
|
||||||
const shuttle = {
|
|
||||||
id: "sh1",
|
|
||||||
name: "Shuttle 1",
|
|
||||||
routeId: route.id,
|
|
||||||
systemId: systemId,
|
|
||||||
coordinates: stop1.coordinates,
|
|
||||||
orientationInDegrees: 0,
|
|
||||||
updatedTime: new Date(),
|
|
||||||
};
|
|
||||||
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopLastWeekArrivalTime.getTime());
|
|
||||||
|
|
||||||
shuttle.coordinates = stop2.coordinates;
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(shuttle, secondStopLastWeekArrivalTime.getTime());
|
|
||||||
|
|
||||||
shuttle.coordinates = stop1.coordinates;
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(
|
|
||||||
shuttle,
|
|
||||||
shuttleSecondArrivalTimeAtFirstStop.getTime()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
shuttle.coordinates = { latitude: 12.5, longitude: 22.5 };
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(
|
|
||||||
shuttle,
|
|
||||||
currentTime.getTime()
|
|
||||||
);
|
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
||||||
|
|
||||||
const eta = await repository.getEtaForShuttleAndStopId(shuttle.id, stop2.id);
|
|
||||||
expect(eta?.secondsRemaining).toEqual(8 * 60);
|
|
||||||
|
|
||||||
}, 60000);
|
}, 60000);
|
||||||
|
|
||||||
test("uses previous day fallback calculation when no data available from one week ago", async () => {
|
test("uses previous day fallback calculation when no data available from one week ago", async () => {
|
||||||
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops();
|
|
||||||
|
|
||||||
const firstStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 0, 0);
|
|
||||||
const secondStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 15, 0);
|
|
||||||
const shuttleSecondArrivalTimeAtFirstStop = new Date(2025, 0, 2, 12, 0, 0);
|
const shuttleSecondArrivalTimeAtFirstStop = new Date(2025, 0, 2, 12, 0, 0);
|
||||||
const currentTime = new Date(shuttleSecondArrivalTimeAtFirstStop.getTime() + 7 * 60 * 1000);
|
const currentTime = new Date(shuttleSecondArrivalTimeAtFirstStop.getTime() + 7 * 60 * 1000);
|
||||||
|
await assertEtaIsValidGivenCurrentTimeAndSecondArrivalTime(
|
||||||
repository.setReferenceTime(currentTime);
|
currentTime, shuttleSecondArrivalTimeAtFirstStop
|
||||||
repository.startListeningForUpdates();
|
|
||||||
|
|
||||||
const shuttle = {
|
|
||||||
id: "sh1",
|
|
||||||
name: "Shuttle 1",
|
|
||||||
routeId: route.id,
|
|
||||||
systemId: systemId,
|
|
||||||
coordinates: stop1.coordinates,
|
|
||||||
orientationInDegrees: 0,
|
|
||||||
updatedTime: new Date(),
|
|
||||||
};
|
|
||||||
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopLastWeekArrivalTime.getTime());
|
|
||||||
|
|
||||||
shuttle.coordinates = stop2.coordinates;
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(shuttle, secondStopLastWeekArrivalTime.getTime());
|
|
||||||
|
|
||||||
shuttle.coordinates = stop1.coordinates;
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(
|
|
||||||
shuttle,
|
|
||||||
shuttleSecondArrivalTimeAtFirstStop.getTime()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
shuttle.coordinates = { latitude: 12.5, longitude: 22.5 };
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(
|
|
||||||
shuttle,
|
|
||||||
currentTime.getTime()
|
|
||||||
);
|
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
||||||
|
|
||||||
const eta = await repository.getEtaForShuttleAndStopId(shuttle.id, stop2.id);
|
|
||||||
expect(eta?.secondsRemaining).toEqual(8 * 60);
|
|
||||||
}, 60000);
|
}, 60000);
|
||||||
|
|
||||||
test("uses previous hour fallback calculation when no data available from one day ago", async () => {
|
test("uses previous hour fallback calculation when no data available from one day ago", async () => {
|
||||||
const { route, systemId, stop1, stop2 } = await setupRouteAndOrderedStops();
|
|
||||||
|
|
||||||
const firstStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 0, 0);
|
|
||||||
const secondStopLastWeekArrivalTime = new Date(2025, 0, 1, 12, 15, 0);
|
|
||||||
const shuttleSecondArrivalTimeAtFirstStop = new Date(2025, 0, 1, 13, 5, 0);
|
const shuttleSecondArrivalTimeAtFirstStop = new Date(2025, 0, 1, 13, 5, 0);
|
||||||
const currentTime = new Date(shuttleSecondArrivalTimeAtFirstStop.getTime() + 7 * 60 * 1000);
|
const currentTime = new Date(shuttleSecondArrivalTimeAtFirstStop.getTime() + 7 * 60 * 1000);
|
||||||
|
await assertEtaIsValidGivenCurrentTimeAndSecondArrivalTime(
|
||||||
repository.setReferenceTime(currentTime);
|
currentTime, shuttleSecondArrivalTimeAtFirstStop
|
||||||
repository.startListeningForUpdates();
|
|
||||||
|
|
||||||
const shuttle = {
|
|
||||||
id: "sh1",
|
|
||||||
name: "Shuttle 1",
|
|
||||||
routeId: route.id,
|
|
||||||
systemId: systemId,
|
|
||||||
coordinates: stop1.coordinates,
|
|
||||||
orientationInDegrees: 0,
|
|
||||||
updatedTime: new Date(),
|
|
||||||
};
|
|
||||||
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopLastWeekArrivalTime.getTime());
|
|
||||||
|
|
||||||
shuttle.coordinates = stop2.coordinates;
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(shuttle, secondStopLastWeekArrivalTime.getTime());
|
|
||||||
|
|
||||||
shuttle.coordinates = stop1.coordinates;
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(
|
|
||||||
shuttle,
|
|
||||||
shuttleSecondArrivalTimeAtFirstStop.getTime()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
shuttle.coordinates = { latitude: 12.5, longitude: 22.5 };
|
|
||||||
await shuttleRepository.addOrUpdateShuttle(
|
|
||||||
shuttle,
|
|
||||||
currentTime.getTime()
|
|
||||||
);
|
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
||||||
|
|
||||||
const eta = await repository.getEtaForShuttleAndStopId(shuttle.id, stop2.id);
|
|
||||||
expect(eta?.secondsRemaining).toEqual(8 * 60);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user