update tests and index with updated scheduler interface

This commit is contained in:
2025-03-31 19:21:10 -07:00
parent ef94055133
commit a95c89c15b
2 changed files with 15 additions and 24 deletions

View File

@@ -30,11 +30,13 @@ async function main() {
const appleNotificationSender = new AppleNotificationSender(false); const appleNotificationSender = new AppleNotificationSender(false);
notificationRepository = new InMemoryNotificationRepository(); notificationRepository = new InMemoryNotificationRepository();
notificationService = new ETANotificationScheduler( notificationService = new ETANotificationScheduler(
shuttleRepository, shuttleRepository,
notificationRepository, notificationRepository,
appleNotificationSender appleNotificationSender
); );
notificationService.startListeningForUpdates();
} else { } else {
const repositoryDataUpdater = new TimedApiBasedShuttleRepositoryLoader( const repositoryDataUpdater = new TimedApiBasedShuttleRepositoryLoader(
shuttleRepository, shuttleRepository,
@@ -46,6 +48,7 @@ async function main() {
shuttleRepository, shuttleRepository,
notificationRepository notificationRepository
); );
notificationService.startListeningForUpdates();
} }
const { url } = await startStandaloneServer(server, { const { url } = await startStandaloneServer(server, {

View File

@@ -1,7 +1,6 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals"; import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { ETANotificationScheduler } from "../../../src/notifications/schedulers/ETANotificationScheduler"; import { ETANotificationScheduler } from "../../../src/notifications/schedulers/ETANotificationScheduler";
import { UnoptimizedInMemoryShuttleRepository } from "../../../src/repositories/UnoptimizedInMemoryShuttleRepository"; import { UnoptimizedInMemoryShuttleRepository } from "../../../src/repositories/UnoptimizedInMemoryShuttleRepository";
import http2 from "http2";
import { IEta, IShuttle, IStop } from "../../../src/entities/entities"; 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";
@@ -17,23 +16,6 @@ function mockNotificationSenderMethods(shouldSimulateNotificationSend: boolean)
MockAppleNotificationSender.prototype.sendNotificationImmediately = jest.fn(async () => shouldSimulateNotificationSend); MockAppleNotificationSender.prototype.sendNotificationImmediately = jest.fn(async () => shouldSimulateNotificationSend);
} }
/**
* Wait for a condition to become true until the timeout
* is hit.
* @param condition
* @param timeoutMilliseconds
* @param intervalMilliseconds
*/
async function waitForCondition(condition: () => boolean, timeoutMilliseconds = 5000, intervalMilliseconds = 500) {
const startTime = Date.now();
while (!condition()) {
if (Date.now() - startTime > timeoutMilliseconds) {
throw new Error("Timeout waiting for condition");
}
await new Promise((resolve) => setTimeout(resolve, intervalMilliseconds));
}
}
/** /**
* Wait for a specified number of milliseconds. * Wait for a specified number of milliseconds.
* @param ms * @param ms
@@ -60,6 +42,7 @@ describe("ETANotificationScheduler", () => {
notificationRepository, notificationRepository,
appleNotificationSender appleNotificationSender
); );
notificationService.startListeningForUpdates();
}); });
function generateNotificationDataAndEta(shuttle: IShuttle, stop: IStop) { function generateNotificationDataAndEta(shuttle: IShuttle, stop: IStop) {
@@ -97,9 +80,8 @@ describe("ETANotificationScheduler", () => {
await shuttleRepository.addOrUpdateEta(eta); await shuttleRepository.addOrUpdateEta(eta);
// Assert // Assert
// Because repository publisher calls subscriber without await // Wait for the callback to actually be called
// wait for the change to occur first await waitForMilliseconds(1000);
await waitForCondition(() => !notificationRepository.isNotificationScheduled(notificationData1));
const isFirstNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData1); const isFirstNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData1);
const isSecondNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData2); const isSecondNotificationScheduled = await notificationRepository.isNotificationScheduled(notificationData2);
@@ -132,12 +114,18 @@ describe("ETANotificationScheduler", () => {
const stop = await addMockStopToRepository(shuttleRepository, "1"); const stop = await addMockStopToRepository(shuttleRepository, "1");
const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop) const { eta, notificationData1 } = generateNotificationDataAndEta(shuttle, stop)
// replace the old notification scheduler with a new one
// detach the old callback method from the shuttle repo
notificationService.stopListeningForUpdates();
mockNotificationSenderMethods(false); mockNotificationSenderMethods(false);
const updatedNotificationSender = new MockAppleNotificationSender(false);
notificationService = new ETANotificationScheduler( notificationService = new ETANotificationScheduler(
shuttleRepository, shuttleRepository,
new InMemoryNotificationRepository(), notificationRepository,
new MockAppleNotificationSender(), updatedNotificationSender
) );
notificationService.startListeningForUpdates();
// Act // Act
await notificationRepository.addOrUpdateNotification(notificationData1); await notificationRepository.addOrUpdateNotification(notificationData1);