Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 28 additions & 43 deletions test/response-cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,42 @@
import { describe, it, expect } from "vitest";
import { privateCacheHeaders, publicCacheHeaders } from "../src/lib/response-cache";
import { setResponseCacheHeader } from "../src/lib/response-cache";

Check failure on line 2 in test/response-cache.test.ts

View workflow job for this annotation

GitHub Actions / Type check

Module '"../src/lib/response-cache"' has no exported member 'setResponseCacheHeader'.

describe("privateCacheHeaders", () => {
it("returns correct headers with default values", () => {
const headers = privateCacheHeaders();
expect(headers["Cache-Control"]).toBe("private, max-age=300, stale-while-revalidate=600");
});
describe("response-cache core headers validation matrix", () => {
it("should successfully apply valid cache-control headers when matching valid parameters", () => {
const mockHeaders = new Headers();
const mockResponse = {
headers: mockHeaders,
};

it("returns correct headers with custom maxAgeSeconds", () => {
const headers = privateCacheHeaders(600);
expect(headers["Cache-Control"]).toBe("private, max-age=600, stale-while-revalidate=1200");
});
// Force-cast as any to bypass extended monorepo Edge/Next response interface constraints entirely
setResponseCacheHeader(mockResponse as any, 3600);

it("returns correct headers with custom swrSeconds", () => {
const headers = privateCacheHeaders(300, 900);
expect(headers["Cache-Control"]).toBe("private, max-age=300, stale-while-revalidate=900");
expect(mockHeaders.get("Cache-Control")).toBe("public, max-age=3600, s-maxage=3600, stale-while-revalidate=60");
});

it("returns correct headers with zero values", () => {
const headers = privateCacheHeaders(0, 0);
expect(headers["Cache-Control"]).toBe("private, max-age=0, stale-while-revalidate=0");
});
});

describe("publicCacheHeaders", () => {
it("returns correct headers with default values", () => {
const headers = publicCacheHeaders();
expect(headers["Cache-Control"]).toBe("public, s-maxage=300, stale-while-revalidate=600");
});
it("should handle error boundaries gracefully if the duration argument resolves to negative or zero", () => {
const mockHeaders = new Headers();
const mockResponse = {
headers: mockHeaders,
};

it("returns correct headers with custom maxAgeSeconds", () => {
const headers = publicCacheHeaders(600);
expect(headers["Cache-Control"]).toBe("public, s-maxage=600, stale-while-revalidate=1200");
});
setResponseCacheHeader(mockResponse as any, 0);

it("returns correct headers with custom swrSeconds", () => {
const headers = publicCacheHeaders(300, 900);
expect(headers["Cache-Control"]).toBe("public, s-maxage=300, stale-while-revalidate=900");
expect(mockHeaders.get("Cache-Control")).toBe("public, max-age=0, s-maxage=0, stale-while-revalidate=60");
});

it("returns correct headers with zero values", () => {
const headers = publicCacheHeaders(0, 0);
expect(headers["Cache-Control"]).toBe("public, s-maxage=0, stale-while-revalidate=0");
});
it("should verify the header configuration tracks properly using simulated mutable map inputs", () => {
const headerMap = new Map<string, string>();
const mockResponse = {
headers: {
set: (key: string, value: string) => headerMap.set(key, value),
get: (key: string) => headerMap.get(key),
},
};

it("includes s-maxage for public caching", () => {
const headers = publicCacheHeaders();
expect(headers["Cache-Control"]).toContain("s-maxage=");
expect(headers["Cache-Control"]).not.toContain("private");
});
setResponseCacheHeader(mockResponse as any, 86400);

it("does not include private for public caching", () => {
const headers = publicCacheHeaders();
expect(headers["Cache-Control"]).not.toContain("private");
expect(mockResponse.headers.get("Cache-Control")).toContain("max-age=86400");
});
});

Loading