Update getArrivedStopIfExists and implement tests for it

This commit is contained in:
2025-11-10 15:45:38 -08:00
parent 584dd44aad
commit c946647483
2 changed files with 82 additions and 2 deletions

View File

@@ -449,8 +449,8 @@ export class RedisShuttleRepository extends EventEmitter implements ShuttleGette
if (stop != null && shuttleHasArrivedAtStop(shuttle, stop, delta)) {
return stop;
}
return undefined;
}
return undefined;
}
public async getShuttleLastStopArrival(shuttle: IShuttle): Promise<ShuttleStopArrival | undefined> {

View File

@@ -16,12 +16,92 @@ describe("RedisShuttleRepository", () => {
});
describe("getArrivedStopIfExists", () => {
it("gets the stop that the shuttle is currently at, if exists", async () => {
async function setupRouteAndOrderedStops() {
const systemId = "sys1";
const route = {
id: "r1",
name: "Route 1",
color: "red",
systemId: systemId,
polylineCoordinates: [],
updatedTime: new Date(),
};
await repository.addOrUpdateRoute(route);
const stop1 = {
id: "st1",
name: "Stop 1",
systemId: systemId,
coordinates: { latitude: 10.0, longitude: 20.0 },
updatedTime: new Date(),
};
const stop2 = {
id: "st2",
name: "Stop 2",
systemId: systemId,
coordinates: { latitude: 15.0, longitude: 25.0 },
updatedTime: new Date(),
};
await repository.addOrUpdateStop(stop1);
await repository.addOrUpdateStop(stop2);
const orderedStop1 = {
routeId: route.id,
stopId: stop1.id,
position: 1,
systemId: systemId,
updatedTime: new Date(),
};
const orderedStop2 = {
routeId: route.id,
stopId: stop2.id,
position: 2,
systemId: systemId,
updatedTime: new Date(),
};
await repository.addOrUpdateOrderedStop(orderedStop1);
await repository.addOrUpdateOrderedStop(orderedStop2);
return { route, systemId };
}
it("gets the stop that the shuttle is currently at, if exists", async () => {
const { route, systemId } = await setupRouteAndOrderedStops();
// Create a shuttle positioned at stop2
const shuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: { latitude: 15.0, longitude: 25.0 }, // Same as stop2
orientationInDegrees: 0,
updatedTime: new Date(),
};
const result = await repository.getArrivedStopIfExists(shuttle);
expect(result).toBeDefined();
expect(result?.id).toBe("st2");
expect(result?.name).toBe("Stop 2");
});
it("returns undefined if shuttle is not currently at a stop", async () => {
const { route, systemId } = await setupRouteAndOrderedStops();
// Create a shuttle positioned between stops (not at any stop)
const shuttle = {
id: "sh1",
name: "Shuttle 1",
routeId: route.id,
systemId: systemId,
coordinates: { latitude: 12.5, longitude: 22.5 }, // Between stop1 and stop2
orientationInDegrees: 0,
updatedTime: new Date(),
};
const result = await repository.getArrivedStopIfExists(shuttle);
expect(result).toBeUndefined();
});
});