Skip to content

Commit

Permalink
cache validation optimization & test
Browse files Browse the repository at this point in the history
  • Loading branch information
NoCymer committed Dec 25, 2023
1 parent d2ce485 commit b5a5b26
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/modules/animeSchedule/AnimeSchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,24 @@ const cacheStructure = {
* @param cache Cache to check
* @returns True if the cache is valid, else false.
*/
const isCacheValid = (cache) => {
export const isCacheValid = (cache: any[]) => {
try {
let first = cache[0];
for(let key in cacheStructure)
if (!Object.keys(first).includes(key)) return false;
for(let i = 0; i < cache.length; i++) {
let curr = cache[i];
for(let key in cacheStructure)
if (!Object.keys(curr).includes(key)) return false;
}
} catch { return false; }
return true;

}

/**
* Transform the given cache into a valid form in case of invalid cache
* @param cache Cache to validate
* @returns validated cache
*/
export const validateCache = (cache) => {
return isCacheValid(cache) ? cache : [{}];
}

activeTabSetting.value = todayDay;
Expand Down Expand Up @@ -88,10 +98,6 @@ const quarterListTwentyFour = [
const scheduleSetting = AnimeScheduleModule
.getSetting("schedule-weekday-entries-array");

let cacheValidity = isCacheValid(scheduleSetting.value);
scheduleSetting.value = cacheValidity ? scheduleSetting.value : [{}];


// Checks for default value and sanitize it for avoiding unwanted quarter
try {
if(scheduleSetting.value[0]["_quarter"] === -1) {
Expand Down Expand Up @@ -254,12 +260,13 @@ const AnimeScheduleWeek = ({shouldBeDisplayedSetting}: IAnimeScheduleWeek) => {
"Results may be outdated"
);

if(cacheValidity) {
if(isCacheValid(scheduleSetting.value)) {
// Converts every entry in the setting to an Anime instance
scheduleSetting.value.forEach((anime: Object) => {
animeList.push(Anime.fromJSON(anime))
})
} else {
scheduleSetting.value = validateCache(scheduleSetting.value);
console.log("[ERROR] : Invalid cache deteted, reload required");
}
}
Expand All @@ -268,13 +275,13 @@ const AnimeScheduleWeek = ({shouldBeDisplayedSetting}: IAnimeScheduleWeek) => {

let animeList: Anime[] = [];

if(cacheValidity) {
if(isCacheValid(scheduleSetting.value)) {
// Loads cached schedule
scheduleSetting.value.forEach((anime: Object) => {
animeList.push(Anime.fromJSON(anime))
});
} else {
console.log("[WARNING] : Invalid cache deteted, fetching new data...");
scheduleSetting.value = validateCache(scheduleSetting.value);
}

const [animeSchedule, setAnimeSchedule] = useState(animeList);
Expand Down
57 changes: 57 additions & 0 deletions src/modules/animeSchedule/tests/Anime.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Anime from "../Anime";
import DateTimeConverter from "../DateTimeConverter";
import { isCacheValid, validateCache } from "../AnimeSchedule";

/**
* Strips spaces from a string and returns it
Expand All @@ -23,6 +24,62 @@ describe("Testing anime class", () => {
dummyDate
);

test("Cache invalidation", () => {
const dummyShouldBeValid = {
"_id": 0,
"_title": "",
"_synopsis": "",
"_thumbnailURL": "",
"_malURL": "",
"_nextReleaseDate": "",
"_quarter": 0
}
const dummyShouldBeValid1 = {
"_id": 56,
"_title": "test1",
"_synopsis": "test2",
"_thumbnailURL": "abc.png",
"_malURL": "test3",
"_nextReleaseDate": "2019-11-03",
"_quarter": 2
}
const dummyShouldBeInValid = {
"_id": 0,
"_title": "",
"_synopsis": "",
"_thumbnailURL": "",
"_nextReleaseDate": "",
"_quarter": 0
}
const dummyArrayInvalid = [dummyShouldBeValid, dummyShouldBeInValid];
const dummyArrayValid = [dummyShouldBeValid, dummyShouldBeValid1];
expect(isCacheValid(dummyArrayInvalid)).toBe(false);
expect(isCacheValid(dummyArrayValid)).toBe(true);
})

test ("Cache validation", () => {
const dummyShouldBeValid = {
"_id": 0,
"_title": "",
"_synopsis": "",
"_thumbnailURL": "",
"_malURL": "",
"_nextReleaseDate": "",
"_quarter": 0
}
const dummyShouldBeInValid = {
"_id": 0,
"_title": "",
"_synopsis": "",
"_thumbnailURL": "",
"_nextReleaseDate": "",
"_quarter": 0
}
let dummyArrayInvalid = [dummyShouldBeValid, dummyShouldBeInValid];
dummyArrayInvalid = validateCache(dummyArrayInvalid);
expect(dummyArrayInvalid).toStrictEqual([{}]);
})

test("Quarter calculation", () => {
expect(dummyAnime.quarter).toBe(3);
})
Expand Down

0 comments on commit b5a5b26

Please sign in to comment.