update eta notification scheduler test to use repository

This commit is contained in:
2025-03-27 11:04:21 -07:00
parent f2a2dd74f6
commit 2b28b94dbd

View File

@@ -6,6 +6,7 @@ import { IEta, IShuttle, IStop } from "../../../src/entities/entities";
import { addMockShuttleToRepository, addMockStopToRepository } from "../../testHelpers/repositorySetupHelpers";
import { AppleNotificationSender } from "../../../src/notifications/senders/AppleNotificationSender";
import { InMemoryNotificationRepository } from "../../../src/repositories/InMemoryNotificationRepository";
import { NotificationRepository } from "../../../src/repositories/NotificationRepository";
jest.mock("http2");
jest.mock("../../../src/notifications/senders/AppleNotificationSender");
@@ -43,18 +44,20 @@ async function waitForMilliseconds(ms: number): Promise<void> {
describe("ETANotificationScheduler", () => {
let repository: UnoptimizedInMemoryShuttleRepository
let shuttleRepository: UnoptimizedInMemoryShuttleRepository
let notificationService: ETANotificationScheduler;
let notificationRepository: NotificationRepository;
beforeEach(() => {
repository = new UnoptimizedInMemoryShuttleRepository();
shuttleRepository = new UnoptimizedInMemoryShuttleRepository();
notificationRepository = new InMemoryNotificationRepository();
mockNotificationSenderMethods(true);
const appleNotificationSender = new MockAppleNotificationSender(false);
notificationService = new ETANotificationScheduler(
repository,
new InMemoryNotificationRepository(),
shuttleRepository,
notificationRepository,
appleNotificationSender
);
});
@@ -80,41 +83,27 @@ describe("ETANotificationScheduler", () => {
return { eta, notificationData1, notificationData2 };
}
describe("scheduleNotification", () => {
it("schedules the notification", async () => {
// arrange
const notificationData = {
deviceId: "1",
shuttleId: "1",
stopId: "1",
secondsThreshold: 120,
};
await notificationService.scheduleNotification(notificationData);
const isNotificationScheduled = notificationService.isNotificationScheduled(notificationData);
expect(isNotificationScheduled).toEqual(true);
});
describe("etaSubscriberCallback", () => {
it("sends and clears correct notification after ETA changed", async () => {
// Arrange
const shuttle = await addMockShuttleToRepository(repository, "1");
const stop = await addMockStopToRepository(repository, "1");
const shuttle = await addMockShuttleToRepository(shuttleRepository, "1");
const stop = await addMockStopToRepository(shuttleRepository, "1");
const { eta, notificationData1, notificationData2 } = generateNotificationDataAndEta(shuttle, stop);
// Act
await notificationService.scheduleNotification(notificationData1);
await notificationService.scheduleNotification(notificationData2);
await repository.addOrUpdateEta(eta);
await notificationRepository.addOrUpdateNotification(notificationData1);
await notificationRepository.addOrUpdateNotification(notificationData2);
await shuttleRepository.addOrUpdateEta(eta);
// Assert
// Because repository publisher calls subscriber without await
// wait for the change to occur first
await waitForCondition(() => !notificationService.isNotificationScheduled(notificationData1));
await waitForCondition(() => !notificationRepository.isNotificationScheduled(notificationData1));
const isFirstNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData1);
const isSecondNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData2);
const isFirstNotificationScheduled = notificationService.isNotificationScheduled(notificationData1);
const isSecondNotificationScheduled = notificationService.isNotificationScheduled(notificationData2);
// No longer scheduled after being sent
expect(isFirstNotificationScheduled).toBe(false);
expect(isSecondNotificationScheduled).toBe(false);
@@ -122,64 +111,44 @@ describe("ETANotificationScheduler", () => {
it("doesn't send notification if seconds threshold not exceeded", async () => {
// Arrange
const shuttle = await addMockShuttleToRepository(repository, "1");
const stop = await addMockStopToRepository(repository, "1");
const shuttle = await addMockShuttleToRepository(shuttleRepository, "1");
const stop = await addMockStopToRepository(shuttleRepository, "1");
const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop);
notificationData1.secondsThreshold = eta.secondsRemaining - 10;
// Act
await notificationService.scheduleNotification(notificationData1);
await repository.addOrUpdateEta(eta);
await notificationRepository.addOrUpdateNotification(notificationData1);
await shuttleRepository.addOrUpdateEta(eta);
// Assert
await waitForMilliseconds(500);
const isNotificationScheduled = notificationService.isNotificationScheduled(notificationData1);
const isNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData1);
expect(isNotificationScheduled).toBe(true);
});
it("leaves notification in array if delivery unsuccessful", async () => {
// Arrange
const shuttle = await addMockShuttleToRepository(repository, "1");
const stop = await addMockStopToRepository(repository, "1");
const shuttle = await addMockShuttleToRepository(shuttleRepository, "1");
const stop = await addMockStopToRepository(shuttleRepository, "1");
const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop)
mockNotificationSenderMethods(false);
notificationService = new ETANotificationScheduler(
repository,
shuttleRepository,
new InMemoryNotificationRepository(),
new MockAppleNotificationSender(),
)
// Act
await notificationService.scheduleNotification(notificationData1);
await repository.addOrUpdateEta(eta);
await notificationRepository.addOrUpdateNotification(notificationData1);
await shuttleRepository.addOrUpdateEta(eta);
// Assert
// The notification should stay scheduled to be retried once
// the ETA updates again
await waitForMilliseconds(500);
const isNotificationScheduled = notificationService.isNotificationScheduled(notificationData1);
const isNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData1);
expect(isNotificationScheduled).toBe(true);
});
});
describe("cancelNotification", () => {
it("stops notification from sending to given shuttle/stop ID", async () => {
// Arrange
const shuttle = await addMockShuttleToRepository(repository, "1");
const stop = await addMockStopToRepository(repository, "1");
const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop);
await notificationService.scheduleNotification(notificationData1);
// Act
await notificationService.cancelNotificationIfExists(notificationData1);
await repository.addOrUpdateEta(eta);
// Assert
await waitForMilliseconds(500);
expect(http2.connect as jest.Mock).toHaveBeenCalledTimes(0);
});
});
});