mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-16 23:40:32 +00:00
Add test for ETA clearing
This commit is contained in:
@@ -415,7 +415,6 @@ export class RedisShuttleRepository extends BaseRedisRepository implements Shutt
|
||||
}
|
||||
|
||||
if (arrivedStop) {
|
||||
// stop if same stop
|
||||
const shuttleArrival = {
|
||||
stopId: arrivedStop.id,
|
||||
timestamp: new Date(travelTimeTimestamp),
|
||||
|
||||
@@ -8,6 +8,7 @@ import { RedisShuttleRepository } from "../../RedisShuttleRepository";
|
||||
import { UnoptimizedInMemoryShuttleRepository } from "../../UnoptimizedInMemoryShuttleRepository";
|
||||
import { setupRouteAndOrderedStopsForShuttleRepository } from "../../../../../testHelpers/setupRouteAndOrderedStopsForShuttleRepository";
|
||||
import { ShuttleGetterSetterRepository } from "../../ShuttleGetterSetterRepository";
|
||||
import { IShuttle, IStop } from "../../../../entities/ShuttleRepositoryEntities";
|
||||
|
||||
class RedisSelfUpdatingETARepositoryHolder implements RepositoryHolder<SelfUpdatingETARepository> {
|
||||
repo: RedisSelfUpdatingETARepository | undefined;
|
||||
@@ -74,6 +75,38 @@ describe.each(repositoryImplementations)('$name', (holder) => {
|
||||
return await setupRouteAndOrderedStopsForShuttleRepository(shuttleRepository);
|
||||
}
|
||||
|
||||
async function populateTravelTimeDataForStops({
|
||||
currentTime,
|
||||
shuttle,
|
||||
stop1,
|
||||
stop2,
|
||||
stop3,
|
||||
firstStopArrivalTime = new Date(2025, 0, 1, 11, 0, 0),
|
||||
secondStopArrivalTime = new Date(2025, 0, 1, 11, 15, 0),
|
||||
thirdStopArrivalTime = new Date(2025, 0, 1, 11, 20, 0),
|
||||
}: {
|
||||
currentTime: Date;
|
||||
shuttle: IShuttle;
|
||||
stop1: IStop;
|
||||
stop2: IStop;
|
||||
stop3: IStop;
|
||||
firstStopArrivalTime?: Date;
|
||||
secondStopArrivalTime?: Date;
|
||||
thirdStopArrivalTime?: Date;
|
||||
}) {
|
||||
repository.setReferenceTime(currentTime);
|
||||
repository.startListeningForUpdates();
|
||||
|
||||
shuttle.coordinates = stop1.coordinates;
|
||||
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopArrivalTime.getTime());
|
||||
|
||||
shuttle.coordinates = stop2.coordinates;
|
||||
await shuttleRepository.addOrUpdateShuttle(shuttle, secondStopArrivalTime.getTime());
|
||||
|
||||
shuttle.coordinates = stop3.coordinates;
|
||||
await shuttleRepository.addOrUpdateShuttle(shuttle, thirdStopArrivalTime.getTime());
|
||||
}
|
||||
|
||||
describe("handleShuttleWillArriveAtStop", () => {
|
||||
test("updates how long the shuttle took to get from one stop to another", async () => {
|
||||
const { route, stop2, stop1, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
|
||||
@@ -113,22 +146,7 @@ describe.each(repositoryImplementations)('$name', (holder) => {
|
||||
const { stop1, stop2, stop3, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
|
||||
|
||||
// Populating travel time data
|
||||
const firstStopArrivalTime = new Date(2025, 0, 1, 11, 0, 0);
|
||||
const secondStopArrivalTime = new Date(2025, 0, 1, 11, 15, 0);
|
||||
const thirdStopArrivalTime = new Date(2025, 0, 1, 11, 20, 0);
|
||||
|
||||
repository.setReferenceTime(currentTime);
|
||||
repository.startListeningForUpdates();
|
||||
|
||||
shuttle.coordinates = stop1.coordinates;
|
||||
|
||||
await shuttleRepository.addOrUpdateShuttle(shuttle, firstStopArrivalTime.getTime());
|
||||
|
||||
shuttle.coordinates = stop2.coordinates;
|
||||
await shuttleRepository.addOrUpdateShuttle(shuttle, secondStopArrivalTime.getTime());
|
||||
|
||||
shuttle.coordinates = stop3.coordinates;
|
||||
await shuttleRepository.addOrUpdateShuttle(shuttle, thirdStopArrivalTime.getTime());
|
||||
await populateTravelTimeDataForStops({ currentTime, shuttle, stop1, stop2, stop3 });
|
||||
|
||||
// Populating ETA data
|
||||
shuttle.coordinates = stop1.coordinates;
|
||||
@@ -178,6 +196,38 @@ describe.each(repositoryImplementations)('$name', (holder) => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleShuttleWillLeaveStop", () => {
|
||||
test("clears ETA of correct stop on leaving stop", async () => {
|
||||
const { stop1, stop2, stop3, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
|
||||
|
||||
const shuttleSecondArrivalTimeAtFirstStop = new Date(2025, 0, 1, 12, 5, 0);
|
||||
const shuttleSecondArrivalTimeAtSecondStop = new Date(2025, 0, 1, 12, 20, 0);
|
||||
const currentTime = new Date(shuttleSecondArrivalTimeAtSecondStop.getTime() + 3 * 60 * 1000);
|
||||
|
||||
repository.setReferenceTime(currentTime);
|
||||
repository.startListeningForUpdates();
|
||||
|
||||
await populateTravelTimeDataForStops({ currentTime, shuttle, stop1, stop2, stop3 });
|
||||
|
||||
// Populating ETA data
|
||||
shuttle.coordinates = stop1.coordinates;
|
||||
await shuttleRepository.addOrUpdateShuttle(shuttle, shuttleSecondArrivalTimeAtFirstStop.getTime());
|
||||
|
||||
shuttle.coordinates = stop2.coordinates;
|
||||
await shuttleRepository.addOrUpdateShuttle(shuttle, shuttleSecondArrivalTimeAtSecondStop.getTime());
|
||||
|
||||
shuttle.coordinates = { latitude: 12.5, longitude: 12.5 }
|
||||
await shuttleRepository.addOrUpdateShuttle(shuttle, currentTime.getTime());
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
|
||||
const etaForStop3 = await repository.getEtaForShuttleAndStopId(shuttle.id, stop3.id);
|
||||
expect(etaForStop3).not.toBeNull();
|
||||
const etaForStop2 = await repository.getEtaForShuttleAndStopId(shuttle.id, stop2.id);
|
||||
expect(etaForStop2).toBeNull();
|
||||
}, 60000);
|
||||
});
|
||||
|
||||
describe("getAverageTravelTimeSeconds", () => {
|
||||
test("returns the average travel time when historical data exists", async () => {
|
||||
const { route, stop1, stop2, sampleShuttleNotInRepository: shuttle } = await setupRouteAndOrderedStops();
|
||||
|
||||
Reference in New Issue
Block a user