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:
Ophelia Goldstein
2025-10-09 03:41:02 +03:00
committed by GitHub
co-authored by Copilot Alexandr
parent 3c22841a82
commit 0afa6c4e78
13 changed files with 158 additions and 103 deletions
+24 -20
View File
@@ -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}`,
],
]);
}
+5 -4
View File
@@ -6,7 +6,8 @@ import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import gist from "../api/gist.js";
import { renderGistCard } from "../src/cards/gist.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";
const gist_data = {
data: {
@@ -192,9 +193,9 @@ describe("Test /api/gist", () => {
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.setHeader).toBeCalledWith(
"Cache-Control",
`max-age=${CONSTANTS.TWO_DAY}, ` +
`s-maxage=${CONSTANTS.TWO_DAY}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
`max-age=${CACHE_TTL.GIST_CARD.DEFAULT}, ` +
`s-maxage=${CACHE_TTL.GIST_CARD.DEFAULT}, ` +
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
);
});
});
+5 -4
View File
@@ -6,7 +6,8 @@ import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import pin from "../api/pin.js";
import { renderRepoCard } from "../src/cards/repo.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";
const data_repo = {
repository: {
@@ -233,9 +234,9 @@ describe("Test /api/pin", () => {
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.setHeader).toBeCalledWith(
"Cache-Control",
`max-age=${CONSTANTS.PIN_CARD_CACHE_SECONDS}, ` +
`s-maxage=${CONSTANTS.PIN_CARD_CACHE_SECONDS}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
`max-age=${CACHE_TTL.PIN_CARD.DEFAULT}, ` +
`s-maxage=${CACHE_TTL.PIN_CARD.DEFAULT}, ` +
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
);
});
});
+5 -4
View File
@@ -6,7 +6,8 @@ import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import topLangs from "../api/top-langs.js";
import { renderTopLanguages } from "../src/cards/top-languages.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";
const data_langs = {
data: {
@@ -238,9 +239,9 @@ describe("Test /api/top-langs", () => {
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.setHeader).toBeCalledWith(
"Cache-Control",
`max-age=${CONSTANTS.TOP_LANGS_CACHE_SECONDS}, ` +
`s-maxage=${CONSTANTS.TOP_LANGS_CACHE_SECONDS}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
`max-age=${CACHE_TTL.TOP_LANGS_CARD.DEFAULT}, ` +
`s-maxage=${CACHE_TTL.TOP_LANGS_CARD.DEFAULT}, ` +
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
);
});
});
+22
View File
@@ -4,6 +4,7 @@ import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import wakatime from "../api/wakatime.js";
import { renderWakatimeCard } from "../src/cards/wakatime.js";
import { CACHE_TTL, DURATIONS } from "../src/common/cache.js";
const wakaTimeData = {
data: {
@@ -119,4 +120,25 @@ describe("Test /api/wakatime", () => {
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.send).toBeCalledWith(renderWakatimeCard(wakaTimeData.data, {}));
});
it("should have proper cache", async () => {
const username = "anuraghazra";
const req = { query: { username } };
const res = { setHeader: jest.fn(), send: jest.fn() };
mock
.onGet(
`https://wakatime.com/api/v1/users/${username}/stats?is_including_today=true`,
)
.reply(200, wakaTimeData);
await wakatime(req, res);
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.setHeader).toBeCalledWith(
"Cache-Control",
`max-age=${CACHE_TTL.WAKATIME_CARD.DEFAULT}, ` +
`s-maxage=${CACHE_TTL.WAKATIME_CARD.DEFAULT}, ` +
`stale-while-revalidate=${DURATIONS.ONE_DAY}`,
);
});
});