define behavior of historical data logging through tests

This commit is contained in:
2025-07-01 21:32:26 -04:00
parent 6759bba2ce
commit 95fa610d77

View File

@@ -1,5 +1,8 @@
import { beforeEach, describe, expect, it } from "@jest/globals"; import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { InMemoryParkingRepository } from "../../src/repositories/InMemoryParkingRepository"; import {
InMemoryParkingRepository,
PARKING_LOGGING_INTERVAL_MS
} from "../../src/repositories/InMemoryParkingRepository";
import { IParkingStructure } from "../../src/entities/ParkingRepositoryEntities"; import { IParkingStructure } from "../../src/entities/ParkingRepositoryEntities";
describe("InMemoryParkingRepository", () => { describe("InMemoryParkingRepository", () => {
@@ -16,9 +19,11 @@ describe("InMemoryParkingRepository", () => {
address: "300 E Walnut Ave, Orange, CA 92867", address: "300 E Walnut Ave, Orange, CA 92867",
updatedTime: new Date(), updatedTime: new Date(),
}; };
let historicalData = new Map();
beforeEach(() => { beforeEach(() => {
repository = new InMemoryParkingRepository(); historicalData = new Map();
repository = new InMemoryParkingRepository(new Map(), historicalData);
}); });
describe("addOrUpdateParkingStructure", () => { describe("addOrUpdateParkingStructure", () => {
@@ -35,6 +40,42 @@ describe("InMemoryParkingRepository", () => {
const result = await repository.getParkingStructureById(testStructure.id); const result = await repository.getParkingStructureById(testStructure.id);
expect(result).toEqual(updatedStructure); expect(result).toEqual(updatedStructure);
}); });
it("should log historical data if past the logging interval", async () => {
const now = Date.now();
jest
.useFakeTimers()
.setSystemTime(now);
const expectedTimestampRecordMatcher = {
spotsAvailable: testStructure.spotsAvailable,
id: testStructure.id,
timestampMs: now,
}
await repository.addOrUpdateParkingStructure(testStructure);
jest.setSystemTime(now + PARKING_LOGGING_INTERVAL_MS + 60);
await repository.addOrUpdateParkingStructure(testStructure);
expect(historicalData.get(testStructure.id)).toContain(expectedTimestampRecordMatcher);
expect(historicalData.get(testStructure.id)).toContain({
...expectedTimestampRecordMatcher,
timestampMs: now + PARKING_LOGGING_INTERVAL_MS + 60,
});
});
it("should not log historical data if not past the logging interval", async () => {
const now = Date.now();
jest
.useFakeTimers()
.setSystemTime(now);
await repository.addOrUpdateParkingStructure(testStructure);
jest.setSystemTime(now + 60);
await repository.addOrUpdateParkingStructure(testStructure);
expect(historicalData.get(testStructure.id)).toHaveLength(1);
});
}); });
describe("removeParkingStructureIfExists", () => { describe("removeParkingStructureIfExists", () => {