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