From 2a80a049bd6705cfb13075268231f1c124b13819 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Mon, 10 Nov 2025 15:09:13 -0800 Subject: [PATCH] Add updated test cases and update call to stop arrival helper --- src/entities/ShuttleRepositoryEntities.ts | 14 ++++--- .../shuttle/RedisShuttleRepository.ts | 9 +++-- .../__tests__/RedisShuttleRepository.test.ts | 37 +++++++++++++++++++ 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 src/repositories/shuttle/__tests__/RedisShuttleRepository.test.ts diff --git a/src/entities/ShuttleRepositoryEntities.ts b/src/entities/ShuttleRepositoryEntities.ts index 73d2090..e16b1f4 100644 --- a/src/entities/ShuttleRepositoryEntities.ts +++ b/src/entities/ShuttleRepositoryEntities.ts @@ -41,11 +41,15 @@ export interface IOrderedStop extends IEntityWithTimestamp { * Checks if a shuttle has arrived at a stop based on coordinate proximity. * Uses a threshold of 0.001 degrees (~111 meters at the equator). */ -export function shuttleHasArrivedAtStop(shuttle: IShuttle, stop: IStop) { - const isWithinLatitudeRange = shuttle.coordinates.latitude > stop.coordinates.latitude - 0.001 - && shuttle.coordinates.latitude < stop.coordinates.latitude + 0.001; - const isWithinLongitudeRange = shuttle.coordinates.longitude > stop.coordinates.longitude - 0.001 - && shuttle.coordinates.longitude < stop.coordinates.longitude + 0.001 +export function shuttleHasArrivedAtStop( + shuttle: IShuttle, + stop: IStop, + delta = 0.001 +) { + const isWithinLatitudeRange = shuttle.coordinates.latitude > stop.coordinates.latitude - delta + && shuttle.coordinates.latitude < stop.coordinates.latitude + delta; + const isWithinLongitudeRange = shuttle.coordinates.longitude > stop.coordinates.longitude - delta + && shuttle.coordinates.longitude < stop.coordinates.longitude + delta return isWithinLatitudeRange && isWithinLongitudeRange; } diff --git a/src/repositories/shuttle/RedisShuttleRepository.ts b/src/repositories/shuttle/RedisShuttleRepository.ts index 5eae7cc..dc321da 100644 --- a/src/repositories/shuttle/RedisShuttleRepository.ts +++ b/src/repositories/shuttle/RedisShuttleRepository.ts @@ -437,12 +437,15 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette } } - public async getArrivedStopIfExists(shuttle: IShuttle): Promise { + public async getArrivedStopIfExists( + shuttle: IShuttle, + delta = 0.001, + ): Promise { const orderedStops = await this.getOrderedStopsByRouteId(shuttle.routeId); for (const orderedStop of orderedStops) { const stop = await this.getStopById(orderedStop.stopId); - if (stop != null && shuttleHasArrivedAtStop(shuttle, stop)) { + if (stop != null && shuttleHasArrivedAtStop(shuttle, stop, delta)) { return stop; } return undefined; @@ -454,7 +457,7 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette throw Error("not implemented"); } - private async updateShuttleLastStopArrival(shuttle: IShuttle, lastStopArrival: ShuttleStopArrival) { + public async updateShuttleLastStopArrival(shuttle: IShuttle, lastStopArrival: ShuttleStopArrival) { // Key: shuttleId:stopId: // Value: just a marker (no numerical value) } diff --git a/src/repositories/shuttle/__tests__/RedisShuttleRepository.test.ts b/src/repositories/shuttle/__tests__/RedisShuttleRepository.test.ts new file mode 100644 index 0000000..c0d1614 --- /dev/null +++ b/src/repositories/shuttle/__tests__/RedisShuttleRepository.test.ts @@ -0,0 +1,37 @@ +import { beforeEach, describe, it } from "@jest/globals"; +import { RedisShuttleRepository } from "../RedisShuttleRepository"; +import { afterEach } from "node:test"; + +describe("RedisShuttleRepository", () => { + let repository: RedisShuttleRepository; + + beforeEach(async () => { + repository = new RedisShuttleRepository(); + await repository.connect(); + }); + + afterEach(async () => { + await repository.clearAllData(); + await repository.disconnect(); + }); + + describe("getArrivedStopIfExists", () => { + it("gets the stop that the shuttle is currently at, if exists", async () => { + + }); + + it("returns undefined if shuttle is not currently at a stop", async () => { + + }); + }); + + describe("getShuttleLastStopArrival", () => { + it("gets the shuttle's last stop if existing in the data", async () => { + // Use updateShuttleLastStopArrival to populate data + }); + + it("returns undefined if the data has never been initialized", async () => { + + }); + }); +});