Add setLoggingInterval method and implementation for tests and other purposes.

Let users control the logging interval of both parking repositories.
This commit is contained in:
2025-07-02 19:47:44 -04:00
parent 868a9f3b1d
commit 19336ce6ec
4 changed files with 57 additions and 42 deletions

View File

@@ -1,7 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, jest } from "@jest/globals";
import { InMemoryParkingRepository, PARKING_LOGGING_INTERVAL_MS, ParkingStructureID } from "../../src/repositories/InMemoryParkingRepository";
import { IParkingStructure, IParkingStructureTimestampRecord } from "../../src/entities/ParkingRepositoryEntities";
import { CircularQueue } from "../../src/types/CircularQueue";
import { InMemoryParkingRepository, } from "../../src/repositories/InMemoryParkingRepository";
import { IParkingStructure } from "../../src/entities/ParkingRepositoryEntities";
import { ParkingStructureCountOptions } from "../../src/repositories/ParkingGetterRepository";
import { ParkingGetterSetterRepository } from "../../src/repositories/ParkingGetterSetterRepository";
import { RedisParkingRepository } from "../../src/repositories/RedisParkingRepository";
@@ -38,7 +37,7 @@ class RedisParkingRepositoryHolder implements RepositoryHolder {
}
const repositoryImplementations = [
// new InMemoryParkingRepositoryHolder(),
new InMemoryParkingRepositoryHolder(),
new RedisParkingRepositoryHolder(),
];
@@ -165,38 +164,42 @@ describe.each(repositoryImplementations)('$name', (holder) => {
expect(await repository.getHistoricalAveragesOfParkingStructureCounts(testStructure.id, options)).toEqual([]);
});
// it("should calculate averages for intervals with manual historical data", async () => {
// await repository.addOrUpdateParkingStructure(testStructure);
//
// const now = Date.now();
// jest.useFakeTimers().setSystemTime(now);
//
// const updates = [
// { ...testStructure, spotsAvailable: 80, updatedTime: new Date(now + 1000) },
// { ...testStructure, spotsAvailable: 70, updatedTime: new Date(now + 2000) },
// ];
//
// for (let i = 0; i < updates.length; i++) {
// jest.setSystemTime(now + (i + 1) * PARKING_LOGGING_INTERVAL_MS + 100);
// await repository.addOrUpdateParkingStructure(updates[i]);
// }
//
// const options: ParkingStructureCountOptions = {
// startUnixEpochMs: now,
// endUnixEpochMs: now + 4000000, // Large range to capture all data
// intervalMs: 1000000
// };
//
// const result = await repository.getHistoricalAveragesOfParkingStructureCounts(testStructure.id, options);
//
// // Should have at least some historical data
// expect(result.length).toBeGreaterThan(0);
// if (result.length > 0) {
// expect(result[0]).toHaveProperty('fromUnixEpochMs');
// expect(result[0]).toHaveProperty('toUnixEpochMs');
// expect(result[0]).toHaveProperty('averageSpotsAvailable');
// expect(typeof result[0].averageSpotsAvailable).toBe('number');
// }
// });
it("should calculate averages for intervals with manual historical data", async () => {
// Set logging interval to 0 so every update creates historical data
repository.setLoggingInterval(0);
await repository.addOrUpdateParkingStructure(testStructure);
const updates = [
{ ...testStructure, spotsAvailable: 80, updatedTime: new Date() },
{ ...testStructure, spotsAvailable: 70, updatedTime: new Date() },
{ ...testStructure, spotsAvailable: 60, updatedTime: new Date() },
];
// Add updates with small delays to ensure different timestamps
for (let i = 0; i < updates.length; i++) {
await repository.addOrUpdateParkingStructure(updates[i]);
await new Promise(resolve => setTimeout(resolve, 10)); // Small delay
}
const now = Date.now();
const options: ParkingStructureCountOptions = {
startUnixEpochMs: now - 10000, // Look back 10 seconds
endUnixEpochMs: now + 10000, // Look forward 10 seconds
intervalMs: 20000 // Single large interval
};
const result = await repository.getHistoricalAveragesOfParkingStructureCounts(testStructure.id, options);
// Should have at least some historical data
expect(result.length).toBeGreaterThan(0);
if (result.length > 0) {
expect(result[0]).toHaveProperty('fromUnixEpochMs');
expect(result[0]).toHaveProperty('toUnixEpochMs');
expect(result[0]).toHaveProperty('averageSpotsAvailable');
expect(typeof result[0].averageSpotsAvailable).toBe('number');
expect(result[0].averageSpotsAvailable).toBeGreaterThan(0);
}
});
});
});