From 6b457d04f11f2fbf4c733207175aa8411f768d9b Mon Sep 17 00:00:00 2001 From: Alexandr Garbuzov Date: Sun, 24 Aug 2025 16:43:02 +0300 Subject: [PATCH] tests: return cache tests which were removed in 53e4041 (#4368) --- tests/api.test.js | 82 +++++++++++++++++++++++++++++++++++++++++ tests/gist.test.js | 23 +++++++++++- tests/pin.test.js | 26 ++++++++++++- tests/top-langs.test.js | 25 ++++++++++++- 4 files changed, 153 insertions(+), 3 deletions(-) diff --git a/tests/api.test.js b/tests/api.test.js index c155220c..1073014f 100644 --- a/tests/api.test.js +++ b/tests/api.test.js @@ -168,6 +168,40 @@ describe("Test /api/", () => { ); }); + it("should have proper cache", async () => { + const { req, res } = faker({}, data_stats); + + await api(req, res); + + expect(res.setHeader.mock.calls).toEqual([ + ["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}`, + ], + ]); + }); + + it("should set proper cache", async () => { + const cache_seconds = CONSTANTS.TWELVE_HOURS; + const { req, res } = faker({ cache_seconds }, data_stats); + await api(req, res); + + expect(res.setHeader.mock.calls).toEqual([ + ["Content-Type", "image/svg+xml"], + [ + "Cache-Control", + `max-age=${ + cache_seconds + }, s-maxage=${cache_seconds}, stale-while-revalidate=${ + CONSTANTS.ONE_DAY + }`, + ], + ]); + }); + it("should set shorter cache when error", async () => { const { req, res } = faker({}, error); await api(req, res); @@ -183,6 +217,54 @@ describe("Test /api/", () => { ]); }); + it("should set proper cache with clamped values", async () => { + { + let { req, res } = faker({ cache_seconds: 200000 }, data_stats); + await api(req, res); + + expect(res.setHeader.mock.calls).toEqual([ + ["Content-Type", "image/svg+xml"], + [ + "Cache-Control", + `max-age=${CONSTANTS.TWO_DAY}, s-maxage=${ + CONSTANTS.TWO_DAY + }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, + ], + ]); + } + + // note i'm using block scoped vars + { + let { req, res } = faker({ cache_seconds: 0 }, data_stats); + await api(req, res); + + expect(res.setHeader.mock.calls).toEqual([ + ["Content-Type", "image/svg+xml"], + [ + "Cache-Control", + `max-age=${CONSTANTS.ONE_DAY}, s-maxage=${ + CONSTANTS.ONE_DAY + }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, + ], + ]); + } + + { + let { req, res } = faker({ cache_seconds: -10000 }, data_stats); + await api(req, res); + + expect(res.setHeader.mock.calls).toEqual([ + ["Content-Type", "image/svg+xml"], + [ + "Cache-Control", + `max-age=${CONSTANTS.TWELVE_HOURS}, s-maxage=${ + CONSTANTS.TWELVE_HOURS + }, stale-while-revalidate=${CONSTANTS.ONE_DAY}`, + ], + ]); + } + }); + it("should allow changing ring_color", async () => { const { req, res } = faker( { diff --git a/tests/gist.test.js b/tests/gist.test.js index bdf95156..8c717d64 100644 --- a/tests/gist.test.js +++ b/tests/gist.test.js @@ -4,7 +4,7 @@ import axios from "axios"; import MockAdapter from "axios-mock-adapter"; import { expect, it, describe, afterEach } from "@jest/globals"; import { renderGistCard } from "../src/cards/gist-card.js"; -import { renderError } from "../src/common/utils.js"; +import { CONSTANTS, renderError } from "../src/common/utils.js"; import gist from "../api/gist.js"; const gist_data = { @@ -170,4 +170,25 @@ describe("Test /api/gist", () => { renderError("Something went wrong", "Language not found"), ); }); + + it("should have proper cache", async () => { + const req = { + query: { + id: "bbfce31e0217a3689c8d961a356cb10d", + }, + }; + const res = { + setHeader: jest.fn(), + send: jest.fn(), + }; + mock.onPost("https://api.github.com/graphql").reply(200, gist_data); + + await gist(req, res); + + 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}`, + ); + }); }); diff --git a/tests/pin.test.js b/tests/pin.test.js index 2583ddfe..5fe21ffe 100644 --- a/tests/pin.test.js +++ b/tests/pin.test.js @@ -4,7 +4,7 @@ import axios from "axios"; import MockAdapter from "axios-mock-adapter"; import pin from "../api/pin.js"; import { renderRepoCard } from "../src/cards/repo-card.js"; -import { renderError } from "../src/common/utils.js"; +import { CONSTANTS, renderError } from "../src/common/utils.js"; import { expect, it, describe, afterEach } from "@jest/globals"; const data_repo = { @@ -201,4 +201,28 @@ describe("Test /api/pin", () => { ), ); }); + + it("should have proper cache", async () => { + const req = { + query: { + username: "anuraghazra", + repo: "convoychat", + }, + }; + const res = { + setHeader: jest.fn(), + send: jest.fn(), + }; + mock.onPost("https://api.github.com/graphql").reply(200, data_user); + + await pin(req, res); + + 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 + }`, + ); + }); }); diff --git a/tests/top-langs.test.js b/tests/top-langs.test.js index 1c8a45b7..907d4bc1 100644 --- a/tests/top-langs.test.js +++ b/tests/top-langs.test.js @@ -4,7 +4,7 @@ import axios from "axios"; import MockAdapter from "axios-mock-adapter"; import topLangs from "../api/top-langs.js"; import { renderTopLanguages } from "../src/cards/top-languages-card.js"; -import { renderError } from "../src/common/utils.js"; +import { CONSTANTS, renderError } from "../src/common/utils.js"; import { expect, it, describe, afterEach } from "@jest/globals"; const data_langs = { @@ -208,4 +208,27 @@ describe("Test /api/top-langs", () => { renderError("Something went wrong", "Locale not found"), ); }); + + it("should have proper cache", async () => { + const req = { + query: { + username: "anuraghazra", + }, + }; + const res = { + setHeader: jest.fn(), + send: jest.fn(), + }; + mock.onPost("https://api.github.com/graphql").reply(200, data_langs); + + await topLangs(req, res); + + expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml"); + expect(res.setHeader).toBeCalledWith( + "Cache-Control", + `max-age=${CONSTANTS.TOP_LANGS_CACHE_SECONDS / 2}, s-maxage=${ + CONSTANTS.TOP_LANGS_CACHE_SECONDS + }`, + ); + }); });