test: Add tests for caching

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-04-08 17:13:01 +02:00
parent 2c6d135be7
commit cd8df3284a
No known key found for this signature in database
GPG Key ID: E59C7734F0E10EB5
3 changed files with 75 additions and 9 deletions

52
lib/caching.test.ts Normal file
View File

@ -0,0 +1,52 @@
import { assertEquals } from "$std/assert/mod.ts";
import { forTestingOnly } from "./caching.ts";
import { storage } from "./storage.ts";
import { testUtils } from "./test-utils.ts";
Deno.test("Verify getTimeSinceLastFetch for correct difference", () => {
const earlyDate = new Date("2024-04-07T20:24:32Z");
const date = new Date("2024-04-07T23:24:32Z");
const timeSinceLastFetch = forTestingOnly.getTimeSinceLastFetch(date, earlyDate);
assertEquals(timeSinceLastFetch, 3);
});
Deno.test("Verify getTimeSinceLastFetch for bad date", () => {
const earlyDate = new Date("2024-04-07T20:24:32Z");
const date = new Date("bad date");
const timeSinceLastFetch = forTestingOnly.getTimeSinceLastFetch(date, earlyDate);
assertEquals(timeSinceLastFetch, NaN);
});
Deno.test("Verify getTimeSinceLastFetch for bad dates (plural)", () => {
const earlyDate = new Date("badest date");
const date = new Date("bad date");
const timeSinceLastFetch = forTestingOnly.getTimeSinceLastFetch(date, earlyDate);
assertEquals(timeSinceLastFetch, NaN);
});
Deno.test("Verify seriesFromStorage is old", () => {
const earlyDate = new Date("2024-04-07T21:24:32Z");
const date = new Date("2024-04-07T23:24:32Z");
const timeSinceLastFetch = forTestingOnly.getTimeSinceLastFetch(date, earlyDate);
const series = testUtils.generateSeries({ lastFetchedAt: date });
const syncInterval = 2;
const isNew = forTestingOnly.isSeriesFromStorageNew(series, syncInterval, timeSinceLastFetch);
assertEquals(isNew, true);
});
Deno.test("Verify seriesFromStorage is new", () => {
const earlyDate = new Date("2024-04-07T20:24:32Z");
const date = new Date("2024-04-07T23:24:32Z");
const timeSinceLastFetch = forTestingOnly.getTimeSinceLastFetch(date, earlyDate);
const series = testUtils.generateSeries({ lastFetchedAt: date });
const syncInterval = 2;
const isNew = forTestingOnly.isSeriesFromStorageNew(series, syncInterval, timeSinceLastFetch);
assertEquals(isNew, false);
});

View File

@ -52,6 +52,22 @@ async function updateFetch(existingSeries: Series): Promise<UpdatedSeries | null
return updated;
}
function getTimeSinceLastFetch(inputDate: Date, againstDate = new Date()): number | null {
return datetime.difference(inputDate, againstDate, { units: ["hours"] }).hours ?? null;
}
function isSeriesFromStorageNew(
seriesFromStorage: Series,
syncInterval = SYNC_INTERVAL_HOURS,
timeSinceLastFetch = getTimeSinceLastFetch(seriesFromStorage.lastFetchedAt),
) {
return !Number.isNaN(timeSinceLastFetch) &&
seriesFromStorage !== null &&
timeSinceLastFetch !== null &&
timeSinceLastFetch !== undefined &&
timeSinceLastFetch <= syncInterval;
}
async function getSeries(options: { id: string }): Promise<Series | null> {
const seriesFromStorage = await storage.read(options);
@ -64,16 +80,8 @@ async function getSeries(options: { id: string }): Promise<Series | null> {
return await initialFetch(options);
}
const timeSinceLastFetch =
datetime.difference(seriesFromStorage.lastFetchedAt, new Date(), { units: ["hours"] }).hours;
// we have the feed in storage and it's not too old
if (
seriesFromStorage !== null &&
timeSinceLastFetch !== null &&
timeSinceLastFetch !== undefined &&
timeSinceLastFetch <= SYNC_INTERVAL_HOURS
) {
if (isSeriesFromStorageNew(seriesFromStorage)) {
return seriesFromStorage;
}
@ -88,3 +96,8 @@ async function getSeries(options: { id: string }): Promise<Series | null> {
export const caching = {
getSeries,
};
export const forTestingOnly = {
getTimeSinceLastFetch,
isSeriesFromStorageNew,
};

View File

@ -11,6 +11,7 @@ function generateSeries(overrides: Partial<Series> = {}): Series {
lastFetchedAt: faker.date.recent(),
episodes: new Array(faker.number.int({ min: 0, max: 100 }))
.fill(null).map(() => (generateEpisode(overrides))),
...overrides,
};
}