refactor: reorganize cache TTL constants (#4550)
* refactor: reorganize cache TTL constants * Update tests/wakatime.test.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Alexandr <qwerty541zxc@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
Alexandr
parent
3c22841a82
commit
0afa6c4e78
+24
-20
@@ -13,7 +13,8 @@ import MockAdapter from "axios-mock-adapter";
|
||||
import api from "../api/index.js";
|
||||
import { calculateRank } from "../src/calculateRank.js";
|
||||
import { renderStatsCard } from "../src/cards/stats.js";
|
||||
import { CONSTANTS, renderError } from "../src/common/utils.js";
|
||||
import { renderError } from "../src/common/utils.js";
|
||||
import { CACHE_TTL, DURATIONS } from "../src/common/cache.js";
|
||||
|
||||
/**
|
||||
* @type {import("../src/fetchers/stats").StatsData}
|
||||
@@ -196,15 +197,15 @@ describe("Test /api/", () => {
|
||||
["Content-Type", "image/svg+xml"],
|
||||
[
|
||||
"Cache-Control",
|
||||
`max-age=${CONSTANTS.CARD_CACHE_SECONDS}, ` +
|
||||
`s-maxage=${CONSTANTS.CARD_CACHE_SECONDS}, ` +
|
||||
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
||||
`max-age=${CACHE_TTL.STATS_CARD.DEFAULT}, ` +
|
||||
`s-maxage=${CACHE_TTL.STATS_CARD.DEFAULT}, ` +
|
||||
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it("should set proper cache", async () => {
|
||||
const cache_seconds = CONSTANTS.TWELVE_HOURS;
|
||||
const cache_seconds = DURATIONS.TWELVE_HOURS;
|
||||
const { req, res } = faker({ cache_seconds }, data_stats);
|
||||
await api(req, res);
|
||||
|
||||
@@ -214,7 +215,7 @@ describe("Test /api/", () => {
|
||||
"Cache-Control",
|
||||
`max-age=${cache_seconds}, ` +
|
||||
`s-maxage=${cache_seconds}, ` +
|
||||
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
||||
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
|
||||
],
|
||||
]);
|
||||
});
|
||||
@@ -227,15 +228,16 @@ describe("Test /api/", () => {
|
||||
["Content-Type", "image/svg+xml"],
|
||||
[
|
||||
"Cache-Control",
|
||||
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS}, ` +
|
||||
`s-maxage=${CONSTANTS.ERROR_CACHE_SECONDS}, ` +
|
||||
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
||||
`max-age=${CACHE_TTL.ERROR}, ` +
|
||||
`s-maxage=${CACHE_TTL.ERROR}, ` +
|
||||
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it("should properly set cache using CACHE_SECONDS env variable", async () => {
|
||||
process.env.CACHE_SECONDS = "10000";
|
||||
const cacheSeconds = "10000";
|
||||
process.env.CACHE_SECONDS = cacheSeconds;
|
||||
|
||||
const { req, res } = faker({}, data_stats);
|
||||
await api(req, res);
|
||||
@@ -244,7 +246,9 @@ describe("Test /api/", () => {
|
||||
["Content-Type", "image/svg+xml"],
|
||||
[
|
||||
"Cache-Control",
|
||||
`max-age=10000, s-maxage=10000, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
||||
`max-age=${cacheSeconds}, ` +
|
||||
`s-maxage=${cacheSeconds}, ` +
|
||||
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
|
||||
],
|
||||
]);
|
||||
});
|
||||
@@ -275,9 +279,9 @@ describe("Test /api/", () => {
|
||||
["Content-Type", "image/svg+xml"],
|
||||
[
|
||||
"Cache-Control",
|
||||
`max-age=${CONSTANTS.TWO_DAY}, ` +
|
||||
`s-maxage=${CONSTANTS.TWO_DAY}, ` +
|
||||
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
||||
`max-age=${CACHE_TTL.STATS_CARD.MAX}, ` +
|
||||
`s-maxage=${CACHE_TTL.STATS_CARD.MAX}, ` +
|
||||
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
|
||||
],
|
||||
]);
|
||||
}
|
||||
@@ -291,9 +295,9 @@ describe("Test /api/", () => {
|
||||
["Content-Type", "image/svg+xml"],
|
||||
[
|
||||
"Cache-Control",
|
||||
`max-age=${CONSTANTS.TWELVE_HOURS}, ` +
|
||||
`s-maxage=${CONSTANTS.TWELVE_HOURS}, ` +
|
||||
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
||||
`max-age=${CACHE_TTL.STATS_CARD.MIN}, ` +
|
||||
`s-maxage=${CACHE_TTL.STATS_CARD.MIN}, ` +
|
||||
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
|
||||
],
|
||||
]);
|
||||
}
|
||||
@@ -306,9 +310,9 @@ describe("Test /api/", () => {
|
||||
["Content-Type", "image/svg+xml"],
|
||||
[
|
||||
"Cache-Control",
|
||||
`max-age=${CONSTANTS.TWELVE_HOURS}, ` +
|
||||
`s-maxage=${CONSTANTS.TWELVE_HOURS}, ` +
|
||||
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
||||
`max-age=${CACHE_TTL.STATS_CARD.MIN}, ` +
|
||||
`s-maxage=${CACHE_TTL.STATS_CARD.MIN}, ` +
|
||||
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user